From f0a7cd62fb3759e0e196af23b711ff9513d3cd6e Mon Sep 17 00:00:00 2001 From: Myles <43725835+MylesAndMore@users.noreply.github.com> Date: Fri, 9 Dec 2022 14:49:47 +0000 Subject: code is now insult-proof --- README.md | 2 +- .../com/MylesAndMore/tumble/EventListener.java | 199 +++++++++++---------- 2 files changed, 106 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index 93d83c4..04a4a38 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,6 @@ once this list is complete and all bugs are fixed, we *should* be ready for rele ## etc -- [ ] refactor EventListener null checker code +- [x] refactor EventListener null checker code - if (TumbleManager.getGameWorld() == null && TumbleManager.getLobbyWorld() == null) { return; } - [ ] add game music? but probably only for us; I feel like the og music must be copyrighted diff --git a/src/main/java/com/MylesAndMore/tumble/EventListener.java b/src/main/java/com/MylesAndMore/tumble/EventListener.java index 7d07ab4..a5169c5 100644 --- a/src/main/java/com/MylesAndMore/tumble/EventListener.java +++ b/src/main/java/com/MylesAndMore/tumble/EventListener.java @@ -15,111 +15,120 @@ import org.bukkit.event.entity.ProjectileLaunchEvent; import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; -public class EventListener implements Listener{ +public class EventListener implements Listener { @EventHandler public void PlayerJoinEvent(PlayerJoinEvent event) { - // On a PlayerJoinEvent, check if the config is set to hide the join/leave messages - // If true, null out the join message (which just makes it so that there is no message) + // On a PlayerJoinEvent, check if the config is set to hide the join/leave + // messages + // If true, null out the join message (which just makes it so that there is no + // message) // If false, nothing will happen, and the default message will display if (TumbleManager.getPlugin().getConfig().getBoolean("hideJoinLeaveMessages")) { event.setJoinMessage(null); } - // If the gameWorld and lobbyWorld is not null, then check - if (TumbleManager.getGameWorld() != null && TumbleManager.getLobbyWorld() != null) { - // if the player joining is in the game world, then - if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { - // send them back to the lobby. - event.getPlayer().teleport(Bukkit.getWorld(TumbleManager.getLobbyWorld()).getSpawnLocation()); - } - // For auto-start function: check if the autoStart is enabled - if (TumbleManager.getPlugin().getConfig().getBoolean("autoStart.enabled")) { - // If so, check if the amount of players has been reached - if (TumbleManager.getPlayersInLobby().size() == TumbleManager.getPlugin().getConfig().getInt("autoStart.players")) { - // The autoStart should begin; pass this to the Game - Game.getGame().autoStart(); - } + // Check if either of the worlds are not defined in config, if so, end + // This is to avoid NPEs and such + if (TumbleManager.getGameWorld() == null || TumbleManager.getLobbyWorld() == null) { + return; + } + // Check if the player joining is in the game world, if true then + if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { + // send them back to the lobby. + event.getPlayer().teleport(Bukkit.getWorld(TumbleManager.getLobbyWorld()).getSpawnLocation()); + } + // For auto-start function: check if the autoStart is enabled + if (TumbleManager.getPlugin().getConfig().getBoolean("autoStart.enabled")) { + // If so, check if the amount of players has been reached + if (TumbleManager.getPlayersInLobby().size() == TumbleManager.getPlugin().getConfig().getInt("autoStart.players")) { + // The autoStart should begin; pass this to the Game + Game.getGame().autoStart(); } } } @EventHandler public void PlayerChangedWorldEvent(PlayerChangedWorldEvent event) { - // If the gameWorld and lobbyWorld is not null, then check - if (TumbleManager.getGameWorld() != null && TumbleManager.getLobbyWorld() != null) { - // if the player changed to the lobbyWorld, then - if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getLobbyWorld())) { - // run the autostart checks (from above) - if (TumbleManager.getPlugin().getConfig().getBoolean("autoStart.enabled")) { - if (TumbleManager.getPlayersInLobby().size() == TumbleManager.getPlugin().getConfig().getInt("autoStart.players")) { - Game.getGame().autoStart(); - } + if (TumbleManager.getGameWorld() == null || TumbleManager.getLobbyWorld() == null) { + return; + } + // Check if the player changed to the lobbyWorld, then + if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getLobbyWorld())) { + // run the autostart checks (commented above) + if (TumbleManager.getPlugin().getConfig().getBoolean("autoStart.enabled")) { + if (TumbleManager.getPlayersInLobby().size() == TumbleManager.getPlugin().getConfig().getInt("autoStart.players")) { + Game.getGame().autoStart(); } } - // also check if the player left to another world - else if (event.getFrom() == Bukkit.getWorld(TumbleManager.getLobbyWorld())) { - if (Objects.equals(Game.getGame().getGameState(), "waiting")) { - Game.getGame().cancelStart(); - } + } + // also check if the player left to another world + else if (event.getFrom() == Bukkit.getWorld(TumbleManager.getLobbyWorld())) { + if (Objects.equals(Game.getGame().getGameState(), "waiting")) { + Game.getGame().cancelStart(); } } } @EventHandler public void PlayerQuitEvent(PlayerQuitEvent event) { - // On a PlayerQuitEvent, check if the config is set to hide the join/leave messages - // If true, null out the quit message (which just makes it so that there is no message) + // On a PlayerQuitEvent, check if the config is set to hide the join/leave + // messages + // If true, null out the quit message (which just makes it so that there is no + // message) // If false, nothing will happen, and the default message will display if (TumbleManager.getPlugin().getConfig().getBoolean("hideJoinLeaveMessages")) { event.setQuitMessage(null); } - // Check if a player left in the lobbyWorld - if (TumbleManager.getLobbyWorld() != null) { - if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getLobbyWorld())) { - // Check if the game is in the process of autostarting - if (Objects.equals(Game.getGame().getGameState(), "waiting")) { - // Cancel the autostart - Game.getGame().cancelStart(); - } + if (TumbleManager.getLobbyWorld() == null) { + return; + } + if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getLobbyWorld())) { + // Check if the game is in the process of autostarting + if (Objects.equals(Game.getGame().getGameState(), "waiting")) { + // Cancel the autostart + Game.getGame().cancelStart(); } } } @EventHandler public void PlayerDeathEvent(PlayerDeathEvent event) { - // On a PlayerDeathEvent, check to make sure the gameWorld is defined, - if (TumbleManager.getGameWorld() != null) { - // then check to see if the player died in the gameWorld, - if (event.getEntity().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { - // then pass this off to the Game - Game.getGame().playerDeath(event.getEntity()); - } + if (TumbleManager.getGameWorld() == null) { + return; + } + // On a PlayerDeathEvent, + // check to see if the player died in the gameWorld, + if (event.getEntity().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { + // then pass this off to the Game + Game.getGame().playerDeath(event.getEntity()); } } @EventHandler public void ItemDamageEvent(PlayerItemDamageEvent event) { - // On a BlockBreakEvent, check to make sure there is a defined gameWorld - if (TumbleManager.getGameWorld() != null) { - // Then check to see if the block was broken in the gameWorld, - if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { - // If it was in the gameWorld, check if the roundType was shovels - if (Objects.equals(Game.getGame().getRoundType(), "shovels")) { - event.setCancelled(true); - } + if (TumbleManager.getGameWorld() == null) { + return; + } + // On a BlockBreakEvent, + // check to see if the block was broken in the gameWorld, + if (event.getPlayer().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { + // If it was in the gameWorld, check if the roundType was shovels + if (Objects.equals(Game.getGame().getRoundType(), "shovels")) { + event.setCancelled(true); } } } @EventHandler public void ProjectileLaunchEvent(ProjectileLaunchEvent event) { - // When A projectile is launched, check to make sure there is a defined gameWorld - if (TumbleManager.getGameWorld() != null) { - // Then check to see if projectile was thrown in the gameWorld. - if (event.getEntity().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { - if (event.getEntity() instanceof Snowball) { - if (event.getEntity().getShooter() instanceof Player player) { - player.getInventory().addItem(new ItemStack(Material.SNOWBALL,1)); - } + if (TumbleManager.getGameWorld() == null) { + return; + } + // When a projectile is launched, + // check to see if projectile was thrown in the gameWorld. + if (event.getEntity().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { + if (event.getEntity() instanceof Snowball) { + if (event.getEntity().getShooter() instanceof Player player) { + player.getInventory().addItem(new ItemStack(Material.SNOWBALL, 1)); } } } @@ -127,22 +136,25 @@ public class EventListener implements Listener{ @EventHandler public void ProjectileHitEvent(ProjectileHitEvent event) { - // When a projectile hits, check to see if the gameWorld is null, - if (TumbleManager.getGameWorld() != null) { - // then check to see if the projectile hit in the gameWorld, - if (event.getHitBlock().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { - // then check if the projectile was a snowball, - if (event.getEntity() instanceof Snowball) { - // then check if a player threw it, - if (event.getEntity().getShooter() instanceof Player player) { - // then check if that block is within the game area, - if (event.getHitBlock().getLocation().distanceSquared(Bukkit.getWorld(TumbleManager.getGameWorld()).getSpawnLocation()) < 402) { - // then remove that block. - event.getHitBlock().setType(Material.AIR); - } + if (TumbleManager.getGameWorld() == null) { + return; + } + // When a projectile hits + // check to see if the projectile hit in the gameWorld, + if (event.getHitBlock().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { + // then check if the projectile was a snowball, + if (event.getEntity() instanceof Snowball) { + // then check if a player threw it, + if (event.getEntity().getShooter() instanceof Player player) { + // then check if that block is within the game area, + if (event.getHitBlock().getLocation().distanceSquared( + Bukkit.getWorld(TumbleManager.getGameWorld()).getSpawnLocation()) < 402) { + // then remove that block. + event.getHitBlock().setType(Material.AIR); } } } + // Weird stacktrace thing else if (event.getHitBlock().getWorld() == null) { event.setCancelled(true); } @@ -151,12 +163,13 @@ public class EventListener implements Listener{ @EventHandler public void PlayerDropItemEvent(PlayerDropItemEvent event) { - // When an item is dropped, make sure there is a defined gameWorld - if (TumbleManager.getGameWorld() != null) { - // Then check if the item was dropped in the game world - if (event.getPlayer().getWorld() == Bukkit.getWorld((TumbleManager.getGameWorld()))) { - event.setCancelled(true); - } + if (TumbleManager.getGameWorld() == null) { + return; + } + // When an item is dropped, + // check if the item was dropped in the game world + if (event.getPlayer().getWorld() == Bukkit.getWorld((TumbleManager.getGameWorld()))) { + event.setCancelled(true); } } @@ -164,23 +177,21 @@ public class EventListener implements Listener{ public void PlayerMoveEvent(PlayerMoveEvent event) { // On a PlayerMoveEvent, check if the game is starting if (Objects.equals(Game.getGame().getGameState(), "starting")) { - // Cancel the event if the game is starting (so players can't move before the game starts) + // Cancel the event if the game is starting (so players can't move before the + // game starts) event.setCancelled(true); } } @EventHandler public void FoodLevelChangeEvent(FoodLevelChangeEvent event) { - // When someone's food level changes, check if the gameWorld is null, - if (TumbleManager.getGameWorld() != null) { - // then check if that happened in the gameWorld - if (event.getEntity().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { - event.setCancelled(true); - } + if (TumbleManager.getGameWorld() == null) { + return; + } + // When someone's food level changes + // check if that happened in the gameWorld + if (event.getEntity().getWorld() == Bukkit.getWorld(TumbleManager.getGameWorld())) { + event.setCancelled(true); } } } - - - - -- cgit v1.2.3