Drastically optimized GUIs with effective cache

This commit is contained in:
dejanzegarac 2022-07-23 03:22:05 +02:00
parent ce5e3193c8
commit b2d909ea6a
6 changed files with 35 additions and 33 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -43,31 +43,26 @@
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.moandjiezana.toml</groupId>
<artifactId>toml4j</artifactId>
<version>0.7.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.cryptomorin</groupId>
<artifactId>XSeries</artifactId>
<version>8.8.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
@ -83,8 +78,8 @@
</dependency>
</dependencies>
<groupId>PlayerServersHubCore</groupId>
<artifactId>PlayerServersHubCore</artifactId>
<groupId>PSHubCore</groupId>
<artifactId>PSHubCore</artifactId>
<version>1.0-SNAPSHOT</version>
</project>

View File

@ -1,11 +1,9 @@
package os.arcadiadevs.playerservers.hubcore;
import com.moandjiezana.toml.Toml;
import com.samjakob.spigui.SpiGUI;
import lombok.Getter;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.configuration.Configuration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import os.arcadiadevs.playerservers.hubcore.commands.CommandManager;
@ -19,7 +17,6 @@ import os.arcadiadevs.playerservers.hubcore.placeholders.PlayerCount;
import java.io.*;
import java.util.Objects;
import java.util.Timer;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -71,7 +68,7 @@ public class PSHubCore extends JavaPlugin {
// Create ServerCache refreshing task
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(serverCache, 1, getConfig().getInt("gui.selector.menu.cache-time"), TimeUnit.SECONDS);
executor.scheduleAtFixedRate(serverCache, 1, getConfig().getInt("cache-time"), TimeUnit.SECONDS);
}
private void createMultiNodeConfig() throws IOException {

View File

@ -23,8 +23,9 @@ public class Server {
private String playerName;
private String node;
private Integer port;
@Setter
private PingInfoStructure cachedData;
@Setter private PingInfoStructure cachedData;
@Setter private ServerStatus cachedStatus;
public String getShortId() {
return serverId.split("-")[0];

View File

@ -28,7 +28,10 @@ public class ServerCache implements Runnable {
return;
}
servers.forEach(server -> server.setCachedData(server.getData()));
servers.forEach(server -> {
server.setCachedData(server.getData());
server.setCachedStatus(server.getServerStatus());
});
servers.sort((s1, s2) -> {
final var p1 = s1.getServerStatus() == ServerStatus.ONLINE;

View File

@ -32,30 +32,34 @@ public class GUIUtils {
final var servers = PSHubCore.getInstance().getServerCache().getServers();
servers.forEach(server -> {
final var itemBuilder = new ItemBuilder(server.getServerStatus() == ServerStatus.ONLINE ? onlineXMaterial : offlineXMaterial);
final var itemBuilder = new ItemBuilder(server.getCachedStatus() == ServerStatus.ONLINE ? onlineXMaterial : offlineXMaterial);
var lore = PSH.getConfig().getStringList(server.getServerStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" : "gui.selector.menu.offline.lore");
var lore = PSH.getConfig().getStringList(server.getCachedStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" : "gui.selector.menu.offline.lore");
System.out.println("test1");
lore = lore.stream()
.map(s -> s.replaceAll("%server%", server.getPlayerName()))
.map(s -> s.replaceAll("%status%", server.getServerStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getOnline() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getMax() + "" : "0"))
.map(s -> s.replaceAll("%status%", server.getCachedStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getOnline() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getMax() + "" : "0"))
.map(s -> s.replaceAll("%port%", server.getPort() + ""))
.map(s -> s.replaceAll("%motd%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getMOTD() : "&cOffline"))
.map(s -> s.replaceAll("%motd%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getMOTD() : "&cOffline"))
.map(s -> s.replaceAll("%node%", server.getNode()))
.map(s -> s.replaceAll("%owner%", server.getPlayerName()))
.map(s -> s.replaceAll("%ip%", server.getHostname()))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
System.out.println("test2");
var item = itemBuilder
.name(ChatUtil.translate(PSH.getConfig().getString(server.getServerStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.name" : "gui.selector.menu.offline.name")
.name(ChatUtil.translate(PSH.getConfig().getString(server.getCachedStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.name" : "gui.selector.menu.offline.name")
.replaceAll("%server%", server.getPlayerName())
.replaceAll("%status%", server.getServerStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.replaceAll("%players%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getOnline() + "" : "0")
.replaceAll("%maxplayers%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getMax() + "" : "0")
.replaceAll("%status%", server.getCachedStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.replaceAll("%players%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getOnline() + "" : "0")
.replaceAll("%maxplayers%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getMax() + "" : "0")
.replaceAll("%port%", server.getPort() + "")
.replaceAll("%motd%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getMOTD() : "&cOffline")
.replaceAll("%motd%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getMOTD() : "&cOffline")
.replaceAll("%node%", server.getNode())
.replaceAll("%owner%", server.getPlayerName())
.replaceAll("%ip%", server.getHostname())
@ -63,6 +67,8 @@ public class GUIUtils {
.lore(lore)
.build();
System.out.println("test3");
menu.setButton(0, menu.getInventory().firstEmpty(), new SGButton(item).withListener(listener -> BungeeUtil.connectPlayer(listener, player, server.getPlayerName())));
});
@ -104,22 +110,22 @@ public class GUIUtils {
final var server = new Server(player);
var lore = PSH.getConfig().getStringList(server.getServerStatus() == ServerStatus.ONLINE ? "gui.player-menu.menu.info.online.lore" : "gui.player-menu.menu.info.offline.lore");
var lore = PSH.getConfig().getStringList(server.getCachedStatus() == ServerStatus.ONLINE ? "gui.player-menu.menu.info.online.lore" : "gui.player-menu.menu.info.offline.lore");
lore = lore.stream()
.map(s -> s.replaceAll("%server%", server.getPlayerName()))
.map(s -> s.replaceAll("%status%", server.getServerStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getOnline() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getMax() + "" : "0"))
.map(s -> s.replaceAll("%status%", server.getCachedStatus() == ServerStatus.ONLINE ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getOnline() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getMax() + "" : "0"))
.map(s -> s.replaceAll("%port%", server.getPort() + ""))
.map(s -> s.replaceAll("%motd%", server.getServerStatus() == ServerStatus.ONLINE ? server.getCachedData().getMOTD() : "&cOffline"))
.map(s -> s.replaceAll("%motd%", server.getCachedStatus() == ServerStatus.ONLINE ? server.getCachedData().getMOTD() : "&cOffline"))
.map(s -> s.replaceAll("%node%", server.getNode()))
.map(s -> s.replaceAll("%owner%", server.getPlayerName()))
.map(s -> s.replaceAll("%ip%", server.getHostname()))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
final var itemPlayer = new ItemBuilder(XMaterial.OAK_SIGN.parseMaterial())
.name(ChatUtil.translate(PSH.getConfig().getString(server.getServerStatus() == ServerStatus.ONLINE ? "gui.player-menu.menu.info.online.name" : "gui.player-menu.menu.info.offline.name")
.name(ChatUtil.translate(PSH.getConfig().getString(server.getCachedStatus() == ServerStatus.ONLINE ? "gui.player-menu.menu.info.online.name" : "gui.player-menu.menu.info.offline.name")
.replaceAll("%server%", server.getPlayerName())))
.lore(lore)
.build();
@ -139,7 +145,7 @@ public class GUIUtils {
.lore(PSH.getConfig().getStringList("gui.player-menu.menu.delete.confirmation.decline.lore"))
.build();
if (server.getServerStatus() == ServerStatus.ONLINE) {
if (server.getCachedStatus() == ServerStatus.ONLINE) {
final var itemJoin = new ItemBuilder(XMaterial.OAK_DOOR.parseMaterial())
.name(ChatUtil.translate(PSH.getConfig().getString("gui.player-menu.menu.join.name")))
.lore(PSH.getConfig().getStringList("gui.player-menu.menu.join.lore"))
@ -162,7 +168,7 @@ public class GUIUtils {
}));
}
if (server.getServerStatus() == ServerStatus.OFFLINE) {
if (server.getCachedStatus() == ServerStatus.OFFLINE) {
final var itemStart = new ItemBuilder(XMaterial.EMERALD_BLOCK.parseMaterial())
.name(ChatUtil.translate(PSH.getConfig().getString("gui.player-menu.menu.start.name")))
.lore(PSH.getConfig().getStringList("gui.player-menu.menu.start.lore"))