Script Turing Completeness #152
@ -22,6 +22,7 @@ package de.steamwar.bausystem.world;
|
|||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import de.steamwar.core.Core;
|
import de.steamwar.core.Core;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
@ -32,10 +33,7 @@ import org.bukkit.event.player.PlayerInteractEvent;
|
|||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
public class ScriptListener implements Listener {
|
public class ScriptListener implements Listener {
|
||||||
@ -80,7 +78,11 @@ public class ScriptListener implements Listener {
|
|||||||
|
|
||||||
private final Player player;
|
private final Player player;
|
||||||
private final List<String> commands = new ArrayList<>();
|
private final List<String> commands = new ArrayList<>();
|
||||||
|
private final Map<String, Integer> jumpPoints = new HashMap<>();
|
||||||
|
|
||||||
|
private boolean lastCommandWasSleep = false;
|
||||||
private int index = 0;
|
private int index = 0;
|
||||||
|
private int executionPoints = 0;
|
||||||
|
|
||||||
public ScriptExecutor(BookMeta bookMeta, Player player) {
|
public ScriptExecutor(BookMeta bookMeta, Player player) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
@ -88,6 +90,10 @@ 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("#") || command.trim().isEmpty()) continue;
|
if (command.startsWith("#") || command.trim().isEmpty()) continue;
|
||||||
|
if (command.startsWith(".")) {
|
||||||
|
jumpPoints.put(command.substring(1), commands.size());
|
||||||
|
continue;
|
||||||
|
}
|
||||||
commands.add(command);
|
commands.add(command);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -103,10 +109,37 @@ public class ScriptListener implements Listener {
|
|||||||
while (index < commands.size()) {
|
while (index < commands.size()) {
|
||||||
String command = commands.get(index++);
|
String command = commands.get(index++);
|
||||||
|
|
||||||
|
executionPoints++;
|
||||||
|
if (executionPoints > 200) {
|
||||||
|
player.sendMessage(BauSystem.PREFIX + "§cBitte füge ein sleep in dein Script ein.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (command.toLowerCase().startsWith("sleep")) {
|
if (command.toLowerCase().startsWith("sleep")) {
|
||||||
|
if (!lastCommandWasSleep) {
|
||||||
|
executionPoints -= 2;
|
||||||
|
}
|
||||||
|
lastCommandWasSleep = true;
|
||||||
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
|
ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
lastCommandWasSleep = false;
|
||||||
|
if (command.toLowerCase().startsWith("exit")) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (command.toLowerCase().startsWith("jump")) {
|
||||||
|
executionPoints += 2;
|
||||||
|
int jumpIndex = ScriptListener.jumpCommand(this, generateArgumentArray("jump", command));
|
||||||
|
if (jumpIndex != -1) {
|
||||||
|
index = jumpIndex;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (command.toLowerCase().startsWith("info")) {
|
||||||
|
executionPoints -= 1;
|
||||||
|
ScriptListener.infoCommand(this, generateArgumentArray("info", command));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
|
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
|
||||||
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
|
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
|
||||||
@ -141,4 +174,15 @@ public class ScriptListener implements Listener {
|
|||||||
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime);
|
Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static int jumpCommand(ScriptExecutor scriptExecutor, String[] args) {
|
||||||
|
if (args.length < 1) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return scriptExecutor.jumpPoints.getOrDefault(args[0], -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void infoCommand(ScriptExecutor scriptExecutor, String[] args) {
|
||||||
|
scriptExecutor.player.sendMessage(ChatColor.translateAlternateColorCodes('&', String.join(" ", args)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren