Skip to content

Commit

Permalink
Add server link's feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
vLuckyyy committed Feb 7, 2025
1 parent 046c0df commit 12f56e9
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.eternalcode.core.feature.helpop.HelpOpSettings;
import com.eternalcode.core.feature.jail.JailSettings;
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettingsImpl;
import com.eternalcode.core.feature.serverlinks.ServerLinksConfig;
import com.eternalcode.core.feature.spawn.SpawnSettings;
import com.eternalcode.core.injector.annotations.Bean;
import com.eternalcode.core.injector.annotations.component.ConfigurationFile;
Expand Down Expand Up @@ -440,6 +441,10 @@ public float getCatboyWalkSpeed() {
}
}

@Bean
@Description({ " ", "# ServerLinks Section" })
ServerLinksConfig serverLinks = new ServerLinksConfig();

@Override
public Resource resource(File folder) {
return Source.of(folder, "config.yml");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.eternalcode.core.feature.serverlinks;

import java.util.List;
import net.dzikoysk.cdn.entity.Contextual;
import net.dzikoysk.cdn.entity.Description;

@Contextual
public class ServerLinksConfig {

@Description({
"# Configuration of server links displayed in the ESC/pause menu",
"# Links will be visible in the game's pause menu under server information",
"# Note: This feature requires Minecraft version 1.21 or newer to work properly"
})
public boolean sendLinksOnJoin = true;

public List<ServerLinksEntry> serverLinks = List.of(
ServerLinksEntry.of("<rainbow>Discord", "https://discord.gg/v2rkPb4Q2r"),
ServerLinksEntry.of("Website", "https://www.eternalcode.pl")
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.eternalcode.core.feature.serverlinks;

import com.eternalcode.core.compatibility.Compatibility;
import com.eternalcode.core.compatibility.Version;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Controller;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;

@Controller
@Compatibility(from = @Version(minor = 21, patch = 0))
public class ServerLinksController implements Listener {

private final ServerLinksService serverLinksService;
private final ServerLinksConfig serverLinksConfig;

@Inject
public ServerLinksController(ServerLinksService serverLinksService, ServerLinksConfig serverLinksConfig) {
this.serverLinksService = serverLinksService;
this.serverLinksConfig = serverLinksConfig;
}

@EventHandler
public void onJoin(PlayerJoinEvent event) {
if (this.serverLinksConfig.sendLinksOnJoin) {
this.serverLinksService.sendServerLinks(event.getPlayer());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.eternalcode.core.feature.serverlinks;

import net.dzikoysk.cdn.entity.Contextual;

@Contextual
public class ServerLinksEntry {

public final String name;
public final String address;

public ServerLinksEntry(String name, String address) {
this.name = name;
this.address = address;
}

public static ServerLinksEntry of(String name, String address) {
return new ServerLinksEntry(name, address);
}

public String name() {
return name;
}

public String address() {
return address;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.eternalcode.core.feature.serverlinks;

import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection;

import com.eternalcode.core.compatibility.Compatibility;
import com.eternalcode.core.compatibility.Version;
import com.eternalcode.core.injector.annotations.Inject;
import com.eternalcode.core.injector.annotations.component.Service;
import java.net.URI;
import java.net.URISyntaxException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.ServerLinks;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

@Service
@Compatibility(from = @Version(minor = 21, patch = 0))
public class ServerLinksService {

private final Plugin plugin;
private final MiniMessage miniMessage;
private final ServerLinksConfig config;

@Inject
public ServerLinksService(Plugin plugin, MiniMessage miniMessage, ServerLinksConfig config) {
this.plugin = plugin;
this.miniMessage = miniMessage;
this.config = config;
}

public void sendServerLinks(Player player) {
ServerLinks serverLinks = this.plugin.getServer().getServerLinks().copy();

for (ServerLinksEntry serverLink : this.config.serverLinks) {
this.parseLinks(serverLinks, serverLink);
}

player.sendLinks(serverLinks);
}

private URI parseUrl(String url) {
try {
if (!url.startsWith("https://") && !url.startsWith("http://")) {
return null;
}
return new URI(url);
}
catch (URISyntaxException exception) {
return null;
}
}

private org.bukkit.ServerLinks.ServerLink parseLinks(org.bukkit.ServerLinks serverLinks, ServerLinksEntry links) {
URI url = parseUrl(links.address());

if (url == null) {
return null;
}

// TODO: Use ServerLinks#addLinks(Component, URI) instead of ServerLinks#addLink(String, URI) when we use
// PaperAPI in nearly future.
Component deserialize = this.miniMessage.deserialize(links.name());
return serverLinks.addLink(legacySection().serialize(deserialize), url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.eternalcode.core.feature.serverlinks;


/*
This feature work's only with Minecraft version 1.21 or newer
*/

0 comments on commit 12f56e9

Please sign in to comment.