Add Exit
Add Sleep Add LinkageType.SCRIPT_COMMAND
Dieser Commit ist enthalten in:
Ursprung
f14165dc68
Commit
098d621508
@ -1,4 +0,0 @@
|
||||
package de.steamwar.bausystem.features.script;
|
||||
|
||||
public interface Command {
|
||||
}
|
@ -1,12 +1,161 @@
|
||||
package de.steamwar.bausystem.features.script;
|
||||
|
||||
import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.features.script.variables.Constants;
|
||||
import de.steamwar.bausystem.features.script.variables.Context;
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public final class ScriptExecutor {
|
||||
|
||||
public ScriptExecutor(BookMeta bookMeta, Player player) {
|
||||
public static final Set<SpecialCommand> SPECIAL_COMMANDS = new HashSet<>();
|
||||
|
||||
@Getter
|
||||
private final Player player;
|
||||
|
||||
@Getter
|
||||
private final Context localVariables = new Context();
|
||||
|
||||
@Getter
|
||||
private final Context globalVariables;
|
||||
|
||||
private final List<String> commands = new ArrayList<>();
|
||||
|
||||
@Getter
|
||||
public final Map<String, Integer> jumpPoints = new HashMap<>();
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
private int index = 0;
|
||||
|
||||
public ScriptExecutor(BookMeta bookMeta, Player player) {
|
||||
this.player = player;
|
||||
globalVariables = ScriptListener.getGlobalContext(player);
|
||||
|
||||
for (String page : bookMeta.getPages()) {
|
||||
for (String command : page.split("\n")) {
|
||||
command = command.replaceAll(" +", " ");
|
||||
if (command.startsWith("#") || command.trim().isEmpty()) continue;
|
||||
if (command.startsWith(".")) {
|
||||
jumpPoints.put(command.substring(1), commands.size());
|
||||
continue;
|
||||
}
|
||||
commands.add(command);
|
||||
}
|
||||
}
|
||||
if (commands.isEmpty()) return;
|
||||
resume();
|
||||
}
|
||||
|
||||
public void resume() {
|
||||
if (!player.isOnline()) {
|
||||
return;
|
||||
}
|
||||
|
||||
int executionPoints = 0;
|
||||
|
||||
while (index < commands.size()) {
|
||||
String command = commands.get(index++);
|
||||
if (executionPoints++ > 200) {
|
||||
player.sendMessage(BauSystem.PREFIX + "§cFüge ein sleep in dein Script ein");
|
||||
return;
|
||||
}
|
||||
|
||||
String[] strings = generateStringArray(command);
|
||||
if (strings.length == 0) {
|
||||
return;
|
||||
}
|
||||
boolean found = false;
|
||||
for (SpecialCommand specialCommand : SPECIAL_COMMANDS) {
|
||||
if (specialCommand.command().equalsIgnoreCase(strings[0])) {
|
||||
found = true;
|
||||
if (!specialCommand.execute(strings, this)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (found) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command);
|
||||
Bukkit.getServer().getPluginManager().callEvent(preprocessEvent);
|
||||
if (preprocessEvent.isCancelled()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Variable Replaces in commands.
|
||||
command = String.join(" ", strings);
|
||||
|
||||
Bukkit.getLogger().log(Level.INFO, player.getName() + " dispatched command: " + command);
|
||||
Bukkit.getServer().dispatchCommand(player, command);
|
||||
}
|
||||
}
|
||||
|
||||
private String[] generateStringArray(String command) {
|
||||
return replaceVariables(command.replaceAll(" +", " ").split(" "));
|
||||
}
|
||||
|
||||
private String[] replaceVariables(String[] args) {
|
||||
String s = String.join(" ", args);
|
||||
Set<String> variables = new HashSet<>(localVariables.allVariables());
|
||||
variables.addAll(Constants.allVariables());
|
||||
variables.addAll(globalVariables.allVariables());
|
||||
|
||||
for (String variable : variables) {
|
||||
s = s.replace("<" + variable + ">", getValue(variable) + "");
|
||||
}
|
||||
for (String constVariable : Constants.allVariables()) {
|
||||
s = s.replace("<const." + constVariable + ">", getConstant(constVariable) + "");
|
||||
}
|
||||
for (String localVariable : localVariables.allVariables()) {
|
||||
s = s.replace("<local." + localVariable + ">", getLocal(localVariable) + "");
|
||||
}
|
||||
for (String globalVariable : globalVariables.allVariables()) {
|
||||
s = s.replace("<global." + globalVariable + ">", getGlobal(globalVariable) + "");
|
||||
}
|
||||
return s.split(" ");
|
||||
}
|
||||
|
||||
public String getValue(String variable) {
|
||||
if (Constants.isConstant(variable)) {
|
||||
return Constants.getConstant(variable, player).asString();
|
||||
}
|
||||
if (globalVariables.hasValue(variable)) {
|
||||
return globalVariables.getValue(variable).asString();
|
||||
}
|
||||
if (localVariables.hasValue(variable)) {
|
||||
return localVariables.getValue(variable).asString();
|
||||
}
|
||||
return "0";
|
||||
}
|
||||
|
||||
public String getConstant(String variable) {
|
||||
if (Constants.isConstant(variable)) {
|
||||
return Constants.getConstant(variable, player).asString();
|
||||
}
|
||||
return "0";
|
||||
}
|
||||
|
||||
public String getGlobal(String variable) {
|
||||
if (globalVariables.hasValue(variable)) {
|
||||
return globalVariables.getValue(variable).asString();
|
||||
}
|
||||
return "0";
|
||||
}
|
||||
|
||||
public String getLocal(String variable) {
|
||||
if (localVariables.hasValue(variable)) {
|
||||
return localVariables.getValue(variable).asString();
|
||||
}
|
||||
return "0";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package de.steamwar.bausystem.features.script;
|
||||
|
||||
import de.steamwar.bausystem.features.script.variables.Context;
|
||||
import de.steamwar.bausystem.linkage.LinkageType;
|
||||
import de.steamwar.bausystem.linkage.Linked;
|
||||
import de.steamwar.core.VersionedCallable;
|
||||
@ -9,15 +10,21 @@ import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.block.Action;
|
||||
import org.bukkit.event.player.PlayerInteractEvent;
|
||||
import org.bukkit.event.player.PlayerJoinEvent;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@Linked(LinkageType.LISTENER)
|
||||
public class ScriptListener implements Listener {
|
||||
|
||||
private static final Map<Player, Context> GLOBAL_CONTEXT = new HashMap<>();
|
||||
|
||||
private Set<Player> playerSet = new HashSet<>();
|
||||
|
||||
@EventHandler(priority = EventPriority.HIGH)
|
||||
@ -48,4 +55,17 @@ public class ScriptListener implements Listener {
|
||||
return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15));
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
GLOBAL_CONTEXT.put(event.getPlayer(), new Context());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
GLOBAL_CONTEXT.remove(event.getPlayer());
|
||||
}
|
||||
|
||||
public static Context getGlobalContext(Player player) {
|
||||
return GLOBAL_CONTEXT.get(player);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
package de.steamwar.bausystem.features.script;
|
||||
|
||||
public interface SpecialCommand {
|
||||
|
||||
default String description() {
|
||||
return "";
|
||||
}
|
||||
|
||||
String command();
|
||||
|
||||
boolean execute(String[] command, ScriptExecutor scriptExecutor);
|
||||
|
||||
default long asLong(String value) {
|
||||
try {
|
||||
return Long.parseLong(value);
|
||||
} catch (NumberFormatException e) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
default boolean asBoolean(String value) {
|
||||
return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
|
||||
}
|
||||
|
||||
default String asString(String value) {
|
||||
return value;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package de.steamwar.bausystem.features.script.command;
|
||||
|
||||
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
||||
import de.steamwar.bausystem.features.script.SpecialCommand;
|
||||
import de.steamwar.bausystem.linkage.LinkageType;
|
||||
import de.steamwar.bausystem.linkage.Linked;
|
||||
|
||||
@Linked(LinkageType.SCRIPT_COMMAND)
|
||||
public class Exit implements SpecialCommand {
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "§eexit §8-§7 Beendet das ausführen des Scripts";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String command() {
|
||||
return "exit";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String[] command, ScriptExecutor scriptExecutor) {
|
||||
return false;
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package de.steamwar.bausystem.features.script.command;
|
||||
|
||||
import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
||||
import de.steamwar.bausystem.features.script.SpecialCommand;
|
||||
import de.steamwar.bausystem.linkage.LinkageType;
|
||||
import de.steamwar.bausystem.linkage.Linked;
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
@Linked(LinkageType.SCRIPT_COMMAND)
|
||||
public class Sleep implements SpecialCommand {
|
||||
|
||||
@Override
|
||||
public String description() {
|
||||
return "§esleep §8-§7 Pausiert das Ausführen des Scripts. Das erste Argument ist eine Zahl und gibt die GameTicks an, die pausiert werden sollen.";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String command() {
|
||||
return "sleep";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(String[] command, ScriptExecutor scriptExecutor) {
|
||||
if (command.length == 0) {
|
||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlte");
|
||||
return true;
|
||||
}
|
||||
long sleepTime = asLong(command[1]);
|
||||
if (sleepTime < 0) {
|
||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDie Zeit muss eine Zahl großer 0 sein.");
|
||||
return true;
|
||||
}
|
||||
if (sleepTime == 0) {
|
||||
return true;
|
||||
}
|
||||
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), scriptExecutor::resume, sleepTime);
|
||||
return false;
|
||||
}
|
||||
}
|
@ -11,6 +11,7 @@ import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
@UtilityClass
|
||||
@ -45,6 +46,10 @@ public class Constants {
|
||||
});
|
||||
}
|
||||
|
||||
public Set<String> allVariables() {
|
||||
return CONSTANTS.keySet();
|
||||
}
|
||||
|
||||
public boolean isConstant(String variableName) {
|
||||
return CONSTANTS.containsKey(variableName);
|
||||
}
|
||||
|
@ -21,6 +21,8 @@ package de.steamwar.bausystem.linkage;
|
||||
|
||||
import de.steamwar.bausystem.BauSystem;
|
||||
import de.steamwar.bausystem.features.gui.BauGUI;
|
||||
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
||||
import de.steamwar.bausystem.features.script.SpecialCommand;
|
||||
import de.steamwar.command.SWCommand;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
@ -42,7 +44,8 @@ public enum LinkageType {
|
||||
PLAIN(1, false, clazz -> true),
|
||||
LISTENER(2, false, Listener.class::isAssignableFrom, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())),
|
||||
UNLINK_LISTENER(2, true, Listener.class::isAssignableFrom, o -> HandlerList.unregisterAll((Listener) o)),
|
||||
GUI_ITEM(3, false, GuiItem.class::isAssignableFrom, o -> BauGUI.addItem((GuiItem) o));
|
||||
GUI_ITEM(3, false, GuiItem.class::isAssignableFrom, o -> BauGUI.addItem((GuiItem) o)),
|
||||
SCRIPT_COMMAND(4, false, SpecialCommand.class::isAssignableFrom, o -> ScriptExecutor.SPECIAL_COMMANDS.add((SpecialCommand) o));
|
||||
|
||||
private final int order;
|
||||
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren