Implemented command argument handling

This commit is contained in:
Maximilian P. Käfer 2020-11-17 18:14:57 +01:00
parent 808ff6c84e
commit d452f76007
4 changed files with 43 additions and 8 deletions

View File

@ -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!");
}
return false;
}
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 true;
}
}

View File

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

View File

@ -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<String, Boolean> 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;
}
}

View File

@ -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