Floor and commands

This commit is contained in:
rory
2024-08-04 02:52:31 +12:00
commit 03744182cb
15 changed files with 1148 additions and 0 deletions

View File

@ -0,0 +1,59 @@
package com.pobnellion.floorGame;
import com.pobnellion.floorGame.command.CommandFloorGame;
import com.pobnellion.floorGame.game.GameConfig;
import com.pobnellion.floorGame.game.GameInstance;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import javax.annotation.Nullable;
public final class FloorGame extends JavaPlugin {
@Nullable
private static GameInstance gameInstance;
public static FileConfiguration config;
private static FloorGame instance;
@Override
public void onEnable() {
saveDefaultConfig();
instance = this;
config = this.getConfig();
// Register commands
this.getCommand("floorgame").setExecutor(new CommandFloorGame());
}
@Override
public void onDisable() {
// Plugin shutdown logic
}
public static boolean StartGame() {
if (gameInstance == null) {
gameInstance = new GameInstance(GameConfig.LoadFromFile());
return true;
}
return false;
}
public static boolean StopGame() {
if (gameInstance != null) {
gameInstance.Stop();
gameInstance = null;
return true;
}
return false;
}
public static void SaveConfig() {
instance.saveConfig();
}
public static FloorGame GetInstance() {
return instance;
}
}

View File

@ -0,0 +1,214 @@
package com.pobnellion.floorGame.command;
import com.pobnellion.floorGame.FloorGame;
import com.pobnellion.floorGame.game.GameConfig;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
public class CommandFloorGame implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0)
return false;
switch (args[0]) {
case "start" -> Start(sender);
case "stop" -> Stop(sender);
case "config" -> Config(sender, args);
default -> sender.sendMessage(ChatColor.RED + "Usage: /floorgame < start | stop |config >");
}
// TODO: tab completer
return true;
}
private void Start(CommandSender sender) {
if (!FloorGame.StartGame())
sender.sendMessage(ChatColor.RED + "There is already a game running");
}
private void Stop(CommandSender sender) {
if (!FloorGame.StopGame())
sender.sendMessage(ChatColor.RED + "No game is currently running");
}
private void Config(CommandSender sender, String[] args) {
var config = GameConfig.LoadFromFile();
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 + "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 + "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());
sender.sendMessage(ChatColor.YELLOW + "Players have knockback stick: " + ChatColor.WHITE + config.playersHaveKnockbackStick());
sender.sendMessage(ChatColor.YELLOW + "Players have fishing rod: " + ChatColor.WHITE + config.playersHaveFishingRod());
sender.sendMessage(ChatColor.YELLOW + "Spectators can mess with players: " + ChatColor.WHITE + config.spectatorsCanMessWithPlayers());
return;
}
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 "tileSize" -> sender.sendMessage(Integer.toString(config.getTileSize()));
case "gridSize" -> sender.sendMessage(Integer.toString(config.getGridSize()));
case "failLimit" -> sender.sendMessage(Integer.toString(config.getFailLimit()));
case "playersHaveKnockbackStick" -> sender.sendMessage(Boolean.toString(config.playersHaveKnockbackStick()));
case "playersHaveFishingRod" -> sender.sendMessage(Boolean.toString(config.playersHaveFishingRod()));
case "spectatorsCanMessWithPlayers" -> sender.sendMessage(Boolean.toString(config.spectatorsCanMessWithPlayers()));
}
return;
}
switch (args[1]) {
case "playerJoinArea", "spectatorJoinArea" -> SetArea(sender, args, config);
case "postGameTpLocation", "floorCenter" -> SetLocation(sender, args, config);
case "tileSize", "gridSize", "failLimit" -> SetInt(sender, args, config);
case "playersHaveKnockbackStick", "playersHaveFishingRod", "spectatorsCanMessWithPlayers" -> SetBoolean(sender, args, config);
}
}
private void SetArea(CommandSender sender, String[] args, GameConfig config) {
if (args.length != 8) {
sender.sendMessage("Invalid number of arguments");
return;
}
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Command must be run by a player");
return;
}
var world = ((Player) sender).getWorld();
try {
var x1 = Integer.parseInt(args[2]);
var y1 = Integer.parseInt(args[3]);
var z1 = Integer.parseInt(args[4]);
var x2 = Integer.parseInt(args[5]);
var y2 = Integer.parseInt(args[6]);
var z2 = Integer.parseInt(args[7]);
var l1 = new Location(world, x1, y1, z1);
var l2 = new Location(world, x2, y2, z2);
switch (args[1]) {
case "playerJoinArea" -> {
config.setPlayerJoinArea(l1, l2);
sender.sendMessage(ChatColor.YELLOW + "Player join area set to " + PrintArea(config.getPlayerJoinArea()));
}
case "spectatorJoinArea" -> {
config.setSpectatorJoinArea(l1, l2);
sender.sendMessage(ChatColor.YELLOW + "Spectator join area set to " + PrintArea(config.getSpectatorJoinArea()));
}
}
}
catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Could not parse int value: " + e.getMessage());
}
}
private void SetLocation(CommandSender sender, String[] args, GameConfig config) {
if (args.length != 5) {
sender.sendMessage("Invalid number of arguments");
return;
}
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Command must be run by a player");
return;
}
var world = ((Player) sender).getWorld();
try {
var x = Integer.parseInt(args[2]);
var y = Integer.parseInt(args[3]);
var z = Integer.parseInt(args[4]);
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()));
}
case "floorCenter" -> {
config.setFloorCenter(new Location(world, x, y, z));
sender.sendMessage(ChatColor.YELLOW + "Floor center set to " + PrintBlockLocation(config.getFloorCenter()));
}
}
}
catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Could not parse int value: " + e.getMessage());
}
}
private void SetInt(CommandSender sender, String[] args, GameConfig config) {
try {
var value = Integer.parseInt(args[2]);
switch (args[1]) {
case "tileSize" -> {
config.setTileSize(value);
sender.sendMessage(ChatColor.YELLOW + "Tile size set to " + config.getTileSize());
}
case "gridSize" -> {
config.setGridSize(value);
sender.sendMessage(ChatColor.YELLOW + "Grid size set to " + config.getGridSize());
}
case "failLimit" -> {
config.setFailLimit(value);
sender.sendMessage(ChatColor.YELLOW + "Fail limit set to " + config.getFailLimit());
}
}
}
catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Could not parse int value " + args[2]);
}
}
private void SetBoolean(CommandSender sender, String[] args, GameConfig config) {
if (!args[2].equalsIgnoreCase("true") && !args[2].equalsIgnoreCase("false")) {
sender.sendMessage(ChatColor.RED + "Could not parse bool value " + args[2]);
return;
}
var value = Boolean.parseBoolean(args[2]);
switch (args[1]) {
case "playersHaveKnockbackStick" -> {
config.setPlayersHaveKnockbackStick(value);
sender.sendMessage(ChatColor.YELLOW + "Players have knockback stick set to " + config.playersHaveKnockbackStick());
}
case "playersHaveFishingRod" -> {
config.setPlayersHaveFishingRod(value);
sender.sendMessage(ChatColor.YELLOW + "Players have fishing rod set to " + config.playersHaveFishingRod());
}
case "spectatorsCanMessWithPlayers" -> {
config.setSpectatorsCanMessWithPlayers(value);
sender.sendMessage(ChatColor.YELLOW + "Spectators can mess with players set to " + config.spectatorsCanMessWithPlayers());
}
}
}
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

@ -0,0 +1,69 @@
package com.pobnellion.floorGame.game;
import org.bukkit.Location;
import org.bukkit.Material;
import java.util.HashSet;
import java.util.Random;
public class Floor {
private final Location xzCorner;
public int tileSize;
public int gridSize;
public Material[] colours;
private final Material[][] colourGrid;
public Floor(Location center, int tileSize, int gridSize, Material[] availableColours) {
this.xzCorner = center.subtract(tileSize * gridSize * 0.5, 0, tileSize * gridSize * 0.5);
this.tileSize = tileSize;
this.gridSize = gridSize;
// Randomize floor colours
colourGrid = new Material[gridSize][gridSize];
var random = new Random();
var colourSet = new HashSet<Material>();
for (int row = 0; row < gridSize; row++) {
for (int col = 0; col < gridSize; col++) {
var colour = availableColours[random.nextInt(availableColours.length)];
colourGrid[row][col] = colour;
colourSet.add(colour);
}
}
colours = colourSet.toArray(new Material[0]);
}
public void Reset() {
for (int row = 0; row < gridSize; row++) {
for (int col = 0; col < gridSize; col++) {
FillTile(xzCorner.clone().add(row * tileSize, 0, col * tileSize), colourGrid[row][col]);
}
}
}
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);
}
}
}
public void SoloTile(Material material) {
for (int row = 0; row < gridSize; row++) {
for (int col = 0; col < gridSize; col++) {
if (colourGrid[row][col] != material)
FillTile(xzCorner.clone().add(row * tileSize, 0, col * tileSize), Material.AIR);
}
}
}
private void FillTile(Location location, Material material) {
for (int row = 0; row < tileSize; row++) {
for (int col = 0; col < tileSize; col++) {
location.clone().add(row, 0, col).getBlock().setType(material);
}
}
}
}

View File

@ -0,0 +1,184 @@
package com.pobnellion.floorGame.game;
import com.pobnellion.floorGame.FloorGame;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import java.util.*;
import java.util.logging.Level;
public class GameConfig {
private int tileSize;
private int gridSize;
private int failLimit;
private boolean playersHaveKnockbackStick;
private boolean playersHaveFishingRod;
private boolean spectatorsCanMessWithPlayers;
private static final Map<String, Material> availableColours = new HashMap<>();
private Location postGameTpLocation;
private Location floorCenter;
private final Location[] playerJoinArea = new Location[2];
private final Location[] spectatorJoinArea= new Location[2];
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"));
var 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));
var coloursSection = FloorGame.config.getConfigurationSection("availableColours");
if (coloursSection == null)
throw new NullPointerException("availableColours section not present in config");
for (String colour : coloursSection.getKeys(false)){
var material = Material.getMaterial(coloursSection.getString(colour));
availableColours.put(colour, material);
}
return config;
}
private static Location ParseLocation(World world, String locationString) {
var coords = locationString.split(", ");
if (coords.length != 3) {
Bukkit.getLogger().log(Level.SEVERE, "Could not parse location " + locationString);
return new Location(world, 0, 0, 0);
}
var x = Double.parseDouble(coords[0]);
var y = Double.parseDouble(coords[1]);
var z = Double.parseDouble(coords[2]);
return new Location(world, x, y, z);
}
public int getTileSize() {
return tileSize;
}
public void setTileSize(int tileSize) {
this.tileSize = tileSize;
FloorGame.config.set("tileSize", tileSize);
FloorGame.SaveConfig();
}
public int getGridSize() {
return gridSize;
}
public void setGridSize(int gridSize) {
this.gridSize = gridSize;
FloorGame.config.set("gridSize", gridSize);
FloorGame.SaveConfig();
}
public int getFailLimit() {
return failLimit;
}
public void setFailLimit(int failLimit) {
this.failLimit = failLimit;
FloorGame.config.set("failLimit", failLimit);
FloorGame.SaveConfig();
}
public boolean playersHaveKnockbackStick() {
return playersHaveKnockbackStick;
}
public void setPlayersHaveKnockbackStick(boolean playersHaveKnockbackStick) {
this.playersHaveKnockbackStick = playersHaveKnockbackStick;
FloorGame.config.set("playersHaveKnockbackStick", playersHaveKnockbackStick);
FloorGame.SaveConfig();
}
public boolean playersHaveFishingRod() {
return playersHaveFishingRod;
}
public void setPlayersHaveFishingRod(boolean playersHaveFishingRod) {
this.playersHaveFishingRod = playersHaveFishingRod;
FloorGame.config.set("playersHaveFishingRod", playersHaveFishingRod);
FloorGame.SaveConfig();
}
public boolean spectatorsCanMessWithPlayers() {
return spectatorsCanMessWithPlayers;
}
public void setSpectatorsCanMessWithPlayers(boolean spectatorsCanMessWithPlayers) {
this.spectatorsCanMessWithPlayers = spectatorsCanMessWithPlayers;
FloorGame.config.set("spectatorsCanMessWithPlayers", spectatorsCanMessWithPlayers);
FloorGame.SaveConfig();
}
public Map<String, Material> getColourMap() {
return availableColours;
}
public Material[] getAvailableColours() {
return availableColours.values().toArray(new Material[0]);
}
public Location getFloorCenter() {
return floorCenter;
}
public void setFloorCenter(Location floorCenter) {
this.floorCenter = floorCenter;
FloorGame.config.set("floorCenter", FormatBlockLocation(floorCenter));
FloorGame.SaveConfig();
}
public Location[] getPlayerJoinArea() {
return playerJoinArea;
}
public void setPlayerJoinArea(Location l1, Location l2) {
playerJoinArea[0] = l1;
playerJoinArea[1] = l2;
FloorGame.config.set("playerJoinArea", playerJoinArea);
FloorGame.SaveConfig();
}
public Location[] getSpectatorJoinArea() {
return spectatorJoinArea;
}
public void setSpectatorJoinArea(Location l1, Location l2) {
spectatorJoinArea[0] = l1;
spectatorJoinArea[1] = l2;
FloorGame.config.set("spectatorJoinArea", spectatorJoinArea);
FloorGame.SaveConfig();
}
public Location getPostGameTpLocation() {
return postGameTpLocation;
}
public void setPostGameTpLocation(Location postGameTpLocation) {
this.postGameTpLocation = postGameTpLocation;
FloorGame.config.set("postGameTpLocation", FormatBlockLocation(postGameTpLocation));
FloorGame.SaveConfig();
}
private String FormatBlockLocation(Location location) {
return location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ();
}
}

View File

@ -0,0 +1,68 @@
package com.pobnellion.floorGame.game;
import com.pobnellion.floorGame.FloorGame;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.Random;
public class GameInstance {
private final Floor floor;
private final ArrayList<Player> players = new ArrayList<>();
private final ArrayList<Player> spectators = new ArrayList<>();
private final GameConfig config;
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();
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);
});
new BukkitRunnable() {
public void run() {
GameLoopTask();
}
}.runTaskTimerAsynchronously(FloorGame.GetInstance(), 0, 10_000);
}
private void GameLoopTask() {
var rand = new Random();
var colour = floor.colours[rand.nextInt(floor.colours.length)];
floor.SoloTile(colour);
new BukkitRunnable() {
public void run() {
floor.Reset();
}
}.runTaskLater(FloorGame.GetInstance(), 1000);
}
public void Stop() {
floor.Clear();
players.forEach(player -> player.teleport(config.getPostGameTpLocation()));
spectators.forEach(player -> player.teleport(config.getPostGameTpLocation()));
}
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());
}
}

View File

@ -0,0 +1,28 @@
availableColours:
red: RED_CONCRETE
orange: ORANGE_CONCRETE
yellow: YELLOW_CONCRETE
green: LIME_CONCRETE
blue: LIGHT_BLUE_CONCRETE
magenta: MAGENTA_CONCRETE
pink: PINK_CONCRETE
purple: PURPLE_CONCRETE
white: WHITE_CONCRETE
playerJoinArea:
- 0, 0, 0
- 0, 0, 0
spectatorJoinArea:
- 0, 0, 0
- 0, 0, 0
postGameTpLocation: 0, 0, 0
floorCenter: 0, 0, 0
world: none
tileSize: 3
gridSize: 5
failLimit: 10
playersHaveKnockbackStick: false
playersHaveFishingRod: false
spectatorsCanMessWithPlayers: false

View File

@ -0,0 +1,10 @@
name: floorGame
version: '1.0-SNAPSHOT'
main: com.pobnellion.floorGame.FloorGame
api-version: '1.21'
commands:
floorgame:
description: "Floor game setup and play"
usage: /floorgame <start|stop|config>
permission: floorGame.admin