nametags fix

This commit is contained in:
Tyler Murphy 2021-10-20 21:37:38 -04:00
parent fc87556d8c
commit d076b77686
11 changed files with 128 additions and 63 deletions

View file

@ -60,7 +60,7 @@ public class Main extends JavaPlugin implements Listener {
//Board //Board
board = new Board(); board = new Board();
board.init(); board.reload();
// Start Tick Timer // Start Tick Timer
onTickTask = Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable(){ onTickTask = Bukkit.getServer().getScheduler().runTaskTimer(this, new Runnable(){

View file

@ -10,7 +10,6 @@ import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.command.*; import net.tylermurphy.hideAndSeek.command.*;
public class CommandHandler { public class CommandHandler {
@ -40,9 +39,6 @@ public class CommandHandler {
} }
public static boolean handleCommand(CommandSender sender, Command cmd, String label, String[] args) { 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) { if(sender instanceof Player == false) {
sender.sendMessage(errorPrefix + "This command can only be run as a player."); sender.sendMessage(errorPrefix + "This command can only be run as a player.");
} else if(args.length < 1 || !COMMAND_REGISTER.containsKey(args[0].toLowerCase()) ) { } else if(args.length < 1 || !COMMAND_REGISTER.containsKey(args[0].toLowerCase()) ) {

View file

@ -34,11 +34,7 @@ public class EventListener implements Listener {
@EventHandler @EventHandler
public void onPlayerJoin(PlayerJoinEvent event) { public void onPlayerJoin(PlayerJoinEvent event) {
event.getPlayer().setLevel(0); event.getPlayer().setLevel(0);
if(Main.plugin.board.isReady()) {
Main.plugin.board.remove(event.getPlayer()); Main.plugin.board.remove(event.getPlayer());
} else {
Main.plugin.board.init();
}
if(!Util.isSetup()) return; if(!Util.isSetup()) return;
if(event.getPlayer().getWorld().getName().equals("hideandseek_"+spawnWorld) || event.getPlayer().getWorld().getName().equals(lobbyWorld)){ 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())); 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(); Entity damager = ((EntityDamageByEntityEvent)event).getDamager();
if(damager instanceof Player) { if(damager instanceof Player) {
attacker = (Player) damager; 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); if(Main.plugin.board.isSpectator(p)) event.setCancelled(true);
} }
} }
@ -99,7 +95,7 @@ public class EventListener implements Listener {
@EventHandler @EventHandler
public void onProjectile(ProjectileLaunchEvent event) { public void onProjectile(ProjectileLaunchEvent event) {
if(Main.plugin.status.equals("Playing")) return; if(!Main.plugin.status.equals("Playing")) return;
if(event.getEntity() instanceof Snowball) { if(event.getEntity() instanceof Snowball) {
Snowball snowball = (Snowball) event.getEntity(); Snowball snowball = (Snowball) event.getEntity();
if(snowball.getShooter() instanceof Player) { if(snowball.getShooter() instanceof Player) {

View file

@ -67,6 +67,7 @@ public class Tick {
if(tick%20 == 0) { if(tick%20 == 0) {
if(gameLength > 0) { if(gameLength > 0) {
Main.plugin.board.reloadGameBoards();
Main.plugin.timeLeft--; Main.plugin.timeLeft--;
for(Player player : Main.plugin.board.getPlayers()) { for(Player player : Main.plugin.board.getPlayers()) {
player.setLevel(Main.plugin.timeLeft); player.setLevel(Main.plugin.timeLeft);

View file

@ -35,6 +35,7 @@ public class Join implements ICommand {
else Util.broadcastMessage(messagePrefix + sender.getName() + " has joined the HideAndSeek lobby"); 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.teleport(new Location(Bukkit.getWorld(lobbyWorld), lobbyPosition.getX(),lobbyPosition.getY(),lobbyPosition.getZ()));
player.setGameMode(GameMode.ADVENTURE); player.setGameMode(GameMode.ADVENTURE);
Main.plugin.board.reloadLobbyBoards();
} else { } else {
Main.plugin.board.addSeeker(player); Main.plugin.board.addSeeker(player);
player.sendMessage(messagePrefix + "You have joined mid game and became a spectator"); player.sendMessage(messagePrefix + "You have joined mid game and became a spectator");

View file

@ -30,6 +30,12 @@ public class Leave implements ICommand {
else Util.broadcastMessage(messagePrefix + sender.getName() + " has left the HideAndSeek lobby"); else Util.broadcastMessage(messagePrefix + sender.getName() + " has left the HideAndSeek lobby");
Main.plugin.board.remove(player); Main.plugin.board.remove(player);
player.teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); 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() { public String getLabel() {

View file

@ -16,7 +16,6 @@ public class Reload implements ICommand {
return; return;
} }
Config.loadConfig(); Config.loadConfig();
Main.plugin.board.reload();
sender.sendMessage(messagePrefix + "Reloaded the config"); sender.sendMessage(messagePrefix + "Reloaded the config");
} }

View file

@ -63,7 +63,7 @@ public class Start implements ICommand {
sender.sendMessage(errorPrefix + "Invalid player: " + seekerName); sender.sendMessage(errorPrefix + "Invalid player: " + seekerName);
return; return;
} }
Main.plugin.board.init(); Main.plugin.board.reload();
for(Player temp : Main.plugin.board.getPlayers()) { for(Player temp : Main.plugin.board.getPlayers()) {
if(temp.getName().equals(seeker.getName())) if(temp.getName().equals(seeker.getName()))
continue; 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); player.sendTitle(ChatColor.GOLD + "" + ChatColor.BOLD + "HIDER", ChatColor.WHITE + "Hide away from the seekers", 10, 70, 20);
} }
Worldborder.resetWorldborder("hideandseek_"+spawnWorld); Worldborder.resetWorldborder("hideandseek_"+spawnWorld);
Main.plugin.board.reloadGameBoards();
Main.plugin.status = "Starting"; Main.plugin.status = "Starting";
int temp = Main.plugin.gameId; int temp = Main.plugin.gameId;
Util.broadcastMessage(messagePrefix + "Hiders have 30 seconds to hide!"); Util.broadcastMessage(messagePrefix + "Hiders have 30 seconds to hide!");

View file

@ -57,6 +57,7 @@ public class Stop implements ICommand {
} }
} }
Util.unloadMap("hideandseek_"+spawnWorld); Util.unloadMap("hideandseek_"+spawnWorld);
Main.plugin.board.reloadLobbyBoards();
} }
public String getUsage() { public String getUsage() {

View file

@ -10,26 +10,23 @@ import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; 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.Scoreboard;
import org.bukkit.scoreboard.ScoreboardManager;
import org.bukkit.scoreboard.Team; import org.bukkit.scoreboard.Team;
import org.bukkit.scoreboard.Team.Option; import org.bukkit.scoreboard.Team.Option;
import org.bukkit.scoreboard.Team.OptionStatus; import org.bukkit.scoreboard.Team.OptionStatus;
import net.tylermurphy.hideAndSeek.Main;
import static net.tylermurphy.hideAndSeek.Config.*; import static net.tylermurphy.hideAndSeek.Config.*;
public class Board { public class Board {
private Team HiderTeam, SeekerTeam, SpectatorTeam;
private List<String> Hider, Seeker, Spectator; private List<String> Hider, Seeker, Spectator;
private Map<String, Player> playerList = new HashMap<String,Player>(); private Map<String, Player> playerList = new HashMap<String,Player>();
private boolean setup = false;
public boolean isReady() {
return setup;
}
public boolean isPlayer(Player player) { public boolean isPlayer(Player player) {
return playerList.containsKey(player.getName()); return playerList.containsKey(player.getName());
} }
@ -90,7 +87,7 @@ public class Board {
Hider.add(player.getName()); Hider.add(player.getName());
Seeker.remove(player.getName()); Seeker.remove(player.getName());
Spectator.remove(player.getName()); Spectator.remove(player.getName());
HiderTeam.addEntry(player.getName()); //HiderTeam.addEntry(player.getName());
if(!playerList.containsKey(player.getName())) if(!playerList.containsKey(player.getName()))
playerList.put(player.getName(), player); playerList.put(player.getName(), player);
} }
@ -99,7 +96,7 @@ public class Board {
Hider.remove(player.getName()); Hider.remove(player.getName());
Seeker.add(player.getName()); Seeker.add(player.getName());
Spectator.remove(player.getName()); Spectator.remove(player.getName());
SeekerTeam.addEntry(player.getName()); //SeekerTeam.addEntry(player.getName());
if(!playerList.containsKey(player.getName())) if(!playerList.containsKey(player.getName()))
playerList.put(player.getName(), player); playerList.put(player.getName(), player);
} }
@ -108,7 +105,7 @@ public class Board {
Hider.remove(player.getName()); Hider.remove(player.getName());
Seeker.remove(player.getName()); Seeker.remove(player.getName());
Spectator.add(player.getName()); Spectator.add(player.getName());
SpectatorTeam.addEntry(player.getName()); //SpectatorTeam.addEntry(player.getName());
if(!playerList.containsKey(player.getName())) if(!playerList.containsKey(player.getName()))
playerList.put(player.getName(), player); playerList.put(player.getName(), player);
} }
@ -117,9 +114,9 @@ public class Board {
Hider.remove(player.getName()); Hider.remove(player.getName());
Seeker.remove(player.getName()); Seeker.remove(player.getName());
Spectator.remove(player.getName()); Spectator.remove(player.getName());
HiderTeam.removeEntry(player.getName()); //HiderTeam.removeEntry(player.getName());
SeekerTeam.removeEntry(player.getName()); //SeekerTeam.removeEntry(player.getName());
SpectatorTeam.removeEntry(player.getName()); //SpectatorTeam.removeEntry(player.getName());
playerList.remove(player.getName()); playerList.remove(player.getName());
} }
@ -130,49 +127,116 @@ public class Board {
else return false; else return false;
} }
public void init() { public void reload() {
Hider = new ArrayList<String>(); Hider = new ArrayList<String>();
Seeker = new ArrayList<String>(); Seeker = new ArrayList<String>();
Spectator = new ArrayList<String>(); Spectator = new ArrayList<String>();
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() { public void reset() {
Hider.clear(); Hider.clear();
Seeker.clear(); Seeker.clear();
Spectator.clear(); Spectator.clear();
for(String entry : HiderTeam.getEntries()) // for(String entry : HiderTeam.getEntries())
HiderTeam.removeEntry(entry); // HiderTeam.removeEntry(entry);
for(String entry : SeekerTeam.getEntries()) // for(String entry : SeekerTeam.getEntries())
SeekerTeam.removeEntry(entry); // SeekerTeam.removeEntry(entry);
for(String entry : SpectatorTeam.getEntries()) // for(String entry : SpectatorTeam.getEntries())
SpectatorTeam.removeEntry(entry); // 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";
} }
} }

View file

@ -49,7 +49,7 @@ public class Util {
Bukkit.getScheduler().runTaskLaterAsynchronously(Main.plugin, new Runnable() { Bukkit.getScheduler().runTaskLaterAsynchronously(Main.plugin, new Runnable() {
public void run() { public void run() {
if(gameId == Main.plugin.gameId) if(gameId == Main.plugin.gameId)
Util.broadcastMessage(messagePrefix + "Hiders have 1 seconds to hide!"); Util.broadcastMessage(message);
} }
}, delay); }, delay);
} }