-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f641ed
commit e534bab
Showing
7 changed files
with
193 additions
and
3 deletions.
There are no files selected for viewing
19 changes: 16 additions & 3 deletions
19
src/main/java/com/expectale/customserverlinks/CustomServerLinks.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,30 @@ | ||
package com.expectale.customserverlinks; | ||
|
||
import com.expectale.customserverlinks.serverlinks.ServerLinkManager; | ||
import com.expectale.customserverlinks.listener.ServerLinkListener; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.util.logging.Logger; | ||
|
||
public final class CustomServerLinks extends JavaPlugin { | ||
|
||
public static CustomServerLinks INSTANCE; | ||
public static Logger LOGGER; | ||
|
||
@Override | ||
public void onEnable() { | ||
// Plugin startup logic | ||
|
||
INSTANCE = this; | ||
LOGGER = getLogger(); | ||
|
||
saveDefaultConfig(); | ||
getServer().getPluginManager().registerEvents(new ServerLinkListener(), this); | ||
ServerLinkManager.reloadLinks(); | ||
|
||
LOGGER.info("CustomServerLinks enabled"); | ||
} | ||
|
||
@Override | ||
public void onDisable() { | ||
// Plugin shutdown logic | ||
LOGGER.info("Disabling CustomServerLinks"); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/expectale/customserverlinks/listener/ServerLinkListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.expectale.customserverlinks.listener; | ||
|
||
import com.expectale.customserverlinks.serverlinks.ServerLinkManager; | ||
import com.expectale.customserverlinks.serverlinks.link.NamedServerLink; | ||
import com.expectale.customserverlinks.serverlinks.link.TypedServerLink; | ||
import org.bukkit.event.EventHandler; | ||
import org.bukkit.event.Listener; | ||
import org.bukkit.event.player.PlayerLinksSendEvent; | ||
|
||
public class ServerLinkListener implements Listener { | ||
|
||
@EventHandler | ||
public void onServerLinksSend(PlayerLinksSendEvent event) { | ||
ServerLinkManager.getLinks() | ||
.forEach(link -> { | ||
if (link instanceof TypedServerLink typedLink) { | ||
event.getLinks().addLink(typedLink.getType(), typedLink.getUrl()); | ||
} else if (link instanceof NamedServerLink namedLink) { | ||
event.getLinks().addLink(namedLink.name(), namedLink.getUrl()); | ||
} | ||
}); | ||
|
||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
src/main/java/com/expectale/customserverlinks/serverlinks/ServerLinkManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package com.expectale.customserverlinks.serverlinks; | ||
|
||
import com.expectale.customserverlinks.CustomServerLinks; | ||
import com.expectale.customserverlinks.serverlinks.link.AbstractServerLink; | ||
import com.expectale.customserverlinks.serverlinks.link.NamedServerLink; | ||
import com.expectale.customserverlinks.serverlinks.link.TypedServerLink; | ||
import org.bukkit.ServerLinks; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
import org.bukkit.configuration.file.FileConfiguration; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class ServerLinkManager { | ||
|
||
private static final List<AbstractServerLink> LINKS = new ArrayList<>(); | ||
|
||
public static void addLink(AbstractServerLink link) { | ||
LINKS.add(link); | ||
} | ||
|
||
public static List<AbstractServerLink> getLinks() { | ||
return new ArrayList<>(LINKS); | ||
} | ||
|
||
public static void reloadLinks() { | ||
FileConfiguration config = CustomServerLinks.INSTANCE.getConfig(); | ||
for (String key : config.getKeys(false)) { | ||
|
||
ConfigurationSection section = config.getConfigurationSection(key); | ||
if (section == null) { | ||
CustomServerLinks.LOGGER.warning("Could not load link for: " + key); | ||
continue; | ||
} | ||
String name = section.getString("name"); | ||
String type = section.getString("type"); | ||
String link = section.getString("link"); | ||
|
||
if (link == null) { | ||
CustomServerLinks.LOGGER.warning("Could not load link for: " + key); | ||
continue; | ||
} | ||
|
||
URI url; | ||
try { | ||
url = new URI(link); | ||
} catch (URISyntaxException e) { | ||
CustomServerLinks.LOGGER.warning("Could not load link for: " + key); | ||
continue; | ||
} | ||
|
||
if (type != null && isValidType(type)) { | ||
addLink(new TypedServerLink(ServerLinks.Type.valueOf(type.toUpperCase()), url)); | ||
} else if (name != null) { | ||
addLink(new NamedServerLink(name, url)); | ||
} else { | ||
CustomServerLinks.LOGGER.warning("Could not load link for: " + key); | ||
} | ||
|
||
} | ||
|
||
} | ||
|
||
public static boolean isValidType(String value) { | ||
return Arrays.stream(ServerLinks.Type.values()) | ||
.map(Enum::name) | ||
.anyMatch(enumName -> enumName.equalsIgnoreCase(value.toUpperCase())); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/expectale/customserverlinks/serverlinks/link/AbstractServerLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.expectale.customserverlinks.serverlinks.link; | ||
|
||
import java.net.URI; | ||
|
||
public abstract class AbstractServerLink { | ||
|
||
private final URI url; | ||
|
||
AbstractServerLink(URI url) { | ||
this.url = url; | ||
} | ||
|
||
public URI getUrl() { | ||
return url; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/expectale/customserverlinks/serverlinks/link/NamedServerLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.expectale.customserverlinks.serverlinks.link; | ||
|
||
import net.kyori.adventure.text.Component; | ||
import net.kyori.adventure.text.TextComponent; | ||
|
||
import java.net.URI; | ||
|
||
public class NamedServerLink extends AbstractServerLink { | ||
|
||
private final String name; | ||
|
||
public NamedServerLink(String name, URI url) { | ||
super(url); | ||
this.name = name; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public TextComponent name() { | ||
return Component.text(name); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/expectale/customserverlinks/serverlinks/link/TypedServerLink.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.expectale.customserverlinks.serverlinks.link; | ||
|
||
import org.bukkit.ServerLinks; | ||
|
||
import java.net.URI; | ||
|
||
public class TypedServerLink extends AbstractServerLink { | ||
|
||
private final ServerLinks.Type type; | ||
|
||
public TypedServerLink(ServerLinks.Type type, URI url) { | ||
super(url); | ||
this.type = type; | ||
} | ||
|
||
public ServerLinks.Type getType() { | ||
return type; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#Typed Server Link | ||
#There are some generic-use link labels that have translations. | ||
#Bug report links which may appear on disconnect/crash screens. | ||
#Available types : REPORT_BUG, COMMUNITY_GUIDELINES, SUPPORT, STATUS, FEEDBACK, COMMUNITY, WEBSITE, FORUMS, | ||
#NEWS, ANNOUNCEMENTS. | ||
Bug: | ||
type: "REPORT_BUG" | ||
link: "https://domain.com/" | ||
|
||
#Named Server Link | ||
#You can also name the link as you want. | ||
#Colors are also supported | ||
Site: | ||
name: "§3Site" | ||
link: "https://domain.com/" | ||
Discord: | ||
name: "§aDiscord" | ||
link: "https://discord.gg/yourdiscord" |