Skip to content

Commit

Permalink
feat: add server links (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
CptbeffHeart authored Aug 23, 2024
1 parent 7f641ed commit e534bab
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 3 deletions.
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");
}
}
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());
}
});

}

}
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()));
}
}
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;
}

}
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);
}

}
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;
}

}
18 changes: 18 additions & 0 deletions src/main/resources/config.yml
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"

0 comments on commit e534bab

Please sign in to comment.