SteamWar/BauSystem
Archiviert
13
0

Make ScriptListener.ScriptExecutor.player final

Make ScriptListener.ScriptExecutor.commands final
Fixing commands without spaces
Fixing negative and zero sleepTime
Add Error message on sleep without number
Remove throw-away-references
Dieser Commit ist enthalten in:
jojo 2020-12-16 09:26:17 +01:00
Ursprung 15570cab7c
Commit 93003de08d

Datei anzeigen

@ -32,17 +32,40 @@ 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.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
public class ScriptListener implements Listener { public class ScriptListener implements Listener {
private static final String scriptPrefix = "§eScriptSystem§8» §7";
@EventHandler
public void onLeftClick(PlayerInteractEvent event) {
if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK)
return;
ItemStack item = event.getItem();
if(item == null || isNoBook(item) || item.getItemMeta() == null)
return;
event.setCancelled(true);
new ScriptExecutor((BookMeta) item.getItemMeta(), event.getPlayer());
}
private boolean isNoBook(ItemStack item){
switch(Core.getVersion()){
case 12:
return ScriptListener_12.isNoBook(item);
case 15:
default:
return ScriptListener_15.isNoBook(item);
}
}
private static class ScriptExecutor { private static class ScriptExecutor {
private Player player; private final Player player;
private List<String> commands = new ArrayList<>(); private final List<String> commands = new ArrayList<>();
private int index = 0; private int index = 0;
public ScriptExecutor(BookMeta bookMeta, Player player) { public ScriptExecutor(BookMeta bookMeta, Player player) {
@ -59,14 +82,13 @@ public class ScriptListener implements Listener {
private void resume() { private void resume() {
if (!player.isOnline()) { if (!player.isOnline()) {
player = null;
commands = null;
return; return;
} }
while (index < commands.size()) { while (index < commands.size()) {
String command = commands.get(index++); String command = commands.get(index++);
if (command.contains(" ")) {
String[] strings = command.split(" "); String[] strings = command.split(" ");
if (strings.length > 0) { if (strings.length > 0) {
if (strings[0].equalsIgnoreCase("sleep")) { if (strings[0].equalsIgnoreCase("sleep")) {
@ -74,14 +96,16 @@ public class ScriptListener implements Listener {
if (strings.length > 1) { if (strings.length > 1) {
try { try {
sleepTime = Integer.parseInt(strings[1]); sleepTime = Integer.parseInt(strings[1]);
if (sleepTime <= 0) sleepTime = 1;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// Ignored player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick");
} }
} }
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime); Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime);
break; break;
} }
} }
}
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
@ -95,29 +119,4 @@ public class ScriptListener implements Listener {
} }
@EventHandler
public void onLeftClick(PlayerInteractEvent event) {
if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK)
return;
ItemStack item = event.getItem();
if(item == null || isNoBook(item) || item.getItemMeta() == null)
return;
event.setCancelled(true);
Player player = event.getPlayer();
BookMeta meta = (BookMeta) item.getItemMeta();
new ScriptExecutor(meta, player);
}
private boolean isNoBook(ItemStack item){
switch(Core.getVersion()){
case 12:
return ScriptListener_12.isNoBook(item);
case 15:
default:
return ScriptListener_15.isNoBook(item);
}
}
} }