refactor: break apart over saturated EventListener.java

This commit is contained in:
bobby29831 2022-05-13 11:51:20 -05:00
parent f900dd3268
commit cd0f60bd13
16 changed files with 518 additions and 403 deletions

View file

@ -25,8 +25,8 @@ import net.tylermurphy.hideAndSeek.configuration.Localization;
import net.tylermurphy.hideAndSeek.database.Database; import net.tylermurphy.hideAndSeek.database.Database;
import net.tylermurphy.hideAndSeek.game.Board; import net.tylermurphy.hideAndSeek.game.Board;
import net.tylermurphy.hideAndSeek.game.CommandHandler; import net.tylermurphy.hideAndSeek.game.CommandHandler;
import net.tylermurphy.hideAndSeek.game.EventListener;
import net.tylermurphy.hideAndSeek.game.Game; import net.tylermurphy.hideAndSeek.game.Game;
import net.tylermurphy.hideAndSeek.game.listener.*;
import net.tylermurphy.hideAndSeek.util.PAPIExpansion; import net.tylermurphy.hideAndSeek.util.PAPIExpansion;
import net.tylermurphy.hideAndSeek.util.TabCompleter; import net.tylermurphy.hideAndSeek.util.TabCompleter;
import net.tylermurphy.hideAndSeek.util.UUIDFetcher; import net.tylermurphy.hideAndSeek.util.UUIDFetcher;
@ -50,7 +50,8 @@ public class Main extends JavaPlugin implements Listener {
plugin = this; plugin = this;
root = this.getServer().getWorldContainer(); root = this.getServer().getWorldContainer();
data = this.getDataFolder(); data = this.getDataFolder();
getServer().getPluginManager().registerEvents(new EventListener(), this);
this.registerListeners();
Config.loadConfig(); Config.loadConfig();
Localization.loadLocalization(); Localization.loadLocalization();
@ -82,6 +83,17 @@ public class Main extends JavaPlugin implements Listener {
UUIDFetcher.cleanup(); UUIDFetcher.cleanup();
Board.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) { public boolean onCommand(@NotNull CommandSender sender, @NotNull Command cmd, @NotNull String label, String[] args) {
return CommandHandler.handleCommand(sender, args); return CommandHandler.handleCommand(sender, args);

View file

@ -40,7 +40,7 @@ public class Join implements ICommand {
sender.sendMessage(errorPrefix + message("COMMAND_ERROR")); sender.sendMessage(errorPrefix + message("COMMAND_ERROR"));
return; return;
} }
if(Board.isPlayer(player)){ if(Board.contains(player)){
sender.sendMessage(errorPrefix + message("GAME_INGAME")); sender.sendMessage(errorPrefix + message("GAME_INGAME"));
return; return;
} }

View file

@ -40,7 +40,7 @@ public class Leave implements ICommand {
sender.sendMessage(errorPrefix + message("COMMAND_ERROR")); sender.sendMessage(errorPrefix + message("COMMAND_ERROR"));
return; return;
} }
if(!Board.isPlayer(player)) { if(!Board.contains(player)) {
sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME")); sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME"));
return; return;
} }

View file

@ -45,7 +45,7 @@ public class Start implements ICommand {
sender.sendMessage(errorPrefix + message("GAME_INPROGRESS")); sender.sendMessage(errorPrefix + message("GAME_INPROGRESS"));
return; return;
} }
if(!Board.isPlayer(sender)) { if(!Board.contains(sender)) {
sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME")); sender.sendMessage(errorPrefix + message("GAME_NOT_INGAME"));
return; return;
} }

View file

@ -40,12 +40,12 @@ public class Board {
private static final Map<String, CustomBoard> customBoards = new HashMap<>(); private static final Map<String, CustomBoard> customBoards = new HashMap<>();
private static final Map<String, Integer> hider_kills = new HashMap<>(), seeker_kills = new HashMap<>(), hider_deaths = new HashMap<>(), seeker_deaths = new HashMap<>(); private static final Map<String, Integer> 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()); return playerList.containsKey(player.getUniqueId().toString());
} }
public static boolean isPlayer(CommandSender sender) { public static boolean contains(CommandSender sender) {
return playerList.containsKey(Bukkit.getPlayer(sender.getName()).getUniqueId().toString()); return contains((Player) sender);
} }
public static boolean isHider(Player player) { public static boolean isHider(Player player) {

View file

@ -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 <http://www.gnu.org/licenses/>.
*
*/
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<UUID, Location> 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);
}
}
}

View file

@ -27,6 +27,7 @@ import net.md_5.bungee.api.ChatColor;
import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.Main;
import net.tylermurphy.hideAndSeek.configuration.Items; import net.tylermurphy.hideAndSeek.configuration.Items;
import net.tylermurphy.hideAndSeek.database.Database; import net.tylermurphy.hideAndSeek.database.Database;
import net.tylermurphy.hideAndSeek.game.listener.RespawnHandler;
import net.tylermurphy.hideAndSeek.util.*; import net.tylermurphy.hideAndSeek.util.*;
import net.tylermurphy.hideAndSeek.world.WorldLoader; import net.tylermurphy.hideAndSeek.world.WorldLoader;
import org.bukkit.*; import org.bukkit.*;
@ -221,7 +222,7 @@ public class Game {
player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1, 100)); player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 1, 100));
} }
} }
EventListener.temp_loc.clear(); RespawnHandler.temp_loc.clear();
if(mapSaveEnabled) worldLoader.unloadMap(); if(mapSaveEnabled) worldLoader.unloadMap();
Board.reloadLobbyBoards(); Board.reloadLobbyBoards();
} }
@ -401,8 +402,8 @@ public class Game {
if(lobbyPosition.getBlockX() == 0 && lobbyPosition.getBlockY() == 0 && lobbyPosition.getBlockZ() == 0) return true; 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(exitPosition.getBlockX() == 0 && exitPosition.getBlockY() == 0 && exitPosition.getBlockZ() == 0) return true;
if(mapSaveEnabled) { if(mapSaveEnabled) {
File destenation = new File(Main.root + File.separator + getGameWorld()); File destination = new File(Main.root + File.separator + getGameWorld());
if (!destenation.exists()) return true; if (!destination.exists()) return true;
} }
return saveMinX == 0 || saveMinZ == 0 || saveMaxX == 0 || saveMaxZ == 0; 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 { class Taunt {
private UUID tauntPlayer; private UUID tauntPlayer;

View file

@ -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;
}
}

View file

@ -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;
}
}
}
}

View file

@ -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()));
}
}
}

View file

@ -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();
}
}

View file

@ -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);
}
}
}
}

View file

@ -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);
}
}

View file

@ -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);
}
}
}

View file

@ -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);
}
}
}

View file

@ -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<UUID, Location> 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());
}
}
}