Compass optimizations

This commit is contained in:
OpenSource 2023-02-15 22:42:04 +01:00
parent 17671b49e6
commit 12d928b4c2
5 changed files with 111 additions and 17 deletions

View File

@ -5,11 +5,15 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import lombok.Getter;
import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.hibernate.SessionFactory;
import os.arcadiadevs.playerservers.hubcore.cache.ServerCache;
import os.arcadiadevs.playerservers.hubcore.commands.CommandManager;
import os.arcadiadevs.playerservers.hubcore.controllers.NodesController;
import os.arcadiadevs.playerservers.hubcore.controllers.ServersController;
@ -43,6 +47,9 @@ public class PsHubCore extends JavaPlugin {
@Getter
private NodesController nodesController;
@Getter
private ServerCache serverCache;
@SneakyThrows
@Override
public void onEnable() {
@ -100,11 +107,11 @@ public class PsHubCore extends JavaPlugin {
spiGui = new SpiGUI(this);
// Initialize ServerCache
//serverCache = new ServerCache();
serverCache = new ServerCache(serversController);
// Create ServerCache refreshing task
//ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
//executor.scheduleAtFixedRate(serverCache, 1, getConfig().getInt("cache-time"), TimeUnit.SECONDS);
ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.scheduleAtFixedRate(serverCache, 1, getConfig().getInt("cache.cache-time"), TimeUnit.SECONDS);
}
/**

View File

@ -0,0 +1,30 @@
package os.arcadiadevs.playerservers.hubcore.cache;
import java.util.HashSet;
import lombok.Getter;
import os.arcadiadevs.playerservers.hubcore.controllers.ServersController;
import os.arcadiadevs.playerservers.hubcore.models.CachedServer;
public class ServerCache implements Runnable {
@Getter
private final HashSet<CachedServer> servers;
private final ServersController serversController;
public ServerCache(ServersController serversController) {
this.serversController = serversController;
this.servers = new HashSet<>();
}
/**
* Refreshes the cache.
*/
@Override
public void run() {
HashSet<CachedServer> newCache = new HashSet<>();
serversController.getServers().forEach(server -> newCache.add(new CachedServer(server)));
servers.clear();
servers.addAll(newCache);
}
}

View File

@ -29,13 +29,21 @@ public class SelectorGui {
final var instance = PsHubCore.getInstance();
final var menu = instance.getSpiGui()
.create(ChatUtil.translate(instance.getConfig().getString("gui.selector.menu.name")), 5);
final var useCache = instance.getConfig().getBoolean("cache.enabled");
menu.setAutomaticPaginationEnabled(true);
menu.setBlockDefaultInteractions(true);
final var servers = PsHubCore.getInstance()
.getServersController()
.getServers();
final var servers = useCache
?
PsHubCore.getInstance()
.getServerCache()
.getServers()
:
PsHubCore.getInstance()
.getServersController()
.getServers();
servers.forEach(server -> {
final var onlinexMaterial =
@ -45,22 +53,24 @@ public class SelectorGui {
XMaterial.matchXMaterial(
instance.getConfig().getString("gui.selector.menu.offline.block"))
.orElse(XMaterial.RED_TERRACOTTA).parseItem();
final var serverInfo = server.getInfo();
final var itemBuilder = new ItemBuilder(
server.getStatus() == ServerStatus.ONLINE ? onlinexMaterial : offlinexMaterial);
final var online = server.getStatus() == ServerStatus.ONLINE;
serverInfo.status() == ServerStatus.ONLINE ? onlinexMaterial : offlinexMaterial);
final var online = serverInfo.status() == ServerStatus.ONLINE;
var lore = instance.getConfig().getStringList(
server.getStatus() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" :
serverInfo.status() == ServerStatus.ONLINE ? "gui.selector.menu.online.lore" :
"gui.selector.menu.offline.lore");
lore = lore.stream()
.map(s -> s.replaceAll("%server%", server.getId()))
.map(s -> s.replaceAll("%status%", online ? "&aOnline" : "&cOffline"))
.map(s -> s.replaceAll("%players%", online ? server.getInfo().players() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", online ? server.getInfo().maxPlayers() + "" : "0"))
.map(s -> s.replaceAll("%players%", online ? serverInfo.players() + "" : "0"))
.map(s -> s.replaceAll("%maxplayers%", online ? serverInfo.maxPlayers() + "" : "0"))
.map(s -> s.replaceAll("%port%", server.getDefaultAllocation().getPort() + ""))
.map(s -> s.replaceAll("%motd%", online ? server.getInfo().motd() : "&cOffline"))
.map(s -> s.replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline"))
.map(s -> s.replaceAll("%node%", server.getNode().getName()))
.map(s -> s.replaceAll("%owner%", server.getOfflinePlayer().getName()))
.map(s -> s.replaceAll("%ip%", server.getNode().getIp()))
@ -71,10 +81,10 @@ public class SelectorGui {
online ? "gui.selector.menu.online.name" : "gui.selector.menu.offline.name")
.replaceAll("%server%", server.getId())
.replaceAll("%status%", online ? "&aOnline" : "&cOffline")
.replaceAll("%players%", online ? server.getInfo().players() + "" : "0")
.replaceAll("%maxplayers%", online ? server.getInfo().maxPlayers() + "" : "0")
.replaceAll("%players%", online ? serverInfo.players() + "" : "0")
.replaceAll("%maxplayers%", online ? serverInfo.maxPlayers() + "" : "0")
.replaceAll("%port%", server.getDefaultAllocation().getPort() + "")
.replaceAll("%motd%", online ? server.getInfo().motd() : "&cOffline")
.replaceAll("%motd%", online ? serverInfo.motd() : "&cOffline")
.replaceAll("%node%", server.getNode().getName())
.replaceAll("%owner%", server.getOfflinePlayer().getName())
.replaceAll("%ip%", server.getNode().getIp())

View File

@ -0,0 +1,43 @@
package os.arcadiadevs.playerservers.hubcore.models;
import lombok.Getter;
import lombok.Setter;
import os.arcadiadevs.playerservers.hubcore.enums.ServerStatus;
import os.arcadiadevs.playerservers.hubcore.utils.ServerPinger;
@Getter
@Setter
public class CachedServer extends Server {
private ServerStatus status;
private ServerPinger.PingResult pingResult;
public CachedServer(Server server) {
this.setId(server.getId());
this.setExternalId(server.getExternalId());
this.setExternalUuid(server.getExternalUuid());
this.setOwner(server.getOwner());
this.setNode(server.getNode());
this.setAllocations(server.getAllocations());
this.setDefaultAllocation(server.getDefaultAllocation());
ServerPinger.PingResult pingResult = server.getInfo();
setStatus(pingResult.status());
setPingResult(pingResult);
}
@Override
public ServerPinger.PingResult getInfo() {
System.out.println("Getting cached info");
return pingResult;
}
@Override
public ServerStatus getStatus() {
System.out.println("Getting cached status");
return status;
}
}

View File

@ -14,8 +14,12 @@ mysql:
debug: false
get-from-file: false
# For how long should we keep Server Data cached?
cache-time: 20
cache:
# Should we cache Server Data for the servers gui?
enabled: false
# For how long should we keep Server Data cached? (in seconds)
cache-time: 20
messages:
no-permission: "&9Error> &7Oops, it seems like you don't have permission to do that."