SteamWar/BauSystem
Archiviert
13
0

Merge ScriptListener.Script and ScriptListener.ScriptExecutor

Dieser Commit ist enthalten in:
jojo 2020-12-16 09:04:18 +01:00
Ursprung ee94b51612
Commit 00a77b378b

Datei anzeigen

@ -39,16 +39,14 @@ import java.util.logging.Level;
public class ScriptListener implements Listener { public class ScriptListener implements Listener {
private Map<BookMeta, Script> scriptMap = new HashMap<>(); private static class ScriptExecutor {
private class Script { private Player player;
private BookMeta bookMeta;
private List<String> commands = new ArrayList<>(); private List<String> commands = new ArrayList<>();
private int uses = 0; private int index = 0;
public Script(BookMeta bookMeta) { public ScriptExecutor(BookMeta bookMeta, Player player) {
this.bookMeta = bookMeta; this.player = player;
for(String page : bookMeta.getPages()) { for(String page : bookMeta.getPages()) {
for (String command : page.split("\n")) { for (String command : page.split("\n")) {
@ -56,50 +54,23 @@ public class ScriptListener implements Listener {
commands.add(command); commands.add(command);
} }
} }
}
public void keep() {
uses++;
}
public void free() {
uses--;
if (uses < 0) {
scriptMap.remove(bookMeta);
}
}
}
private class ScriptExecutor {
private Player player;
private Script script;
private int index = 0;
public ScriptExecutor(BookMeta bookMeta, Player player) {
this.player = player;
if (!scriptMap.containsKey(bookMeta)) {
scriptMap.put(bookMeta, new Script(bookMeta));
}
script = scriptMap.get(bookMeta);
script.keep();
}
public void start() {
resume(); resume();
} }
private void resume() { private void resume() {
while (index < script.commands.size()) { if (!player.isOnline()) {
String command = script.commands.get(index++); player = null;
commands = null;
return;
}
while (index < commands.size()) {
String command = commands.get(index++);
if (command.startsWith("sleep")) { if (command.startsWith("sleep")) {
String sleepTimeString = command.substring(5).trim(); String sleepTimeString = command.substring(5).trim();
int sleepTime = 1; int sleepTime = 1;
try { try {
Integer.parseInt(sleepTimeString);
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// Ignored // Ignored
} }
@ -131,7 +102,7 @@ public class ScriptListener implements Listener {
event.setCancelled(true); event.setCancelled(true);
Player player = event.getPlayer(); Player player = event.getPlayer();
BookMeta meta = (BookMeta) item.getItemMeta(); BookMeta meta = (BookMeta) item.getItemMeta();
new ScriptExecutor(meta, player).resume(); new ScriptExecutor(meta, player);
} }
private boolean isNoBook(ItemStack item){ private boolean isNoBook(ItemStack item){