From 416e459af261994ad702462812c29d55da8cb2d5 Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Mon, 16 May 2022 13:56:52 -0400 Subject: [PATCH] refactor: Jump Event, InventoryHandler, Relocate One function Utility Classes, Config Manager Getters Use Contains() --- .../net/tylermurphy/hideAndSeek/Main.java | 15 ++++ .../hideAndSeek/command/Debug.java | 15 ++-- .../hideAndSeek/configuration/Config.java | 11 ++- .../configuration/ConfigManager.java | 15 ++-- .../hideAndSeek/configuration/Items.java | 9 +-- .../tylermurphy/hideAndSeek/game/Board.java | 7 +- .../tylermurphy/hideAndSeek/game/Game.java | 16 ++-- .../PlayerUtil.java => PlayerLoader.java} | 30 ++++++-- .../hideAndSeek/game/events/Glow.java | 33 +++++++- .../game/listener/DamageHandler.java | 9 +-- .../game/listener/InteractHandler.java | 39 ---------- .../game/listener/InventoryHandler.java | 77 +++++++++++++++++++ .../game/listener/MovementHandler.java | 12 ++- .../game/listener/events/PlayerJumpEvent.java | 49 ++++++++++++ .../hideAndSeek/game/util/Packet.java | 56 -------------- .../hideAndSeek/game/util/Version.java | 45 ----------- 16 files changed, 241 insertions(+), 197 deletions(-) rename src/main/java/net/tylermurphy/hideAndSeek/game/{util/PlayerUtil.java => PlayerLoader.java} (85%) create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/listener/events/PlayerJumpEvent.java delete mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/util/Packet.java delete mode 100644 src/main/java/net/tylermurphy/hideAndSeek/game/util/Version.java diff --git a/src/main/java/net/tylermurphy/hideAndSeek/Main.java b/src/main/java/net/tylermurphy/hideAndSeek/Main.java index fc4b2cc..2737e81 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/Main.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/Main.java @@ -39,10 +39,13 @@ import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class Main extends JavaPlugin implements Listener { private static Main instance; + private static int version; private Database database; private Board board; @@ -72,6 +75,13 @@ public class Main extends JavaPlugin implements Listener { if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { new PAPIExpansion().register(); } + + Matcher matcher = Pattern.compile("MC: \\d\\.(\\d+)").matcher(Bukkit.getVersion()); + if (matcher.find()) { + version = Integer.parseInt(matcher.group(1)); + } else { + throw new IllegalArgumentException("Failed to parse server version from: " + Bukkit.getVersion()); + } } public void onDisable() { @@ -89,6 +99,7 @@ public class Main extends JavaPlugin implements Listener { getServer().getPluginManager().registerEvents(new ChatHandler(), this); getServer().getPluginManager().registerEvents(new DamageHandler(), this); getServer().getPluginManager().registerEvents(new InteractHandler(), this); + getServer().getPluginManager().registerEvents(new InventoryHandler(), this); getServer().getPluginManager().registerEvents(new JoinLeaveHandler(), this); getServer().getPluginManager().registerEvents(new MovementHandler(), this); getServer().getPluginManager().registerEvents(new PlayerHandler(), this); @@ -122,5 +133,9 @@ public class Main extends JavaPlugin implements Listener { public Game getGame(){ return game; } + + public boolean supports(int v){ + return version >= v; + } } \ No newline at end of file diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java index a051d8e..c1cec56 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Debug.java @@ -2,14 +2,13 @@ package net.tylermurphy.hideAndSeek.command; import com.cryptomorin.xseries.XMaterial; import net.tylermurphy.hideAndSeek.Main; -import net.tylermurphy.hideAndSeek.game.util.PlayerUtil; +import net.tylermurphy.hideAndSeek.game.PlayerLoader; import net.tylermurphy.hideAndSeek.game.util.Status; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Player; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -38,27 +37,27 @@ public class Debug implements ICommand { if(Bukkit.getWorld(Main.getInstance().getGame().getGameWorld()) == null) Main.getInstance().getGame().getWorldLoader().loadMap(); } Main.getInstance().getBoard().addHider(player); - PlayerUtil.loadHider(player, Main.getInstance().getGame().getGameWorld()); - PlayerUtil.resetPlayer(player, Main.getInstance().getBoard()); + PlayerLoader.loadHider(player, Main.getInstance().getGame().getGameWorld()); + PlayerLoader.resetPlayer(player, Main.getInstance().getBoard()); })); debugMenu.setItem(1, createOption(1, XMaterial.GOLDEN_CHESTPLATE.parseMaterial(), "&cBecome a &lSeeker", 1, player -> { if(mapSaveEnabled) { if(Bukkit.getWorld(Main.getInstance().getGame().getGameWorld()) == null) Main.getInstance().getGame().getWorldLoader().loadMap(); } Main.getInstance().getBoard().addSeeker(player); - PlayerUtil.loadSeeker(player, Main.getInstance().getGame().getGameWorld()); - PlayerUtil.resetPlayer(player, Main.getInstance().getBoard()); + PlayerLoader.loadSeeker(player, Main.getInstance().getGame().getGameWorld()); + PlayerLoader.resetPlayer(player, Main.getInstance().getBoard()); })); debugMenu.setItem(2, createOption(2, XMaterial.IRON_CHESTPLATE.parseMaterial(), "&8Become a &lSpectator", 1, player -> { if(mapSaveEnabled) { if(Bukkit.getWorld(Main.getInstance().getGame().getGameWorld()) == null) Main.getInstance().getGame().getWorldLoader().loadMap(); } Main.getInstance().getBoard().addSpectator(player); - PlayerUtil.loadSpectator(player, Main.getInstance().getGame().getGameWorld()); + PlayerLoader.loadSpectator(player, Main.getInstance().getGame().getGameWorld()); })); debugMenu.setItem(3, createOption(3, XMaterial.BARRIER.parseMaterial(), "&cUnload from Game", 1, player -> { Main.getInstance().getBoard().remove(player); - PlayerUtil.unloadPlayer(player); + PlayerLoader.unloadPlayer(player); player.teleport(new Location(Bukkit.getWorld(exitWorld), exitPosition.getX(), exitPosition.getY(), exitPosition.getZ())); })); debugMenu.setItem(4, createOption(4, XMaterial.BARRIER.parseMaterial(), "&cDie In Game", 2, player -> { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java index 7ed8454..03d7504 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Config.java @@ -24,7 +24,6 @@ import com.cryptomorin.xseries.XMaterial; import com.cryptomorin.xseries.XSound; import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.game.util.CountdownDisplay; -import net.tylermurphy.hideAndSeek.game.util.Version; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; @@ -160,7 +159,7 @@ public class Config { //Spawn spawnPosition = new Vector( config.getDouble("spawns.game.x"), - Math.max(Version.atLeast("1.18") ? -64 : 0, Math.min(255, config.getDouble("spawns.game.y"))), + Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.game.y"))), config.getDouble("spawns.game.z") ); spawnWorld = config.getString("spawns.game.world"); @@ -168,7 +167,7 @@ public class Config { ///Lobby lobbyPosition = new Vector( config.getDouble("spawns.lobby.x"), - Math.max(Version.atLeast("1.18") ? -64 : 0, Math.min(255, config.getDouble("spawns.lobby.y"))), + Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.lobby.y"))), config.getDouble("spawns.lobby.z") ); lobbyWorld = config.getString("spawns.lobby.world"); @@ -177,7 +176,7 @@ public class Config { exitPosition = new Vector( config.getDouble("spawns.exit.x"), - Math.max(Version.atLeast("1.18") ? -64 : 0, Math.min(255, config.getDouble("spawns.exit.y"))), + Math.max(Main.getInstance().supports(18) ? -64 : 0, Math.min(255, config.getDouble("spawns.exit.y"))), config.getDouble("spawns.exit.z") ); exitWorld = config.getString("spawns.exit.world"); @@ -221,7 +220,7 @@ public class Config { //Glow glowLength = Math.max(1, config.getInt("glow.time")); glowStackable = config.getBoolean("glow.stackable"); - glowEnabled = config.getBoolean("glow.enabled") && Version.atLeast("1.9"); + glowEnabled = config.getBoolean("glow.enabled") && Main.getInstance().supports(9); if (glowEnabled) { glowPowerupItem = createItemStack("glow"); } @@ -330,7 +329,7 @@ public class Config { ConfigurationSection item = new YamlConfiguration().createSection("temp"); item.set("name", ChatColor.translateAlternateColorCodes('&',config.getString(path+".name"))); item.set("material", config.getString(path+".material")); - if (Version.atLeast("1.14")) { + if (Main.getInstance().supports(14)) { if (config.contains(path+".model-data") && config.getInt(path+".model-data") != 0) { item.set("model-data", config.getInt(path+".model-data")); } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java index 3a95fe9..f02eaba 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/ConfigManager.java @@ -112,20 +112,18 @@ public class ConfigManager { } public double getDouble(String path) { - double value = config.getDouble(path); - if (value == 0.0D) { + if (!config.contains(path)) { return defaultConfig.getDouble(path); } else { - return value; + return config.getDouble(path); } } public int getInt(String path) { - int value = config.getInt(path); - if (value == 0) { + if (!config.contains(path)) { return defaultConfig.getInt(path); } else { - return value; + return config.getInt(path); } } @@ -134,11 +132,10 @@ public class ConfigManager { } public float getFloat(String path) { - float value = (float) config.getDouble(path); - if (value == 0.0F) { + if (!config.contains(path)) { return (float) defaultConfig.getDouble(path); } else { - return value; + return (float) config.getDouble(path); } } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java index 18a31b2..dfdb197 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/configuration/Items.java @@ -20,7 +20,7 @@ package net.tylermurphy.hideAndSeek.configuration; import com.cryptomorin.xseries.XItemStack; -import net.tylermurphy.hideAndSeek.game.util.Version; +import net.tylermurphy.hideAndSeek.Main; import org.bukkit.Material; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; @@ -30,7 +30,6 @@ import org.bukkit.potion.PotionEffectType; import java.util.ArrayList; import java.util.List; -import java.util.Objects; public class Items { @@ -90,7 +89,7 @@ public class Items { ConfigurationSection config = new YamlConfiguration().createSection("temp"); String material = item.getString("material").toUpperCase(); boolean splash = false; - if (!Version.atLeast("1.9")) { + if (!Main.getInstance().supports(9)) { if (material.contains("POTION")) { config.set("level", 1); } @@ -103,7 +102,7 @@ public class Items { config.set("material", material); config.set("enchants", item.getConfigurationSection("enchantments")); config.set("unbreakable", item.getBoolean("unbreakable")); - if (Version.atLeast("1.14")) { + if (Main.getInstance().supports(14)) { if (item.contains("model-data")) { config.set("model-data", item.getInt("model-data")); } @@ -123,7 +122,7 @@ public class Items { if (type == null) return null; if (PotionEffectType.getByName(type.toUpperCase()) == null) return null; return new PotionEffect( - Objects.requireNonNull(PotionEffectType.getByName(type.toUpperCase())), + PotionEffectType.getByName(type.toUpperCase()), item.getInt("duration"), item.getInt("amplifier"), item.getBoolean("ambient"), diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java index f13942f..04b0d37 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Board.java @@ -24,7 +24,6 @@ import net.tylermurphy.hideAndSeek.game.events.Border; import net.tylermurphy.hideAndSeek.game.events.Glow; import net.tylermurphy.hideAndSeek.game.events.Taunt; import net.tylermurphy.hideAndSeek.game.util.Status; -import net.tylermurphy.hideAndSeek.game.util.Version; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -368,7 +367,7 @@ class CustomBoard { this.board = manager.getNewScoreboard(); this.LINES = new HashMap<>(); this.player = player; - if (Version.atLeast("1.13")) { + if (Main.getInstance().supports(13)) { this.obj = board.registerNewObjective( "Scoreboard", "dummy", ChatColor.translateAlternateColorCodes('&', title)); } else { @@ -395,7 +394,7 @@ class CustomBoard { seekerTeam.removeEntry(entry); for(Player player : Main.getInstance().getBoard().getSeekers()) seekerTeam.addEntry(player.getName()); - if (Version.atLeast("1.9")) { + if (Main.getInstance().supports(9)) { if (nameTagsVisible) { hiderTeam.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.FOR_OWN_TEAM); seekerTeam.setOption(Team.Option.NAME_TAG_VISIBILITY, Team.OptionStatus.FOR_OTHER_TEAMS); @@ -412,7 +411,7 @@ class CustomBoard { seekerTeam.setNameTagVisibility(NameTagVisibility.NEVER); } } - if (Version.atLeast("1.12")) { + if (Main.getInstance().supports(12)) { hiderTeam.setColor(ChatColor.GOLD); seekerTeam.setColor(ChatColor.RED); } else { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java b/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java index e81d0e5..c261b52 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/Game.java @@ -119,12 +119,12 @@ public class Game { if (mapSaveEnabled) worldLoader.rollback(); board.reload(); board.addSeeker(seeker); - PlayerUtil.loadSeeker(seeker, getGameWorld()); + PlayerLoader.loadSeeker(seeker, getGameWorld()); board.getPlayers().forEach(player -> { board.createGameBoard(player); if(board.isSeeker(player)) return; board.addHider(player); - PlayerUtil.loadHider(player, getGameWorld()); + PlayerLoader.loadHider(player, getGameWorld()); }); worldBorder.resetWorldBorder(getGameWorld()); if (gameLength > 0) gameTimer = gameLength; @@ -147,7 +147,7 @@ public class Game { } public void end() { - board.getPlayers().forEach(PlayerUtil::unloadPlayer); + board.getPlayers().forEach(PlayerLoader::unloadPlayer); worldBorder.resetWorldBorder(getGameWorld()); board.getPlayers().forEach(player -> { if (leaveOnEnd) { @@ -158,7 +158,7 @@ public class Game { player.teleport(new Location(Bukkit.getWorld(lobbyWorld), lobbyPosition.getX(),lobbyPosition.getY(),lobbyPosition.getZ())); board.createLobbyBoard(player); board.addHider(player); - PlayerUtil.joinPlayer(player); + PlayerLoader.joinPlayer(player); } }); RespawnHandler.temp_loc.clear(); @@ -169,14 +169,14 @@ public class Game { public void join(Player player) { if (status != Status.STARTING && status != Status.PLAYING) { - PlayerUtil.joinPlayer(player); + PlayerLoader.joinPlayer(player); board.addHider(player); board.createLobbyBoard(player); board.reloadLobbyBoards(); if (announceMessagesToNonPlayers) Bukkit.broadcastMessage(messagePrefix + message("GAME_JOIN").addPlayer(player)); else broadcastMessage(messagePrefix + message("GAME_JOIN").addPlayer(player)); } else { - PlayerUtil.loadSpectator(player, getGameWorld()); + PlayerLoader.loadSpectator(player, getGameWorld()); board.addSpectator(player); board.createGameBoard(player); player.sendMessage(messagePrefix + message("GAME_JOIN_SPECTATOR")); @@ -184,7 +184,7 @@ public class Game { } public void leave(Player player) { - PlayerUtil.unloadPlayer(player); + PlayerLoader.unloadPlayer(player); if (announceMessagesToNonPlayers) Bukkit.broadcastMessage(messagePrefix + message("GAME_LEAVE").addPlayer(player)); else broadcastMessage(messagePrefix + message("GAME_LEAVE").addPlayer(player)); if (board.isHider(player) && status != Status.ENDING && status != Status.STANDBY) { @@ -247,7 +247,7 @@ public class Game { if (startingTimer == 0) { message = message("START").toString(); status = Status.PLAYING; - board.getPlayers().forEach(player -> PlayerUtil.resetPlayer(player, board)); + board.getPlayers().forEach(player -> PlayerLoader.resetPlayer(player, board)); } else { message = message("START_COUNTDOWN").addAmount(startingTimer).toString(); } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/util/PlayerUtil.java b/src/main/java/net/tylermurphy/hideAndSeek/game/PlayerLoader.java similarity index 85% rename from src/main/java/net/tylermurphy/hideAndSeek/game/util/PlayerUtil.java rename to src/main/java/net/tylermurphy/hideAndSeek/game/PlayerLoader.java index 59c7b73..0ce7ac7 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/util/PlayerUtil.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/PlayerLoader.java @@ -1,10 +1,28 @@ -package net.tylermurphy.hideAndSeek.game.util; +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 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.messages.Titles; import net.md_5.bungee.api.ChatColor; import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.configuration.Items; -import net.tylermurphy.hideAndSeek.game.Board; import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; @@ -19,7 +37,7 @@ import static net.tylermurphy.hideAndSeek.configuration.Config.*; import static net.tylermurphy.hideAndSeek.configuration.Config.lobbyPosition; import static net.tylermurphy.hideAndSeek.configuration.Localization.message; -public class PlayerUtil { +public class PlayerLoader { public static void loadHider(Player player, String gameWorld){ player.teleport(new Location(Bukkit.getWorld(gameWorld), spawnPosition.getX(),spawnPosition.getY(),spawnPosition.getZ())); @@ -77,11 +95,11 @@ public class PlayerUtil { for(PotionEffect effect : player.getActivePotionEffects()) { player.removePotionEffect(effect.getType()); } - if (Version.atLeast("1.9")) { + if (Main.getInstance().supports(9)) { AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); if (attribute != null) player.setHealth(attribute.getValue()); for(Player temp : Main.getInstance().getBoard().getPlayers()) { - Packet.setGlow(player, temp, false); + Main.getInstance().getGame().getGlow().setGlow(player, temp, false); } } else { player.setHealth(player.getMaxHealth()); @@ -111,7 +129,7 @@ public class PlayerUtil { player.removePotionEffect(effect.getType()); } player.setFoodLevel(20); - if (Version.atLeast("1.9")) { + if (Main.getInstance().supports(9)) { AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH); if (attribute != null) player.setHealth(attribute.getValue()); } else { diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/events/Glow.java b/src/main/java/net/tylermurphy/hideAndSeek/game/events/Glow.java index a0031e5..a1d7295 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/events/Glow.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/events/Glow.java @@ -1,14 +1,22 @@ package net.tylermurphy.hideAndSeek.game.events; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.wrappers.WrappedDataWatcher; import net.tylermurphy.hideAndSeek.Main; -import net.tylermurphy.hideAndSeek.game.util.Packet; import org.bukkit.entity.Player; +import java.lang.reflect.InvocationTargetException; + import static net.tylermurphy.hideAndSeek.configuration.Config.glowLength; import static net.tylermurphy.hideAndSeek.configuration.Config.glowStackable; public class Glow { + private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); + private int glowTime; private boolean running; @@ -25,7 +33,7 @@ public class Glow { private void sendPackets() { for (Player hider : Main.getInstance().getBoard().getHiders()) for (Player seeker : Main.getInstance().getBoard().getSeekers()) - Packet.setGlow(hider, seeker, true); + setGlow(hider, seeker, true); } public void update() { @@ -43,7 +51,7 @@ public class Glow { running = false; for (Player hider : Main.getInstance().getBoard().getHiders()) { for (Player seeker : Main.getInstance().getBoard().getSeekers()) { - Packet.setGlow(hider, seeker, false); + setGlow(hider, seeker, false); } } } @@ -52,4 +60,23 @@ public class Glow { return running; } + public void setGlow(Player player, Player target, boolean glowing) { + PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_METADATA); + packet.getIntegers().write(0, target.getEntityId()); + WrappedDataWatcher watcher = new WrappedDataWatcher(); + WrappedDataWatcher.Serializer serializer = WrappedDataWatcher.Registry.get(Byte.class); + watcher.setEntity(target); + if (glowing) { + watcher.setObject(0, serializer, (byte) (0x40)); + } else { + watcher.setObject(0, serializer, (byte) (0x0)); + } + packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects()); + try { + protocolManager.sendServerPacket(player, packet); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java index ee63cc2..701f446 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/DamageHandler.java @@ -4,9 +4,8 @@ import com.cryptomorin.xseries.XSound; import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.game.Board; import net.tylermurphy.hideAndSeek.game.Game; -import net.tylermurphy.hideAndSeek.game.util.PlayerUtil; +import net.tylermurphy.hideAndSeek.game.PlayerLoader; import net.tylermurphy.hideAndSeek.game.util.Status; -import net.tylermurphy.hideAndSeek.game.util.Version; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -70,7 +69,7 @@ public class DamageHandler implements Listener { // Spectators cannot take damage if (board.isSpectator(player)) { event.setCancelled(true); - if (Version.atLeast("1.18") && player.getLocation().getY() < -64) { + if (Main.getInstance().supports(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())); @@ -87,7 +86,7 @@ public class DamageHandler implements Listener { // Handle death event event.setCancelled(true); // Play death effect - if (Version.atLeast("1.9")) { + if (Main.getInstance().supports(9)) { XSound.ENTITY_PLAYER_DEATH.play(player, 1, 1); } else { XSound.ENTITY_PLAYER_HURT.play(player, 1, 1); @@ -112,7 +111,7 @@ public class DamageHandler implements Listener { // Add leaderboard kills if attacker if (attacker != null && ( board.isHider(attacker) || board.getFirstSeeker().getName().equals(attacker.getName()) ) ) board.addKill(attacker.getUniqueId()); - PlayerUtil.resetPlayer(player, board); + PlayerLoader.resetPlayer(player, board); 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 index 4e30a50..c8dd488 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InteractHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InteractHandler.java @@ -3,19 +3,13 @@ package net.tylermurphy.hideAndSeek.game.listener; import com.cryptomorin.xseries.XMaterial; import com.cryptomorin.xseries.messages.ActionBar; import net.tylermurphy.hideAndSeek.Main; -import net.tylermurphy.hideAndSeek.command.Debug; -import net.tylermurphy.hideAndSeek.game.util.PlayerUtil; import net.tylermurphy.hideAndSeek.game.util.Status; -import net.tylermurphy.hideAndSeek.game.util.Version; -import org.bukkit.Bukkit; import org.bukkit.ChatColor; -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.block.Action; -import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; @@ -123,37 +117,4 @@ public class InteractHandler implements Listener { playerhead.setItemMeta(playerheadmeta); return playerhead; } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onInventoryClick(InventoryClickEvent event) { - if (event.getWhoClicked() instanceof Player) { - Player player = (Player) event.getWhoClicked(); - // Block players from moving lobby items - if (Main.getInstance().getBoard().contains(player) && Main.getInstance().getGame().getStatus() == Status.STANDBY) { - event.setCancelled(true); - } - // Spectator Teleport Menu - if (Main.getInstance().getBoard().isSpectator(player) && event.getCurrentItem().getType() == XMaterial.PLAYER_HEAD.parseMaterial()) { - event.setCancelled(true); - player.closeInventory(); - String name = event.getCurrentItem().getItemMeta().getDisplayName(); - Player clicked = Main.getInstance().getServer().getPlayer(name); - if(clicked == null) return; - player.teleport(clicked); - } - // Debug Menu - boolean debug = false; - if(Version.atLeast("1.14")){ - debug = event.getView().getTitle().equals("Debug Menu") && player.hasPermission("hideandseek.debug"); - } else { - debug = event.getInventory().getName().equals("Debug Menu") && player.hasPermission("hideandseek.debug"); - } - if (debug){ - event.setCancelled(true); - player.closeInventory(); - Debug.handleOption(player, event.getRawSlot()); - } - } - } - } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java new file mode 100644 index 0000000..f0fb4f9 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/InventoryHandler.java @@ -0,0 +1,77 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 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.listener; + +import com.cryptomorin.xseries.XMaterial; +import net.tylermurphy.hideAndSeek.Main; +import net.tylermurphy.hideAndSeek.command.Debug; +import net.tylermurphy.hideAndSeek.game.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.inventory.InventoryClickEvent; + +public class InventoryHandler implements Listener { + + @EventHandler(priority = EventPriority.HIGHEST) + public void onInventoryClick(InventoryClickEvent event) { + if (!(event.getWhoClicked() instanceof Player)) return; + + Player player = (Player) event.getWhoClicked(); + checkForInventoryMove(event); + checkForSpectatorTeleportMenu(event); + checkForDebugMenu(event); + } + + private void checkForInventoryMove(InventoryClickEvent event){ + if (Main.getInstance().getBoard().contains((Player) event.getWhoClicked()) && Main.getInstance().getGame().getStatus() == Status.STANDBY) { + event.setCancelled(true); + } + } + + private void checkForSpectatorTeleportMenu(InventoryClickEvent event){ + Player player = (Player) event.getWhoClicked(); + if (Main.getInstance().getBoard().isSpectator(player) && event.getCurrentItem().getType() == XMaterial.PLAYER_HEAD.parseMaterial()) { + event.setCancelled(true); + player.closeInventory(); + String name = event.getCurrentItem().getItemMeta().getDisplayName(); + Player clicked = Main.getInstance().getServer().getPlayer(name); + if(clicked == null) return; + player.teleport(clicked); + } + } + + private void checkForDebugMenu(InventoryClickEvent event){ + Player player = (Player) event.getWhoClicked(); + boolean debug; + if(Main.getInstance().supports(14)){ + debug = event.getView().getTitle().equals("Debug Menu") && player.hasPermission("hideandseek.debug"); + } else { + debug = event.getInventory().getName().equals("Debug Menu") && player.hasPermission("hideandseek.debug"); + } + if (debug){ + event.setCancelled(true); + player.closeInventory(); + Debug.handleOption(player, event.getRawSlot()); + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java index aba8275..7a7e8ae 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/MovementHandler.java @@ -3,6 +3,7 @@ package net.tylermurphy.hideAndSeek.game.listener; import com.comphenix.protocol.PacketType; import com.google.common.collect.Sets; import net.tylermurphy.hideAndSeek.Main; +import net.tylermurphy.hideAndSeek.game.listener.events.PlayerJumpEvent; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -26,13 +27,18 @@ public class MovementHandler implements Listener { checkBounds(event); } + @EventHandler(priority = EventPriority.HIGHEST) + public void onJump(PlayerJumpEvent event) { + if(Main.getInstance().getBoard().isSpectator(event.getPlayer()) && event.getPlayer().getAllowFlight()) { + event.getPlayer().setFlying(true); + } + } + private void checkJumping(PlayerMoveEvent event){ - if (!Main.getInstance().getBoard().isSpectator(event.getPlayer())) return; if (event.getPlayer().getVelocity().getY() > 0) { if (event.getPlayer().getLocation().getBlock().getType() != Material.LADDER && prevPlayersOnGround.contains(event.getPlayer().getUniqueId())) { if (!event.getPlayer().isOnGround()) { - // JUMPING :o - if(event.getPlayer().getAllowFlight()) event.getPlayer().setFlying(true); + Main.getInstance().getServer().getPluginManager().callEvent(new PlayerJumpEvent(event.getPlayer())); } } } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/events/PlayerJumpEvent.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/events/PlayerJumpEvent.java new file mode 100644 index 0000000..f42a33c --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/events/PlayerJumpEvent.java @@ -0,0 +1,49 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 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.listener.events; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +public class PlayerJumpEvent extends PlayerEvent implements Cancellable { + + private boolean cancel = false; + + public PlayerJumpEvent(Player player) { + super(player); + } + + @Override + public boolean isCancelled() { + return cancel; + } + + @Override + public void setCancelled(boolean b) { + this.cancel = !b; + } + + @Override + public HandlerList getHandlers() { + return null; + } +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/util/Packet.java b/src/main/java/net/tylermurphy/hideAndSeek/game/util/Packet.java deleted file mode 100644 index 0fad539..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/util/Packet.java +++ /dev/null @@ -1,56 +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.util; - -import com.comphenix.protocol.PacketType; -import com.comphenix.protocol.ProtocolLibrary; -import com.comphenix.protocol.ProtocolManager; -import com.comphenix.protocol.events.PacketContainer; -import com.comphenix.protocol.wrappers.WrappedDataWatcher; -import com.comphenix.protocol.wrappers.WrappedDataWatcher.Registry; -import com.comphenix.protocol.wrappers.WrappedDataWatcher.Serializer; -import org.bukkit.entity.Player; - -import java.lang.reflect.InvocationTargetException; - -public class Packet { - - private static final ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); - - public static void setGlow(Player player, Player target, boolean glowing) { - PacketContainer packet = protocolManager.createPacket(PacketType.Play.Server.ENTITY_METADATA); - packet.getIntegers().write(0, target.getEntityId()); - WrappedDataWatcher watcher = new WrappedDataWatcher(); - Serializer serializer = Registry.get(Byte.class); - watcher.setEntity(target); - if (glowing) { - watcher.setObject(0, serializer, (byte) (0x40)); - } else { - watcher.setObject(0, serializer, (byte) (0x0)); - } - packet.getWatchableCollectionModifier().write(0, watcher.getWatchableObjects()); - try { - protocolManager.sendServerPacket(player, packet); - } catch (InvocationTargetException e) { - e.printStackTrace(); - } - } - -} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/util/Version.java b/src/main/java/net/tylermurphy/hideAndSeek/game/util/Version.java deleted file mode 100644 index e2dcfdd..0000000 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/util/Version.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.tylermurphy.hideAndSeek.game.util; - -import org.bukkit.Bukkit; - -import java.util.HashMap; -import java.util.Map; - -public class Version { - - private static final Map CACHE = new HashMap<>(); - - public static boolean atLeast(String testVersion) { - - - if (CACHE.containsKey(testVersion)) return CACHE.get(testVersion); - - String[] serverCheckTemp = Bukkit.getBukkitVersion().substring(2,Bukkit.getBukkitVersion().indexOf('-')).split("\\."); - int[] serverCheck = new int[serverCheckTemp.length]; - for(int i=0; i serverCheck[0]) return false; - else if (customCheck[0] < serverCheck[0]) return true; - else { - if (customCheck.length == 1 && serverCheck.length == 1) return true; - else if (customCheck.length == 2 && serverCheck.length == 2) { - return customCheck[1] <= serverCheck[1]; - } - else return serverCheck.length == 2; - } - } -}