Skip to content

Commit fc10e6e

Browse files
vLuckyyyimDMK
andauthored
GH-905 Add server links feature to pause menu (1.21+ only) (#905)
* Add server link's feature. * Add docs * Update eternalcore-core/src/main/java/com/eternalcode/core/feature/serverlinks/ServerLinksEntry.java Co-authored-by: DMK <[email protected]> --------- Co-authored-by: DMK <[email protected]>
1 parent b208ad9 commit fc10e6e

File tree

7 files changed

+152
-0
lines changed

7 files changed

+152
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Get the latest development builds from our [GitHub Actions](https://github.com/E
5151
- :house: Home, Warp, and Spawn System
5252
- :page_facing_up: PlaceholderAPI Support
5353
- :memo: Customizable and Translatable Messages (Player language selection available)
54+
- <details><summary>Server links feature (Click to see how it's works)</summary><img src="assets/server-links-showcase.gif" alt="Video Guide"></details>
5455
- :gear: Advanced Configuration System for customization
5556
- :card_index: Database Integration (PostgreSQL, SQLite, MySQL, MariaDB, H2)
5657
- :rainbow: Adventure and [MiniMessage](https://docs.advntr.dev/minimessage/format.html) integration with legacy color processing (e.g., &7, &e)

assets/server-links-showcase.gif

5.2 MB
Loading

eternalcore-core/src/main/java/com/eternalcode/core/configuration/implementation/PluginConfiguration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.eternalcode.core.feature.helpop.HelpOpSettings;
1010
import com.eternalcode.core.feature.jail.JailSettings;
1111
import com.eternalcode.core.feature.randomteleport.RandomTeleportSettingsImpl;
12+
import com.eternalcode.core.feature.serverlinks.ServerLinksConfig;
1213
import com.eternalcode.core.feature.spawn.SpawnSettings;
1314
import com.eternalcode.core.feature.teleportrequest.TeleportRequestSettings;
1415
import com.eternalcode.core.injector.annotations.Bean;
@@ -440,6 +441,10 @@ public float getCatboyWalkSpeed() {
440441
}
441442
}
442443

444+
@Bean
445+
@Description({ " ", "# ServerLinks Section" })
446+
ServerLinksConfig serverLinks = new ServerLinksConfig();
447+
443448
@Override
444449
public Resource resource(File folder) {
445450
return Source.of(folder, "config.yml");
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.eternalcode.core.feature.serverlinks;
2+
3+
import java.util.List;
4+
import net.dzikoysk.cdn.entity.Contextual;
5+
import net.dzikoysk.cdn.entity.Description;
6+
7+
@Contextual
8+
public class ServerLinksConfig {
9+
10+
@Description({
11+
"# Configuration of server links displayed in the ESC/pause menu",
12+
"# Links will be visible in the game's pause menu under server information",
13+
"# Note: This feature requires Minecraft version 1.21 or newer to work properly"
14+
})
15+
public boolean sendLinksOnJoin = true;
16+
17+
public List<ServerLinksEntry> serverLinks = List.of(
18+
ServerLinksEntry.of("<rainbow>Discord", "https://discord.gg/v2rkPb4Q2r"),
19+
ServerLinksEntry.of("Website", "https://www.eternalcode.pl")
20+
);
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.eternalcode.core.feature.serverlinks;
2+
3+
import com.eternalcode.core.compatibility.Compatibility;
4+
import com.eternalcode.core.compatibility.Version;
5+
import com.eternalcode.core.injector.annotations.Inject;
6+
import com.eternalcode.core.injector.annotations.component.Controller;
7+
import org.bukkit.event.EventHandler;
8+
import org.bukkit.event.Listener;
9+
import org.bukkit.event.player.PlayerJoinEvent;
10+
11+
@Controller
12+
@Compatibility(from = @Version(minor = 21, patch = 0))
13+
public class ServerLinksController implements Listener {
14+
15+
private final ServerLinksService serverLinksService;
16+
private final ServerLinksConfig serverLinksConfig;
17+
18+
@Inject
19+
public ServerLinksController(ServerLinksService serverLinksService, ServerLinksConfig serverLinksConfig) {
20+
this.serverLinksService = serverLinksService;
21+
this.serverLinksConfig = serverLinksConfig;
22+
}
23+
24+
@EventHandler
25+
public void onJoin(PlayerJoinEvent event) {
26+
if (this.serverLinksConfig.sendLinksOnJoin) {
27+
this.serverLinksService.sendServerLinks(event.getPlayer());
28+
}
29+
}
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.eternalcode.core.feature.serverlinks;
2+
3+
import net.dzikoysk.cdn.entity.Contextual;
4+
5+
@Contextual
6+
public class ServerLinksEntry {
7+
8+
public final String name;
9+
public final String address;
10+
11+
public ServerLinksEntry(String name, String address) {
12+
this.name = name;
13+
this.address = address;
14+
}
15+
16+
public static ServerLinksEntry of(String name, String address) {
17+
return new ServerLinksEntry(name, address);
18+
}
19+
20+
public String name() {
21+
return name;
22+
}
23+
24+
public String address() {
25+
return this.address;
26+
}
27+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.eternalcode.core.feature.serverlinks;
2+
3+
import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection;
4+
5+
import com.eternalcode.annotations.scan.feature.FeatureDocs;
6+
import com.eternalcode.core.compatibility.Compatibility;
7+
import com.eternalcode.core.compatibility.Version;
8+
import com.eternalcode.core.injector.annotations.Inject;
9+
import com.eternalcode.core.injector.annotations.component.Service;
10+
import java.net.URI;
11+
import java.net.URISyntaxException;
12+
import net.kyori.adventure.text.Component;
13+
import net.kyori.adventure.text.minimessage.MiniMessage;
14+
import org.bukkit.ServerLinks;
15+
import org.bukkit.entity.Player;
16+
import org.bukkit.plugin.Plugin;
17+
18+
@Service
19+
@Compatibility(from = @Version(minor = 21, patch = 0))
20+
@FeatureDocs(name = "ServerLinks", description = "Server links to players allow to display link's dedicated to server social media. Displayed under the pause menu (ESC).")
21+
public class ServerLinksService {
22+
23+
private final Plugin plugin;
24+
private final MiniMessage miniMessage;
25+
private final ServerLinksConfig config;
26+
27+
@Inject
28+
public ServerLinksService(Plugin plugin, MiniMessage miniMessage, ServerLinksConfig config) {
29+
this.plugin = plugin;
30+
this.miniMessage = miniMessage;
31+
this.config = config;
32+
}
33+
34+
public void sendServerLinks(Player player) {
35+
ServerLinks serverLinks = this.plugin.getServer().getServerLinks().copy();
36+
37+
for (ServerLinksEntry serverLink : this.config.serverLinks) {
38+
this.parseLinks(serverLinks, serverLink);
39+
}
40+
41+
player.sendLinks(serverLinks);
42+
}
43+
44+
private URI parseUrl(String url) {
45+
try {
46+
if (!url.startsWith("https://") && !url.startsWith("http://")) {
47+
return null;
48+
}
49+
return new URI(url);
50+
}
51+
catch (URISyntaxException exception) {
52+
return null;
53+
}
54+
}
55+
56+
private org.bukkit.ServerLinks.ServerLink parseLinks(org.bukkit.ServerLinks serverLinks, ServerLinksEntry links) {
57+
URI url = parseUrl(links.address());
58+
59+
if (url == null) {
60+
return null;
61+
}
62+
63+
// TODO: Use ServerLinks#addLinks(Component, URI) instead of ServerLinks#addLink(String, URI) when we use
64+
// PaperAPI in nearly future.
65+
Component deserialize = this.miniMessage.deserialize(links.name());
66+
return serverLinks.addLink(legacySection().serialize(deserialize), url);
67+
}
68+
}

0 commit comments

Comments
 (0)