From f900dd326828c63a5c81eb123ee6848e1c72b8ea Mon Sep 17 00:00:00 2001 From: bobby29831 Date: Fri, 13 May 2022 11:28:13 -0500 Subject: [PATCH] refactor: clean up reused code --- .../hideAndSeek/command/SetBorder.java | 4 +-- .../hideAndSeek/configuration/Config.java | 6 ++--- .../hideAndSeek/database/PlayerInfo.java | 11 ++++++-- .../hideAndSeek/game/CommandHandler.java | 2 +- .../hideAndSeek/game/EventListener.java | 27 ++++++++----------- .../tylermurphy/hideAndSeek/game/Game.java | 21 +++++++-------- .../hideAndSeek/world/WorldLoader.java | 4 +-- 7 files changed, 37 insertions(+), 38 deletions(-) diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/SetBorder.java b/src/main/java/net/tylermurphy/hideAndSeek/command/SetBorder.java index 11ff225..8b578f0 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/SetBorder.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/SetBorder.java @@ -44,7 +44,7 @@ public class SetBorder implements ICommand { addToConfig("worldBorder.enabled",false); saveConfig(); sender.sendMessage(messagePrefix + message("WORLDBORDER_DISABLE")); - Game.resetWorldborder(spawnWorld); + Game.resetWorldBorder(spawnWorld); return; } int num,delay,change; @@ -90,7 +90,7 @@ public class SetBorder implements ICommand { addToConfig("worldBorder.move", worldborderChange); sender.sendMessage(messagePrefix + message("WORLDBORDER_ENABLE").addAmount(num).addAmount(delay)); saveConfig(); - Game.resetWorldborder(spawnWorld); + Game.resetWorldBorder(spawnWorld); } public String getLabel() { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java index 22f4480..5ce2942 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java @@ -39,8 +39,8 @@ import java.util.Optional; public class Config { - private static ConfigManager config, leaderboard; - + private static ConfigManager config; + public static String messagePrefix, errorPrefix, @@ -150,7 +150,7 @@ public class Config { config = ConfigManager.create("config.yml"); config.saveConfig(); - leaderboard = ConfigManager.create("leaderboard.yml"); + ConfigManager leaderboard = ConfigManager.create("leaderboard.yml"); //Spawn spawnPosition = new Vector( diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java b/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java index 5fbb96b..0070446 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java @@ -23,8 +23,15 @@ import java.util.UUID; public class PlayerInfo { - public UUID uuid; - public int hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths; + public final UUID uuid; + public final int hider_wins; + public final int seeker_wins; + public final int hider_games; + public final int seeker_games; + public final int hider_kills; + public final int seeker_kills; + public final int hider_deaths; + public final int seeker_deaths; public PlayerInfo(UUID uuid, int hider_wins, int seeker_wins, int hider_games, int seeker_games, int hider_kills, int seeker_kills, int hider_deaths, int seeker_deaths) { this.uuid = uuid; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/CommandHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/CommandHandler.java index 430e645..85a81dd 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/CommandHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/CommandHandler.java @@ -34,7 +34,7 @@ import static net.tylermurphy.hideAndSeek.configuration.Localization.message; public class CommandHandler { - public static Map COMMAND_REGISTER = new LinkedHashMap<>(); + public static final Map COMMAND_REGISTER = new LinkedHashMap<>(); private static void registerCommand(ICommand command) { if(!COMMAND_REGISTER.containsKey(command.getLabel())) { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java b/src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java index 1b49218..28110fb 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java @@ -76,32 +76,27 @@ public class EventListener implements Listener { } } - @EventHandler(priority = EventPriority.MONITOR) - public void onQuit(PlayerQuitEvent event) { - Board.remove(event.getPlayer()); + private void handleLeave(Player player) { + Board.remove(player); if(Game.status == Status.STANDBY) { Board.reloadLobbyBoards(); } else { Board.reloadGameBoards(); } - for(PotionEffect effect : event.getPlayer().getActivePotionEffects()){ - event.getPlayer().removePotionEffect(effect.getType()); + for(PotionEffect effect : player.getActivePotionEffects()){ + player.removePotionEffect(effect.getType()); } - Game.removeItems(event.getPlayer()); + Game.removeItems(player); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onQuit(PlayerQuitEvent event) { + handleLeave(event.getPlayer()); } @EventHandler(priority = EventPriority.MONITOR) public void onKick(PlayerKickEvent event) { - Board.remove(event.getPlayer()); - if(Game.status == Status.STANDBY) { - Board.reloadLobbyBoards(); - } else { - Board.reloadGameBoards(); - } - for(PotionEffect effect : event.getPlayer().getActivePotionEffects()){ - event.getPlayer().removePotionEffect(effect.getType()); - } - Game.removeItems(event.getPlayer()); + handleLeave(event.getPlayer()); } @EventHandler(priority = EventPriority.HIGHEST) diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java index d49ad1a..cc38bb0 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java @@ -204,14 +204,7 @@ public class Game { Board.removeBoard(player); Board.remove(player); player.getInventory().clear(); - if(bungeeLeave) { - ByteArrayDataOutput out = ByteStreams.newDataOutput(); - out.writeUTF("Connect"); - out.writeUTF(leaveServer); - player.sendPluginMessage(Main.plugin, "BungeeCord", out.toByteArray()); - } else { - player.teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); - } + handleBungeeLeave(player); } else { player.teleport(new Location(Bukkit.getWorld(lobbyWorld), lobbyPosition.getX(),lobbyPosition.getY(),lobbyPosition.getZ())); Board.createLobbyBoard(player); @@ -274,7 +267,7 @@ public class Game { } } - public static void leave(Player player){ + public static void leave(Player player) { player.setFlying(false); player.setAllowFlight(false); player.setFallDistance(0.0F); @@ -299,7 +292,11 @@ public class Game { for(PotionEffect effect : player.getActivePotionEffects()){ player.removePotionEffect(effect.getType()); } - if(bungeeLeave) { + handleBungeeLeave(player); + } + + private static void handleBungeeLeave(Player player) { + if (bungeeLeave) { ByteArrayDataOutput out = ByteStreams.newDataOutput(); out.writeUTF("Connect"); out.writeUTF(leaveServer); @@ -336,7 +333,7 @@ public class Game { } } - private static void whileStarting(){ + private static void whileStarting() { for(Player spectator : Board.getSpectators()){ spectator.setFlying(spectator.getAllowFlight()); } @@ -388,7 +385,7 @@ public class Game { checkWinConditions(); } - public static void resetWorldborder(String worldName){ + public static void resetWorldBorder(String worldName){ worldBorder = new Border(); worldBorder.resetWorldborder(worldName); } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java b/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java index 5a5c17a..e4d8b1a 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/world/WorldLoader.java @@ -31,8 +31,8 @@ import static net.tylermurphy.hideAndSeek.configuration.Localization.message; public class WorldLoader { - String mapname; - String savename; + final String mapname; + final String savename; public WorldLoader(String mapname) { this.mapname = mapname;