From d452f76007c1678b44b2adc6fa06478f83689d20 Mon Sep 17 00:00:00 2001 From: DieGurke Date: Tue, 17 Nov 2020 18:14:57 +0100 Subject: [PATCH] Implemented command argument handling --- .../java/dev/kske/spawnfly/SpawnCommand.java | 28 ++++++++++++++++--- src/main/java/dev/kske/spawnfly/SpawnFly.java | 11 ++++++-- .../java/dev/kske/spawnfly/SpawnListener.java | 10 +++++++ src/main/resources/plugin.yml | 2 +- 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main/java/dev/kske/spawnfly/SpawnCommand.java b/src/main/java/dev/kske/spawnfly/SpawnCommand.java index 6c88927..5041612 100644 --- a/src/main/java/dev/kske/spawnfly/SpawnCommand.java +++ b/src/main/java/dev/kske/spawnfly/SpawnCommand.java @@ -3,16 +3,36 @@ package dev.kske.spawnfly; import org.bukkit.command.*; public class SpawnCommand implements CommandExecutor { - public SpawnCommand() {} + SpawnFly spawnFly; + + public SpawnCommand(SpawnFly spawnFly) { this.spawnFly = spawnFly;} @Override public boolean onCommand(CommandSender sender, org.bukkit.command.Command command, String label, String[] args) { if(command.getName().equals("setspawnflyarea")) { - sender.sendMessage("Hello this works!"); - return true; + if(args.length == 6) { + int[] cords = new int[6]; + int cnt = 0; + for (int i = 0; i < args.length; i++) { + try { + int value = Integer.parseInt(args[i]); + cords[i] = value; + //sender.sendMessage("Argument " +(i + 1) + " is " + value); + cnt++; + } catch (Exception e) { + //sender.sendMessage("Argument " + (i + 1) + " is not valid as a coordinate!"); + } + } + if(cnt == 6) { + spawnFly.setCoordinates(cords[0], cords[1], cords[2], cords[3], cords[4], cords[5]); + } + else { + sender.sendMessage("Please use valid coordinates!"); + } + } } - return false; + return true; } } diff --git a/src/main/java/dev/kske/spawnfly/SpawnFly.java b/src/main/java/dev/kske/spawnfly/SpawnFly.java index d53da15..c759b69 100644 --- a/src/main/java/dev/kske/spawnfly/SpawnFly.java +++ b/src/main/java/dev/kske/spawnfly/SpawnFly.java @@ -4,16 +4,21 @@ import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; public final class SpawnFly extends JavaPlugin { - + SpawnListener spawnListener = new SpawnListener(); + @Override public void onEnable() { getLogger().info("onEnable has been invoked!"); - Bukkit.getPluginManager().registerEvents(new SpawnListener(), this); - getCommand("setspawnflyarea").setExecutor(new SpawnCommand()); + Bukkit.getPluginManager().registerEvents(spawnListener, this); + getCommand("setspawnflyarea").setExecutor(new SpawnCommand(this)); } @Override public void onDisable() { getLogger().info("onDisable has been invoked!"); } + + public void setCoordinates(int x1, int y1, int z1, int x2, int y2, int z2) { + spawnListener.setCoordinates(x1, y1, z1, x2, y2, z2); + } } diff --git a/src/main/java/dev/kske/spawnfly/SpawnListener.java b/src/main/java/dev/kske/spawnfly/SpawnListener.java index 5f11d6d..d866efd 100644 --- a/src/main/java/dev/kske/spawnfly/SpawnListener.java +++ b/src/main/java/dev/kske/spawnfly/SpawnListener.java @@ -9,6 +9,7 @@ import org.bukkit.event.entity.EntityToggleGlideEvent; import org.bukkit.event.player.*; public class SpawnListener implements Listener { + int x1, y1, z1, x2, y2, z2; Map canceledEvents = new HashMap<>(); @@ -47,4 +48,13 @@ public class SpawnListener implements Listener { canceledEvents.put(e.getEntity().getName(), false); } } + + public void setCoordinates(int x1, int y1, int z1, int x2, int y2, int z2) { + this.x1 = x1; + this.y1 = y1; + this.z1 = z1; + this.x2 = x2; + this.y2 = y2; + this.z2 = z2; + } } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index c0e979c..d6b6989 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,4 +3,4 @@ main: dev.kske.spawnfly.SpawnFly version: 0.0.1 commands: setspawnflyarea: - usage: /setspawnflyarea + usage: /setspawnflyarea x1 y1 z1 x2 y2 z2