From 8d7b9fc5b987dbe07b15f42c98e873d7dfa7088b Mon Sep 17 00:00:00 2001 From: ruby Date: Sat, 10 Aug 2024 02:23:47 +1200 Subject: [PATCH] Fixin issues --- .../com/pobnellion/floorGame/FloorGame.java | 2 +- .../java/com/pobnellion/floorGame/Util.java | 23 +++++++++++ .../floorGame/command/CommandFloorGame.java | 34 +++++++--------- .../command/TabCompleterFloorGame.java | 7 ++-- .../com/pobnellion/floorGame/game/Floor.java | 26 ++++++++----- .../pobnellion/floorGame/game/GameConfig.java | 39 +++++++++++-------- .../floorGame/game/GameInstance.java | 33 +++++++--------- 7 files changed, 97 insertions(+), 67 deletions(-) create mode 100644 src/main/java/com/pobnellion/floorGame/Util.java diff --git a/src/main/java/com/pobnellion/floorGame/FloorGame.java b/src/main/java/com/pobnellion/floorGame/FloorGame.java index 0089ef9..02de023 100644 --- a/src/main/java/com/pobnellion/floorGame/FloorGame.java +++ b/src/main/java/com/pobnellion/floorGame/FloorGame.java @@ -34,7 +34,7 @@ public final class FloorGame extends JavaPlugin { public static boolean StartGame() { if (gameInstance == null) { - gameInstance = new GameInstance(GameConfig.LoadFromFile()); + gameInstance = new GameInstance(new GameConfig()); gameInstance.Start(); return true; } diff --git a/src/main/java/com/pobnellion/floorGame/Util.java b/src/main/java/com/pobnellion/floorGame/Util.java new file mode 100644 index 0000000..a2da606 --- /dev/null +++ b/src/main/java/com/pobnellion/floorGame/Util.java @@ -0,0 +1,23 @@ +package com.pobnellion.floorGame; + +import org.bukkit.Location; + +public class Util { + public static boolean IsInBounds(Location location, Location bounds1, Location bounds2) { + return location.getX() > Math.min(bounds1.getX(), bounds2.getX()) + && location.getX() < Math.max(bounds1.getX(), bounds2.getX()) + && location.getY() > Math.min(bounds1.getY(), bounds2.getY()) + && location.getY() < Math.max(bounds1.getY(), bounds2.getY()) + && location.getZ() > Math.min(bounds1.getZ(), bounds2.getZ()) + && location.getZ() < Math.max(bounds1.getZ(), bounds2.getZ()); + } + + public static String PrintBlockLocation(Location location) { + return location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ(); + } + + public static String PrintArea(Location[] location) { + return "[" + PrintBlockLocation(location[0]) + "] - [" + PrintBlockLocation(location[1]) + "]"; + } +} + diff --git a/src/main/java/com/pobnellion/floorGame/command/CommandFloorGame.java b/src/main/java/com/pobnellion/floorGame/command/CommandFloorGame.java index 80a3f31..5651b21 100644 --- a/src/main/java/com/pobnellion/floorGame/command/CommandFloorGame.java +++ b/src/main/java/com/pobnellion/floorGame/command/CommandFloorGame.java @@ -2,6 +2,7 @@ package com.pobnellion.floorGame.command; import com.pobnellion.floorGame.FloorGame; import com.pobnellion.floorGame.game.GameConfig; +import com.pobnellion.floorGame.Util; import org.bukkit.ChatColor; import org.bukkit.Location; import org.bukkit.command.Command; @@ -42,15 +43,15 @@ public class CommandFloorGame implements CommandExecutor { } private void Config(CommandSender sender, String[] args) { - var config = GameConfig.LoadFromFile(); + var config = new GameConfig(); if (args.length == 1) { sender.sendMessage(ChatColor.AQUA + "Floor game config:"); - sender.sendMessage(ChatColor.YELLOW + "Player join area: " + ChatColor.WHITE + PrintArea(config.getPlayerJoinArea())); - sender.sendMessage(ChatColor.YELLOW + "Spectator join area: " + ChatColor.WHITE + PrintArea(config.getSpectatorJoinArea())); + sender.sendMessage(ChatColor.YELLOW + "Player join area: " + ChatColor.WHITE + Util.PrintArea(config.getPlayerJoinArea())); + sender.sendMessage(ChatColor.YELLOW + "Spectator join area: " + ChatColor.WHITE + Util.PrintArea(config.getSpectatorJoinArea())); sender.sendMessage(ChatColor.YELLOW + "World: " + ChatColor.WHITE + config.getFloorCenter().getWorld()); - sender.sendMessage(ChatColor.YELLOW + "Post game TP location: " + ChatColor.WHITE + PrintBlockLocation(config.getPostGameTpLocation())); - sender.sendMessage(ChatColor.YELLOW + "Floor center: " + ChatColor.WHITE + PrintBlockLocation(config.getFloorCenter())); + sender.sendMessage(ChatColor.YELLOW + "Post game TP location: " + ChatColor.WHITE + Util.PrintBlockLocation(config.getPostGameTpLocation())); + sender.sendMessage(ChatColor.YELLOW + "Floor center: " + ChatColor.WHITE + Util.PrintBlockLocation(config.getFloorCenter())); sender.sendMessage(ChatColor.YELLOW + "Tile size: " + ChatColor.WHITE + config.getTileSize()); sender.sendMessage(ChatColor.YELLOW + "Grid size: " + ChatColor.WHITE + config.getGridSize()); sender.sendMessage(ChatColor.YELLOW + "Fail limit: " + ChatColor.WHITE + config.getFailLimit()); @@ -62,10 +63,10 @@ public class CommandFloorGame implements CommandExecutor { if (args.length == 2) { switch (args[1]) { - case "playerJoinArea" -> sender.sendMessage(PrintArea(config.getPlayerJoinArea())); - case "spectatorJoinArea" -> sender.sendMessage(PrintArea(config.getSpectatorJoinArea())); - case "postGameTpLocation" -> sender.sendMessage(PrintBlockLocation(config.getPostGameTpLocation())); - case "floorCenter" -> sender.sendMessage(PrintBlockLocation(config.getFloorCenter())); + case "playerJoinArea" -> sender.sendMessage(Util.PrintArea(config.getPlayerJoinArea())); + case "spectatorJoinArea" -> sender.sendMessage(Util.PrintArea(config.getSpectatorJoinArea())); + case "postGameTpLocation" -> sender.sendMessage(Util.PrintBlockLocation(config.getPostGameTpLocation())); + case "floorCenter" -> sender.sendMessage(Util.PrintBlockLocation(config.getFloorCenter())); case "tileSize" -> sender.sendMessage(Integer.toString(config.getTileSize())); case "gridSize" -> sender.sendMessage(Integer.toString(config.getGridSize())); case "failLimit" -> sender.sendMessage(Integer.toString(config.getFailLimit())); @@ -111,11 +112,11 @@ public class CommandFloorGame implements CommandExecutor { switch (args[1]) { case "playerJoinArea" -> { config.setPlayerJoinArea(l1, l2); - sender.sendMessage(ChatColor.YELLOW + "Player join area set to " + PrintArea(config.getPlayerJoinArea())); + sender.sendMessage(ChatColor.YELLOW + "Player join area set to " + Util.PrintArea(config.getPlayerJoinArea())); } case "spectatorJoinArea" -> { config.setSpectatorJoinArea(l1, l2); - sender.sendMessage(ChatColor.YELLOW + "Spectator join area set to " + PrintArea(config.getSpectatorJoinArea())); + sender.sendMessage(ChatColor.YELLOW + "Spectator join area set to " + Util.PrintArea(config.getSpectatorJoinArea())); } } @@ -146,11 +147,12 @@ public class CommandFloorGame implements CommandExecutor { switch (args[1]) { case "postGameTpLocation" -> { config.setPostGameTpLocation(new Location(world, x, y, z)); - sender.sendMessage(ChatColor.YELLOW + "Post game TP location set to " + PrintBlockLocation(config.getPostGameTpLocation())); + sender.sendMessage(ChatColor.YELLOW + "Post game TP location set to " + Util.PrintBlockLocation(config.getPostGameTpLocation())); } case "floorCenter" -> { config.setFloorCenter(new Location(world, x, y, z)); - sender.sendMessage(ChatColor.YELLOW + "Floor center set to " + PrintBlockLocation(config.getFloorCenter())); + config.setWorld(world); + sender.sendMessage(ChatColor.YELLOW + "Floor center set to " + Util.PrintBlockLocation(config.getFloorCenter())); } } } @@ -207,11 +209,5 @@ public class CommandFloorGame implements CommandExecutor { } } - private String PrintBlockLocation(Location location) { - return location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ(); - } - private String PrintArea(Location[] location) { - return "[" + PrintBlockLocation(location[0]) + "] - [" + PrintBlockLocation(location[1]) + "]"; - } } diff --git a/src/main/java/com/pobnellion/floorGame/command/TabCompleterFloorGame.java b/src/main/java/com/pobnellion/floorGame/command/TabCompleterFloorGame.java index 6b44e2a..69b0fef 100644 --- a/src/main/java/com/pobnellion/floorGame/command/TabCompleterFloorGame.java +++ b/src/main/java/com/pobnellion/floorGame/command/TabCompleterFloorGame.java @@ -57,10 +57,11 @@ public class TabCompleterFloorGame implements TabCompleter { return List.of(); var targetBlock = ((Player) sender).getTargetBlockExact(10); - var targetXYZ = targetBlock == null - ? List.of("~", "~", "~") - : List.of(Integer.toString(targetBlock.getX()), Integer.toString(targetBlock.getY()), Integer.toString(targetBlock.getZ())); + if (targetBlock == null) + return List.of(); + + var targetXYZ = List.of(Integer.toString(targetBlock.getX()), Integer.toString(targetBlock.getY()), Integer.toString(targetBlock.getZ())); return List.of(String.join(" ", targetXYZ.subList(args.length - locationArgIndex, 3))); } } diff --git a/src/main/java/com/pobnellion/floorGame/game/Floor.java b/src/main/java/com/pobnellion/floorGame/game/Floor.java index 33d3d32..e196a1a 100644 --- a/src/main/java/com/pobnellion/floorGame/game/Floor.java +++ b/src/main/java/com/pobnellion/floorGame/game/Floor.java @@ -1,11 +1,14 @@ package com.pobnellion.floorGame.game; +import com.pobnellion.floorGame.Util; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; import java.util.HashSet; import java.util.Random; +import java.util.logging.Level; public class Floor { private final Location xzCorner; @@ -52,11 +55,14 @@ public class Floor { } public void Clear() { - for (int row = 0; row < gridSize * tileSize; row++) { - for (int col = 0; col < gridSize * tileSize; col++) { - xzCorner.clone().add(row, 0, col).getBlock().setType(Material.AIR); - } - } + FillArea(xzCorner.getWorld(), + xzCorner.getBlockX(), + xzCorner.getBlockY(), + xzCorner.getBlockZ(), + xzCorner.getBlockX() + gridSize * tileSize, + xzCorner.getBlockY() + gridSize * tileSize, + xzCorner.getBlockZ() + gridSize * tileSize, + Material.AIR); FillWalls(xzCorner.getBlockY(), xzCorner.getBlockY() + 15, Material.AIR); } @@ -108,19 +114,19 @@ public class Floor { material); FillArea(xzCorner.getWorld(), - xzCorner.getBlockX() + floorSize, + xzCorner.getBlockX() + -1, yMin, xzCorner.getBlockZ() + floorSize, - xzCorner.getBlockX() - 1, + xzCorner.getBlockX() + floorSize, yMax, xzCorner.getBlockX() + floorSize, material); } private void FillArea(World world, int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, Material material) { - for (int x = xMin; x < xMax; x++) { - for (int y = yMin; y < yMax; y++) { - for (int z = zMin; z < zMax; z++) { + for (int x = xMin; x <= xMax; x++) { + for (int y = yMin; y <= yMax; y++) { + for (int z = zMin; z <= zMax; z++) { world.getBlockAt(x, y, z).setType(material); } } diff --git a/src/main/java/com/pobnellion/floorGame/game/GameConfig.java b/src/main/java/com/pobnellion/floorGame/game/GameConfig.java index 2437218..c2bf6f5 100644 --- a/src/main/java/com/pobnellion/floorGame/game/GameConfig.java +++ b/src/main/java/com/pobnellion/floorGame/game/GameConfig.java @@ -21,26 +21,26 @@ public class GameConfig { private Location floorCenter; private final Location[] playerJoinArea = new Location[2]; private final Location[] spectatorJoinArea= new Location[2]; + private World world; - public static GameConfig LoadFromFile() { - var config = new GameConfig(); - config.setTileSize(FloorGame.config.getInt("tileSize")); - config.setGridSize(FloorGame.config.getInt("gridSize")); - config.setFailLimit(FloorGame.config.getInt("failLimit")); - config.setPlayersHaveKnockbackStick(FloorGame.config.getBoolean("playersHaveKnockbackStick")); - config.setPlayersHaveFishingRod(FloorGame.config.getBoolean("playersHaveFishingRod")); - config.setSpectatorsCanMessWithPlayers(FloorGame.config.getBoolean("spectatorsCanMessWithPlayers")); + public GameConfig() { + this.tileSize = FloorGame.config.getInt("tileSize"); + this.gridSize = FloorGame.config.getInt("gridSize"); + this.failLimit = FloorGame.config.getInt("failLimit"); + this.playersHaveKnockbackStick = FloorGame.config.getBoolean("playersHaveKnockbackStick"); + this.playersHaveFishingRod = FloorGame.config.getBoolean("playersHaveFishingRod"); + this.spectatorsCanMessWithPlayers = FloorGame.config.getBoolean("spectatorsCanMessWithPlayers"); - var world = Bukkit.getWorld(FloorGame.config.getString("world")); + world = Bukkit.getWorld(FloorGame.config.getString("world")); var playerJoinArea = FloorGame.config.getStringList("playerJoinArea"); var spectatorJoinArea = FloorGame.config.getStringList("spectatorJoinArea"); - config.setPostGameTpLocation(ParseLocation(world, FloorGame.config.getString("postGameTpLocation"))); - config.setFloorCenter(ParseLocation(world, FloorGame.config.getString("floorCenter"))); - config.getPlayerJoinArea()[0] = ParseLocation(world, playerJoinArea.get(0)); - config.getPlayerJoinArea()[1] = ParseLocation(world, playerJoinArea.get(1)); - config.getSpectatorJoinArea()[0] = ParseLocation(world, spectatorJoinArea.get(0)); - config.getSpectatorJoinArea()[1] = ParseLocation(world, spectatorJoinArea.get(1)); + this.postGameTpLocation = ParseLocation(world, FloorGame.config.getString("postGameTpLocation")); + this.floorCenter = ParseLocation(world, FloorGame.config.getString("floorCenter")); + this.playerJoinArea[0] = ParseLocation(world, playerJoinArea.get(0)); + this.playerJoinArea[1] = ParseLocation(world, playerJoinArea.get(1)); + this.spectatorJoinArea[0] = ParseLocation(world, spectatorJoinArea.get(0)); + this.spectatorJoinArea[1] = ParseLocation(world, spectatorJoinArea.get(1)); var coloursSection = FloorGame.config.getConfigurationSection("availableColours"); @@ -51,7 +51,6 @@ public class GameConfig { var material = Material.getMaterial(coloursSection.getString(colour)); availableColours.put(colour, material); } - return config; } private static Location ParseLocation(World world, String locationString) { @@ -141,11 +140,19 @@ public class GameConfig { } public void setFloorCenter(Location floorCenter) { + if (floorCenter.getWorld() == null) + floorCenter.setWorld(world); this.floorCenter = floorCenter; FloorGame.config.set("floorCenter", FormatBlockLocation(floorCenter)); FloorGame.SaveConfig(); } + public void setWorld(World world) { + this.world = world; + FloorGame.config.set("world", world.getName()); + FloorGame.SaveConfig(); + } + public Location[] getPlayerJoinArea() { return playerJoinArea; } diff --git a/src/main/java/com/pobnellion/floorGame/game/GameInstance.java b/src/main/java/com/pobnellion/floorGame/game/GameInstance.java index 5396549..d2085ae 100644 --- a/src/main/java/com/pobnellion/floorGame/game/GameInstance.java +++ b/src/main/java/com/pobnellion/floorGame/game/GameInstance.java @@ -1,15 +1,20 @@ package com.pobnellion.floorGame.game; import com.pobnellion.floorGame.FloorGame; +import com.pobnellion.floorGame.Util; +import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import java.util.ArrayList; import java.util.Random; +import java.util.logging.Level; public class GameInstance { private final Floor floor; + private final World world; private final ArrayList players = new ArrayList<>(); private final ArrayList spectators = new ArrayList<>(); private final GameConfig config; @@ -17,23 +22,23 @@ public class GameInstance { public GameInstance(GameConfig config) { floor = new Floor(config.getFloorCenter(), config.getGridSize(), config.getTileSize(), config.getAvailableColours()); - floor.Reset(); this.config = config; - var world = config.getFloorCenter().getWorld(); + world = config.getFloorCenter().getWorld(); if (world == null) throw new NullPointerException("floorCenter world is not set"); - - world.getPlayers().forEach(player -> { - if (IsInBounds(player.getLocation(), config.getPlayerJoinArea()[0], config.getPlayerJoinArea()[1])) - players.add(player); - else if (IsInBounds(player.getLocation(), config.getSpectatorJoinArea()[0], config.getSpectatorJoinArea()[1])) - spectators.add(player); - }); } public void Start() { + Bukkit.getLogger().log(Level.INFO, "Starting floor game"); + world.getPlayers().forEach(player -> { + if (Util.IsInBounds(player.getLocation(), config.getPlayerJoinArea()[0], config.getPlayerJoinArea()[1])) + players.add(player); + else if (Util.IsInBounds(player.getLocation(), config.getSpectatorJoinArea()[0], config.getSpectatorJoinArea()[1])) + spectators.add(player); + }); + floor.Init(); var playerTpLocation = config.getFloorCenter().clone().add(0, 2, 0); @@ -75,14 +80,6 @@ public class GameInstance { return 1 + 0.5 * floor.tileSize - 0.08 * floor.tileSize * round * 0.1; // TODO: flatter line after round 40 - } - - private boolean IsInBounds(Location location, Location bounds1, Location bounds2) { - return location.getX() > Math.min(bounds1.getX(), bounds2.getX()) - && location.getX() < Math.max(bounds1.getX(), bounds2.getX()) - && location.getY() > Math.min(bounds1.getY(), bounds2.getY()) - && location.getY() < Math.max(bounds1.getY(), bounds2.getY()) - && location.getZ() > Math.min(bounds1.getZ(), bounds2.getZ()) - && location.getZ() < Math.max(bounds1.getZ(), bounds2.getZ()); + return 0; } }