Simplify sleep Command
Simplify further command additions
Dieser Commit ist enthalten in:
Ursprung
203519b15f
Commit
4a9a317428
@ -32,7 +32,6 @@ import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@ -40,7 +39,35 @@ public class ScriptListener implements Listener {
|
||||
|
||||
private static final String scriptPrefix = "§eScriptSystem§8» §7";
|
||||
|
||||
@EventHandler
|
||||
private static final List<ScriptCommand> scriptCommands = new ArrayList<>();
|
||||
|
||||
static {
|
||||
scriptCommands.add(new ScriptCommand() {
|
||||
@Override
|
||||
public String command() {
|
||||
return "sleep";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(ScriptExecutor scriptExecutor, String[] args) {
|
||||
int sleepTime = 1;
|
||||
if (args.length > 0) {
|
||||
try {
|
||||
sleepTime = Integer.parseInt(args[0]);
|
||||
if (sleepTime <= 0) {
|
||||
scriptExecutor.player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick");
|
||||
sleepTime = 1;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
scriptExecutor.player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick");
|
||||
}
|
||||
}
|
||||
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void onLeftClick(PlayerInteractEvent event) {
|
||||
if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK)
|
||||
return;
|
||||
@ -75,9 +102,11 @@ public class ScriptListener implements Listener {
|
||||
for(String page : bookMeta.getPages()) {
|
||||
for (String command : page.split("\n")) {
|
||||
if (command.startsWith("#")) continue;
|
||||
if (command.trim().isEmpty()) continue;
|
||||
commands.add(command);
|
||||
}
|
||||
}
|
||||
if (commands.isEmpty()) return;
|
||||
resume();
|
||||
}
|
||||
|
||||
@ -89,24 +118,11 @@ public class ScriptListener implements Listener {
|
||||
while (index < commands.size()) {
|
||||
String command = commands.get(index++);
|
||||
|
||||
if (command.contains(" ")) {
|
||||
String[] strings = command.split(" ");
|
||||
if (strings.length > 0) {
|
||||
if (strings[0].equalsIgnoreCase("sleep")) {
|
||||
int sleepTime = 1;
|
||||
if (strings.length > 1) {
|
||||
try {
|
||||
sleepTime = Integer.parseInt(strings[1]);
|
||||
if (sleepTime <= 0) {
|
||||
player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick");
|
||||
sleepTime = 1;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick");
|
||||
}
|
||||
}
|
||||
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime);
|
||||
break;
|
||||
for (ScriptCommand scriptCommand : scriptCommands) {
|
||||
if (command.toLowerCase().startsWith(scriptCommand.command())) {
|
||||
String[] args = command.substring(scriptCommand.command().length() + 1).split(" ");
|
||||
if (!scriptCommand.execute(this, args)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -114,8 +130,6 @@ public class ScriptListener implements Listener {
|
||||
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
|
||||
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
|
||||
if (preprocessEvent.isCancelled()) {
|
||||
player.sendMessage(scriptPrefix + "Befehl konnte nicht ausgeführt werden:");
|
||||
player.sendMessage(scriptPrefix + command);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -126,4 +140,15 @@ public class ScriptListener implements Listener {
|
||||
|
||||
}
|
||||
|
||||
private interface ScriptCommand {
|
||||
|
||||
String command();
|
||||
|
||||
/**
|
||||
* Should return {@code true} if execution should not stop, {@code false} otherwise.
|
||||
*/
|
||||
boolean execute(ScriptExecutor scriptExecutor, String[] args);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren