diff --git a/README.md b/README.md index 121e4df..eac585c 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ A simple minecraft plugin for sending commands to a server. -`The plugin was developed with spigot version 1.21.5`. +`The plugin was developed with spigot version 1.21.5` [download](https://getbukkit.org/download/spigot). ## Building @@ -12,6 +12,22 @@ This project can be build using maven, just run the mvn command in the directory mvn ``` +### Creating the Server +Follow the [instructions](https://www.spigotmc.org/wiki/spigot-installation/#windows). +Basically.: +- Create an arbitrary folder + - Create a new file (`.txt` is the simplest) + - Put these three lines below in it (make sure to replace the version in the .jar's filename if needed. + - Rename the file to `start.bat` (accept window's warning, we want the file to be executable). + - Put the downloaded server-jar in the folder, next to the bat + - Create a new folder inside your current folder, name it `plugins` + - Put the plugin-jar, you built or downloaded in it + + ```@echo off +java -Xms#G -Xmx#G -XX:+UseG1GC -jar spigot-1.21.5.jar nogui +pause +``` + ## Usage - This is a plugin intended to be used with a spigot server, just put the jar into the plugin folder. @@ -28,10 +44,17 @@ mvn - Throughout the commands there are references to indices of online players, it is important to note that currently the index is completly based on the order the players joined in and will change if the players disconnect and rejoin. Currently there is no way to identify the players unqiuely based on a id. - You can of course still differantiate the players based on their names +#### Dimensions + +- Currently the following dimensions are available + - `world`: the normal overworld + - `world_nether`: The Nether + - ` world_the_end`: The End + ### Player - `getPlayer ` gets information about a player. [Details](#command-getplayer) -- `setPlayerPos ?rotation:int?` Set the player position [Details](#command-setplayerpos) +- `setPlayerPos ?rotation:int?` Set the player position [Details](#command-setplayerpos) - `setPlayerStat ` Set a player stat [Details](#command-setplayerstat) - `showTitle ` Show a Title to players [Details](#command-showtitle) @@ -42,8 +65,8 @@ mvn ## Block -- `setBlock ` Set a block at a location [Deailts](#command-setblock) -- `getBlock ` gets information about a block [Details](#command-getblock) +- `setBlock ` Set a block at a location [Deailts](#command-setblock) +- `getBlock ` gets information about a block [Details](#command-getblock) ## Chat @@ -53,8 +76,8 @@ mvn ### Entity -- `spawnEntity ` [Details](#command-spawnentity) -- `editEntity ?name:String? ?position:x;y;z? ?ai:boolean?` [Details](#command-editentity) +- `spawnEntity ` [Details](#command-spawnentity) +- `editEntity ?name:String? ?position:x;y;z;dimension? ?ai:boolean?` [Details](#command-editentity) - `getEntity ` [Details](#command-getentity) ### Boss Bar @@ -108,10 +131,10 @@ Flags are arguments that toggle an effect, the send command just needs to contai #### Returns -- ` ` +- ` ` - `` the index of the player - `` the name of the player - - `, , ` The tile position of the player + - `, , ` `` The tile position of the player (see [dimensions](#dimensions)) - `` The minecraft rotation of the player. - `` The block type the player is looking at - `` depending of wether or not the player is sneaking `true` or `false` @@ -131,7 +154,7 @@ Flags are arguments that toggle an effect, the send command just needs to contai ```bash getPlayer 0 -0 ThisRyan 39 83 72 17 STONE false +0 Player1 39 83 72 17 STONE false ``` ### Command: setPlayerStat @@ -233,21 +256,22 @@ getInv 0 ### Command: setPlayerPos -- `setPlayerPos ?rotation:int?` +- `setPlayerPos ?rotation:int?` #### Args - `playerIndex` the index of the online player -- `, , ` the new position of the player +- `, , ` the new position of the player (see [dimensions](#dimensions)) - `?rotation?` optionaly the direction the player should be looking in ### Command: setBlock -- `setBlock ` Set a block at a location +- `setBlock ` Set a block at a location #### Args - ` ` position of the block +- `` see [dimensions](#dimensions) - `` the block id as a string i.e. "stone" #### Example @@ -259,11 +283,12 @@ setBlock 10 10 10 stone ### Command: getBlock -- `getBlock ` gets information about a block +- `getBlock ` gets information about a block #### Args - ` :int int int` position of the block +- `` see [dimensions](#dimensions) #### Example @@ -308,22 +333,23 @@ chatCommand say Hello from plugin! #### Return -- A list of block of `:` seperated by `𝇉` +- A list of blocks of `:|||||| ::` seperated by `𝇉` #### Example ```bash pollChat -Player1:Hallo𝇉Player1:A second message𝇉Player2:Last message +Player1:2025|12|21|17|49|27:0:This is a message:2025|12|21|17|49|29:1:Message 2 ``` ### Command: spawnEntity -- `spawnEntity ` Spawns an entity +- `spawnEntity ` Spawns an entity #### Args - ` ` position +- `` see [dimensions](#dimensions) - `` a string with entity id full list can be cound here: [Entities](https://hub.spigotmc.org/javadocs/spigot/org/bukkit/entity/EntityType.html) #### Returns @@ -339,13 +365,13 @@ ff568527-7c0c-4536-aed2-ef77429d61b8 ### Command: editEntity -- `editEntity ?name:String? ?position:x;y;z? ?ai:boolean?` Edits a existing entity which was previously spawned by the `spawnEntity` command +- `editEntity ?name:String? ?position:x;y;z;dimension? ?ai:boolean?` Edits a existing entity which was previously spawned by the `spawnEntity` command #### Args - `target` is the uuid returned by `spawnEntity` - `name` if is set the entites custom name is changed -- `position`if is set the entities position will be set to the given x,y,z coordinates +- `position`if is set the entities position will be set to the given x,y,z coordinates as well as the [dimension](#dimensions) , - `ai` sets the ai of the entity, if its turned off the entity will not move. #### Example @@ -365,7 +391,9 @@ editEntity ff568527-7c0c-4536-aed2-ef77429d61b8 name:Zomb position:10;10;200 ai: #### Returns -- ` ` +- ` ` + +- `` see [dimensions](#dimensions) #### Example @@ -426,6 +454,8 @@ deleteBossBar myuniquename ### Command: batch +!!This command is currently not supported!! + - `batch ;|;;|;` Runs all commands in a batch, the `;|;` acts as the seperator. Please make sure there are no spaces around the seperator except before the first one. #### Args diff --git a/src/main/java/org/sk/skMinecraft/CommandFactory.java b/src/main/java/org/sk/skMinecraft/CommandFactory.java index 090a8e0..266cd13 100644 --- a/src/main/java/org/sk/skMinecraft/CommandFactory.java +++ b/src/main/java/org/sk/skMinecraft/CommandFactory.java @@ -1,5 +1,6 @@ package org.sk.skMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; import org.sk.skMinecraft.commands.*; import java.util.HashMap; @@ -7,7 +8,7 @@ public class CommandFactory { - private final HashMap> commands; + private final HashMap> commands; public CommandFactory() { this.commands = new HashMap<>(); @@ -29,7 +30,7 @@ public CommandFactory() { this.commands.put("addInv", AddInventory::new); this.commands.put("getInv", GetInventory::new); - this.commands.put("batch", Batching::new); + // this.commands.put("batch", Batching::new); this.commands.put("spawnBossBar", SpawnBossBar::new); this.commands.put("editBossBar", EditBossBar::new); @@ -39,14 +40,12 @@ public CommandFactory() { this.commands.put("setPlayerPos", SetPlayerPos::new); - this.commands.put("validate", Validator::new); + // this.commands.put("validate", Validator::new); this.commands.put("showTitle", ShowTitle::new); } - public Command build(String command) { - int firstSpace = command.indexOf(SkMinecraft.seperator); - String commandName = firstSpace == -1 ? command : command.substring(0, firstSpace); - return this.commands.get(commandName).apply(command); + public Command build(StringCommand command) { + return this.commands.get(command.name()).apply(command); } } diff --git a/src/main/java/org/sk/skMinecraft/SkMinecraft.java b/src/main/java/org/sk/skMinecraft/SkMinecraft.java index 31956ac..2e03920 100644 --- a/src/main/java/org/sk/skMinecraft/SkMinecraft.java +++ b/src/main/java/org/sk/skMinecraft/SkMinecraft.java @@ -1,5 +1,7 @@ package org.sk.skMinecraft; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.AsyncPlayerChatEvent; @@ -13,6 +15,8 @@ import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; +import java.time.LocalDateTime; +import java.util.Arrays; public final class SkMinecraft extends JavaPlugin implements Listener { @@ -20,6 +24,7 @@ public final class SkMinecraft extends JavaPlugin implements Listener { private ServerSocket serverSocket; public static String seperator = "𝇉"; + public record StringCommand(String name, String[] arguments) {}; public static String joinWithSeperator(Object... args) { String result = ""; @@ -40,6 +45,23 @@ public static String joinWithSeperator(Object... args) { return result; } + public static StringCommand splitCommand(String command) { + String[] parts = command.split(SkMinecraft.seperator); + return new StringCommand(parts[0], Arrays.copyOfRange(parts, 1, parts.length)); + } + + public static int playerIndexFromName(String name) { + Player[] players = Bukkit.getOnlinePlayers().toArray(new Player[0]); + + for(int i = 0;i < players.length;i++){ + if(players[i].getName().equals(name)) { + return i; + } + } + + return -1; + } + @Override public void onEnable() { CentralResourceHandler.init(); @@ -55,8 +77,8 @@ public void onDisable() { private void startTcpListener() { tcpListenerThread = new Thread(() -> { try { - serverSocket = new ServerSocket(12345); // Use your desired port - getLogger().info("TCP server started on port 12345"); + serverSocket = new ServerSocket(25595); + getLogger().info("TCP server started on port 25595"); while (!serverSocket.isClosed()) { Socket client = serverSocket.accept(); @@ -76,7 +98,7 @@ private void startTcpListener() { @EventHandler public void onPlayerChat(AsyncPlayerChatEvent event) { - ChatMessage message = new ChatMessage(event.getPlayer(), event.getMessage()); + ChatMessage message = new ChatMessage(event.getPlayer(), event.getMessage(), LocalDateTime.now()); CentralResourceHandler.addChatMessage(message); } @@ -108,7 +130,8 @@ private void handleClient(Socket client) { getLogger().info("Received: " + command); - Command commandObject = commandFactory.build(command); + StringCommand stringCommand = splitCommand(command); + Command commandObject = commandFactory.build(stringCommand); if(commandObject == null) continue; diff --git a/src/main/java/org/sk/skMinecraft/commands/AddInventory.java b/src/main/java/org/sk/skMinecraft/commands/AddInventory.java index ae74894..21619ac 100644 --- a/src/main/java/org/sk/skMinecraft/commands/AddInventory.java +++ b/src/main/java/org/sk/skMinecraft/commands/AddInventory.java @@ -6,10 +6,9 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; -import java.util.Arrays; -import java.util.HashMap; public class AddInventory extends Command{ @@ -21,47 +20,37 @@ public class AddInventory extends Command{ private int slot; private boolean unbreakable; - public AddInventory(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public AddInventory(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 4) { - this.valid = false; - return; - } + parser.addPositionalArguments( + ArgumentParser.IntParser, + ArgumentParser.StringParser, + ArgumentParser.IntParser + ); - try { - this.playerIndex = Integer.parseInt(parts[1]); - this.amount = Integer.parseInt(parts[3]); - } catch(NumberFormatException e){ - this.valid = false; - return; - } + parser.addOptionalArgument("name", ArgumentParser.StringParser, ""); + parser.addOptionalArgument("slot", Integer::parseInt, -1); - if(playerIndex < 0 || playerIndex >= Bukkit.getOnlinePlayers().toArray(new Player[0]).length) { - this.valid = false; - return; - } + parser.addFlag("unbreakable"); - String blockId = parts[2].toUpperCase(); - this.material = Material.matchMaterial(blockId); + ParseResult result = parser.parse(command.arguments()); - if(this.material == null) { + if(!this.isValid()) { this.valid = false; return; } - ArgumentParser parser = new ArgumentParser(); - - parser.addArgument("name", ArgumentParser.StringParser, ""); - parser.addArgument("slot", Integer::parseInt, -1); + this.playerIndex = result.getPositional(0); - parser.addFlag("unbreakable"); + String blockId = result.getPositional(1); + this.material = Material.matchMaterial(blockId.toUpperCase()); - HashMap result = parser.parse(Arrays.copyOfRange(parts,4, parts.length)); + this.amount = result.getPositional(2); - this.name = result.get("name").asString(); - this.slot = result.get("slot").asInt(); - this.unbreakable = result.get("unbreakable").asBool(); + this.name = result.getOptional("name"); + this.slot = result.getOptional("slot"); + this.unbreakable = result.getOptional("unbreakable"); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/ArgumentParser.java b/src/main/java/org/sk/skMinecraft/commands/ArgumentParser.java index 7ec4ed9..449e125 100644 --- a/src/main/java/org/sk/skMinecraft/commands/ArgumentParser.java +++ b/src/main/java/org/sk/skMinecraft/commands/ArgumentParser.java @@ -1,5 +1,7 @@ package org.sk.skMinecraft.commands; +import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.function.Function; @@ -8,7 +10,8 @@ public class ArgumentParser { public static Function StringParser = (arg) -> arg; - + public static Function IntParser = Integer::parseInt; + public static Function DoubleParser = Double::parseDouble; public static class ArgumentResult { Object value; @@ -17,6 +20,11 @@ public ArgumentResult(Object value) { this.value = value; } + @SuppressWarnings("unchecked") + public T get() { + return (T)this.value; + } + public int asInt() { return (int)value; } @@ -37,67 +45,121 @@ public float asFloat() { return (Float)value; } - public Object get() {return this.value;} + // public Object get() {return this.value;} } - private record argument(Function parser, Object defaultValue) {} - private final HashMap arguments; - private final HashSet flags; + private record Argument(Function parser, Object defaultValue) {} + public record ParseResult(ArrayList positional, HashMap optionals, boolean valid) { + public T getPositional(int index) { + return positional.get(index).get(); + } + + public T getOptional(String name) { + return optionals.get(name).get(); + } + + public boolean isValid() { + return valid; + } + + public boolean getFlag(String name) { + return optionals.get(name).asBool(); + } + + public boolean isSet(String name) { + return !optionals.get(name).isNull(); + } + } + + private final ArrayList> positionalArguments; + private final HashMap optionalArguments; + private final HashSet flags; + public ArgumentParser(){ - this.arguments = new HashMap<>(); + this.positionalArguments = new ArrayList<>(); + this.optionalArguments = new HashMap<>(); this.flags = new HashSet<>(); } - public void addArgument(String name, Function parser, Object defaultValue) { - this.arguments.put(name, new argument(parser, defaultValue)); + @SafeVarargs + public final void addPositionalArguments(Function ...parsers) { + for(Function parser : parsers) { + this.positionalArguments.add(parser); + } + } + + public void addOptionalArgument(String name, Function parser, Object defaultValue) { + this.optionalArguments.put(name, new Argument(parser, defaultValue)); } - public void addArgument(String name, Function parser) { - this.addArgument(name, parser, null); + public void addOptionalArgument(String name, Function parser) { + this.addOptionalArgument(name, parser, null); } public void addFlag(String name){ this.flags.add(name); } - public HashMap parse(String[] parts) { - HashMap result = new HashMap<>(); - - for (String part : parts) { + public ParseResult parse(String[] parts) { + if(parts.length < positionalArguments.size()) { + return new ParseResult(null, null, false); + } + + ArrayList positionals = new ArrayList<>(); + for(int i = 0;i < positionalArguments.size();i++) { + try { + String part = parts[i]; + Function parser = positionalArguments.get(i); + Object parsedResult = parser.apply(part); + + positionals.add(new ArgumentResult(parsedResult)); + } catch(Exception e) { + return new ParseResult(null, null, false); + } + } + + String[] optionalParts = Arrays + .stream(parts, positionalArguments.size(), parts.length) + .toArray(String[]::new); + + HashMap optionals = new HashMap<>(); + for (String part : optionalParts) { System.out.println(part); String[] arg = part.split(":"); String arg_name = arg[0]; if(this.flags.contains(arg_name)) { - result.put(arg_name, new ArgumentResult(true)); + optionals.put(arg_name, new ArgumentResult(true)); continue; } - if (arg.length != 2 || !arguments.containsKey(arg_name)) { + if (arg.length != 2 || !optionalArguments.containsKey(arg_name)) { continue; } - Object value = arguments.get(arg_name).defaultValue; + Object value = optionalArguments.get(arg_name).defaultValue; try { - value = arguments.get(arg_name).parser.apply(arg[1]); - } catch (Exception ignored) {} + value = optionalArguments.get(arg_name).parser.apply(arg[1]); + } catch (Exception ignored) { + return new ParseResult(null, null, false); + } - result.put(arg_name, new ArgumentResult(value)); + optionals.put(arg_name, new ArgumentResult(value)); } - for(String key: this.arguments.keySet()) { - if(result.containsKey(key)) continue; + for(String key: this.optionalArguments.keySet()) { + if(optionals.containsKey(key)) continue; - result.put(key, new ArgumentResult(this.arguments.get(key).defaultValue)); + optionals.put(key, new ArgumentResult(this.optionalArguments.get(key).defaultValue)); } for(String key : this.flags) { - if(result.containsKey(key)) continue; + if(optionals.containsKey(key)) continue; - result.put(key, new ArgumentResult(false)); + optionals.put(key, new ArgumentResult(false)); } - return result; + return new ParseResult(positionals, optionals, true); } } diff --git a/src/main/java/org/sk/skMinecraft/commands/Batching.java b/src/main/java/org/sk/skMinecraft/commands/Batching.java index ec65cc5..26b3229 100644 --- a/src/main/java/org/sk/skMinecraft/commands/Batching.java +++ b/src/main/java/org/sk/skMinecraft/commands/Batching.java @@ -1,6 +1,8 @@ package org.sk.skMinecraft.commands; import org.sk.skMinecraft.CommandFactory; +import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; import java.util.Arrays; import java.util.regex.Pattern; @@ -9,8 +11,8 @@ public class Batching extends Command { private final String[] commands; - public Batching(String command) { - String[] parts = command.split(Pattern.quote(";|;")); + public Batching(StringCommand command) { + String[] parts = command.arguments()[0].split(Pattern.quote(";|;")); this.commands = Arrays.copyOfRange(parts, 1, parts.length); } @@ -19,8 +21,9 @@ public Batching(String command) { public void apply() { CommandFactory factory = new CommandFactory(); - for(String command_str : commands) { - Command command = factory.build(command_str); + for(String commandStr : commands) { + StringCommand commandObj = SkMinecraft.splitCommand(commandStr); + Command command = factory.build(commandObj); if(!command.isValid()) continue; diff --git a/src/main/java/org/sk/skMinecraft/commands/ChatCommand.java b/src/main/java/org/sk/skMinecraft/commands/ChatCommand.java index dd21dcd..cc0a843 100644 --- a/src/main/java/org/sk/skMinecraft/commands/ChatCommand.java +++ b/src/main/java/org/sk/skMinecraft/commands/ChatCommand.java @@ -1,13 +1,17 @@ package org.sk.skMinecraft.commands; +import java.util.Arrays; +import java.util.stream.Collectors; + import org.bukkit.Bukkit; +import org.sk.skMinecraft.SkMinecraft.StringCommand; public class ChatCommand extends Command { private String command; - public ChatCommand(String command) { - this.command = command.replace("chatCommand ", ""); + public ChatCommand(StringCommand command) { + this.command = Arrays.stream(command.arguments()).collect(Collectors.joining("")); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/DeleteBossBar.java b/src/main/java/org/sk/skMinecraft/commands/DeleteBossBar.java index 832e725..ea8733c 100644 --- a/src/main/java/org/sk/skMinecraft/commands/DeleteBossBar.java +++ b/src/main/java/org/sk/skMinecraft/commands/DeleteBossBar.java @@ -3,21 +3,28 @@ import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; import org.bukkit.boss.KeyedBossBar; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class DeleteBossBar extends Command { private String name; - public DeleteBossBar(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public DeleteBossBar(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length != 2) { + parser.addPositionalArguments( + ArgumentParser.StringParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; return; } - this.name = parts[1]; + this.name = result.getPositional(0); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/EditBossBar.java b/src/main/java/org/sk/skMinecraft/commands/EditBossBar.java index f70b82b..1ff87ad 100644 --- a/src/main/java/org/sk/skMinecraft/commands/EditBossBar.java +++ b/src/main/java/org/sk/skMinecraft/commands/EditBossBar.java @@ -5,10 +5,8 @@ import org.bukkit.boss.BarColor; import org.bukkit.boss.BarStyle; import org.bukkit.boss.KeyedBossBar; -import org.sk.skMinecraft.SkMinecraft; - -import java.util.Arrays; -import java.util.HashMap; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class EditBossBar extends Command { @@ -44,31 +42,34 @@ private EditType typeFromText(String command) { float value; BarStyle style; - public EditBossBar(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public EditBossBar(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 3) { - this.valid = false; - return; - } + parser.addPositionalArguments( + ArgumentParser.StringParser, + ArgumentParser.StringParser + ); + + parser.addOptionalArgument("text", ArgumentParser.StringParser, ""); + parser.addOptionalArgument("color", ArgumentParser.StringParser, "PURPLE"); + parser.addOptionalArgument("value", Float::parseFloat, 0.0f); + parser.addOptionalArgument("style", ArgumentParser.StringParser, "SOLID"); - type = typeFromText(parts[1]); - this.typeName = parts[1]; - this.name = parts[2]; + ParseResult result = parser.parse(command.arguments()); - ArgumentParser parser = new ArgumentParser(); + this.typeName = result.getPositional(0); + this.type = typeFromText(this.typeName); + this.name = result.getPositional(01); + + this.text = result.getOptional("text"); - parser.addArgument("text", ArgumentParser.StringParser, ""); - parser.addArgument("color", ArgumentParser.StringParser, "PURPLE"); - parser.addArgument("value", Float::parseFloat, 0.0f); - parser.addArgument("style", ArgumentParser.StringParser, "SOLID"); + String colorName = result.getOptional("color"); + this.color = BarColor.valueOf(colorName.toUpperCase()); - HashMap result = parser.parse(Arrays.copyOfRange(parts,3, parts.length)); + this.value = result.getOptional("value"); - this.text = result.get("text").asString(); - this.color = BarColor.valueOf(result.get("color").asString().toUpperCase()); - this.value = result.get("value").asFloat(); - this.style = BarStyle.valueOf(result.get("style").asString().toUpperCase()); + String styleName = result.getOptional("style"); + this.style = BarStyle.valueOf(styleName.toUpperCase()); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/EditEntity.java b/src/main/java/org/sk/skMinecraft/commands/EditEntity.java index f2752cb..08adb96 100644 --- a/src/main/java/org/sk/skMinecraft/commands/EditEntity.java +++ b/src/main/java/org/sk/skMinecraft/commands/EditEntity.java @@ -5,15 +5,11 @@ import org.bukkit.World; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; -import org.sk.skMinecraft.SkMinecraft; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.stream.Collectors; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; +import org.sk.skMinecraft.data.Position; public class EditEntity extends Command { - private String target; private boolean setAi; @@ -21,52 +17,54 @@ public class EditEntity extends Command { private boolean setPosition; private int x,y,z; + private World world; private String name; private boolean setHealth; private double health; - public EditEntity(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public EditEntity(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 2) { - this.valid = false; - return; - } + parser.addPositionalArguments( + ArgumentParser.StringParser + ); - this.target = parts[1]; + parser.addOptionalArgument("ai", Boolean::parseBoolean); + parser.addOptionalArgument("position", arg -> Position.fromString(arg, ";")); + parser.addOptionalArgument("name", ArgumentParser.StringParser); + parser.addOptionalArgument("health", Double::parseDouble); - ArgumentParser parser = new ArgumentParser(); + ParseResult result = parser.parse(command.arguments()); - parser.addArgument("ai", Boolean::parseBoolean); - parser.addArgument("position", arg-> Arrays.stream(arg.split(";")).map(Integer::parseInt).collect(Collectors.toList())); - parser.addArgument("name", ArgumentParser.StringParser); - parser.addArgument("health", Double::parseDouble); + if(!result.isValid()) { + this.valid = false; + return; + } - HashMap result = parser.parse(Arrays.copyOfRange(parts,2, parts.length)); + this.target = result.getPositional(0); - this.setAi = !result.get("ai").isNull(); + this.setAi = result.isSet("ai"); if(this.setAi) { - this.ai = result.get("ai").asBool(); + this.ai = result.getFlag("ai"); } - this.setPosition = !result.get("position").isNull(); + this.setPosition = result.isSet("position"); if(this.setPosition) { - @SuppressWarnings("unchecked") - List positions = (List)result.get("position").get();; - this.x = positions.get(0); - this.y = positions.get(1); - this.z = positions.get(2); + Position position = result.getOptional("position"); + this.x = position.x; + this.y = position.y; + this.z = position.z; + this.world = position.world; } - this.name = result.get("name").asString(); + this.name = result.getOptional("name"); - this.setHealth = !result.get("health").isNull(); + this.setHealth = result.isSet("health"); if(this.setHealth) { - this.health = result.get("health").asInt(); + this.health = result.getOptional("health"); } - } @Override @@ -101,6 +99,7 @@ public void apply() { loc.setX(this.x); loc.setY(this.y); loc.setZ(this.z); + loc.setWorld(this.world); ent.teleport(loc); } diff --git a/src/main/java/org/sk/skMinecraft/commands/GetBlock.java b/src/main/java/org/sk/skMinecraft/commands/GetBlock.java index cc215a2..bdfe9cc 100644 --- a/src/main/java/org/sk/skMinecraft/commands/GetBlock.java +++ b/src/main/java/org/sk/skMinecraft/commands/GetBlock.java @@ -3,26 +3,43 @@ import org.bukkit.Bukkit; import org.bukkit.World; import org.bukkit.block.Block; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class GetBlock extends Command{ private int x,y,z; + private World world; - public GetBlock(String command) { - String[] parts = command.split(SkMinecraft.seperator); - try { - this.x = Integer.parseInt(parts[1]); - this.y = Integer.parseInt(parts[2]); - this.z = Integer.parseInt(parts[3]); - } catch(Exception ignored){} + public GetBlock(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); + + parser.addPositionalArguments( + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.StringParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { + this.valid = false; + return; + } + + this.x = result.getPositional(0); + this.y = result.getPositional(1); + this.z = result.getPositional(2); + + String worldName = result.getPositional(3); + this.world = Bukkit.getWorld(worldName); } @Override public void apply() { Bukkit.getScheduler().runTask(plugin, () -> { - World world = Bukkit.getWorlds().getFirst(); - Block block = world.getBlockAt(x,y,z); + Block block = this.world.getBlockAt(x,y,z); this.writer.println(block.getType().name()); }); diff --git a/src/main/java/org/sk/skMinecraft/commands/GetEntity.java b/src/main/java/org/sk/skMinecraft/commands/GetEntity.java index 9dfb3ee..96703df 100644 --- a/src/main/java/org/sk/skMinecraft/commands/GetEntity.java +++ b/src/main/java/org/sk/skMinecraft/commands/GetEntity.java @@ -6,20 +6,28 @@ import org.bukkit.entity.LivingEntity; import org.sk.skMinecraft.CentralResourceHandler; import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class GetEntity extends Command { private String target; - public GetEntity(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public GetEntity(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 2) { + parser.addPositionalArguments( + ArgumentParser.StringParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; return; } - - this.target = parts[1]; + + this.target = result.getPositional(0); } public static String informationString(String uuid, Entity ent) { @@ -28,6 +36,7 @@ public static String informationString(String uuid, Entity ent) { int x = loc.getBlockX(); int y = loc.getBlockY(); int z = loc.getBlockZ(); + String world = loc.getWorld().getName(); double health = 0; boolean ai = false; if(ent instanceof LivingEntity lent) { @@ -36,7 +45,7 @@ public static String informationString(String uuid, Entity ent) { } String type = ent.getType().toString(); - String result = SkMinecraft.joinWithSeperator(uuid, type, name, x, y, z, health, ai); + String result = SkMinecraft.joinWithSeperator(uuid, type, name, x, y, z, world, health, ai); return result; } diff --git a/src/main/java/org/sk/skMinecraft/commands/GetInventory.java b/src/main/java/org/sk/skMinecraft/commands/GetInventory.java index f0cd5ef..e94c211 100644 --- a/src/main/java/org/sk/skMinecraft/commands/GetInventory.java +++ b/src/main/java/org/sk/skMinecraft/commands/GetInventory.java @@ -6,22 +6,28 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class GetInventory extends Command { private int playerIndex; - public GetInventory(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public GetInventory(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 2) { + parser.addPositionalArguments( + ArgumentParser.IntParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; return; } - try { - this.playerIndex = Integer.parseInt(parts[1]); - } catch (Exception ignored) {} + this.playerIndex = result.getPositional(0); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/GetPlayer.java b/src/main/java/org/sk/skMinecraft/commands/GetPlayer.java index 685ee37..8c7bb2c 100644 --- a/src/main/java/org/sk/skMinecraft/commands/GetPlayer.java +++ b/src/main/java/org/sk/skMinecraft/commands/GetPlayer.java @@ -5,24 +5,28 @@ import org.bukkit.attribute.Attribute; import org.bukkit.entity.Player; import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class GetPlayer extends Command { private int playerIndex; - public GetPlayer(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public GetPlayer(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length != 2) { + parser.addPositionalArguments( + ArgumentParser.IntParser + ); + + ParseResult arguments = parser.parse(command.arguments()); + + if(!arguments.isValid()) { this.valid = false; return; } - try { - this.playerIndex = Integer.parseInt(parts[1]); - } catch (NumberFormatException e) { - this.valid = false; - } + this.playerIndex = arguments.getPositional(0); } @Override @@ -45,6 +49,7 @@ public void apply() { int x = loc.getBlockX(); int y = loc.getBlockY(); int z = loc.getBlockZ(); + String world_name = loc.getWorld().getName(); int rotation = (int)loc.getYaw(); boolean sneak = target.isSneaking(); double maxhealth = target.getAttribute(Attribute.MAX_HEALTH).getBaseValue(); @@ -60,6 +65,7 @@ public void apply() { x, y, z, + world_name, rotation, looking_at, sneak, diff --git a/src/main/java/org/sk/skMinecraft/commands/PollChat.java b/src/main/java/org/sk/skMinecraft/commands/PollChat.java index 0019961..3760246 100644 --- a/src/main/java/org/sk/skMinecraft/commands/PollChat.java +++ b/src/main/java/org/sk/skMinecraft/commands/PollChat.java @@ -3,13 +3,32 @@ import org.bukkit.Bukkit; import org.sk.skMinecraft.CentralResourceHandler; import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; import org.sk.skMinecraft.data.ChatMessage; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; public class PollChat extends Command { - public PollChat(String s) { + public PollChat(StringCommand s) {} + + private String formatMessage(ChatMessage message) { + StringBuilder builder = new StringBuilder(); + + builder.append(message.player.getName()); + builder.append(":"); + + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy|MM|dd|HH|mm|ss"); + builder.append(message.timestamp.format(formatter)); + builder.append(":"); + + builder.append(SkMinecraft.playerIndexFromName(message.player.getName())); + builder.append(":"); + + builder.append(message.message); + + return builder.toString(); } @Override @@ -17,8 +36,8 @@ public void apply() { Bukkit.getScheduler().runTask(plugin, () -> { ArrayList messages = CentralResourceHandler.getChatMessages(); String output = messages.stream() - .map(message -> message.player.getName() + ":" + message.message) - .reduce("", (arg, msg) -> arg == "" ? msg : arg + SkMinecraft.seperator + msg); + .map(message -> formatMessage(message)) + .reduce("", (arg, msg) -> arg == "" ? msg : arg + SkMinecraft.seperator + msg);; this.writer.println(output); }); diff --git a/src/main/java/org/sk/skMinecraft/commands/PostChat.java b/src/main/java/org/sk/skMinecraft/commands/PostChat.java index e1ee801..ea46e3d 100644 --- a/src/main/java/org/sk/skMinecraft/commands/PostChat.java +++ b/src/main/java/org/sk/skMinecraft/commands/PostChat.java @@ -1,14 +1,17 @@ package org.sk.skMinecraft.commands; +import java.util.Arrays; +import java.util.stream.Collectors; + import org.bukkit.Bukkit; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; public class PostChat extends Command { private final String message; - public PostChat(String command) { - this.message = command.replaceFirst("postChat" + SkMinecraft.seperator, ""); + public PostChat(StringCommand command) { + this.message = Arrays.stream(command.arguments()).collect(Collectors.joining("")); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/SetBlock.java b/src/main/java/org/sk/skMinecraft/commands/SetBlock.java index b3a186f..778d41a 100644 --- a/src/main/java/org/sk/skMinecraft/commands/SetBlock.java +++ b/src/main/java/org/sk/skMinecraft/commands/SetBlock.java @@ -4,36 +4,51 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class SetBlock extends Command { private int x,y,z; + private World world; private Material material; - public SetBlock(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public SetBlock(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - this.valid = true; - try { - this.x = Integer.parseInt(parts[1]); - this.y = Integer.parseInt(parts[2]); - this.z = Integer.parseInt(parts[3]); - } catch (NumberFormatException e) { + parser.addPositionalArguments( + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.StringParser, + ArgumentParser.StringParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; + return; } - String blockId = parts[4].toUpperCase(); + this.x = result.getPositional(0); + this.y = result.getPositional(1); + this.z = result.getPositional(2); + + String worldName = result.getPositional(3); + this.world = Bukkit.getWorld(worldName); + + String blockId = result.getPositional(4); this.material = Material.matchMaterial(blockId); if (this.material == null || !this.material.isBlock()) { this.valid = false; + return; } } public void apply() { Bukkit.getScheduler().runTask(this.plugin, () -> { - World world = Bukkit.getWorlds().getFirst(); // default world - Location loc = new Location(world, x, y, z); + Location loc = new Location(this.world, x, y, z); loc.getBlock().setType(this.material); }); } diff --git a/src/main/java/org/sk/skMinecraft/commands/SetPlayerPos.java b/src/main/java/org/sk/skMinecraft/commands/SetPlayerPos.java index bcff359..b2e3584 100644 --- a/src/main/java/org/sk/skMinecraft/commands/SetPlayerPos.java +++ b/src/main/java/org/sk/skMinecraft/commands/SetPlayerPos.java @@ -2,47 +2,51 @@ import org.bukkit.Bukkit; import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.Player; -import org.sk.skMinecraft.SkMinecraft; - -import java.util.Arrays; -import java.util.HashMap; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class SetPlayerPos extends Command { private int x; private int y; private int z; + private World world; private int rot; private boolean setRot; private int playerIndex; - public SetPlayerPos(String command ){ - String[] parts = command.split(SkMinecraft.seperator); + public SetPlayerPos(StringCommand command ){ + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 5) { - this.valid = false; - return; - } + parser.addPositionalArguments( + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.StringParser + ); - try { - this.playerIndex = Integer.parseInt(parts[1]); - this.x = Integer.parseInt(parts[2]); - this.y = Integer.parseInt(parts[3]); - this.z = Integer.parseInt(parts[4]); - } catch (Exception e) { - this.valid = false; - } + parser.addOptionalArgument("rotation", Integer::parseInt); - ArgumentParser parser = new ArgumentParser(); + ParseResult result = parser.parse(command.arguments()); - parser.addArgument("rot", Integer::parseInt); + if(!result.isValid()) { + this.valid = false; + return; + } - HashMap result = parser.parse(Arrays.copyOfRange(parts,5, parts.length)); + this.playerIndex = result.getPositional(0); + this.x = result.getPositional(1); + this.y = result.getPositional(2); + this.z = result.getPositional(3); + String worldName = result.getPositional(4); + this.world = Bukkit.getWorld(worldName); - this.setRot = !result.get("rot").isNull(); + this.setRot = result.isSet("rotation"); if(this.setRot) { - this.rot = result.get("rot").asInt(); + this.rot = result.getOptional("rotation"); } } @@ -56,7 +60,7 @@ public void apply() { } Player player = players[playerIndex]; - Location loc = new Location(player.getWorld(), this.x, this.y, this.z); + Location loc = new Location(this.world, this.x, this.y, this.z); if(!this.setRot) { loc.setYaw(player.getLocation().getYaw()); }else { diff --git a/src/main/java/org/sk/skMinecraft/commands/SetPlayerStat.java b/src/main/java/org/sk/skMinecraft/commands/SetPlayerStat.java index c3524c4..f205623 100644 --- a/src/main/java/org/sk/skMinecraft/commands/SetPlayerStat.java +++ b/src/main/java/org/sk/skMinecraft/commands/SetPlayerStat.java @@ -4,7 +4,8 @@ import org.bukkit.attribute.Attribute; import org.bukkit.attribute.AttributeInstance; import org.bukkit.entity.Player; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class SetPlayerStat extends Command { @@ -21,21 +22,27 @@ private enum Type { private int playerIndex; private double value; - public SetPlayerStat(String command){ - String[] parts = command.split(SkMinecraft.seperator); + public SetPlayerStat(StringCommand command){ + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 4) { + parser.addPositionalArguments( + ArgumentParser.StringParser, + ArgumentParser.IntParser, + ArgumentParser.DoubleParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; return; } - this.type = Type.valueOf(parts[1].toUpperCase()); - try { - this.playerIndex = Integer.parseInt(parts[2]); - this.value = Double.parseDouble(parts[3]); - }catch (Exception ignored) { - this.valid = false; - } + String typeName = result.getPositional(0); + this.type = Type.valueOf(typeName.toUpperCase()); + + this.playerIndex = result.getPositional(1); + this.value = result.getPositional(2); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/SetPlayerVelocity.java b/src/main/java/org/sk/skMinecraft/commands/SetPlayerVelocity.java index 8d9cdae..21f9022 100644 --- a/src/main/java/org/sk/skMinecraft/commands/SetPlayerVelocity.java +++ b/src/main/java/org/sk/skMinecraft/commands/SetPlayerVelocity.java @@ -3,7 +3,8 @@ import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.util.Vector; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class SetPlayerVelocity extends Command { @@ -18,20 +19,25 @@ private enum Type { private int playerIndex; private double strength; - public SetPlayerVelocity(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public SetPlayerVelocity(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length < 4) { + parser.addPositionalArguments( + ArgumentParser.StringParser, + ArgumentParser.IntParser, + ArgumentParser.DoubleParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; return; } - try { - this.type = Type.valueOf(parts[1]); - this.playerIndex = Integer.parseInt(parts[2]); - this.strength = Double.parseDouble(parts[3]); - }catch (Exception e) { - this.valid = false; - } + + this.type = Type.valueOf(result.getPositional(0)); + this.playerIndex = result.getPositional(1); + this.strength = result.getPositional(2); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/ShowTitle.java b/src/main/java/org/sk/skMinecraft/commands/ShowTitle.java index 2822b72..453f2b1 100644 --- a/src/main/java/org/sk/skMinecraft/commands/ShowTitle.java +++ b/src/main/java/org/sk/skMinecraft/commands/ShowTitle.java @@ -1,13 +1,9 @@ package org.sk.skMinecraft.commands; -import java.util.Arrays; -import java.util.HashMap; - import org.bukkit.Bukkit; -import org.bukkit.boss.BarColor; -import org.bukkit.boss.BarStyle; import org.bukkit.entity.Player; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class ShowTitle extends Command { @@ -18,25 +14,31 @@ public class ShowTitle extends Command { private int stay; private int fadeOut; - public ShowTitle(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public ShowTitle(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length != 7) { - this.valid = false; - return; - } + parser.addPositionalArguments( + ArgumentParser.IntParser, + ArgumentParser.StringParser, + ArgumentParser.StringParser, + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.IntParser + ); - this.title = parts[2]; - this.subtitle = parts[3]; + ParseResult result = parser.parse(command.arguments()); - try { - this.player = Integer.parseInt(parts[1]); - this.fadeIn = Integer.parseInt(parts[4]); - this.stay = Integer.parseInt(parts[5]); - this.fadeOut = Integer.parseInt(parts[6]); - } catch(NumberFormatException e) { + if(!result.isValid()) { this.valid = false; + return; } + + this.player = result.getPositional(0); + this.title = result.getPositional(1); + this.subtitle = result.getPositional(2); + this.fadeIn = result.getPositional(3); + this.stay = result.getPositional(4); + this.fadeOut = result.getPositional(5); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/SpawnBossBar.java b/src/main/java/org/sk/skMinecraft/commands/SpawnBossBar.java index ad7284d..a1dd93c 100644 --- a/src/main/java/org/sk/skMinecraft/commands/SpawnBossBar.java +++ b/src/main/java/org/sk/skMinecraft/commands/SpawnBossBar.java @@ -6,26 +6,31 @@ import org.bukkit.boss.BarStyle; import org.bukkit.boss.KeyedBossBar; import org.bukkit.entity.Player; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class SpawnBossBar extends Command { private String name; private String text; - public SpawnBossBar(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public SpawnBossBar(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - this.name = ""; - this.text = ""; - if (parts.length < 2) { + parser.addPositionalArguments( + ArgumentParser.StringParser, + ArgumentParser.StringParser + ); + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; return; } - name = parts[1]; - text = parts[2]; + this.name = result.getPositional(0); + this.text = result.getPositional(1); } @Override diff --git a/src/main/java/org/sk/skMinecraft/commands/SpawnEntity.java b/src/main/java/org/sk/skMinecraft/commands/SpawnEntity.java index 1feb611..86c852b 100644 --- a/src/main/java/org/sk/skMinecraft/commands/SpawnEntity.java +++ b/src/main/java/org/sk/skMinecraft/commands/SpawnEntity.java @@ -6,37 +6,48 @@ import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.sk.skMinecraft.CentralResourceHandler; -import org.sk.skMinecraft.SkMinecraft; +import org.sk.skMinecraft.SkMinecraft.StringCommand; +import org.sk.skMinecraft.commands.ArgumentParser.ParseResult; public class SpawnEntity extends Command{ private int x,y,z; + private World world; private EntityType type; - public SpawnEntity(String command) { - String[] parts = command.split(SkMinecraft.seperator); + public SpawnEntity(StringCommand command) { + ArgumentParser parser = new ArgumentParser(); - if(parts.length != 5) { + parser.addPositionalArguments( + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.IntParser, + ArgumentParser.StringParser, + ArgumentParser.StringParser + ); + + ParseResult result = parser.parse(command.arguments()); + + if(!result.isValid()) { this.valid = false; return; } - try { - this.x = Integer.parseInt(parts[1]); - this.y = Integer.parseInt(parts[2]); - this.z = Integer.parseInt(parts[3]); - } catch (NumberFormatException e) { - this.valid = false; - } + this.x = result.getPositional(0); + this.y = result.getPositional(1); + this.z = result.getPositional(2); + + String worldName = result.getPositional(3); + this.world = Bukkit.getWorld(worldName); - this.type = EntityType.valueOf(parts[4].toUpperCase()); + String typeName = result.getPositional(4); + this.type = EntityType.valueOf(typeName.toUpperCase()); } @Override public void apply() { Bukkit.getScheduler().runTask(this.plugin, () -> { - World world = Bukkit.getWorlds().getFirst(); // default world - Location loc = new Location(world, x, y, z); + Location loc = new Location(this.world, x, y, z); Entity entity = world.spawnEntity(loc, this.type); CentralResourceHandler.addEntity(entity.getUniqueId().toString(), entity); diff --git a/src/main/java/org/sk/skMinecraft/data/ChatMessage.java b/src/main/java/org/sk/skMinecraft/data/ChatMessage.java index aad73d7..1c0020d 100644 --- a/src/main/java/org/sk/skMinecraft/data/ChatMessage.java +++ b/src/main/java/org/sk/skMinecraft/data/ChatMessage.java @@ -1,14 +1,18 @@ package org.sk.skMinecraft.data; +import java.time.LocalDateTime; + import org.bukkit.entity.Player; public class ChatMessage { public Player player; public String message; + public LocalDateTime timestamp; - public ChatMessage(Player player, String message) { + public ChatMessage(Player player, String message, LocalDateTime timestamp) { this.player = player; this.message = message; + this.timestamp = timestamp; } } \ No newline at end of file diff --git a/src/main/java/org/sk/skMinecraft/data/Position.java b/src/main/java/org/sk/skMinecraft/data/Position.java new file mode 100644 index 0000000..4e38d46 --- /dev/null +++ b/src/main/java/org/sk/skMinecraft/data/Position.java @@ -0,0 +1,31 @@ +package org.sk.skMinecraft.data; + +import org.bukkit.Bukkit; +import org.bukkit.World; + +public class Position { + public int x,y,z; + public World world; + + public static Position fromString(String position, String seperator) { + String[] pos = position.split(seperator); + + + int x = Integer.parseInt(pos[0]); + int y = Integer.parseInt(pos[1]); + int z = Integer.parseInt(pos[2]); + + World world = Bukkit.getWorld(pos[3]); + + return new Position(x,y,z, world); + } + + public Position(int x, int y, int z, World world) { + this.x = x; + this.y = y; + this.z = z; + + this.world = world; + } + +}