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