diff --git a/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java b/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java index 341165a..b628b8b 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java +++ b/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java @@ -1,7 +1,9 @@ package de.fanta.challengesjoinentities; +import de.fanta.challenges.Challenges; import de.fanta.challengesjoinentities.ChallengesGlobalDataHelper.ChallengeMessageType; import de.iani.cubesideutils.bukkit.plugin.api.GlobalDataHelperBukkit; +import org.bukkit.Bukkit; import java.io.DataInputStream; import java.io.IOException; @@ -31,10 +33,32 @@ plugin.setServerStatus(serverName, online); break; - case PLAYER_COUNT: + case PLAYER_COUNT: { int count = data.readInt(); - plugin.setPlayerCount(serverName, count); + int maxPlayers = data.readInt(); + plugin.setPlayerCount(serverName, count, maxPlayers); break; + } + + case INITIAL_DATA_REQUEST: { + if (Bukkit.getPluginManager().getPlugin("Challenges") != null) { + Challenges challenges = (Challenges) Bukkit.getPluginManager().getPlugin("Challenges"); + boolean timerRunning = challenges.getTimer().isRunning(); + int playerCount = Bukkit.getOnlinePlayers().size(); + int maxPlayers = Bukkit.getMaxPlayers(); + + sendInitialData(serverName, timerRunning, playerCount, maxPlayers); + } + break; + } + + case INITIAL_DATA: { + boolean timerRunning = data.readBoolean(); + int playerCount = data.readInt(); + int maxPlayers = data.readInt(); + plugin.updateEntityData(serverName, timerRunning, playerCount, maxPlayers); + break; + } default: throw new UnsupportedOperationException(); @@ -50,10 +74,18 @@ } public void sendPlayerCountUpdate(int count) { - sendData(ChallengeMessageType.PLAYER_COUNT, getThisServerName(), count); + sendData(ChallengeMessageType.PLAYER_COUNT, getThisServerName(), count, Bukkit.getMaxPlayers()); + } + + public void requestInitialData(String serverName) { + sendData(getServer(serverName), ChallengeMessageType.INITIAL_DATA_REQUEST, getThisServerName()); + } + + public void sendInitialData(String toServer, boolean timerRunning, int playerCount, int maxPlayers) { + sendData(getServer(toServer), ChallengeMessageType.INITIAL_DATA, getThisServerName(), timerRunning, playerCount, maxPlayers); } public enum ChallengeMessageType { - TIMER, SERVER_STATUS, PLAYER_COUNT + TIMER, SERVER_STATUS, PLAYER_COUNT, INITIAL_DATA_REQUEST, INITIAL_DATA } } diff --git a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java index afa967a..b3ca034 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java +++ b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java @@ -1,5 +1,6 @@ package de.fanta.challengesjoinentities; +import de.fanta.challengesjoinentities.JoinEntityData.ServerStatus; import de.fanta.challengesjoinentities.commands.AddEntityCommand; import de.fanta.challengesjoinentities.commands.RemoveEntityCommand; import de.fanta.challengesjoinentities.listeners.ChallengesEventListener; @@ -25,12 +26,10 @@ private Config config; private ChallengesGlobalDataHelper globalDataHelper; - private Map entities; - private Map entityPortLocations; + private Map entities; public ChallengesJoinEntities() { this.entities = new HashMap<>(); - this.entityPortLocations = new HashMap<>(); } @Override @@ -40,17 +39,17 @@ if (Bukkit.getPluginManager().getPlugin("Challenges") != null) { Bukkit.getPluginManager().registerEvents(new ChallengesEventListener(this), this); + } else { + CommandRouter router = new CommandRouter(getCommand("challengepiglins")); + router.addCommandMapping(new AddEntityCommand(this), "spawn"); + router.addCommandMapping(new RemoveEntityCommand(this), "remove"); } Bukkit.getPluginManager().registerEvents(new EntityListener(this), this); - - CommandRouter router = new CommandRouter(getCommand("challengepiglins")); - router.addCommandMapping(new AddEntityCommand(this), "spawn"); - router.addCommandMapping(new RemoveEntityCommand(this), "remove"); } - public void spawnPiglin(Location location, String serverName, String gpLocationName) { + public void spawnPiglin(Location location, String serverName, String gpLocationName, String serverDisplayName) { if (entities.containsKey(serverName)) { - despawnPiglin(entities.get(serverName), serverName); + despawnPiglin(entities.get(serverName).getEntityUUID(), serverName); } CraftPiglin piglin = (CraftPiglin) location.getWorld().spawnEntity(location, EntityType.PIGLIN); @@ -60,7 +59,9 @@ piglin.setRemoveWhenFarAway(false); piglin.setCustomNameVisible(true); - addPiglin(piglin.getUniqueId(), serverName, gpLocationName); + addPiglin(piglin.getUniqueId(), serverName, gpLocationName, serverDisplayName); + + globalDataHelper.requestInitialData(serverName); } public void despawnPiglin(UUID piglinUUID, String serverName) { @@ -71,21 +72,20 @@ } } - public void addPiglin(UUID piglinUUID, String serverName, String gpLocationName) { - addPiglin(piglinUUID, serverName, gpLocationName, true); + public void addPiglin(UUID piglinUUID, String serverName, String gpLocationName, String serverDisplayName) { + addPiglin(piglinUUID, serverName, gpLocationName, serverDisplayName, true); } - public void addPiglin(UUID piglinUUID, String serverName, String gpLocationName, boolean saveToConfig) { - this.entities.put(serverName, piglinUUID); - this.entityPortLocations.put(piglinUUID, gpLocationName); + public void addPiglin(UUID piglinUUID, String serverName, String gpLocationName, String serverDisplayName, boolean saveToConfig) { + JoinEntityData entityData = new JoinEntityData(serverName, piglinUUID, gpLocationName, serverDisplayName, ServerStatus.OFFLINE, 0, 0); + this.entities.put(serverName, entityData); if (saveToConfig) { - this.config.savePiglin(piglinUUID, serverName, gpLocationName); + this.config.savePiglin(entityData); } } public void removePiglin(String serverName) { - this.entityPortLocations.remove(this.entities.get(serverName)); this.entities.remove(serverName); this.config.removePiglin(serverName); @@ -93,33 +93,48 @@ public void setTimerStatus(String serverName, boolean running) { CraftPiglin piglin = getPiglinForServerName(serverName); - if (piglin != null) { + JoinEntityData data = entities.get(serverName); + + if (piglin != null && data != null) { piglin.getHandle().u(!running); + data.setServerStatus(running ? ServerStatus.RUNNING : ServerStatus.ONLINE); + updateCustomName(piglin, serverName); } } public void setServerStatus(String serverName, boolean online) { CraftPiglin piglin = getPiglinForServerName(serverName); - if (piglin != null) { + JoinEntityData data = entities.get(serverName); + + if (piglin != null && data != null) { if (!online) { - piglin.getEquipment().setItemInOffHand(new ItemStack(Material.GOLD_NUGGET)); + piglin.getHandle().u(false); + piglin.getEquipment().setItemInOffHand(new ItemStack(Material.GOLD_INGOT)); + + data.setServerStatus(ServerStatus.OFFLINE); } else { piglin.getEquipment().clear(); + data.setServerStatus(ServerStatus.ONLINE); } + updateCustomName(piglin, serverName); } } - public void setPlayerCount(String serverName, int count) { + public void setPlayerCount(String serverName, int count, int maxPlayers) { CraftPiglin piglin = getPiglinForServerName(serverName); - if (piglin != null) { - // TODO displayname etc + JoinEntityData data = entities.get(serverName); + + if (piglin != null && data != null) { + data.setPlayerCount(count); + data.setMaxPlayers(maxPlayers); + updateCustomName(piglin, serverName); } } public CraftPiglin getPiglinForServerName(String serverName) { - UUID entityUUID = entities.get(serverName); - if (entityUUID != null) { - Entity piglin = Bukkit.getEntity(entityUUID); + JoinEntityData data = entities.get(serverName); + if (data != null) { + Entity piglin = Bukkit.getEntity(data.getEntityUUID()); if (piglin instanceof CraftPiglin) { return (CraftPiglin) piglin; @@ -128,6 +143,26 @@ return null; } + public void updateEntityData(String serverName, boolean timerRunning, int playerCount, int maxPlayers) { + JoinEntityData entityData = entities.get(serverName); + if (entityData != null) { + entityData.setServerStatus(timerRunning ? ServerStatus.RUNNING : ServerStatus.ONLINE); + } + + CraftPiglin piglin = getPiglinForServerName(serverName); + if (piglin != null) { + updateCustomName(piglin, serverName); + } + } + + public void updateCustomName(CraftPiglin piglin, String serverName) { + JoinEntityData data = entities.get(serverName); + if (data != null) { + String customName = data.createCustomEntityName(); + piglin.setCustomName(customName); + } + } + // TODO bounce player back when too close to piglin diff --git a/src/main/java/de/fanta/challengesjoinentities/Config.java b/src/main/java/de/fanta/challengesjoinentities/Config.java index 8ebfbc7..3d893f2 100644 --- a/src/main/java/de/fanta/challengesjoinentities/Config.java +++ b/src/main/java/de/fanta/challengesjoinentities/Config.java @@ -1,5 +1,6 @@ package de.fanta.challengesjoinentities; +import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; @@ -11,8 +12,10 @@ public Config(ChallengesJoinEntities plugin) { this.plugin = plugin; - plugin.saveDefaultConfig(); - reloadConfig(); + if (Bukkit.getPluginManager().getPlugin("Challenges") == null) { + plugin.saveDefaultConfig(); + reloadConfig(); + } } private void reloadConfig() { @@ -24,12 +27,13 @@ if (piglinsSection != null) { for (String serverName : piglinsSection.getKeys(false)) { ConfigurationSection piglin = piglinsSection.getConfigurationSection(serverName); - plugin.addPiglin(UUID.fromString(piglin.getString("uuid")), serverName, piglin.getString("gpLocation"), false); + plugin.addPiglin(UUID.fromString(piglin.getString("uuid")), serverName, piglin.getString("gpLocation"), piglin.getString("displayName"), false); + plugin.getGlobalDataHelper().requestInitialData(serverName); } } } - public void savePiglin(UUID piglinUUID, String serverName, String gpLocation) { + public void savePiglin(JoinEntityData data) { FileConfiguration config = plugin.getConfig(); ConfigurationSection piglinsSection = config.getConfigurationSection("piglins"); @@ -38,9 +42,10 @@ piglinsSection = config.getConfigurationSection("piglins"); } - ConfigurationSection piglin = piglinsSection.createSection(serverName); - piglin.set("uuid", piglinUUID.toString()); - piglin.set("gpLocation", gpLocation); + ConfigurationSection piglin = piglinsSection.createSection(data.getServerName()); + piglin.set("uuid", data.getEntityUUID().toString()); + piglin.set("gpLocation", data.getGlobalPortLocationName()); + piglin.set("displayName", data.getServerDisplayName()); plugin.saveConfig(); } diff --git a/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java b/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java new file mode 100644 index 0000000..54665a0 --- /dev/null +++ b/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java @@ -0,0 +1,79 @@ +package de.fanta.challengesjoinentities; + +import net.md_5.bungee.api.ChatColor; + +import java.util.UUID; + +public class JoinEntityData { + + private String serverName; + private UUID entityUUID; + private String globalPortLocationName; + private String serverDisplayName; + private ServerStatus serverStatus; + private int playerCount; + private int maxPlayers; + + public JoinEntityData(String serverName, UUID entityUUID, String globalPortLocationName, String serverDisplayName, ServerStatus serverStatus, int playerCount, int maxPlayers) { + this.serverName = serverName; + this.entityUUID = entityUUID; + this.globalPortLocationName = globalPortLocationName; + this.serverDisplayName = serverDisplayName; + this.serverStatus = serverStatus; + this.playerCount = playerCount; + this.maxPlayers = maxPlayers; + } + + public String getServerName() { + return serverName; + } + + public UUID getEntityUUID() { + return entityUUID; + } + + public String getGlobalPortLocationName() { + return globalPortLocationName; + } + + public String getServerDisplayName() { + return serverDisplayName; + } + + public ServerStatus getServerStatus() { + return serverStatus; + } + + public void setServerStatus(ServerStatus serverStatus) { + this.serverStatus = serverStatus; + } + + public int getPlayerCount() { + return playerCount; + } + + public void setPlayerCount(int playerCount) { + this.playerCount = playerCount; + } + + public int getMaxPlayers() { + return maxPlayers; + } + + public void setMaxPlayers(int maxPlayers) { + this.maxPlayers = maxPlayers; + } + + public String createCustomEntityName() { + String statusString = serverStatus == ServerStatus.ONLINE ? ChatColor.GREEN + "ONLINE" : + serverStatus == ServerStatus.OFFLINE ? ChatColor.RED + "OFFLINE" : + serverStatus == ServerStatus.RUNNING ? ChatColor.YELLOW + "RUNNING" : ""; + String playerCountString = ChatColor.AQUA + "(" + playerCount + " / " + maxPlayers + ")"; + + return ChatColor.BLUE + serverDisplayName + " " + statusString + " " + playerCountString; + } + + public enum ServerStatus { + ONLINE, RUNNING, OFFLINE + } +} diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/AddEntityCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/AddEntityCommand.java index 1c121a4..77c7f39 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/AddEntityCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/AddEntityCommand.java @@ -3,6 +3,7 @@ import de.cubeside.connection.GlobalServer; import de.fanta.challengesjoinentities.ChallengesJoinEntities; import de.fanta.challengesjoinentities.ChatUtil; +import de.iani.cubesideutils.StringUtil; import de.iani.cubesideutils.bukkit.commands.SubCommand; import de.iani.cubesideutils.commands.ArgsParser; import org.bukkit.command.Command; @@ -44,7 +45,9 @@ String locationName = args.getNext(); if (plugin.getGlobalDataHelper().getServer(serverName) != null) { - plugin.spawnPiglin(((Player) sender).getLocation(), serverName, locationName); + String serverDisplayName = StringUtil.convertColors(args.getAll(serverName)); + + plugin.spawnPiglin(((Player) sender).getLocation(), serverName, locationName, serverDisplayName); ChatUtil.sendNormalMessage(sender, "Piglin hinzugefügt! *grunz*"); } else { ChatUtil.sendErrorMessage(sender, "Server nicht gefunden!"); @@ -64,7 +67,7 @@ @Override public String getUsage() { - return " "; + return " [Anzeigename]"; } @Override