diff --git a/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java b/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java index b628b8b..dde1d71 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java +++ b/src/main/java/de/fanta/challengesjoinentities/ChallengesGlobalDataHelper.java @@ -1,14 +1,18 @@ package de.fanta.challengesjoinentities; +import de.cubeside.connection.event.GlobalServerConnectedEvent; +import de.cubeside.connection.event.GlobalServerDisconnectedEvent; import de.fanta.challenges.Challenges; import de.fanta.challengesjoinentities.ChallengesGlobalDataHelper.ChallengeMessageType; import de.iani.cubesideutils.bukkit.plugin.api.GlobalDataHelperBukkit; import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import java.io.DataInputStream; import java.io.IOException; -public class ChallengesGlobalDataHelper extends GlobalDataHelperBukkit { +public class ChallengesGlobalDataHelper extends GlobalDataHelperBukkit implements Listener { public static final String CHANNEL = "ChallengesJoinEntities"; @@ -25,18 +29,18 @@ switch (challengeMessageType) { case TIMER: boolean running = data.readBoolean(); - plugin.setTimerStatus(serverName, running); + plugin.updateTimerStatus(serverName, running); break; case SERVER_STATUS: boolean online = data.readBoolean(); - plugin.setServerStatus(serverName, online); + plugin.updateServerStatus(serverName, online); break; case PLAYER_COUNT: { int count = data.readInt(); int maxPlayers = data.readInt(); - plugin.setPlayerCount(serverName, count, maxPlayers); + plugin.updatePlayerCount(serverName, count, maxPlayers); break; } @@ -56,7 +60,8 @@ boolean timerRunning = data.readBoolean(); int playerCount = data.readInt(); int maxPlayers = data.readInt(); - plugin.updateEntityData(serverName, timerRunning, playerCount, maxPlayers); + plugin.updateTimerStatus(serverName, timerRunning); + plugin.updatePlayerCount(serverName, playerCount, maxPlayers); break; } @@ -77,8 +82,8 @@ sendData(ChallengeMessageType.PLAYER_COUNT, getThisServerName(), count, Bukkit.getMaxPlayers()); } - public void requestInitialData(String serverName) { - sendData(getServer(serverName), ChallengeMessageType.INITIAL_DATA_REQUEST, getThisServerName()); + public void requestInitialData(String fromServer) { + sendData(getServer(fromServer), ChallengeMessageType.INITIAL_DATA_REQUEST, getThisServerName()); } public void sendInitialData(String toServer, boolean timerRunning, int playerCount, int maxPlayers) { @@ -88,4 +93,19 @@ public enum ChallengeMessageType { TIMER, SERVER_STATUS, PLAYER_COUNT, INITIAL_DATA_REQUEST, INITIAL_DATA } + + @EventHandler + public void onGlobalServerConnected(GlobalServerConnectedEvent event) { + if (plugin.getEntityData(event.getServer().getName()) != null) { + plugin.updateServerStatus(event.getServer().getName(), true); + this.requestInitialData(event.getServer().getName()); + } + } + + @EventHandler + public void onGlobalServerDisconnected(GlobalServerDisconnectedEvent event) { + if (plugin.getEntityData(event.getServer().getName()) != null) { + plugin.updateServerStatus(event.getServer().getName(), false); + } + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java index b3ca034..a18acea 100644 --- a/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java +++ b/src/main/java/de/fanta/challengesjoinentities/ChallengesJoinEntities.java @@ -26,10 +26,12 @@ private Config config; private ChallengesGlobalDataHelper globalDataHelper; - private Map entities; + private Map entityData; + private Map entityServerMapping; public ChallengesJoinEntities() { - this.entities = new HashMap<>(); + this.entityData = new HashMap<>(); + this.entityServerMapping = new HashMap<>(); } @Override @@ -45,11 +47,12 @@ router.addCommandMapping(new RemoveEntityCommand(this), "remove"); } Bukkit.getPluginManager().registerEvents(new EntityListener(this), this); + Bukkit.getPluginManager().registerEvents(globalDataHelper, this); } public void spawnPiglin(Location location, String serverName, String gpLocationName, String serverDisplayName) { - if (entities.containsKey(serverName)) { - despawnPiglin(entities.get(serverName).getEntityUUID(), serverName); + if (entityData.containsKey(serverName)) { + despawnPiglin(entityData.get(serverName).getEntityUUID(), serverName); } CraftPiglin piglin = (CraftPiglin) location.getWorld().spawnEntity(location, EntityType.PIGLIN); @@ -78,7 +81,8 @@ 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); + this.entityData.put(serverName, entityData); + this.entityServerMapping.put(piglinUUID, serverName); if (saveToConfig) { this.config.savePiglin(entityData); @@ -86,53 +90,49 @@ } public void removePiglin(String serverName) { - this.entities.remove(serverName); + JoinEntityData data = this.entityData.remove(serverName); + if (data != null) { + this.entityServerMapping.remove(data.getEntityUUID()); + } this.config.removePiglin(serverName); } - public void setTimerStatus(String serverName, boolean running) { - CraftPiglin piglin = getPiglinForServerName(serverName); - JoinEntityData data = entities.get(serverName); - - if (piglin != null && data != null) { - piglin.getHandle().u(!running); + public void updateTimerStatus(String serverName, boolean running) { + JoinEntityData data = entityData.get(serverName); + if (data != null) { data.setServerStatus(running ? ServerStatus.RUNNING : ServerStatus.ONLINE); - updateCustomName(piglin, serverName); + + updatePiglinCustomName(serverName); + updatePiglinBehaviour(serverName); } } - public void setServerStatus(String serverName, boolean online) { - CraftPiglin piglin = getPiglinForServerName(serverName); - JoinEntityData data = entities.get(serverName); + public void updateServerStatus(String serverName, boolean online) { + JoinEntityData data = entityData.get(serverName); - if (piglin != null && data != null) { - if (!online) { - piglin.getHandle().u(false); - piglin.getEquipment().setItemInOffHand(new ItemStack(Material.GOLD_INGOT)); + if (data != null) { + data.setServerStatus(online ? ServerStatus.ONLINE : ServerStatus.OFFLINE); - data.setServerStatus(ServerStatus.OFFLINE); - } else { - piglin.getEquipment().clear(); - data.setServerStatus(ServerStatus.ONLINE); - } - updateCustomName(piglin, serverName); + updatePiglinCustomName(serverName); + updatePiglinBehaviour(serverName); } } - public void setPlayerCount(String serverName, int count, int maxPlayers) { - CraftPiglin piglin = getPiglinForServerName(serverName); - JoinEntityData data = entities.get(serverName); + public void updatePlayerCount(String serverName, int count, int maxPlayers) { + JoinEntityData data = entityData.get(serverName); - if (piglin != null && data != null) { + if (data != null) { data.setPlayerCount(count); data.setMaxPlayers(maxPlayers); - updateCustomName(piglin, serverName); + + updatePiglinCustomName(serverName); + updatePiglinBehaviour(serverName); } } public CraftPiglin getPiglinForServerName(String serverName) { - JoinEntityData data = entities.get(serverName); + JoinEntityData data = entityData.get(serverName); if (data != null) { Entity piglin = Bukkit.getEntity(data.getEntityUUID()); @@ -143,26 +143,46 @@ 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); - } - + public void updatePiglinCustomName(String serverName) { CraftPiglin piglin = getPiglinForServerName(serverName); - if (piglin != null) { - updateCustomName(piglin, serverName); - } - } + JoinEntityData data = entityData.get(serverName); - public void updateCustomName(CraftPiglin piglin, String serverName) { - JoinEntityData data = entities.get(serverName); - if (data != null) { + if (piglin != null && data != null) { String customName = data.createCustomEntityName(); piglin.setCustomName(customName); } } + public void updatePiglinBehaviour(String serverName) { + CraftPiglin piglin = getPiglinForServerName(serverName); + JoinEntityData data = entityData.get(serverName); + + if (piglin != null && data != null) { + if (data.getPlayerCount() < data.getMaxPlayers()) { + switch (data.getServerStatus()) { + case ONLINE: + piglin.getEquipment().clear(); + piglin.getHandle().u(true); + break; + + case OFFLINE: + piglin.getHandle().u(false); + piglin.getEquipment().setItemInOffHand(new ItemStack(Material.GOLD_INGOT)); + break; + + case RUNNING: + piglin.getEquipment().clear(); + piglin.getHandle().u(false); + break; + } + } else { // server is full + piglin.getEquipment().clear(); + piglin.getHandle().u(false); + } + } + + } + // TODO bounce player back when too close to piglin @@ -173,4 +193,20 @@ public ChallengesGlobalDataHelper getGlobalDataHelper() { return globalDataHelper; } + + public boolean isJoinEntity(Entity entity) { + return entityServerMapping.containsKey(entity.getUniqueId()); + } + + public JoinEntityData getEntityData(String serverName) { + return entityData.get(serverName); + } + + public String getServerNameForEntity(UUID entityUUID) { + return entityServerMapping.get(entityUUID); + } + + public Map getEntityData() { + return entityData; + } } diff --git a/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java b/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java index 54665a0..e00a819 100644 --- a/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java +++ b/src/main/java/de/fanta/challengesjoinentities/JoinEntityData.java @@ -70,7 +70,7 @@ serverStatus == ServerStatus.RUNNING ? ChatColor.YELLOW + "RUNNING" : ""; String playerCountString = ChatColor.AQUA + "(" + playerCount + " / " + maxPlayers + ")"; - return ChatColor.BLUE + serverDisplayName + " " + statusString + " " + playerCountString; + return ChatColor.BLUE + serverDisplayName + " " + statusString + (serverStatus != ServerStatus.OFFLINE ? (" " + playerCountString) : ""); } public enum ServerStatus { diff --git a/src/main/java/de/fanta/challengesjoinentities/commands/RemoveEntityCommand.java b/src/main/java/de/fanta/challengesjoinentities/commands/RemoveEntityCommand.java index c8df3a1..72c08a9 100644 --- a/src/main/java/de/fanta/challengesjoinentities/commands/RemoveEntityCommand.java +++ b/src/main/java/de/fanta/challengesjoinentities/commands/RemoveEntityCommand.java @@ -47,7 +47,7 @@ public Collection onTabComplete(CommandSender sender, Command command, String alias, ArgsParser args) { args.next(); if (!args.hasNext()) { - return plugin.getGlobalDataHelper().getServers().stream().map(GlobalServer::getName).collect(Collectors.toList()); + return plugin.getEntityData().keySet(); } return Collections.emptyList(); diff --git a/src/main/java/de/fanta/challengesjoinentities/listeners/EntityListener.java b/src/main/java/de/fanta/challengesjoinentities/listeners/EntityListener.java index 1268905..51f8fa9 100644 --- a/src/main/java/de/fanta/challengesjoinentities/listeners/EntityListener.java +++ b/src/main/java/de/fanta/challengesjoinentities/listeners/EntityListener.java @@ -1,15 +1,56 @@ package de.fanta.challengesjoinentities.listeners; import de.fanta.challengesjoinentities.ChallengesJoinEntities; +import de.fanta.challengesjoinentities.ChatUtil; +import de.fanta.challengesjoinentities.JoinEntityData; +import de.speedy64.globalport.GlobalApi; +import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent; +import org.bukkit.event.player.PlayerInteractEntityEvent; +import org.bukkit.inventory.EquipmentSlot; public class EntityListener implements Listener { - private ChallengesJoinEntities plugin; + private final ChallengesJoinEntities plugin; public EntityListener(ChallengesJoinEntities plugin) { this.plugin = plugin; } + @EventHandler + public void onEntityInteract(PlayerInteractEntityEvent event) { + if (!plugin.isJoinEntity(event.getRightClicked()) || event.getHand() != EquipmentSlot.HAND) { + return; + } + String serverName = plugin.getServerNameForEntity(event.getRightClicked().getUniqueId()); + if (serverName != null) { + JoinEntityData data = plugin.getEntityData(serverName); + if (data != null) { + switch (data.getServerStatus()) { + case ONLINE: + if (data.getPlayerCount() < data.getMaxPlayers()) { + GlobalApi.portOnlinePlayerToLocation(event.getPlayer().getName(), data.getGlobalPortLocationName()); + } else { + ChatUtil.sendErrorMessage(event.getPlayer(), "Der Server ist bereits voll!"); + } + break; + case OFFLINE: + ChatUtil.sendErrorMessage(event.getPlayer(), "Der Server ist gerade offline."); + break; + case RUNNING: + ChatUtil.sendErrorMessage(event.getPlayer(), "Hier läuft bereits eine Challenge!"); + break; + } + } + } + } + + @EventHandler + public void onEntityDamage(EntityDamageEvent event) { + if (plugin.isJoinEntity(event.getEntity())) { + event.setCancelled(true); + } + } }