diff options
author | sowgro <tpoke.ferrari@gmail.com> | 2024-06-12 00:46:56 -0400 |
---|---|---|
committer | sowgro <tpoke.ferrari@gmail.com> | 2024-06-12 00:46:56 -0400 |
commit | c78d343eed0ff8f37e1f77cfe12fec0d47e47be0 (patch) | |
tree | ed85b36fb57131f9a854852654b5c5d2e158e327 /src/main | |
parent | 519cf806ea12fcbfffa6e8fbae0efc0c617b87ed (diff) | |
download | Tumble-c78d343eed0ff8f37e1f77cfe12fec0d47e47be0.tar.gz Tumble-c78d343eed0ff8f37e1f77cfe12fec0d47e47be0.tar.bz2 Tumble-c78d343eed0ff8f37e1f77cfe12fec0d47e47be0.zip |
New config format, kill-at-y setting and more bug fixes
Diffstat (limited to 'src/main')
11 files changed, 154 insertions, 95 deletions
diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/Config.java b/src/main/java/com/MylesAndMore/Tumble/commands/Config.java index 256b45e..5fc2de6 100644 --- a/src/main/java/com/MylesAndMore/Tumble/commands/Config.java +++ b/src/main/java/com/MylesAndMore/Tumble/commands/Config.java @@ -35,7 +35,7 @@ public class Config implements CommandExecutor, TabCompleter { switch (args[0]) { case "add" -> { String arenaName = args[1]; - ConfigManager.arenas.put(arenaName, new Arena(arenaName, ((Player)sender).getLocation())); + ConfigManager.arenas.put(arenaName, new Arena(arenaName, ((Player)sender).getLocation(), null)); sender.sendMessage(ChatColor.GREEN + "Arena added."); } case "set" -> { diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/ForceStop.java b/src/main/java/com/MylesAndMore/Tumble/commands/ForceStop.java index 8845e25..d862586 100644 --- a/src/main/java/com/MylesAndMore/Tumble/commands/ForceStop.java +++ b/src/main/java/com/MylesAndMore/Tumble/commands/ForceStop.java @@ -34,6 +34,11 @@ public class ForceStop implements CommandExecutor, TabCompleter { game = ConfigManager.arenas.get(args[0]).game; } + if (game == null) { + sender.sendMessage(ChatColor.RED + "No game is currently running in this arena"); + return false; + } + game.killGame(); sender.sendMessage(ChatColor.GREEN + "Game stopped."); return true; diff --git a/src/main/java/com/MylesAndMore/Tumble/commands/Reload.java b/src/main/java/com/MylesAndMore/Tumble/commands/Reload.java index 5d35a03..a19b04b 100644 --- a/src/main/java/com/MylesAndMore/Tumble/commands/Reload.java +++ b/src/main/java/com/MylesAndMore/Tumble/commands/Reload.java @@ -1,5 +1,7 @@ package com.MylesAndMore.Tumble.commands; +import com.MylesAndMore.Tumble.game.Arena; +import com.MylesAndMore.Tumble.plugin.ConfigManager; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -20,8 +22,15 @@ public class Reload implements CommandExecutor, TabCompleter { sender.sendMessage(ChatColor.RED + "You do not have permission to perform this command!"); return false; } + + for (Arena a : ConfigManager.arenas.values()) { + if (a.game != null) { + a.game.killGame(); + } + } + plugin.onEnable(); - sender.sendMessage(ChatColor.GREEN + "Tumble configuration reloaded successfully."); + sender.sendMessage(ChatColor.GREEN + "Tumble configuration reloaded. Check console for errors."); return true; } diff --git a/src/main/java/com/MylesAndMore/Tumble/game/Arena.java b/src/main/java/com/MylesAndMore/Tumble/game/Arena.java index 499fa6c..831f251 100644 --- a/src/main/java/com/MylesAndMore/Tumble/game/Arena.java +++ b/src/main/java/com/MylesAndMore/Tumble/game/Arena.java @@ -12,15 +12,17 @@ public class Arena { public final World world; public Location location; public final String name; + public Integer killAtY; /** * Creates a new Arena * @param name Name of the arena * @param location Center point / spawn point. */ - public Arena(@NotNull String name, @NotNull Location location) { + public Arena(@NotNull String name, @NotNull Location location, Integer killAtY) { this.location = location; this.world = location.getWorld(); this.name = name; + this.killAtY = killAtY; } } diff --git a/src/main/java/com/MylesAndMore/Tumble/game/EventListener.java b/src/main/java/com/MylesAndMore/Tumble/game/EventListener.java index 6f6a1ca..28fb0d8 100644 --- a/src/main/java/com/MylesAndMore/Tumble/game/EventListener.java +++ b/src/main/java/com/MylesAndMore/Tumble/game/EventListener.java @@ -19,6 +19,7 @@ import org.bukkit.event.player.*; import org.bukkit.util.Vector; import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.Nullable; import static com.MylesAndMore.Tumble.Main.plugin; @@ -131,9 +132,16 @@ public class EventListener implements Listener { public void PlayerMoveEvent(PlayerMoveEvent event) { // Cancel movement if the game is starting (so players can't move before the game starts) if (Objects.equals(game.gameState, GameState.STARTING) - && event.getPlayer().getWorld().equals(gameWorld)) { + && event.getPlayer().getWorld().equals(gameWorld) + && !equalPosition(event.getFrom(),event.getTo())) { event.setCancelled(true); } + // kill player if they are below a Y level + if (event.getPlayer().getWorld().equals(gameWorld) && game.arena.killAtY != null) { + if (event.getPlayer().getLocation().getY() <= game.arena.killAtY) { + event.getPlayer().setHealth(0); + } + } } @EventHandler @@ -146,7 +154,7 @@ public class EventListener implements Listener { @EventHandler public void PlayerInteractEvent(PlayerInteractEvent event) { - if (game.roundType != GameType.SHOVELS) {return;} +// if (game.roundType != GameType.SHOVELS) {return;} // Remove blocks when clicked in the game world (all gamemodes require this functionality) if (event.getAction() == Action.LEFT_CLICK_BLOCK && Objects.requireNonNull(event.getClickedBlock()).getWorld() == gameWorld) { @@ -182,8 +190,8 @@ public class EventListener implements Listener { if (event.getEntity().getWorld() == gameWorld) { if (event.getEntity() instanceof Player) { if (event.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK - && event.getCause() == EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK - && event.getCause() == EntityDamageEvent.DamageCause.FALL) { + || event.getCause() == EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK + || event.getCause() == EntityDamageEvent.DamageCause.FALL) { event.setCancelled(true); } } @@ -205,5 +213,15 @@ public class EventListener implements Listener { } } - // TODO: stop tile drops for pistons -} + // TODO: stop tile drops for pistons, stop player from getting stuck in the waiting area after they leave + + public static boolean equalPosition(Location l1, @Nullable Location l2) { + if (l2 == null) { + return true; + } + return (l1.getX() == l2.getX()) && + (l1.getY() == l2.getY()) && + (l1.getZ() == l2.getZ()); + } + +}
\ No newline at end of file diff --git a/src/main/java/com/MylesAndMore/Tumble/game/Game.java b/src/main/java/com/MylesAndMore/Tumble/game/Game.java index f266375..c8244d9 100644 --- a/src/main/java/com/MylesAndMore/Tumble/game/Game.java +++ b/src/main/java/com/MylesAndMore/Tumble/game/Game.java @@ -131,7 +131,9 @@ public class Game { */ private void roundEnd() { // Cancel the tasks that auto-end the round + gameState = GameState.ENDING; Bukkit.getServer().getScheduler().cancelTask(gameID); + gameID = -1; // Clear old layers (as a fill command, this would be /fill ~-20 ~-20 ~-20 ~20 ~ ~20 relative to spawn) playSound(gamePlayers, Sound.BLOCK_NOTE_BLOCK_PLING, SoundCategory.BLOCKS, 5, 0); // Check if there was a definite winner or not @@ -162,6 +164,10 @@ public class Game { */ private void gameEnd() { if (!gamePlayers.isEmpty()) { + Bukkit.getServer().getScheduler().cancelTask(gameID); + gameID = -1; + Bukkit.getServer().getScheduler().cancelTask(autoStartID); + autoStartID = -1; Player winner = getPlayerWithMostWins(gameWins); setGamemode(gamePlayers, GameMode.SPECTATOR); clearInventories(gamePlayers); @@ -193,6 +199,9 @@ public class Game { */ public void killGame() { Bukkit.getServer().getScheduler().cancelTask(gameID); + gameID = -1; + Bukkit.getServer().getScheduler().cancelTask(autoStartID); + autoStartID = -1; HandlerList.unregisterAll(eventListener); clearInventories(gamePlayers); for (Player aPlayer : gamePlayers) { @@ -253,7 +262,7 @@ public class Game { // remove that player (who just died) from the roundPlayersArray, effectively eliminating them, playersAlive.remove(player); // If there are less than 2 players in the game (1 just died), - if (playersAlive.size() < 2) { + if (playersAlive.size() < 2 && gameState == GameState.RUNNING) { roundEnd(); } } diff --git a/src/main/java/com/MylesAndMore/Tumble/game/Layers.java b/src/main/java/com/MylesAndMore/Tumble/game/Layers.java index b87df26..9c60393 100644 --- a/src/main/java/com/MylesAndMore/Tumble/game/Layers.java +++ b/src/main/java/com/MylesAndMore/Tumble/game/Layers.java @@ -190,12 +190,9 @@ public class Layers { add(Material.REDSTONE_BLOCK); add(Material.REDSTONE_LAMP); add(Material.TARGET); - add(Material.DAYLIGHT_DETECTOR); add(Material.PISTON); - add(Material.STICKY_PISTON); add(Material.SLIME_BLOCK); add(Material.OBSERVER); - add(Material.HOPPER); }}; matList.add(gen8); matList.add(gen9); diff --git a/src/main/java/com/MylesAndMore/Tumble/plugin/ConfigManager.java b/src/main/java/com/MylesAndMore/Tumble/plugin/ConfigManager.java index 66bcb09..855e755 100644 --- a/src/main/java/com/MylesAndMore/Tumble/plugin/ConfigManager.java +++ b/src/main/java/com/MylesAndMore/Tumble/plugin/ConfigManager.java @@ -29,59 +29,75 @@ public class ConfigManager { public static void readConfig() { plugin.reloadConfig(); FileConfiguration config = plugin.getConfig(); - - // arenas - if (config.getConfigurationSection("arenas") == null) { - plugin.getLogger().warning("Section arenas is missing from config"); - return; - } - arenas = new HashMap<>(); - for (String arenaName: Objects.requireNonNull(config.getConfigurationSection("arenas")).getKeys(false)) { - ConfigurationSection section = Objects.requireNonNull(config.getConfigurationSection("arenas")).getConfigurationSection(arenaName); - Result<Location> res = readWorld(section); + HideLeaveJoin = config.getBoolean("hideJoinLeaveMessages", false); + waitDuration = config.getInt("wait-duration", 15); + + // wait area + if (config.getBoolean("wait-area.enable", false)) { + Result<Location>res = readWorld(config.getConfigurationSection("wait-area.spawn")); if (!res.success) { - plugin.getLogger().warning("Failed to load arena "+arenaName+": "+res.error); - continue; + plugin.getLogger().warning("Failed to load winner lobby: "+res.error); + waitArea = null; + } + else { + waitArea = res.value; } - - arenas.put(arenaName, new Arena(arenaName, res.value)); } // lobby { - Result<Location>res = readWorld(config.getConfigurationSection("lobby-spawn")); + Result<Location>res = readWorld(config.getConfigurationSection("lobby.spawn")); if (!res.success) { plugin.getLogger().warning("Failed to load lobby: "+res.error); - plugin.getLogger().severe("Lobby world is required. Run '/tumble-config set lobbyWorld' ASAP"); + plugin.getLogger().severe("Lobby spawn is required! Lobby spawn will default to spawn in the default world. Run '/tumble-config set lobbyWorld' to change it"); + lobby = Bukkit.getServer().getWorlds().get(0).getSpawnLocation(); + } + else { + lobby = res.value; } - - lobby = res.value; } // winner lobby - if (config.getBoolean("enable-winner-lobby-spawn")) { - Result<Location>res = readWorld(config.getConfigurationSection("winner-lobby-spawn")); + if (config.getBoolean("winner-lobby.enable", false)) { + Result<Location>res = readWorld(config.getConfigurationSection("winner-lobby.spawn")); if (!res.success) { plugin.getLogger().warning("Failed to load winner lobby: "+res.error); + winnerLobby = null; } + else { + winnerLobby = res.value; + } + } - winnerLobby = res.value; + // arenas + ConfigurationSection arenasSection = config.getConfigurationSection("arenas"); + if (arenasSection == null) { + plugin.getLogger().warning("Section 'arenas' is missing from config"); + return; } - - // wait area - if (config.getBoolean("enable-wait-area")) { - Result<Location>res = readWorld(config.getConfigurationSection("wait-area")); + arenas = new HashMap<>(); + for (String arenaName: arenasSection.getKeys(false)) { + + ConfigurationSection anArenaSection = arenasSection.getConfigurationSection(arenaName); + if (anArenaSection == null) { + plugin.getLogger().warning("Failed to load arena "+arenaName+": Error loading config section"); + continue; + } + + Integer killAtY = anArenaSection.getInt("kill-at-y", 0); + if (killAtY == 0) { + killAtY = null; + } + + Result<Location> res = readWorld(anArenaSection.getConfigurationSection("spawn")); if (!res.success) { - plugin.getLogger().warning("Failed to load winner lobby: "+res.error); + plugin.getLogger().warning("Failed to load arena "+arenaName+": "+res.error); + continue; } - waitArea = res.value; + arenas.put(arenaName, new Arena(arenaName, res.value, killAtY)); } - - // other - HideLeaveJoin = config.getBoolean("hideJoinLeaveMessages"); - waitDuration = config.getInt("wait-duration", 15); } /** @@ -99,69 +115,54 @@ public class ConfigManager { private static Result<Location> readWorld(@Nullable ConfigurationSection section) { if (section == null) { - Result<Location> res = new Result<>(); - res.success = false; - res.error = "Section missing from config"; - return res; + return new Result<>("Section missing from config"); } double x = section.getDouble("x"); double y = section.getDouble("y"); double z = section.getDouble("x"); if (x == 0 || y == 0 || z == 0) { - Result<Location> res = new Result<>(); - res.success = false; - res.error = "Arena coordinates are missing or are zero. Coordinates cannot be zero."; - return res; + return new Result<>("Arena coordinates are missing or are zero. Coordinates cannot be zero."); } String worldName = section.getString("world"); if (worldName == null) { - Result<Location> res = new Result<>(); - res.success = false; - res.error = "World name is missing"; - return res; + return new Result<>("World name is missing"); } World world = Bukkit.getWorld(worldName); if (world == null) { - Result<Location> res = new Result<>(); - res.success = false; - res.error = "Failed to load world " + worldName; - return res; + return new Result<>("Failed to load world " + worldName); } - Result<Location> res = new Result<>(); - res.success = true; - res.value = new Location(world,x,y,z); - return res; + return new Result<>(new Location(world,x,y,z)); } public static void WriteConfig() { if (waitArea != null) { WriteWorld(Objects.requireNonNull(plugin.getConfig().getConfigurationSection("wait-area")), waitArea); - plugin.getConfig().set("enable-wait-area", true); + plugin.getConfig().set("wait-area.enable", true); } else { - plugin.getConfig().set("enable-wait-area", false); + plugin.getConfig().set("wait-area.enable", false); } if (lobby != null) { - WriteWorld(Objects.requireNonNull(plugin.getConfig().getConfigurationSection("lobby-spawn")), lobby); + WriteWorld(Objects.requireNonNull(plugin.getConfig().getConfigurationSection("lobby.spawn")), lobby); } if (winnerLobby != null) { - WriteWorld(Objects.requireNonNull(plugin.getConfig().getConfigurationSection("winner-spawn")), winnerLobby); - plugin.getConfig().set("enable-winner-lobby-spawn", true); + WriteWorld(Objects.requireNonNull(plugin.getConfig().getConfigurationSection("winner-lobby.spawn")), winnerLobby); + plugin.getConfig().set("winner-lobby.enable", true); } else { - plugin.getConfig().set("enable-winner-lobby-spawn", true); + plugin.getConfig().set("winner-lobby.enable", false); } for (String arenaName: arenas.keySet()) { - ConfigurationSection c = plugin.getConfig().getConfigurationSection("arenas."+arenaName); + ConfigurationSection c = plugin.getConfig().getConfigurationSection("arenas."+arenaName+".spawn"); if (c == null) { - c = plugin.getConfig().createSection("arenas."+arenaName); + c = plugin.getConfig().createSection("arenas."+arenaName+".spawn"); } WriteWorld(c, arenas.get(arenaName).location); } diff --git a/src/main/java/com/MylesAndMore/Tumble/plugin/GameState.java b/src/main/java/com/MylesAndMore/Tumble/plugin/GameState.java index 879a1f5..06b2abe 100644 --- a/src/main/java/com/MylesAndMore/Tumble/plugin/GameState.java +++ b/src/main/java/com/MylesAndMore/Tumble/plugin/GameState.java @@ -4,4 +4,5 @@ public enum GameState { WAITING, STARTING, RUNNING, + ENDING } diff --git a/src/main/java/com/MylesAndMore/Tumble/plugin/Result.java b/src/main/java/com/MylesAndMore/Tumble/plugin/Result.java index 68e1133..9a07771 100644 --- a/src/main/java/com/MylesAndMore/Tumble/plugin/Result.java +++ b/src/main/java/com/MylesAndMore/Tumble/plugin/Result.java @@ -2,7 +2,19 @@ package com.MylesAndMore.Tumble.plugin; // java does not have result types (i miss rust </3) so i did this public class Result<T> { - public boolean success; - public T value; - public String error; + public final boolean success; + public final T value; + public final String error; + + public Result(String error) { + success = false; + this.error = error; + this.value = null; + } + + public Result(T value) { + success = true; + this.value = value; + this.error = null; + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b81d268..b13570e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,35 +5,40 @@ hideJoinLeaveMessages: false wait-duration: 15 # Teleport players somewhere while waiting for the game to start # Keep in mind that these coordinates cannot be zero! Use something like 0.5 instead -enable-wait-area: false wait-area: - x: - y: - z: - world: + enable: false + spawn: + x: + y: + z: + world: # Place where everyone is teleported to after a game ends REQUIRED # Keep in mind that these coordinates cannot be zero! Use something like 0.5 instead -lobby-spawn: - x: 0.5 - y: 100 - z: 0.5 - world: world +lobby: + spawn: + x: 0.5 + y: 100 + z: 0.5 + world: world # Place that the winner is teleported after a game ends # Keep in mind that these coordinates cannot be zero! Use something like 0.5 instead -enable-winner-lobby-spawn: false -winner-lobby-spawn: - x: - y: - z: - world: +winner-lobby: + enable: false + spawn: + x: + y: + z: + world: # Add/remove as you wish # Keep in mind that these coordinates cannot be zero! Use something like 0.5 instead arenas: 'test': - x: 100 - y: 100 - z: 100 - world: world
\ No newline at end of file + kill-at-y: 5 + spawn: + x: 100 + y: 100 + z: 100 + world: world
\ No newline at end of file |