Fixin issues

This commit is contained in:
ruby
2024-08-10 02:23:47 +12:00
parent 1c1c51d5ca
commit 8d7b9fc5b9
7 changed files with 97 additions and 67 deletions

View File

@ -34,7 +34,7 @@ public final class FloorGame extends JavaPlugin {
public static boolean StartGame() { public static boolean StartGame() {
if (gameInstance == null) { if (gameInstance == null) {
gameInstance = new GameInstance(GameConfig.LoadFromFile()); gameInstance = new GameInstance(new GameConfig());
gameInstance.Start(); gameInstance.Start();
return true; return true;
} }

View File

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

View File

@ -2,6 +2,7 @@ package com.pobnellion.floorGame.command;
import com.pobnellion.floorGame.FloorGame; import com.pobnellion.floorGame.FloorGame;
import com.pobnellion.floorGame.game.GameConfig; import com.pobnellion.floorGame.game.GameConfig;
import com.pobnellion.floorGame.Util;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -42,15 +43,15 @@ public class CommandFloorGame implements CommandExecutor {
} }
private void Config(CommandSender sender, String[] args) { private void Config(CommandSender sender, String[] args) {
var config = GameConfig.LoadFromFile(); var config = new GameConfig();
if (args.length == 1) { if (args.length == 1) {
sender.sendMessage(ChatColor.AQUA + "Floor game config:"); sender.sendMessage(ChatColor.AQUA + "Floor game config:");
sender.sendMessage(ChatColor.YELLOW + "Player join area: " + ChatColor.WHITE + PrintArea(config.getPlayerJoinArea())); sender.sendMessage(ChatColor.YELLOW + "Player join area: " + ChatColor.WHITE + Util.PrintArea(config.getPlayerJoinArea()));
sender.sendMessage(ChatColor.YELLOW + "Spectator join area: " + ChatColor.WHITE + PrintArea(config.getSpectatorJoinArea())); 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 + "World: " + ChatColor.WHITE + config.getFloorCenter().getWorld());
sender.sendMessage(ChatColor.YELLOW + "Post game TP location: " + ChatColor.WHITE + PrintBlockLocation(config.getPostGameTpLocation())); sender.sendMessage(ChatColor.YELLOW + "Post game TP location: " + ChatColor.WHITE + Util.PrintBlockLocation(config.getPostGameTpLocation()));
sender.sendMessage(ChatColor.YELLOW + "Floor center: " + ChatColor.WHITE + PrintBlockLocation(config.getFloorCenter())); 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 + "Tile size: " + ChatColor.WHITE + config.getTileSize());
sender.sendMessage(ChatColor.YELLOW + "Grid size: " + ChatColor.WHITE + config.getGridSize()); sender.sendMessage(ChatColor.YELLOW + "Grid size: " + ChatColor.WHITE + config.getGridSize());
sender.sendMessage(ChatColor.YELLOW + "Fail limit: " + ChatColor.WHITE + config.getFailLimit()); sender.sendMessage(ChatColor.YELLOW + "Fail limit: " + ChatColor.WHITE + config.getFailLimit());
@ -62,10 +63,10 @@ public class CommandFloorGame implements CommandExecutor {
if (args.length == 2) { if (args.length == 2) {
switch (args[1]) { switch (args[1]) {
case "playerJoinArea" -> sender.sendMessage(PrintArea(config.getPlayerJoinArea())); case "playerJoinArea" -> sender.sendMessage(Util.PrintArea(config.getPlayerJoinArea()));
case "spectatorJoinArea" -> sender.sendMessage(PrintArea(config.getSpectatorJoinArea())); case "spectatorJoinArea" -> sender.sendMessage(Util.PrintArea(config.getSpectatorJoinArea()));
case "postGameTpLocation" -> sender.sendMessage(PrintBlockLocation(config.getPostGameTpLocation())); case "postGameTpLocation" -> sender.sendMessage(Util.PrintBlockLocation(config.getPostGameTpLocation()));
case "floorCenter" -> sender.sendMessage(PrintBlockLocation(config.getFloorCenter())); case "floorCenter" -> sender.sendMessage(Util.PrintBlockLocation(config.getFloorCenter()));
case "tileSize" -> sender.sendMessage(Integer.toString(config.getTileSize())); case "tileSize" -> sender.sendMessage(Integer.toString(config.getTileSize()));
case "gridSize" -> sender.sendMessage(Integer.toString(config.getGridSize())); case "gridSize" -> sender.sendMessage(Integer.toString(config.getGridSize()));
case "failLimit" -> sender.sendMessage(Integer.toString(config.getFailLimit())); case "failLimit" -> sender.sendMessage(Integer.toString(config.getFailLimit()));
@ -111,11 +112,11 @@ public class CommandFloorGame implements CommandExecutor {
switch (args[1]) { switch (args[1]) {
case "playerJoinArea" -> { case "playerJoinArea" -> {
config.setPlayerJoinArea(l1, l2); 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" -> { case "spectatorJoinArea" -> {
config.setSpectatorJoinArea(l1, l2); 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]) { switch (args[1]) {
case "postGameTpLocation" -> { case "postGameTpLocation" -> {
config.setPostGameTpLocation(new Location(world, x, y, z)); 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" -> { case "floorCenter" -> {
config.setFloorCenter(new Location(world, x, y, z)); 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]) + "]";
}
} }

View File

@ -57,10 +57,11 @@ public class TabCompleterFloorGame implements TabCompleter {
return List.of(); return List.of();
var targetBlock = ((Player) sender).getTargetBlockExact(10); 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))); return List.of(String.join(" ", targetXYZ.subList(args.length - locationArgIndex, 3)));
} }
} }

View File

@ -1,11 +1,14 @@
package com.pobnellion.floorGame.game; package com.pobnellion.floorGame.game;
import com.pobnellion.floorGame.Util;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import java.util.HashSet; import java.util.HashSet;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
public class Floor { public class Floor {
private final Location xzCorner; private final Location xzCorner;
@ -52,11 +55,14 @@ public class Floor {
} }
public void Clear() { public void Clear() {
for (int row = 0; row < gridSize * tileSize; row++) { FillArea(xzCorner.getWorld(),
for (int col = 0; col < gridSize * tileSize; col++) { xzCorner.getBlockX(),
xzCorner.clone().add(row, 0, col).getBlock().setType(Material.AIR); 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); FillWalls(xzCorner.getBlockY(), xzCorner.getBlockY() + 15, Material.AIR);
} }
@ -108,19 +114,19 @@ public class Floor {
material); material);
FillArea(xzCorner.getWorld(), FillArea(xzCorner.getWorld(),
xzCorner.getBlockX() + floorSize, xzCorner.getBlockX() + -1,
yMin, yMin,
xzCorner.getBlockZ() + floorSize, xzCorner.getBlockZ() + floorSize,
xzCorner.getBlockX() - 1, xzCorner.getBlockX() + floorSize,
yMax, yMax,
xzCorner.getBlockX() + floorSize, xzCorner.getBlockX() + floorSize,
material); material);
} }
private void FillArea(World world, int xMin, int yMin, int zMin, int xMax, int yMax, int zMax, Material 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 x = xMin; x <= xMax; x++) {
for (int y = yMin; y < yMax; y++) { for (int y = yMin; y <= yMax; y++) {
for (int z = zMin; z < zMax; z++) { for (int z = zMin; z <= zMax; z++) {
world.getBlockAt(x, y, z).setType(material); world.getBlockAt(x, y, z).setType(material);
} }
} }

View File

@ -21,26 +21,26 @@ public class GameConfig {
private Location floorCenter; private Location floorCenter;
private final Location[] playerJoinArea = new Location[2]; private final Location[] playerJoinArea = new Location[2];
private final Location[] spectatorJoinArea= new Location[2]; private final Location[] spectatorJoinArea= new Location[2];
private World world;
public static GameConfig LoadFromFile() { public GameConfig() {
var config = new GameConfig(); this.tileSize = FloorGame.config.getInt("tileSize");
config.setTileSize(FloorGame.config.getInt("tileSize")); this.gridSize = FloorGame.config.getInt("gridSize");
config.setGridSize(FloorGame.config.getInt("gridSize")); this.failLimit = FloorGame.config.getInt("failLimit");
config.setFailLimit(FloorGame.config.getInt("failLimit")); this.playersHaveKnockbackStick = FloorGame.config.getBoolean("playersHaveKnockbackStick");
config.setPlayersHaveKnockbackStick(FloorGame.config.getBoolean("playersHaveKnockbackStick")); this.playersHaveFishingRod = FloorGame.config.getBoolean("playersHaveFishingRod");
config.setPlayersHaveFishingRod(FloorGame.config.getBoolean("playersHaveFishingRod")); this.spectatorsCanMessWithPlayers = FloorGame.config.getBoolean("spectatorsCanMessWithPlayers");
config.setSpectatorsCanMessWithPlayers(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 playerJoinArea = FloorGame.config.getStringList("playerJoinArea");
var spectatorJoinArea = FloorGame.config.getStringList("spectatorJoinArea"); var spectatorJoinArea = FloorGame.config.getStringList("spectatorJoinArea");
config.setPostGameTpLocation(ParseLocation(world, FloorGame.config.getString("postGameTpLocation"))); this.postGameTpLocation = ParseLocation(world, FloorGame.config.getString("postGameTpLocation"));
config.setFloorCenter(ParseLocation(world, FloorGame.config.getString("floorCenter"))); this.floorCenter = ParseLocation(world, FloorGame.config.getString("floorCenter"));
config.getPlayerJoinArea()[0] = ParseLocation(world, playerJoinArea.get(0)); this.playerJoinArea[0] = ParseLocation(world, playerJoinArea.get(0));
config.getPlayerJoinArea()[1] = ParseLocation(world, playerJoinArea.get(1)); this.playerJoinArea[1] = ParseLocation(world, playerJoinArea.get(1));
config.getSpectatorJoinArea()[0] = ParseLocation(world, spectatorJoinArea.get(0)); this.spectatorJoinArea[0] = ParseLocation(world, spectatorJoinArea.get(0));
config.getSpectatorJoinArea()[1] = ParseLocation(world, spectatorJoinArea.get(1)); this.spectatorJoinArea[1] = ParseLocation(world, spectatorJoinArea.get(1));
var coloursSection = FloorGame.config.getConfigurationSection("availableColours"); var coloursSection = FloorGame.config.getConfigurationSection("availableColours");
@ -51,7 +51,6 @@ public class GameConfig {
var material = Material.getMaterial(coloursSection.getString(colour)); var material = Material.getMaterial(coloursSection.getString(colour));
availableColours.put(colour, material); availableColours.put(colour, material);
} }
return config;
} }
private static Location ParseLocation(World world, String locationString) { private static Location ParseLocation(World world, String locationString) {
@ -141,11 +140,19 @@ public class GameConfig {
} }
public void setFloorCenter(Location floorCenter) { public void setFloorCenter(Location floorCenter) {
if (floorCenter.getWorld() == null)
floorCenter.setWorld(world);
this.floorCenter = floorCenter; this.floorCenter = floorCenter;
FloorGame.config.set("floorCenter", FormatBlockLocation(floorCenter)); FloorGame.config.set("floorCenter", FormatBlockLocation(floorCenter));
FloorGame.SaveConfig(); FloorGame.SaveConfig();
} }
public void setWorld(World world) {
this.world = world;
FloorGame.config.set("world", world.getName());
FloorGame.SaveConfig();
}
public Location[] getPlayerJoinArea() { public Location[] getPlayerJoinArea() {
return playerJoinArea; return playerJoinArea;
} }

View File

@ -1,15 +1,20 @@
package com.pobnellion.floorGame.game; package com.pobnellion.floorGame.game;
import com.pobnellion.floorGame.FloorGame; import com.pobnellion.floorGame.FloorGame;
import com.pobnellion.floorGame.Util;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import java.util.logging.Level;
public class GameInstance { public class GameInstance {
private final Floor floor; private final Floor floor;
private final World world;
private final ArrayList<Player> players = new ArrayList<>(); private final ArrayList<Player> players = new ArrayList<>();
private final ArrayList<Player> spectators = new ArrayList<>(); private final ArrayList<Player> spectators = new ArrayList<>();
private final GameConfig config; private final GameConfig config;
@ -17,23 +22,23 @@ public class GameInstance {
public GameInstance(GameConfig config) { public GameInstance(GameConfig config) {
floor = new Floor(config.getFloorCenter(), config.getGridSize(), config.getTileSize(), config.getAvailableColours()); floor = new Floor(config.getFloorCenter(), config.getGridSize(), config.getTileSize(), config.getAvailableColours());
floor.Reset();
this.config = config; this.config = config;
var world = config.getFloorCenter().getWorld(); world = config.getFloorCenter().getWorld();
if (world == null) if (world == null)
throw new NullPointerException("floorCenter world is not set"); 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() { 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(); floor.Init();
var playerTpLocation = config.getFloorCenter().clone().add(0, 2, 0); 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; return 1 + 0.5 * floor.tileSize - 0.08 * floor.tileSize * round * 0.1;
// TODO: flatter line after round 40 // TODO: flatter line after round 40
} return 0;
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());
} }
} }