SteamWar/BauSystem
Archiviert
13
0

Simplify sleep Command

Simplify further command additions
Dieser Commit ist enthalten in:
jojo 2020-12-16 10:00:29 +01:00
Ursprung 203519b15f
Commit 4a9a317428

Datei anzeigen

@ -32,7 +32,6 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta; import org.bukkit.inventory.meta.BookMeta;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
@ -40,7 +39,35 @@ public class ScriptListener implements Listener {
private static final String scriptPrefix = "§eScriptSystem§8» §7"; 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) { public void onLeftClick(PlayerInteractEvent event) {
if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK)
return; return;
@ -75,9 +102,11 @@ public class ScriptListener implements Listener {
for(String page : bookMeta.getPages()) { for(String page : bookMeta.getPages()) {
for (String command : page.split("\n")) { for (String command : page.split("\n")) {
if (command.startsWith("#")) continue; if (command.startsWith("#")) continue;
if (command.trim().isEmpty()) continue;
commands.add(command); commands.add(command);
} }
} }
if (commands.isEmpty()) return;
resume(); resume();
} }
@ -89,24 +118,11 @@ public class ScriptListener implements Listener {
while (index < commands.size()) { while (index < commands.size()) {
String command = commands.get(index++); String command = commands.get(index++);
if (command.contains(" ")) { for (ScriptCommand scriptCommand : scriptCommands) {
String[] strings = command.split(" "); if (command.toLowerCase().startsWith(scriptCommand.command())) {
if (strings.length > 0) { String[] args = command.substring(scriptCommand.command().length() + 1).split(" ");
if (strings[0].equalsIgnoreCase("sleep")) { if (!scriptCommand.execute(this, args)) {
int sleepTime = 1; return;
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;
} }
} }
} }
@ -114,8 +130,6 @@ public class ScriptListener implements Listener {
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
if (preprocessEvent.isCancelled()) { if (preprocessEvent.isCancelled()) {
player.sendMessage(scriptPrefix + "Befehl konnte nicht ausgeführt werden:");
player.sendMessage(scriptPrefix + command);
continue; 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);
}
} }