From d076b77686c4ce44d9aa034b33fa6d818110cb01 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Wed, 20 Oct 2021 21:37:38 -0400 Subject: [PATCH] nametags fix --- .../net/tylermurphy/hideAndSeek/Main.java | 2 +- .../hideAndSeek/bukkit/CommandHandler.java | 4 - .../hideAndSeek/bukkit/EventListener.java | 10 +- .../tylermurphy/hideAndSeek/bukkit/Tick.java | 1 + .../tylermurphy/hideAndSeek/command/Join.java | 1 + .../hideAndSeek/command/Leave.java | 6 + .../hideAndSeek/command/Reload.java | 1 - .../hideAndSeek/command/Start.java | 3 +- .../tylermurphy/hideAndSeek/command/Stop.java | 1 + .../tylermurphy/hideAndSeek/util/Board.java | 160 ++++++++++++------ .../tylermurphy/hideAndSeek/util/Util.java | 2 +- 11 files changed, 128 insertions(+), 63 deletions(-) diff --git a/src/main/java/net/tylermurphy/hideAndSeek/Main.java b/src/main/java/net/tylermurphy/hideAndSeek/Main.java index 648df25..6bdc8d2 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/Main.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/Main.java @@ -60,7 +60,7 @@ public class Main extends JavaPlugin implements Listener { //Board board = new Board(); - board.init(); + board.reload(); // Start Tick Timer onTickTask = Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable(){ diff --git a/src/main/java/net/tylermurphy/hideAndSeek/bukkit/CommandHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/bukkit/CommandHandler.java index 23a37a1..af44954 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/bukkit/CommandHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/bukkit/CommandHandler.java @@ -10,7 +10,6 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; -import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.command.*; public class CommandHandler { @@ -40,9 +39,6 @@ public class CommandHandler { } public static boolean handleCommand(CommandSender sender, Command cmd, String label, String[] args) { - if(!Main.plugin.board.isReady()) { - Main.plugin.board.reload(); - } if(sender instanceof Player == false) { sender.sendMessage(errorPrefix + "This command can only be run as a player."); } else if(args.length < 1 || !COMMAND_REGISTER.containsKey(args[0].toLowerCase()) ) { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/bukkit/EventListener.java b/src/main/java/net/tylermurphy/hideAndSeek/bukkit/EventListener.java index 393be5b..da8f714 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/bukkit/EventListener.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/bukkit/EventListener.java @@ -34,11 +34,7 @@ public class EventListener implements Listener { @EventHandler public void onPlayerJoin(PlayerJoinEvent event) { event.getPlayer().setLevel(0); - if(Main.plugin.board.isReady()) { - Main.plugin.board.remove(event.getPlayer()); - } else { - Main.plugin.board.init(); - } + Main.plugin.board.remove(event.getPlayer()); if(!Util.isSetup()) return; if(event.getPlayer().getWorld().getName().equals("hideandseek_"+spawnWorld) || event.getPlayer().getWorld().getName().equals(lobbyWorld)){ event.getPlayer().teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); @@ -70,7 +66,7 @@ public class EventListener implements Listener { Entity damager = ((EntityDamageByEntityEvent)event).getDamager(); if(damager instanceof Player) { attacker = (Player) damager; - if(Main.plugin.board.onSameTeam(p, p)) event.setCancelled(true); + if(Main.plugin.board.onSameTeam(p, attacker)) event.setCancelled(true); if(Main.plugin.board.isSpectator(p)) event.setCancelled(true); } } @@ -99,7 +95,7 @@ public class EventListener implements Listener { @EventHandler public void onProjectile(ProjectileLaunchEvent event) { - if(Main.plugin.status.equals("Playing")) return; + if(!Main.plugin.status.equals("Playing")) return; if(event.getEntity() instanceof Snowball) { Snowball snowball = (Snowball) event.getEntity(); if(snowball.getShooter() instanceof Player) { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/bukkit/Tick.java b/src/main/java/net/tylermurphy/hideAndSeek/bukkit/Tick.java index 3fb48d6..a876df1 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/bukkit/Tick.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/bukkit/Tick.java @@ -67,6 +67,7 @@ public class Tick { if(tick%20 == 0) { if(gameLength > 0) { + Main.plugin.board.reloadGameBoards(); Main.plugin.timeLeft--; for(Player player : Main.plugin.board.getPlayers()) { player.setLevel(Main.plugin.timeLeft); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java index e70411e..3ad24b8 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java @@ -35,6 +35,7 @@ public class Join implements ICommand { else Util.broadcastMessage(messagePrefix + sender.getName() + " has joined the HideAndSeek lobby"); player.teleport(new Location(Bukkit.getWorld(lobbyWorld), lobbyPosition.getX(),lobbyPosition.getY(),lobbyPosition.getZ())); player.setGameMode(GameMode.ADVENTURE); + Main.plugin.board.reloadLobbyBoards(); } else { Main.plugin.board.addSeeker(player); player.sendMessage(messagePrefix + "You have joined mid game and became a spectator"); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java index d79d019..908df5e 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java @@ -30,6 +30,12 @@ public class Leave implements ICommand { else Util.broadcastMessage(messagePrefix + sender.getName() + " has left the HideAndSeek lobby"); Main.plugin.board.remove(player); player.teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); + Main.plugin.board.removeBoard(player); + if(Main.plugin.status.equals("Standby")) { + Main.plugin.board.reloadLobbyBoards(); + } else { + Main.plugin.board.reloadGameBoards(); + } } public String getLabel() { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Reload.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Reload.java index e7ab942..55147d6 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Reload.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Reload.java @@ -16,7 +16,6 @@ public class Reload implements ICommand { return; } Config.loadConfig(); - Main.plugin.board.reload(); sender.sendMessage(messagePrefix + "Reloaded the config"); } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java index 7d5242f..9d75401 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java @@ -63,7 +63,7 @@ public class Start implements ICommand { sender.sendMessage(errorPrefix + "Invalid player: " + seekerName); return; } - Main.plugin.board.init(); + Main.plugin.board.reload(); for(Player temp : Main.plugin.board.getPlayers()) { if(temp.getName().equals(seeker.getName())) continue; @@ -89,6 +89,7 @@ public class Start implements ICommand { player.sendTitle(ChatColor.GOLD + "" + ChatColor.BOLD + "HIDER", ChatColor.WHITE + "Hide away from the seekers", 10, 70, 20); } Worldborder.resetWorldborder("hideandseek_"+spawnWorld); + Main.plugin.board.reloadGameBoards(); Main.plugin.status = "Starting"; int temp = Main.plugin.gameId; Util.broadcastMessage(messagePrefix + "Hiders have 30 seconds to hide!"); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Stop.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Stop.java index cacf7cd..416d673 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Stop.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Stop.java @@ -57,6 +57,7 @@ public class Stop implements ICommand { } } Util.unloadMap("hideandseek_"+spawnWorld); + Main.plugin.board.reloadLobbyBoards(); } public String getUsage() { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/Board.java b/src/main/java/net/tylermurphy/hideAndSeek/util/Board.java index f1fceba..76698aa 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/Board.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/Board.java @@ -10,26 +10,23 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Objective; +import org.bukkit.scoreboard.Score; import org.bukkit.scoreboard.Scoreboard; -import org.bukkit.scoreboard.ScoreboardManager; import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team.Option; import org.bukkit.scoreboard.Team.OptionStatus; +import net.tylermurphy.hideAndSeek.Main; + import static net.tylermurphy.hideAndSeek.Config.*; public class Board { - private Team HiderTeam, SeekerTeam, SpectatorTeam; private List Hider, Seeker, Spectator; private Map playerList = new HashMap(); - private boolean setup = false; - - public boolean isReady() { - return setup; - } - public boolean isPlayer(Player player) { return playerList.containsKey(player.getName()); } @@ -90,7 +87,7 @@ public class Board { Hider.add(player.getName()); Seeker.remove(player.getName()); Spectator.remove(player.getName()); - HiderTeam.addEntry(player.getName()); + //HiderTeam.addEntry(player.getName()); if(!playerList.containsKey(player.getName())) playerList.put(player.getName(), player); } @@ -99,7 +96,7 @@ public class Board { Hider.remove(player.getName()); Seeker.add(player.getName()); Spectator.remove(player.getName()); - SeekerTeam.addEntry(player.getName()); + //SeekerTeam.addEntry(player.getName()); if(!playerList.containsKey(player.getName())) playerList.put(player.getName(), player); } @@ -108,7 +105,7 @@ public class Board { Hider.remove(player.getName()); Seeker.remove(player.getName()); Spectator.add(player.getName()); - SpectatorTeam.addEntry(player.getName()); + //SpectatorTeam.addEntry(player.getName()); if(!playerList.containsKey(player.getName())) playerList.put(player.getName(), player); } @@ -117,9 +114,9 @@ public class Board { Hider.remove(player.getName()); Seeker.remove(player.getName()); Spectator.remove(player.getName()); - HiderTeam.removeEntry(player.getName()); - SeekerTeam.removeEntry(player.getName()); - SpectatorTeam.removeEntry(player.getName()); + //HiderTeam.removeEntry(player.getName()); + //SeekerTeam.removeEntry(player.getName()); + //SpectatorTeam.removeEntry(player.getName()); playerList.remove(player.getName()); } @@ -130,49 +127,116 @@ public class Board { else return false; } - public void init() { + public void reload() { Hider = new ArrayList(); Seeker = new ArrayList(); Spectator = new ArrayList(); - reload(); - } - - public void reload() { - - ScoreboardManager manager = Bukkit.getScoreboardManager(); - if(manager == null) return; - Scoreboard board = manager.getMainScoreboard(); - - try { board.registerNewTeam("Seeker"); } catch(Exception e) {} - SeekerTeam = board.getTeam("Seeker"); - SeekerTeam.setColor(ChatColor.RED); - if(nametagsVisible) SeekerTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.FOR_OTHER_TEAMS); - else SeekerTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.NEVER); - - try { board.registerNewTeam("Hider"); } catch(Exception e) {} - HiderTeam = board.getTeam("Hider"); - HiderTeam.setColor(ChatColor.GOLD); - if(nametagsVisible) HiderTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.FOR_OWN_TEAM); - else HiderTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.NEVER); - - try { board.registerNewTeam("Spectator"); } catch(Exception e) {} - SpectatorTeam = board.getTeam("Spectator"); - SpectatorTeam.setColor(ChatColor.GRAY); - SpectatorTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.NEVER); - - setup = true; } public void reset() { Hider.clear(); Seeker.clear(); Spectator.clear(); - for(String entry : HiderTeam.getEntries()) - HiderTeam.removeEntry(entry); - for(String entry : SeekerTeam.getEntries()) - SeekerTeam.removeEntry(entry); - for(String entry : SpectatorTeam.getEntries()) - SpectatorTeam.removeEntry(entry); +// for(String entry : HiderTeam.getEntries()) +// HiderTeam.removeEntry(entry); +// for(String entry : SeekerTeam.getEntries()) +// SeekerTeam.removeEntry(entry); +// for(String entry : SpectatorTeam.getEntries()) +// SpectatorTeam.removeEntry(entry); + } + + private void createTeamsForBoard(Scoreboard board) { + Team hiderTeam = board.registerNewTeam("Hider"); + for(String name : Hider) + hiderTeam.addEntry(name); + Team seekerTeam = board.registerNewTeam("Seeker"); + for(String name : Seeker) + seekerTeam.addEntry(name); + if(nametagsVisible) { + hiderTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.FOR_OWN_TEAM); + seekerTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.FOR_OTHER_TEAMS); + } else { + hiderTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.NEVER); + seekerTeam.setOption(Option.NAME_TAG_VISIBILITY, OptionStatus.NEVER); + } + } + + private void createLobbyBoard(Player player) { + + Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard(); + Objective obj = board.registerNewObjective("LobbyScoreboard", "dummy", + ChatColor.translateAlternateColorCodes('&', "&l&eHIDE AND SEEK")); + createTeamsForBoard(board); + obj.setDisplaySlot(DisplaySlot.SIDEBAR); + Score waiting = obj.getScore("Waiting to start..."); + waiting.setScore(6); + Score blank1 = obj.getScore(ChatColor.RESET.toString()); + blank1.setScore(5); + Score players = obj.getScore("Players: "+playerList.size()); + players.setScore(4); + Score blank2 = obj.getScore(ChatColor.RESET.toString() + ChatColor.RESET.toString()); + blank2.setScore(3); + Score seeker = obj.getScore(ChatColor.BOLD + "" + ChatColor.RED + "SEEKER%" + ChatColor.WHITE + getSeekerPercent()); + seeker.setScore(2); + Score hider = obj.getScore(ChatColor.BOLD + "" + ChatColor.GOLD + "HIDER%" + ChatColor.WHITE + getHiderPercent()); + hider.setScore(1); + player.setScoreboard(board); + } + + private void createGameBoard(Player player) { + Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard(); + Objective obj = board.registerNewObjective("GameScoreboard", "dummy", + ChatColor.translateAlternateColorCodes('&', "&l&eHIDE AND SEEK")); + createTeamsForBoard(board); + obj.setDisplaySlot(DisplaySlot.SIDEBAR); + Score team = obj.getScore("Team: " + getTeam(player)); + team.setScore(6); + Score blank1 = obj.getScore(ChatColor.RESET.toString()); + blank1.setScore(5); + Score waiting = obj.getScore(ChatColor.GREEN + "Time Left: " + ChatColor.WHITE + Main.plugin.timeLeft/60 + "m" + Main.plugin.timeLeft%60 + "s"); + waiting.setScore(4); + Score blank2 = obj.getScore(ChatColor.RESET.toString() + ChatColor.RESET.toString()); + blank2.setScore(3); + Score seeker = obj.getScore(ChatColor.BOLD + "" + ChatColor.RED + "SEEKERS:" + ChatColor.WHITE + Seeker.size()); + seeker.setScore(2); + Score hider = obj.getScore(ChatColor.BOLD + "" + ChatColor.GOLD + "HIDERS:" + ChatColor.WHITE + Hider.size()); + hider.setScore(1); + player.setScoreboard(board); + } + + public void removeBoard(Player player) { + player.setScoreboard(Bukkit.getScoreboardManager().getMainScoreboard()); + } + + public void reloadLobbyBoards() { + for(Player player : playerList.values()) + createLobbyBoard(player); + } + + public void reloadGameBoards() { + for(Player player : playerList.values()) + createGameBoard(player); + } + + private String getSeekerPercent() { + if(playerList.size() < 2) + return " --"; + else + return " "+(int)(100*(1.0/playerList.size())); + } + + private String getHiderPercent() { + if(playerList.size() < 2) + return " --"; + else + return " "+(int)(100-100*(1.0/playerList.size())); + } + + private String getTeam(Player player) { + if(isHider(player)) return ChatColor.GOLD + "HIDER"; + else if(isSeeker(player)) return ChatColor.RED + "SEEKER"; + else if(isSpectator(player)) return ChatColor.GRAY + "SPECTATOR"; + else return ChatColor.WHITE + "UNKNOWN"; } } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/Util.java b/src/main/java/net/tylermurphy/hideAndSeek/util/Util.java index 7c7e230..ee158dc 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/Util.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/Util.java @@ -49,7 +49,7 @@ public class Util { Bukkit.getScheduler().runTaskLaterAsynchronously(Main.plugin, new Runnable() { public void run() { if(gameId == Main.plugin.gameId) - Util.broadcastMessage(messagePrefix + "Hiders have 1 seconds to hide!"); + Util.broadcastMessage(message); } }, delay); }