From cd0f60bd1369fe80b0163c8f85de8540d01b0524 Mon Sep 17 00:00:00 2001 From: bobby29831 Date: Fri, 13 May 2022 11:51:20 -0500 Subject: [PATCH] refactor: break apart over saturated EventListener.java --- .../net/tylermurphy/hideAndSeek/Main.java | 16 +- .../tylermurphy/hideAndSeek/command/Join.java | 2 +- .../hideAndSeek/command/Leave.java | 2 +- .../hideAndSeek/command/Start.java | 2 +- .../tylermurphy/hideAndSeek/game/Board.java | 6 +- .../hideAndSeek/game/EventListener.java | 345 ------------------ .../tylermurphy/hideAndSeek/game/Game.java | 54 +-- .../tylermurphy/hideAndSeek/game/Glow.java | 54 +++ .../game/listener/BlockedCommandHandler.java | 37 ++ .../game/listener/ChatHandler.java | 28 ++ .../game/listener/DamageHandler.java | 111 ++++++ .../game/listener/InteractHandler.java | 74 ++++ .../game/listener/JoinLeaveHandler.java | 70 ++++ .../game/listener/MovementHandler.java | 27 ++ .../game/listener/PlayerHandler.java | 52 +++ .../game/listener/RespawnHandler.java | 41 +++ 16 files changed, 518 insertions(+), 403 deletions(-) delete mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/Glow.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/BlockedCommandHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/ChatHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/InteractHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/PlayerHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/RespawnHandler.java diff --git a/src/main/java/net/tylermurphy/hideAndSeek/Main.java b/src/main/java/net/tylermurphy/hideAndSeek/Main.java index 8267ea5..9506d7c 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/Main.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/Main.java @@ -25,8 +25,8 @@ import net.tylermurphy.hideAndSeek.configuration.Localization; import net.tylermurphy.hideAndSeek.database.Database; import net.tylermurphy.hideAndSeek.game.Board; import net.tylermurphy.hideAndSeek.game.CommandHandler; -import net.tylermurphy.hideAndSeek.game.EventListener; import net.tylermurphy.hideAndSeek.game.Game; +import net.tylermurphy.hideAndSeek.game.listener.*; import net.tylermurphy.hideAndSeek.util.PAPIExpansion; import net.tylermurphy.hideAndSeek.util.TabCompleter; import net.tylermurphy.hideAndSeek.util.UUIDFetcher; @@ -50,7 +50,8 @@ public class Main extends JavaPlugin implements Listener { plugin = this; root = this.getServer().getWorldContainer(); data = this.getDataFolder(); - getServer().getPluginManager().registerEvents(new EventListener(), this); + + this.registerListeners(); Config.loadConfig(); Localization.loadLocalization(); @@ -82,6 +83,17 @@ public class Main extends JavaPlugin implements Listener { UUIDFetcher.cleanup(); Board.cleanup(); } + + private void registerListeners() { + getServer().getPluginManager().registerEvents(new BlockedCommandHandler(), this); + getServer().getPluginManager().registerEvents(new ChatHandler(), this); + getServer().getPluginManager().registerEvents(new DamageHandler(), this); + getServer().getPluginManager().registerEvents(new InteractHandler(), this); + getServer().getPluginManager().registerEvents(new JoinLeaveHandler(), this); + getServer().getPluginManager().registerEvents(new MovementHandler(), this); + getServer().getPluginManager().registerEvents(new PlayerHandler(), this); + getServer().getPluginManager().registerEvents(new RespawnHandler(), this); + } public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) { return CommandHandler.handleCommand(sender, args); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java index 025ec7f..8fb002f 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Join.java @@ -40,7 +40,7 @@ public class Join implements ICommand { sender.sendMessage(errorPrefix + message("COMMAND_ERROR")); return; } - if(Board.isPlayer(player)){ + if(Board.contains(player)){ sender.sendMessage(errorPrefix + message("GAME_INGAME")); return; } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java index c625fdc..e6e95ee 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Leave.java @@ -40,7 +40,7 @@ public class Leave implements ICommand { sender.sendMessage(errorPrefix + message("COMMAND_ERROR")); return; } - if(!Board.isPlayer(player)) { + if(!Board.contains(player)) { sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME")); return; } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java index bab9c5f..87ef2af 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Start.java @@ -45,7 +45,7 @@ public class Start implements ICommand { sender.sendMessage(errorPrefix + message("GAME_INPROGRESS")); return; } - if(!Board.isPlayer(sender)) { + if(!Board.contains(sender)) { sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME")); return; } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java index 8177bee..da9e755 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java @@ -40,12 +40,12 @@ public class Board { private static final Map customBoards = new HashMap<>(); private static final Map hider_kills = new HashMap<>(), seeker_kills = new HashMap<>(), hider_deaths = new HashMap<>(), seeker_deaths = new HashMap<>(); - public static boolean isPlayer(Player player) { + public static boolean contains(Player player) { return playerList.containsKey(player.getUniqueId().toString()); } - public static boolean isPlayer(CommandSender sender) { - return playerList.containsKey(Bukkit.getPlayer(sender.getName()).getUniqueId().toString()); + public static boolean contains(CommandSender sender) { + return contains((Player) sender); } public static boolean isHider(Player player) { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java b/src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java deleted file mode 100644 index 28110fb..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/EventListener.java +++ /dev/null @@ -1,345 +0,0 @@ -/* - * This file is part of Kenshins Hide and Seek - * - * Copyright (c) 2021 Tyler Murphy. - * - * Kenshins Hide and Seek free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * he Free Software Foundation version 3. - * - * Kenshins Hide and Seek is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * - */ - -package net.tylermurphy.hideAndSeek.game; - -import com.cryptomorin.xseries.XMaterial; -import com.cryptomorin.xseries.XSound; -import net.tylermurphy.hideAndSeek.Main; -import net.tylermurphy.hideAndSeek.util.Status; -import net.tylermurphy.hideAndSeek.util.Version; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import org.bukkit.GameMode; -import org.bukkit.Location; -import org.bukkit.attribute.Attribute; -import org.bukkit.attribute.AttributeInstance; -import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.entity.*; -import org.bukkit.event.entity.EntityRegainHealthEvent.RegainReason; -import org.bukkit.event.inventory.InventoryClickEvent; -import org.bukkit.event.player.*; -import org.bukkit.inventory.ItemStack; -import org.bukkit.potion.PotionEffect; - -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; - -import static net.tylermurphy.hideAndSeek.configuration.Config.*; -import static net.tylermurphy.hideAndSeek.configuration.Localization.message; - -public class EventListener implements Listener { - - @EventHandler(priority = EventPriority.HIGHEST) - public void onPlayerJoin(PlayerJoinEvent event) { - Board.remove(event.getPlayer()); - Game.removeItems(event.getPlayer()); - if(Game.isNotSetup()) return; - if(autoJoin){ - Game.join(event.getPlayer()); - } else if(teleportToExit) { - if (event.getPlayer().getWorld().getName().equals(Game.getGameWorld()) || event.getPlayer().getWorld().getName().equals(lobbyWorld)) { - event.getPlayer().teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); - event.getPlayer().setGameMode(GameMode.ADVENTURE); - } - } else { - if (mapSaveEnabled && event.getPlayer().getWorld().getName().equals(Game.getGameWorld())) { - if(Game.status != Status.STANDBY && Game.status != Status.ENDING){ - Game.join(event.getPlayer()); - } else { - event.getPlayer().teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); - event.getPlayer().setGameMode(GameMode.ADVENTURE); - } - } - } - } - - private void handleLeave(Player player) { - Board.remove(player); - if(Game.status == Status.STANDBY) { - Board.reloadLobbyBoards(); - } else { - Board.reloadGameBoards(); - } - for(PotionEffect effect : player.getActivePotionEffects()){ - player.removePotionEffect(effect.getType()); - } - Game.removeItems(player); - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onQuit(PlayerQuitEvent event) { - handleLeave(event.getPlayer()); - } - - @EventHandler(priority = EventPriority.MONITOR) - public void onKick(PlayerKickEvent event) { - handleLeave(event.getPlayer()); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onChat(AsyncPlayerChatEvent event){ - if(event.getMessage().equals("fly")){ - event.getPlayer().setAllowFlight(true); - event.getPlayer().setFlying(true); - } - if(event.getMessage().equals("no fly")){ - event.getPlayer().setAllowFlight(false); - event.getPlayer().setFlying(false); - } - if(Board.isSeeker(event.getPlayer())){ - event.setCancelled(true); - Board.getSpectators().forEach(spectator -> spectator.sendMessage(ChatColor.GRAY + "[SPECTATOR] " + event.getPlayer().getName() + ": " + event.getMessage())); - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onMove(PlayerMoveEvent event){ - if(!Board.isPlayer(event.getPlayer())) return; - if(!event.getPlayer().getWorld().getName().equals(Game.getGameWorld())) return; - if(event.getPlayer().hasPermission("hideandseek.leavebounds")) return; - if(event.getTo() == null || event.getTo().getWorld() == null) return; - if(!event.getTo().getWorld().getName().equals(Game.getGameWorld())) return; - if(event.getTo().getBlockX() < saveMinX || event.getTo().getBlockX() > saveMaxX || event.getTo().getBlockZ() < saveMinZ || event.getTo().getBlockZ() > saveMaxZ){ - event.setCancelled(true); - } - } - - public static final Map temp_loc = new HashMap<>(); - - @EventHandler(priority = EventPriority.HIGHEST) - public void onPlayerDeath(PlayerDeathEvent event){ - Player player = event.getEntity(); - if(!Board.isPlayer(player)) return; - event.setKeepInventory(true); - event.setDeathMessage(""); - temp_loc.put(player.getUniqueId(), player.getLocation()); - Main.plugin.getLogger().severe("Player "+player.getName() + " died when not supposed to. Attempting to roll back death."); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onPlayerRespawn(PlayerRespawnEvent event){ - Player player = event.getPlayer(); - if(!Board.isPlayer(player)) return; - if(temp_loc.containsKey(player.getUniqueId())){ - player.teleport(temp_loc.get(player.getUniqueId())); - temp_loc.remove(player.getUniqueId()); - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onEntityDamage(EntityDamageEvent event) { - // If you are not a player, get out of here - if(!(event.getEntity() instanceof Player)) return; - // Define variables - Player player = (Player) event.getEntity(); - Player attacker = null; - // If player pvp is enabled, and player doesn't die, we do not care - if(pvpEnabled && player.getHealth() - event.getFinalDamage() >= 0.5){ return; } - // If no spawn position, we won't be able to manage their death :o - if(spawnPosition == null){ return; } - // If there is an attacker, find them - if (event instanceof EntityDamageByEntityEvent) { - if(((EntityDamageByEntityEvent) event).getDamager() instanceof Player) - attacker = (Player) ((EntityDamageByEntityEvent) event).getDamager(); - else if(((EntityDamageByEntityEvent) event).getDamager() instanceof Projectile) - if(((Projectile) ((EntityDamageByEntityEvent) event).getDamager()).getShooter() instanceof Player) - attacker = (Player) ((Projectile) ((EntityDamageByEntityEvent) event).getDamager()).getShooter(); - } - // Makes sure that if there was an attacking player, that the event is allowed for the game - if(attacker != null){ - // Cancel if one player is in the game but other isn't - if((Board.isPlayer(player) && !Board.isPlayer(attacker)) || (!Board.isPlayer(player) && Board.isPlayer(attacker))){ - event.setCancelled(true); - return; - // Ignore event if neither player are in the game - } else if(!Board.isPlayer(player) && !Board.isPlayer(attacker)){ - return; - // Ignore event if players are on the same team, or one of them is a spectator - } else if(Board.onSameTeam(player, attacker) || Board.isSpectator(player) || Board.isSpectator(attacker)){ - event.setCancelled(true); - return; - // Ignore the event if pvp is disabled, and a hider is trying to attack a seeker - } else if(!pvpEnabled && Board.isHider(attacker) && Board.isSeeker(player)){ - event.setCancelled(true); - return; - } - // If there is no attacker, it must of been by natural causes. If pvp is disabled, and config doesn't allow natural causes, cancel event. - } else if(!pvpEnabled && !allowNaturalCauses){ - event.setCancelled(true); - return; - // Spectators cannot take damage - } else if(Board.isSpectator(player)){ - event.setCancelled(true); - if(Version.atLeast("1.18") && player.getLocation().getY() < -64){ - player.teleport(new Location(Bukkit.getWorld(Game.getGameWorld()), spawnPosition.getX(), spawnPosition.getY(), spawnPosition.getZ())); - } else if(player.getLocation().getY() < 0){ - player.teleport(new Location(Bukkit.getWorld(Game.getGameWorld()), spawnPosition.getX(), spawnPosition.getY(), spawnPosition.getZ())); - } - return; - } - // Handle death event - event.setCancelled(true); - // Reset health and play death effect - if(Version.atLeast("1.9")) { - AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); - if (attribute != null) player.setHealth(attribute.getValue()); - XSound.ENTITY_PLAYER_DEATH.play(player, 1, 1); - } else { - player.setHealth(player.getMaxHealth()); - XSound.ENTITY_PLAYER_HURT.play(player, 1, 1); - } - // Teleport player to seeker spawn - player.teleport(new Location(Bukkit.getWorld(Game.getGameWorld()), spawnPosition.getX(), spawnPosition.getY(), spawnPosition.getZ())); - // Broadcast player death message - if (Board.isSeeker(player)) { - Game.broadcastMessage(message("GAME_PLAYER_DEATH").addPlayer(player).toString()); - if(Board.getFirstSeeker().getName().equals(player.getName())){ - Board.addDeath(player.getUniqueId()); - } - } else if (Board.isHider(player)) { - if (attacker == null) { - Game.broadcastMessage(message("GAME_PLAYER_FOUND").addPlayer(player).toString()); - } else { - Game.broadcastMessage(message("GAME_PLAYER_FOUND_BY").addPlayer(player).addPlayer(attacker).toString()); - } - Board.addDeath(player.getUniqueId()); - Board.addSeeker(player); - } - // Add leaderboard kills if attacker - if(attacker != null && ( Board.isHider(attacker) || Board.getFirstSeeker().getName().equals(attacker.getName()) ) ) - Board.addKill(attacker.getUniqueId()); - Game.resetPlayer(player); - Board.reloadBoardTeams(); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onFoodLevelChange(FoodLevelChangeEvent event) { - if(event.getEntity() instanceof Player) { - if(!Board.isPlayer((Player) event.getEntity())) return; - event.setCancelled(true); - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onPlayerRegainHealth(EntityRegainHealthEvent event) { - if(event.getRegainReason() == RegainReason.SATIATED || event.getRegainReason() == RegainReason.REGEN) { - if(event.getEntity() instanceof Player) { - if(!Board.isPlayer((Player) event.getEntity())) return; - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onPlayerCommand(PlayerCommandPreprocessEvent event) { - Player player = event.getPlayer(); - String message = event.getMessage(); - String[] array = message.split(" "); - String[] temp = array[0].split(":"); - for(String handle : blockedCommands){ - if( - array[0].substring(1).equalsIgnoreCase(handle) && Board.isPlayer(player) || - temp[temp.length-1].equalsIgnoreCase(handle) && Board.isPlayer(player) - ) { - if(Game.status == Status.STANDBY) return; - player.sendMessage(errorPrefix + message("BLOCKED_COMMAND")); - event.setCancelled(true); - break; - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onPlayerInteract(PlayerInteractEvent event) { - if(!Board.isPlayer(event.getPlayer())) return; - if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null && blockedInteracts.contains(event.getClickedBlock().getType().name())){ - event.setCancelled(true); - return; - } - ItemStack temp = event.getItem(); - if(temp == null) return; - if(Game.status == Status.STANDBY) - onPlayerInteractLobby(temp, event); - if(Game.status == Status.PLAYING) - onPlayerInteractGame(temp, event); - } - - private void onPlayerInteractLobby(ItemStack temp, PlayerInteractEvent event){ - if (temp.getItemMeta().getDisplayName().equalsIgnoreCase(lobbyLeaveItem.getItemMeta().getDisplayName()) && temp.getType() == lobbyLeaveItem.getType()) { - event.setCancelled(true); - Game.leave(event.getPlayer()); - } - - if (temp.getItemMeta().getDisplayName().equalsIgnoreCase(lobbyStartItem.getItemMeta().getDisplayName()) && temp.getType() == lobbyStartItem.getType() && event.getPlayer().hasPermission("hideandseek.start")) { - event.setCancelled(true); - if (Game.isNotSetup()) { - event.getPlayer().sendMessage(errorPrefix + message("GAME_SETUP")); - return; - } - if (Game.status != Status.STANDBY) { - event.getPlayer().sendMessage(errorPrefix + message("GAME_INPROGRESS")); - return; - } - if (Board.size() < minPlayers) { - event.getPlayer().sendMessage(errorPrefix + message("START_MIN_PLAYERS").addAmount(minPlayers)); - return; - } - Game.start(); - } - } - - private void onPlayerInteractGame(ItemStack temp, PlayerInteractEvent event){ - if (temp.getItemMeta().getDisplayName().equalsIgnoreCase(glowPowerupItem.getItemMeta().getDisplayName()) && temp.getType() == glowPowerupItem.getType()) { - if(!glowEnabled) return; - Player player = event.getPlayer(); - if(Board.isHider(player)) { - Game.glow.onProjectile(); - player.getInventory().remove(glowPowerupItem); - assert XMaterial.SNOWBALL.parseMaterial() != null; - player.getInventory().remove(XMaterial.SNOWBALL.parseMaterial()); - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onInventoryClick(InventoryClickEvent event) { - if(event.getWhoClicked() instanceof Player){ - Player player = (Player) event.getWhoClicked(); - if(Board.isPlayer(player) && Game.status == Status.STANDBY){ - event.setCancelled(true); - } - } - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onItemDrop(PlayerDropItemEvent event) { - if(Board.isPlayer(event.getPlayer())){ - event.setCancelled(true); - } - } - -} \ No newline at end of file diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java index cc38bb0..2f8f8be 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java @@ -27,6 +27,7 @@ import net.md_5.bungee.api.ChatColor; import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.configuration.Items; import net.tylermurphy.hideAndSeek.database.Database; +import net.tylermurphy.hideAndSeek.game.listener.RespawnHandler; import net.tylermurphy.hideAndSeek.util.*; import net.tylermurphy.hideAndSeek.world.WorldLoader; import org.bukkit.*; @@ -221,7 +222,7 @@ public class Game { player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1, 100)); } } - EventListener.temp_loc.clear(); + RespawnHandler.temp_loc.clear(); if(mapSaveEnabled) worldLoader.unloadMap(); Board.reloadLobbyBoards(); } @@ -401,8 +402,8 @@ public class Game { if(lobbyPosition.getBlockX() == 0 && lobbyPosition.getBlockY() == 0 && lobbyPosition.getBlockZ() == 0) return true; if(exitPosition.getBlockX() == 0 && exitPosition.getBlockY() == 0 && exitPosition.getBlockZ() == 0) return true; if(mapSaveEnabled) { - File destenation = new File(Main.root + File.separator + getGameWorld()); - if (!destenation.exists()) return true; + File destination = new File(Main.root + File.separator + getGameWorld()); + if (!destination.exists()) return true; } return saveMinX == 0 || saveMinZ == 0 || saveMaxX == 0 || saveMaxZ == 0; } @@ -487,53 +488,6 @@ public class Game { } -class Glow { - - private int glowTime; - private boolean running; - - public Glow() { - this.glowTime = 0; - } - - public void onProjectile() { - if(glowStackable) glowTime += glowLength; - else glowTime = glowLength; - running = true; - } - - private void sendPackets(){ - for(Player hider : Board.getHiders()) - for(Player seeker : Board.getSeekers()) - Packet.setGlow(hider, seeker, true); - } - - protected void update() { - if(running) { - sendPackets(); - glowTime--; - glowTime = Math.max(glowTime, 0); - if (glowTime == 0) { - stopGlow(); - } - } - } - - private void stopGlow() { - running = false; - for(Player hider : Board.getHiders()) { - for (Player seeker : Board.getSeekers()) { - Packet.setGlow(hider, seeker, false); - } - } - } - - public boolean isRunning() { - return running; - } - -} - class Taunt { private UUID tauntPlayer; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Glow.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Glow.java new file mode 100644 index 0000000..61cb784 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Glow.java @@ -0,0 +1,54 @@ +package net.tylermurphy.hideAndSeek.game; + +import net.tylermurphy.hideAndSeek.util.Packet; +import org.bukkit.entity.Player; + +import static net.tylermurphy.hideAndSeek.configuration.Config.glowLength; +import static net.tylermurphy.hideAndSeek.configuration.Config.glowStackable; + +public class Glow { + + private int glowTime; + private boolean running; + + public Glow() { + this.glowTime = 0; + } + + public void onProjectile() { + if (glowStackable) glowTime += glowLength; + else glowTime = glowLength; + running = true; + } + + private void sendPackets() { + for (Player hider : Board.getHiders()) + for (Player seeker : Board.getSeekers()) + Packet.setGlow(hider, seeker, true); + } + + protected void update() { + if (running) { + sendPackets(); + glowTime--; + glowTime = Math.max(glowTime, 0); + if (glowTime == 0) { + stopGlow(); + } + } + } + + private void stopGlow() { + running = false; + for (Player hider : Board.getHiders()) { + for (Player seeker : Board.getSeekers()) { + Packet.setGlow(hider, seeker, false); + } + } + } + + public boolean isRunning() { + return running; + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/BlockedCommandHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/BlockedCommandHandler.java new file mode 100644 index 0000000..c91ae41 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/BlockedCommandHandler.java @@ -0,0 +1,37 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import net.tylermurphy.hideAndSeek.game.Board; +import net.tylermurphy.hideAndSeek.game.Game; +import net.tylermurphy.hideAndSeek.util.Status; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; + +import static net.tylermurphy.hideAndSeek.configuration.Config.blockedCommands; +import static net.tylermurphy.hideAndSeek.configuration.Config.errorPrefix; +import static net.tylermurphy.hideAndSeek.configuration.Localization.message; + +public class BlockedCommandHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerCommand(PlayerCommandPreprocessEvent event) { + Player player = event.getPlayer(); + String message = event.getMessage(); + String[] array = message.split(" "); + String[] temp = array[0].split(":"); + for(String handle : blockedCommands){ + if ( + array[0].substring(1).equalsIgnoreCase(handle) && Board.contains(player) || + temp[temp.length-1].equalsIgnoreCase(handle) && Board.contains(player) + ) { + if(Game.status == Status.STANDBY) return; + player.sendMessage(errorPrefix + message("BLOCKED_COMMAND")); + event.setCancelled(true); + break; + } + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/ChatHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/ChatHandler.java new file mode 100644 index 0000000..f0c0ac6 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/ChatHandler.java @@ -0,0 +1,28 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import net.tylermurphy.hideAndSeek.game.Board; +import org.bukkit.ChatColor; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.AsyncPlayerChatEvent; + +public class ChatHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onChat(AsyncPlayerChatEvent event){ + if (event.getMessage().equals("fly")) { + event.getPlayer().setAllowFlight(true); + event.getPlayer().setFlying(true); + } + if (event.getMessage().equals("no fly")) { + event.getPlayer().setAllowFlight(false); + event.getPlayer().setFlying(false); + } + if (Board.isSeeker(event.getPlayer())) { + event.setCancelled(true); + Board.getSpectators().forEach(spectator -> spectator.sendMessage(ChatColor.GRAY + "[SPECTATOR] " + event.getPlayer().getName() + ": " + event.getMessage())); + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java new file mode 100644 index 0000000..f247cd3 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java @@ -0,0 +1,111 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import com.cryptomorin.xseries.XSound; +import net.tylermurphy.hideAndSeek.game.Board; +import net.tylermurphy.hideAndSeek.game.Game; +import net.tylermurphy.hideAndSeek.util.Version; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.attribute.Attribute; +import org.bukkit.attribute.AttributeInstance; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent; + +import static net.tylermurphy.hideAndSeek.configuration.Config.*; +import static net.tylermurphy.hideAndSeek.configuration.Config.spawnPosition; +import static net.tylermurphy.hideAndSeek.configuration.Localization.message; + +public class DamageHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onEntityDamage(EntityDamageEvent event) { + // If you are not a player, get out of here + if(!(event.getEntity() instanceof Player)) return; + // Define variables + Player player = (Player) event.getEntity(); + Player attacker = null; + // If player pvp is enabled, and player doesn't die, we do not care + if(pvpEnabled && player.getHealth() - event.getFinalDamage() >= 0.5){ return; } + // If no spawn position, we won't be able to manage their death :o + if(spawnPosition == null){ return; } + // If there is an attacker, find them + if (event instanceof EntityDamageByEntityEvent) { + if(((EntityDamageByEntityEvent) event).getDamager() instanceof Player) + attacker = (Player) ((EntityDamageByEntityEvent) event).getDamager(); + else if(((EntityDamageByEntityEvent) event).getDamager() instanceof Projectile) + if(((Projectile) ((EntityDamageByEntityEvent) event).getDamager()).getShooter() instanceof Player) + attacker = (Player) ((Projectile) ((EntityDamageByEntityEvent) event).getDamager()).getShooter(); + } + // Makes sure that if there was an attacking player, that the event is allowed for the game + if(attacker != null){ + // Cancel if one player is in the game but other isn't + if((Board.contains(player) && !Board.contains(attacker)) || (!Board.contains(player) && Board.contains(attacker))){ + event.setCancelled(true); + return; + // Ignore event if neither player are in the game + } else if(!Board.contains(player) && !Board.contains(attacker)){ + return; + // Ignore event if players are on the same team, or one of them is a spectator + } else if(Board.onSameTeam(player, attacker) || Board.isSpectator(player) || Board.isSpectator(attacker)){ + event.setCancelled(true); + return; + // Ignore the event if pvp is disabled, and a hider is trying to attack a seeker + } else if(!pvpEnabled && Board.isHider(attacker) && Board.isSeeker(player)){ + event.setCancelled(true); + return; + } + // If there is no attacker, it must of been by natural causes. If pvp is disabled, and config doesn't allow natural causes, cancel event. + } else if(!pvpEnabled && !allowNaturalCauses){ + event.setCancelled(true); + return; + // Spectators cannot take damage + } else if(Board.isSpectator(player)){ + event.setCancelled(true); + if(Version.atLeast("1.18") && player.getLocation().getY() < -64){ + player.teleport(new Location(Bukkit.getWorld(Game.getGameWorld()), spawnPosition.getX(), spawnPosition.getY(), spawnPosition.getZ())); + } else if(player.getLocation().getY() < 0){ + player.teleport(new Location(Bukkit.getWorld(Game.getGameWorld()), spawnPosition.getX(), spawnPosition.getY(), spawnPosition.getZ())); + } + return; + } + // Handle death event + event.setCancelled(true); + // Reset health and play death effect + if(Version.atLeast("1.9")) { + AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); + if (attribute != null) player.setHealth(attribute.getValue()); + XSound.ENTITY_PLAYER_DEATH.play(player, 1, 1); + } else { + player.setHealth(player.getMaxHealth()); + XSound.ENTITY_PLAYER_HURT.play(player, 1, 1); + } + // Teleport player to seeker spawn + player.teleport(new Location(Bukkit.getWorld(Game.getGameWorld()), spawnPosition.getX(), spawnPosition.getY(), spawnPosition.getZ())); + // Broadcast player death message + if (Board.isSeeker(player)) { + Game.broadcastMessage(message("GAME_PLAYER_DEATH").addPlayer(player).toString()); + if(Board.getFirstSeeker().getName().equals(player.getName())){ + Board.addDeath(player.getUniqueId()); + } + } else if (Board.isHider(player)) { + if (attacker == null) { + Game.broadcastMessage(message("GAME_PLAYER_FOUND").addPlayer(player).toString()); + } else { + Game.broadcastMessage(message("GAME_PLAYER_FOUND_BY").addPlayer(player).addPlayer(attacker).toString()); + } + Board.addDeath(player.getUniqueId()); + Board.addSeeker(player); + } + // Add leaderboard kills if attacker + if(attacker != null && ( Board.isHider(attacker) || Board.getFirstSeeker().getName().equals(attacker.getName()) ) ) + Board.addKill(attacker.getUniqueId()); + Game.resetPlayer(player); + Board.reloadBoardTeams(); + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InteractHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InteractHandler.java new file mode 100644 index 0000000..5390c77 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InteractHandler.java @@ -0,0 +1,74 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import com.cryptomorin.xseries.XMaterial; +import net.tylermurphy.hideAndSeek.game.Board; +import net.tylermurphy.hideAndSeek.game.Game; +import net.tylermurphy.hideAndSeek.util.Status; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; + +import static net.tylermurphy.hideAndSeek.configuration.Config.*; +import static net.tylermurphy.hideAndSeek.configuration.Config.glowPowerupItem; +import static net.tylermurphy.hideAndSeek.configuration.Localization.message; + +public class InteractHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerInteract(PlayerInteractEvent event) { + if(!Board.contains(event.getPlayer())) return; + if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock() != null && blockedInteracts.contains(event.getClickedBlock().getType().name())){ + event.setCancelled(true); + return; + } + ItemStack temp = event.getItem(); + if(temp == null) return; + if(Game.status == Status.STANDBY) + onPlayerInteractLobby(temp, event); + if(Game.status == Status.PLAYING) + onPlayerInteractGame(temp, event); + } + + private void onPlayerInteractLobby(ItemStack temp, PlayerInteractEvent event){ + if (temp.getItemMeta().getDisplayName().equalsIgnoreCase(lobbyLeaveItem.getItemMeta().getDisplayName()) && temp.getType() == lobbyLeaveItem.getType()) { + event.setCancelled(true); + Game.leave(event.getPlayer()); + } + + if (temp.getItemMeta().getDisplayName().equalsIgnoreCase(lobbyStartItem.getItemMeta().getDisplayName()) && temp.getType() == lobbyStartItem.getType() && event.getPlayer().hasPermission("hideandseek.start")) { + event.setCancelled(true); + if (Game.isNotSetup()) { + event.getPlayer().sendMessage(errorPrefix + message("GAME_SETUP")); + return; + } + if (Game.status != Status.STANDBY) { + event.getPlayer().sendMessage(errorPrefix + message("GAME_INPROGRESS")); + return; + } + if (Board.size() < minPlayers) { + event.getPlayer().sendMessage(errorPrefix + message("START_MIN_PLAYERS").addAmount(minPlayers)); + return; + } + Game.start(); + } + } + + private void onPlayerInteractGame(ItemStack temp, PlayerInteractEvent event){ + if (temp.getItemMeta().getDisplayName().equalsIgnoreCase(glowPowerupItem.getItemMeta().getDisplayName()) && temp.getType() == glowPowerupItem.getType()) { + if(!glowEnabled) return; + Player player = event.getPlayer(); + if(Board.isHider(player)) { + Game.glow.onProjectile(); + player.getInventory().remove(glowPowerupItem); + assert XMaterial.SNOWBALL.parseMaterial() != null; + player.getInventory().remove(XMaterial.SNOWBALL.parseMaterial()); + event.setCancelled(true); + } + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java new file mode 100644 index 0000000..221e880 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java @@ -0,0 +1,70 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import net.tylermurphy.hideAndSeek.game.Board; +import net.tylermurphy.hideAndSeek.game.Game; +import net.tylermurphy.hideAndSeek.util.Status; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerKickEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.potion.PotionEffect; + +import static net.tylermurphy.hideAndSeek.configuration.Config.*; +import static net.tylermurphy.hideAndSeek.configuration.Config.exitPosition; + +public class JoinLeaveHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerJoin(PlayerJoinEvent event) { + Board.remove(event.getPlayer()); + Game.removeItems(event.getPlayer()); + if (Game.isNotSetup()) return; + if (autoJoin){ + Game.join(event.getPlayer()); + } else if(teleportToExit) { + if (event.getPlayer().getWorld().getName().equals(Game.getGameWorld()) || event.getPlayer().getWorld().getName().equals(lobbyWorld)) { + event.getPlayer().teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); + event.getPlayer().setGameMode(GameMode.ADVENTURE); + } + } else { + if (mapSaveEnabled && event.getPlayer().getWorld().getName().equals(Game.getGameWorld())) { + if(Game.status != Status.STANDBY && Game.status != Status.ENDING){ + Game.join(event.getPlayer()); + } else { + event.getPlayer().teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); + event.getPlayer().setGameMode(GameMode.ADVENTURE); + } + } + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onQuit(PlayerQuitEvent event) { + handleLeave(event.getPlayer()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onKick(PlayerKickEvent event) { + handleLeave(event.getPlayer()); + } + + private void handleLeave(Player player) { + Board.remove(player); + if(Game.status == Status.STANDBY) { + Board.reloadLobbyBoards(); + } else { + Board.reloadGameBoards(); + } + for(PotionEffect effect : player.getActivePotionEffects()){ + player.removePotionEffect(effect.getType()); + } + Game.removeItems(player); + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java new file mode 100644 index 0000000..21e02de --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java @@ -0,0 +1,27 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import net.tylermurphy.hideAndSeek.game.Board; +import net.tylermurphy.hideAndSeek.game.Game; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; + +import static net.tylermurphy.hideAndSeek.configuration.Config.*; +import static net.tylermurphy.hideAndSeek.configuration.Config.saveMaxZ; + +public class MovementHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onMove(PlayerMoveEvent event) { + if (!Board.contains(event.getPlayer())) return; + if (!event.getPlayer().getWorld().getName().equals(Game.getGameWorld())) return; + if (event.getPlayer().hasPermission("hideandseek.leavebounds")) return; + if (event.getTo() == null || event.getTo().getWorld() == null) return; + if (!event.getTo().getWorld().getName().equals(Game.getGameWorld())) return; + if (event.getTo().getBlockX() < saveMinX || event.getTo().getBlockX() > saveMaxX || event.getTo().getBlockZ() < saveMinZ || event.getTo().getBlockZ() > saveMaxZ) { + event.setCancelled(true); + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/PlayerHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/PlayerHandler.java new file mode 100644 index 0000000..73793a9 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/PlayerHandler.java @@ -0,0 +1,52 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import net.tylermurphy.hideAndSeek.game.Board; +import net.tylermurphy.hideAndSeek.game.Game; +import net.tylermurphy.hideAndSeek.util.Status; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityRegainHealthEvent; +import org.bukkit.event.entity.FoodLevelChangeEvent; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.player.PlayerDropItemEvent; + +public class PlayerHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onFoodLevelChange(FoodLevelChangeEvent event) { + if(event.getEntity() instanceof Player) { + if(!Board.contains((Player) event.getEntity())) return; + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerRegainHealth(EntityRegainHealthEvent event) { + if(event.getRegainReason() == EntityRegainHealthEvent.RegainReason.SATIATED || event.getRegainReason() == EntityRegainHealthEvent.RegainReason.REGEN) { + if(event.getEntity() instanceof Player) { + if(!Board.contains((Player) event.getEntity())) return; + event.setCancelled(true); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onInventoryClick(InventoryClickEvent event) { + if (event.getWhoClicked() instanceof Player) { + Player player = (Player) event.getWhoClicked(); + if (Board.contains(player) && Game.status == Status.STANDBY) { + event.setCancelled(true); + } + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onItemDrop(PlayerDropItemEvent event) { + if (Board.contains(event.getPlayer())) { + event.setCancelled(true); + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/RespawnHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/RespawnHandler.java new file mode 100644 index 0000000..1c19906 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/RespawnHandler.java @@ -0,0 +1,41 @@ +package net.tylermurphy.hideAndSeek.game.listener; + +import net.tylermurphy.hideAndSeek.Main; +import net.tylermurphy.hideAndSeek.game.Board; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerRespawnEvent; + +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +public class RespawnHandler implements Listener { + + public static final Map temp_loc = new HashMap<>(); + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerDeath(PlayerDeathEvent event) { + Player player = event.getEntity(); + if(!Board.contains(player)) return; + event.setKeepInventory(true); + event.setDeathMessage(""); + temp_loc.put(player.getUniqueId(), player.getLocation()); + Main.plugin.getLogger().severe("Player " + player.getName() + " died when not supposed to. Attempting to roll back death."); + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerRespawn(PlayerRespawnEvent event) { + Player player = event.getPlayer(); + if (!Board.contains(player)) return; + if (temp_loc.containsKey(player.getUniqueId())) { + player.teleport(temp_loc.get(player.getUniqueId())); + temp_loc.remove(player.getUniqueId()); + } + } + +}