ScriptEvents #45
@ -19,38 +19,16 @@
|
|||||||
|
|
||||||
package de.steamwar.bausystem.features.gui;
|
package de.steamwar.bausystem.features.gui;
|
||||||
|
|
||||||
import de.steamwar.bausystem.BauSystem;
|
|
||||||
import de.steamwar.bausystem.linkage.LinkageType;
|
import de.steamwar.bausystem.linkage.LinkageType;
|
||||||
import de.steamwar.bausystem.linkage.Linked;
|
import de.steamwar.bausystem.linkage.Linked;
|
||||||
import org.bukkit.Bukkit;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
import org.bukkit.event.block.Action;
|
import org.bukkit.event.block.Action;
|
||||||
import org.bukkit.event.player.PlayerInteractEvent;
|
import org.bukkit.event.player.PlayerInteractEvent;
|
||||||
import org.bukkit.event.player.PlayerSwapHandItemsEvent;
|
|
||||||
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@Linked(LinkageType.LISTENER)
|
@Linked(LinkageType.LISTENER)
|
||||||
public class BauGuiListener implements Listener {
|
public class BauGuiListener implements Listener {
|
||||||
|
|
||||||
private static final Set<Player> LAST_FS = new HashSet<>();
|
|
||||||
|
|
||||||
public BauGuiListener() {
|
|
||||||
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), LAST_FS::clear, 20, 20);
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
|
|
||||||
if (LAST_FS.contains(event.getPlayer())) {
|
|
||||||
BauGUI.openBauGui(event.getPlayer());
|
|
||||||
} else {
|
|
||||||
LAST_FS.add(event.getPlayer());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
@EventHandler
|
||||||
public void onPlayerInteract(PlayerInteractEvent event) {
|
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||||
if (event.getItem() == null) {
|
if (event.getItem() == null) {
|
||||||
|
@ -1,342 +0,0 @@
|
|||||||
/*
|
|
||||||
* This file is a part of the SteamWar software.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2021 SteamWar.de-Serverteam
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package de.steamwar.bausystem.features.script;
|
|
||||||
|
|
||||||
import de.steamwar.bausystem.SWUtils;
|
|
||||||
import de.steamwar.bausystem.features.script.variables.Value;
|
|
||||||
import de.steamwar.bausystem.linkage.LinkageType;
|
|
||||||
import de.steamwar.bausystem.linkage.Linked;
|
|
||||||
import de.steamwar.core.VersionedCallable;
|
|
||||||
import de.steamwar.inventory.SWItem;
|
|
||||||
import de.steamwar.inventory.SWListInv;
|
|
||||||
import de.steamwar.sql.UserConfig;
|
|
||||||
import lombok.AccessLevel;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.bukkit.Material;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.event.EventHandler;
|
|
||||||
import org.bukkit.event.Listener;
|
|
||||||
import org.bukkit.event.inventory.InventoryCloseEvent;
|
|
||||||
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
|
||||||
import org.bukkit.event.player.PlayerJoinEvent;
|
|
||||||
import org.bukkit.event.player.PlayerQuitEvent;
|
|
||||||
import org.bukkit.inventory.ItemStack;
|
|
||||||
import org.bukkit.inventory.meta.BookMeta;
|
|
||||||
import yapion.hierarchy.output.LengthOutput;
|
|
||||||
import yapion.hierarchy.output.StringOutput;
|
|
||||||
import yapion.hierarchy.types.YAPIONArray;
|
|
||||||
import yapion.hierarchy.types.YAPIONMap;
|
|
||||||
import yapion.hierarchy.types.YAPIONObject;
|
|
||||||
import yapion.hierarchy.types.YAPIONValue;
|
|
||||||
import yapion.parser.YAPIONParser;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Linked(LinkageType.LISTENER)
|
|
||||||
public class CustomCommandListener implements Listener {
|
|
||||||
|
|
||||||
private interface CustomCommand {
|
|
||||||
default Map<String, Value> check(String[] args, String[] command) {
|
|
||||||
if (args.length < command.length) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!args[0].equals(command[0])) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<String, Value> arguments = new HashMap<>();
|
|
||||||
if (!check(arguments, args, command, 0, 0)) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
return arguments;
|
|
||||||
}
|
|
||||||
|
|
||||||
default boolean check(Map<String, Value> arguments, String[] args, String[] command, int argsIndex, int commandIndex) {
|
|
||||||
if (command.length <= commandIndex) {
|
|
||||||
for (int i = argsIndex; i < args.length; i++) {
|
|
||||||
if (!(args[i].startsWith("(") && args[i].endsWith(")"))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (args.length <= argsIndex) return true;
|
|
||||||
|
|
||||||
String currentArg = args[argsIndex];
|
|
||||||
String currentCommand = command[commandIndex];
|
|
||||||
|
|
||||||
if (currentArg.startsWith("<") && currentArg.endsWith(">")) {
|
|
||||||
arguments.put(trim(currentArg, 1), new Value.StringValue(currentCommand));
|
|
||||||
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
|
||||||
} else if (currentArg.startsWith("(<") && currentArg.endsWith(">)")) {
|
|
||||||
arguments.put(trim(currentArg, 2), new Value.StringValue(currentCommand));
|
|
||||||
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
|
||||||
} else if (currentArg.startsWith("(") && currentArg.endsWith(")")) {
|
|
||||||
if (!trim(currentArg, 1).equals(currentCommand)) {
|
|
||||||
arguments.put(trim(currentArg, 1), new Value.BooleanValue(false));
|
|
||||||
return check(arguments, args, command, argsIndex + 1, commandIndex);
|
|
||||||
} else {
|
|
||||||
arguments.put(trim(currentArg, 1), new Value.BooleanValue(true));
|
|
||||||
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!currentArg.equals(currentCommand)) return false;
|
|
||||||
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
default String trim(String s, int count) {
|
|
||||||
return s.substring(count, s.length() - count);
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean execute(String[] command, PlayerCommandPreprocessEvent e);
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
|
||||||
private static class InventoryCommand implements CustomCommand {
|
|
||||||
private final BookMeta bookMeta;
|
|
||||||
private final String[] args;
|
|
||||||
|
|
||||||
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
|
|
||||||
Map<String, Value> arguments = check(args, command);
|
|
||||||
if (arguments == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
e.setCancelled(true);
|
|
||||||
new ScriptExecutor(bookMeta, e.getPlayer(), arguments);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
|
|
||||||
private static class MenuCommand implements CustomCommand {
|
|
||||||
private final List<String> pages;
|
|
||||||
private final String[] args;
|
|
||||||
|
|
||||||
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
|
|
||||||
Map<String, Value> arguments = check(args, command);
|
|
||||||
if (arguments == null) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
e.setCancelled(true);
|
|
||||||
new ScriptExecutor(pages, e.getPlayer(), arguments);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void toYAPION(YAPIONMap yapionMap) {
|
|
||||||
YAPIONArray yapionArray = new YAPIONArray();
|
|
||||||
pages.forEach(yapionArray::add);
|
|
||||||
yapionMap.put(String.join(" ", args), yapionArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStack toItem() {
|
|
||||||
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, String.join(" ", args));
|
|
||||||
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
|
|
||||||
bookMeta.setPages(pages.toArray(new String[0]));
|
|
||||||
swItem.setItemMeta(bookMeta);
|
|
||||||
return swItem.getItemStack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Map<Player, List<CustomCommand>> playerMap = new HashMap<>();
|
|
||||||
|
|
||||||
private void updateInventory(Player p) {
|
|
||||||
playerMap.computeIfPresent(p, (player, customCommands) -> {
|
|
||||||
customCommands.removeIf(InventoryCommand.class::isInstance);
|
|
||||||
return customCommands;
|
|
||||||
});
|
|
||||||
for (ItemStack item : p.getInventory().getContents()) {
|
|
||||||
if (item == null || isNoBook(item) || item.getItemMeta() == null) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
BookMeta bookMeta = ((BookMeta) item.getItemMeta());
|
|
||||||
if (bookMeta.getPageCount() == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (bookMeta.getPage(1).isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
String s = bookMeta.getPage(1).split("\n")[0];
|
|
||||||
if (!s.startsWith("#!CMD /")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryCommand(bookMeta, s.substring(6).split(" ")));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
|
||||||
Player p = event.getPlayer();
|
|
||||||
updateInventory(p);
|
|
||||||
|
|
||||||
String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-commands");
|
|
||||||
YAPIONObject yapionObject;
|
|
||||||
if (s == null) {
|
|
||||||
yapionObject = new YAPIONObject();
|
|
||||||
} else {
|
|
||||||
yapionObject = YAPIONParser.parse(s);
|
|
||||||
}
|
|
||||||
|
|
||||||
yapionObject.getYAPIONMapOrSetDefault("", new YAPIONMap()).forEach((key, value) -> {
|
|
||||||
String[] command = ((YAPIONValue<String>) key).get().split(" ");
|
|
||||||
List<String> pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList());
|
|
||||||
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuCommand(pages, command));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onPlayerQuit(PlayerQuitEvent e) {
|
|
||||||
save(e.getPlayer());
|
|
||||||
playerMap.remove(e.getPlayer());
|
|
||||||
}
|
|
||||||
|
|
||||||
private YAPIONObject output(Player p) {
|
|
||||||
if (!playerMap.containsKey(p)) return new YAPIONObject();
|
|
||||||
YAPIONObject yapionObject = new YAPIONObject();
|
|
||||||
YAPIONMap yapionMap = new YAPIONMap();
|
|
||||||
yapionObject.add("", yapionMap);
|
|
||||||
playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> {
|
|
||||||
menuCommand.toYAPION(yapionMap);
|
|
||||||
});
|
|
||||||
return yapionObject;
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean save(Player p) {
|
|
||||||
if (!playerMap.containsKey(p)) {
|
|
||||||
UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-commands");
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
YAPIONObject yapionObject = output(p);
|
|
||||||
if (yapionObject.toYAPION(new LengthOutput()).getLength() > 64 * 1024) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
UserConfig.updatePlayerConfig(p.getUniqueId(), "bausystem-commands", yapionObject.toYAPION(new StringOutput()).getResult());
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onInventoryClose(InventoryCloseEvent e) {
|
|
||||||
if (e.getPlayer() instanceof Player) {
|
|
||||||
updateInventory((Player) e.getPlayer());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@EventHandler
|
|
||||||
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
|
|
||||||
if (e.getMessage().startsWith("/script:")) {
|
|
||||||
e.setMessage("/" + e.getMessage().substring(8));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
List<CustomCommand> inventoryCommands = playerMap.get(e.getPlayer());
|
|
||||||
if (inventoryCommands == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
String[] command = e.getMessage().split(" ");
|
|
||||||
for (CustomCommand customCommand : inventoryCommands) {
|
|
||||||
if (customCommand.execute(command, e)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private boolean isNoBook(ItemStack item) {
|
|
||||||
return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void openCommandsMenu(Player p) {
|
|
||||||
List<SWListInv.SWListEntry<MenuCommand>> menuCommands = new ArrayList<>();
|
|
||||||
playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> {
|
|
||||||
String command = "§e" + String.join(" ", menuCommand.args);
|
|
||||||
|
|
||||||
menuCommands.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, command, Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren"), false, clickType -> {
|
|
||||||
}), menuCommand));
|
|
||||||
});
|
|
||||||
int length = (int) output(p).toYAPION(new LengthOutput()).getLength();
|
|
||||||
StringBuilder menuName = new StringBuilder();
|
|
||||||
menuName.append("§eScript Commands ");
|
|
||||||
double percentage = ((int) ((length / 655336.0) * 1000)) / 10.0;
|
|
||||||
if (percentage > 99) {
|
|
||||||
menuName.append("§c");
|
|
||||||
} else if (percentage >= 75) {
|
|
||||||
menuName.append("§6");
|
|
||||||
} else {
|
|
||||||
menuName.append("§a");
|
|
||||||
}
|
|
||||||
menuName.append(percentage).append("§7%");
|
|
||||||
SWListInv<MenuCommand> menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> {
|
|
||||||
if (!clickType.isCreativeAction()) {
|
|
||||||
playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand);
|
|
||||||
}
|
|
||||||
SWUtils.giveItemToPlayer(p, menuCommand.toItem());
|
|
||||||
p.closeInventory();
|
|
||||||
save(p);
|
|
||||||
});
|
|
||||||
menuCommandSWListInv.setItem(49, new SWItem(Material.HOPPER, "§eHinzufügen", Arrays.asList("§7Klicke mit einem Buch zum hinzufügen"), false, clickType -> {
|
|
||||||
ItemStack item = p.getItemOnCursor();
|
|
||||||
if (item.getType().isAir()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (isNoBook(item) || item.getItemMeta() == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
BookMeta bookMeta = ((BookMeta) item.getItemMeta());
|
|
||||||
if (bookMeta.getPageCount() == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (bookMeta.getPage(1).isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
String s = bookMeta.getPage(1).split("\n")[0];
|
|
||||||
if (!s.startsWith("#!CMD /")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
MenuCommand menuCommand = new MenuCommand(bookMeta.getPages(), s.substring(6).split(" "));
|
|
||||||
for (CustomCommand customCommand : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) {
|
|
||||||
if (!(customCommand instanceof MenuCommand)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (Arrays.equals(((MenuCommand) customCommand).args, menuCommand.args)) {
|
|
||||||
p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand);
|
|
||||||
if (!save(p)) {
|
|
||||||
playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand);
|
|
||||||
p.closeInventory();
|
|
||||||
SWUtils.giveItemToPlayer(p, p.getItemOnCursor());
|
|
||||||
save(p);
|
|
||||||
p.sendMessage("§cScript CMD-Buch Limit erreicht");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
p.setItemOnCursor(null);
|
|
||||||
openCommandsMenu(p);
|
|
||||||
}));
|
|
||||||
menuCommandSWListInv.open();
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,240 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.bausystem.features.script;
|
||||||
|
|
||||||
|
import de.steamwar.bausystem.features.script.variables.Value;
|
||||||
|
import de.steamwar.inventory.SWItem;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.experimental.UtilityClass;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import yapion.hierarchy.types.YAPIONArray;
|
||||||
|
import yapion.hierarchy.types.YAPIONMap;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
@UtilityClass
|
||||||
|
public class CustomScript {
|
||||||
|
|
||||||
|
public interface Script {
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface MenuScript {
|
||||||
|
void toYAPION(YAPIONMap yapionMap);
|
||||||
|
SWItem toItem();
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface CustomEvent extends Script {
|
||||||
|
String eventName();
|
||||||
|
boolean execute(Event e, Player p, Map<String, Value> variables);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public static class InventoryEvent implements CustomEvent {
|
||||||
|
public final BookMeta bookMeta;
|
||||||
|
public final String[] args;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String eventName() {
|
||||||
|
return args[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(Event e, Player p, Map<String, Value> variables) {
|
||||||
|
new ScriptExecutor(bookMeta, p, variables);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public static class MenuEvent implements CustomEvent, MenuScript {
|
||||||
|
public final List<String> pages;
|
||||||
|
public final String[] args;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String eventName() {
|
||||||
|
return args[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(Event e, Player p, Map<String, Value> variables) {
|
||||||
|
new ScriptExecutor(pages, p, variables);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toYAPION(YAPIONMap yapionMap) {
|
||||||
|
YAPIONArray yapionArray = new YAPIONArray();
|
||||||
|
pages.forEach(yapionArray::add);
|
||||||
|
yapionMap.put(String.join(" ", args), yapionArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SWItem toItem() {
|
||||||
|
StringBuilder st = new StringBuilder();
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
if (i != 0) st.append(" ");
|
||||||
|
if (i == 0) st.append("§e");
|
||||||
|
st.append(args[i]);
|
||||||
|
if (i == 0) st.append("§8 -§7");
|
||||||
|
}
|
||||||
|
|
||||||
|
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: " + st);
|
||||||
|
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
|
||||||
|
bookMeta.setPages(pages.toArray(new String[0]));
|
||||||
|
swItem.setItemMeta(bookMeta);
|
||||||
|
return swItem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface CustomCommand extends Script {
|
||||||
|
default Map<String, Value> check(String[] args, String[] command) {
|
||||||
|
if (args.length < command.length) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!args[0].equals(command[0])) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<String, Value> arguments = new HashMap<>();
|
||||||
|
if (!check(arguments, args, command, 0, 0)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return arguments;
|
||||||
|
}
|
||||||
|
|
||||||
|
default boolean check(Map<String, Value> arguments, String[] args, String[] command, int argsIndex, int commandIndex) {
|
||||||
|
if (command.length <= commandIndex) {
|
||||||
|
for (int i = argsIndex; i < args.length; i++) {
|
||||||
|
if (!(args[i].startsWith("(") && args[i].endsWith(")"))) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (args.length <= argsIndex) return true;
|
||||||
|
|
||||||
|
String currentArg = args[argsIndex];
|
||||||
|
String currentCommand = command[commandIndex];
|
||||||
|
|
||||||
|
if (currentArg.startsWith("<") && currentArg.endsWith(">")) {
|
||||||
|
arguments.put(trim(currentArg, 1), new Value.StringValue(currentCommand));
|
||||||
|
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
||||||
|
} else if (currentArg.startsWith("(<") && currentArg.endsWith(">)")) {
|
||||||
|
arguments.put(trim(currentArg, 2), new Value.StringValue(currentCommand));
|
||||||
|
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
||||||
|
} else if (currentArg.startsWith("(") && currentArg.endsWith(")")) {
|
||||||
|
if (!trim(currentArg, 1).equals(currentCommand)) {
|
||||||
|
arguments.put(trim(currentArg, 1), new Value.BooleanValue(false));
|
||||||
|
return check(arguments, args, command, argsIndex + 1, commandIndex);
|
||||||
|
} else {
|
||||||
|
arguments.put(trim(currentArg, 1), new Value.BooleanValue(true));
|
||||||
|
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!currentArg.equals(currentCommand)) return false;
|
||||||
|
return check(arguments, args, command, argsIndex + 1, commandIndex + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
default String trim(String s, int count) {
|
||||||
|
return s.substring(count, s.length() - count);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean execute(String[] command, PlayerCommandPreprocessEvent e);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public static class InventoryCommand implements CustomCommand {
|
||||||
|
public final BookMeta bookMeta;
|
||||||
|
public final String[] args;
|
||||||
|
|
||||||
|
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
|
||||||
|
Map<String, Value> arguments = check(args, command);
|
||||||
|
if (arguments == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
e.setCancelled(true);
|
||||||
|
new ScriptExecutor(bookMeta, e.getPlayer(), arguments);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public static class MenuCommand implements CustomCommand, MenuScript {
|
||||||
|
public final List<String> pages;
|
||||||
|
public final String[] args;
|
||||||
|
|
||||||
|
public boolean execute(String[] command, PlayerCommandPreprocessEvent e) {
|
||||||
|
Map<String, Value> arguments = check(args, command);
|
||||||
|
if (arguments == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
e.setCancelled(true);
|
||||||
|
new ScriptExecutor(pages, e.getPlayer(), arguments);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean equals(MenuCommand menuCommand) {
|
||||||
|
if (menuCommand.args.length != args.length) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
for (int i = 0; i < args.length; i++) {
|
||||||
|
if (i == 0) continue;
|
||||||
|
String s1 = args[i];
|
||||||
|
String s2 = menuCommand.args[i];
|
||||||
|
if (s1.equals(s2)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (s1.startsWith("(<") && s1.endsWith(">)") && s2.startsWith("(<") && s2.endsWith(">)")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void toYAPION(YAPIONMap yapionMap) {
|
||||||
|
YAPIONArray yapionArray = new YAPIONArray();
|
||||||
|
pages.forEach(yapionArray::add);
|
||||||
|
yapionMap.put(String.join(" ", args), yapionArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SWItem toItem() {
|
||||||
|
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Command§8: §e" + String.join(" ", args));
|
||||||
|
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
|
||||||
|
bookMeta.setPages(pages.toArray(new String[0]));
|
||||||
|
swItem.setItemMeta(bookMeta);
|
||||||
|
return swItem;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,385 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.bausystem.features.script;
|
||||||
|
|
||||||
|
import de.steamwar.bausystem.BauSystem;
|
||||||
|
import de.steamwar.bausystem.SWUtils;
|
||||||
|
import de.steamwar.bausystem.features.script.variables.Value;
|
||||||
|
import de.steamwar.bausystem.linkage.LinkageType;
|
||||||
|
import de.steamwar.bausystem.linkage.Linked;
|
||||||
|
import de.steamwar.core.VersionedCallable;
|
||||||
|
import de.steamwar.inventory.SWItem;
|
||||||
|
import de.steamwar.inventory.SWListInv;
|
||||||
|
import de.steamwar.sql.UserConfig;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.event.Cancellable;
|
||||||
|
import org.bukkit.event.Event;
|
||||||
|
import org.bukkit.event.EventHandler;
|
||||||
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.event.block.Action;
|
||||||
|
import org.bukkit.event.block.BlockBreakEvent;
|
||||||
|
import org.bukkit.event.block.BlockPlaceEvent;
|
||||||
|
import org.bukkit.event.inventory.InventoryCloseEvent;
|
||||||
|
import org.bukkit.event.player.*;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
import org.bukkit.inventory.meta.BookMeta;
|
||||||
|
import yapion.hierarchy.output.LengthOutput;
|
||||||
|
import yapion.hierarchy.output.StringOutput;
|
||||||
|
import yapion.hierarchy.types.YAPIONArray;
|
||||||
|
import yapion.hierarchy.types.YAPIONMap;
|
||||||
|
import yapion.hierarchy.types.YAPIONObject;
|
||||||
|
import yapion.hierarchy.types.YAPIONValue;
|
||||||
|
import yapion.parser.YAPIONParser;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Linked(LinkageType.LISTENER)
|
||||||
|
public class CustomScriptListener implements Listener {
|
||||||
|
|
||||||
|
private Map<Player, List<CustomScript.Script>> playerMap = new HashMap<>();
|
||||||
|
|
||||||
|
private void updateInventory(Player p) {
|
||||||
|
playerMap.computeIfPresent(p, (player, customCommands) -> {
|
||||||
|
customCommands.removeIf(script -> !(script instanceof CustomScript.MenuScript));
|
||||||
|
return customCommands;
|
||||||
|
});
|
||||||
|
for (ItemStack item : p.getInventory().getContents()) {
|
||||||
|
if (item == null || isNoBook(item) || item.getItemMeta() == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookMeta bookMeta = ((BookMeta) item.getItemMeta());
|
||||||
|
if (bookMeta.getPageCount() == 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (bookMeta.getPage(1).isEmpty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
String s = bookMeta.getPage(1).split("\n")[0];
|
||||||
|
if (s.startsWith("#!CMD /")) {
|
||||||
|
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" ")));
|
||||||
|
} else if (s.startsWith("#!EVENT ")) {
|
||||||
|
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryEvent(bookMeta, s.substring(8).split(" ")));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerJoin(PlayerJoinEvent e) {
|
||||||
|
Player p = e.getPlayer();
|
||||||
|
updateInventory(p);
|
||||||
|
|
||||||
|
String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-scripts");
|
||||||
|
if (s == null) {
|
||||||
|
s = UserConfig.getConfig(p.getUniqueId(), "bausystem-commands");
|
||||||
|
UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-commands");
|
||||||
|
}
|
||||||
|
YAPIONObject yapionObject;
|
||||||
|
if (s == null) {
|
||||||
|
yapionObject = new YAPIONObject();
|
||||||
|
yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui"));
|
||||||
|
} else {
|
||||||
|
yapionObject = YAPIONParser.parse(s);
|
||||||
|
if (yapionObject.containsKey("")) {
|
||||||
|
yapionObject.add("commands", yapionObject.getMap(""));
|
||||||
|
yapionObject.remove("");
|
||||||
|
yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
yapionObject.getYAPIONMapOrSetDefault("commands", new YAPIONMap()).forEach((key, value) -> {
|
||||||
|
String[] command = ((YAPIONValue<String>) key).get().split(" ");
|
||||||
|
List<String> pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList());
|
||||||
|
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuCommand(pages, command));
|
||||||
|
});
|
||||||
|
|
||||||
|
yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> {
|
||||||
|
String[] event = ((YAPIONValue<String>) key).get().split(" ");
|
||||||
|
List<String> pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList());
|
||||||
|
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuEvent(pages, event));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerQuit(PlayerQuitEvent e) {
|
||||||
|
save(e.getPlayer());
|
||||||
|
playerMap.remove(e.getPlayer());
|
||||||
|
}
|
||||||
|
|
||||||
|
private YAPIONObject output(Player p) {
|
||||||
|
if (!playerMap.containsKey(p)) return new YAPIONObject();
|
||||||
|
YAPIONObject yapionObject = new YAPIONObject();
|
||||||
|
YAPIONMap commandsMap = new YAPIONMap();
|
||||||
|
yapionObject.add("commands", commandsMap);
|
||||||
|
playerMap.get(p).stream().filter(CustomScript.MenuCommand.class::isInstance).map(CustomScript.MenuCommand.class::cast).forEach(menuCommand -> {
|
||||||
|
menuCommand.toYAPION(commandsMap);
|
||||||
|
});
|
||||||
|
YAPIONMap eventsMap = new YAPIONMap();
|
||||||
|
yapionObject.add("events", eventsMap);
|
||||||
|
playerMap.get(p).stream().filter(CustomScript.MenuEvent.class::isInstance).map(CustomScript.MenuEvent.class::cast).forEach(menuCommand -> {
|
||||||
|
menuCommand.toYAPION(eventsMap);
|
||||||
|
});
|
||||||
|
return yapionObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean save(Player p) {
|
||||||
|
if (!playerMap.containsKey(p)) {
|
||||||
|
UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-scripts");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
YAPIONObject yapionObject = output(p);
|
||||||
|
if (yapionObject.toYAPION(new LengthOutput()).getLength() > 64 * 1024) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
UserConfig.updatePlayerConfig(p.getUniqueId(), "bausystem-scripts", yapionObject.toYAPION(new StringOutput()).getResult());
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onInventoryClose(InventoryCloseEvent e) {
|
||||||
|
if (e.getPlayer() instanceof Player) {
|
||||||
|
updateInventory((Player) e.getPlayer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isNoBook(ItemStack item) {
|
||||||
|
return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void openCommandsMenu(Player p) {
|
||||||
|
List<SWListInv.SWListEntry<CustomScript.MenuScript>> menuCommands = new ArrayList<>();
|
||||||
|
playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> {
|
||||||
|
SWItem swItem = menuItem.toItem();
|
||||||
|
ItemStack itemStack = swItem.getItemStack();
|
||||||
|
if (menuItem instanceof CustomScript.MenuEvent) {
|
||||||
|
itemStack.setType(Material.REPEATING_COMMAND_BLOCK);
|
||||||
|
} else {
|
||||||
|
itemStack.setType(Material.COMMAND_BLOCK);
|
||||||
|
}
|
||||||
|
swItem.setItemStack(itemStack);
|
||||||
|
swItem.setLore(Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren"));
|
||||||
|
|
||||||
|
menuCommands.add(new SWListInv.SWListEntry<>(swItem, menuItem));
|
||||||
|
});
|
||||||
|
|
||||||
|
int length = (int) output(p).toYAPION(new LengthOutput()).getLength();
|
||||||
|
StringBuilder menuName = new StringBuilder();
|
||||||
|
menuName.append("§eScript Commands ");
|
||||||
|
double percentage = ((int) ((length / 655336.0) * 1000)) / 10.0;
|
||||||
|
if (percentage > 99) {
|
||||||
|
menuName.append("§c");
|
||||||
|
} else if (percentage >= 75) {
|
||||||
|
menuName.append("§6");
|
||||||
|
} else {
|
||||||
|
menuName.append("§a");
|
||||||
|
}
|
||||||
|
menuName.append(percentage).append("§7%");
|
||||||
|
|
||||||
|
SWListInv<CustomScript.MenuScript> menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> {
|
||||||
|
if (!clickType.isCreativeAction()) {
|
||||||
|
playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand);
|
||||||
|
}
|
||||||
|
SWUtils.giveItemToPlayer(p, menuCommand.toItem().getItemStack());
|
||||||
|
p.closeInventory();
|
||||||
|
save(p);
|
||||||
|
});
|
||||||
|
menuCommandSWListInv.setItem(49, new SWItem(Material.HOPPER, "§eHinzufügen", Arrays.asList("§7Klicke mit einem Buch zum hinzufügen"), false, clickType -> {
|
||||||
|
ItemStack item = p.getItemOnCursor();
|
||||||
|
if (item.getType().isAir()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (isNoBook(item) || item.getItemMeta() == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
BookMeta bookMeta = ((BookMeta) item.getItemMeta());
|
||||||
|
if (bookMeta.getPageCount() == 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (bookMeta.getPage(1).isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String s = bookMeta.getPage(1).split("\n")[0];
|
||||||
|
if (s.startsWith("#!CMD /")) {
|
||||||
|
CustomScript.MenuCommand menuCommand = new CustomScript.MenuCommand(bookMeta.getPages(), s.substring(6).split(" "));
|
||||||
|
for (CustomScript.Script script : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) {
|
||||||
|
if (!(script instanceof CustomScript.MenuCommand)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (((CustomScript.MenuCommand) script).equals(menuCommand)) {
|
||||||
|
p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scriptBookLimit(p, menuCommand);
|
||||||
|
} else if (s.startsWith("#!EVENT ")) {
|
||||||
|
CustomScript.MenuEvent menuEvent = new CustomScript.MenuEvent(bookMeta.getPages(), s.substring(8).split(" "));
|
||||||
|
try {
|
||||||
|
EventType.valueOf(menuEvent.eventName());
|
||||||
|
} catch (Exception e) {
|
||||||
|
p.sendMessage("§cEvent '" + menuEvent.eventName() + "' ist nicht definierbar");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
scriptBookLimit(p, menuEvent);
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
menuCommandSWListInv.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void scriptBookLimit(Player p, CustomScript.Script menuScript) {
|
||||||
|
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuScript);
|
||||||
|
if (!save(p)) {
|
||||||
|
playerMap.get(p).removeIf(script -> script == menuScript);
|
||||||
|
p.closeInventory();
|
||||||
|
SWUtils.giveItemToPlayer(p, p.getItemOnCursor());
|
||||||
|
save(p);
|
||||||
|
p.sendMessage("§cScript-Buch Limit erreicht");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
p.setItemOnCursor(null);
|
||||||
|
openCommandsMenu(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public enum EventType {
|
||||||
|
FF(PlayerSwapHandItemsEvent.class, event -> null),
|
||||||
|
PlaceBlock(BlockPlaceEvent.class, event -> {
|
||||||
|
Map<String, Value> valueMap = new HashMap<>();
|
||||||
|
valueMap.put("blockX", new Value.LongValue(event.getBlockPlaced().getX()));
|
||||||
|
valueMap.put("blockY", new Value.LongValue(event.getBlockPlaced().getY()));
|
||||||
|
valueMap.put("blockZ", new Value.LongValue(event.getBlockPlaced().getZ()));
|
||||||
|
valueMap.put("blockType", new Value.StringValue(event.getBlockPlaced().getType().name()));
|
||||||
|
return valueMap;
|
||||||
|
}),
|
||||||
|
BreakBlock(BlockBreakEvent.class, event -> {
|
||||||
|
Map<String, Value> valueMap = new HashMap<>();
|
||||||
|
valueMap.put("blockX", new Value.LongValue(event.getBlock().getX()));
|
||||||
|
valueMap.put("blockY", new Value.LongValue(event.getBlock().getY()));
|
||||||
|
valueMap.put("blockZ", new Value.LongValue(event.getBlock().getZ()));
|
||||||
|
valueMap.put("blockType", new Value.StringValue(event.getBlock().getType().name()));
|
||||||
|
return valueMap;
|
||||||
|
}),
|
||||||
|
RightClick(PlayerInteractEvent.class, event -> {
|
||||||
|
Map<String, Value> valueMap = new HashMap<>();
|
||||||
|
valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand()));
|
||||||
|
return valueMap;
|
||||||
|
}),
|
||||||
|
LeftClick(PlayerInteractEvent.class, event -> {
|
||||||
|
Map<String, Value> valueMap = new HashMap<>();
|
||||||
|
valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand()));
|
||||||
|
return valueMap;
|
||||||
|
});
|
||||||
|
|
||||||
|
private Class<? extends Event> eventType;
|
||||||
|
private Function<Event, Map<String, Value>> eventValues;
|
||||||
|
|
||||||
|
<T extends Event> EventType(Class<T> eventType, Function<T, Map<String, Value>> eventValues) {
|
||||||
|
this.eventType = eventType;
|
||||||
|
this.eventValues = event -> eventValues.apply((T) event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void callEvent(EventType eventType, Player p, Event e) {
|
||||||
|
if (!eventType.getEventType().equals(e.getClass())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<CustomScript.CustomEvent> customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList());
|
||||||
|
for (CustomScript.CustomEvent customEvent : customEvents) {
|
||||||
|
if (customEvent.eventName().equals(eventType.name())) {
|
||||||
|
Map<String, Value> variables = eventType.getEventValues().apply(e);
|
||||||
|
if (variables == null) {
|
||||||
|
variables = new HashMap<>();
|
||||||
|
}
|
||||||
|
if (e instanceof Cancellable) {
|
||||||
|
variables.put("cancel", new Value.BooleanValue(false));
|
||||||
|
}
|
||||||
|
customEvent.execute(e, p, variables);
|
||||||
|
if (variables.containsKey("cancel")) {
|
||||||
|
Value value = variables.get("cancel");
|
||||||
|
if (value.asBoolean()) {
|
||||||
|
((Cancellable) e).setCancelled(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// EventListener for Commands as well as Events
|
||||||
|
|
||||||
|
// Event Command
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
|
||||||
|
if (e.getMessage().startsWith("/script:")) {
|
||||||
|
e.setMessage("/" + e.getMessage().substring(8));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<CustomScript.CustomCommand> customCommands = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList());
|
||||||
|
String[] command = e.getMessage().split(" ");
|
||||||
|
for (CustomScript.CustomCommand customCommand : customCommands) {
|
||||||
|
if (customCommand.execute(command, e)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Event FF
|
||||||
|
private static final Set<Player> LAST_FS = new HashSet<>();
|
||||||
|
|
||||||
|
{
|
||||||
|
Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), LAST_FS::clear, 20, 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) {
|
||||||
|
if (LAST_FS.contains(event.getPlayer())) {
|
||||||
|
callEvent(EventType.FF, event.getPlayer(), event);
|
||||||
|
} else {
|
||||||
|
LAST_FS.add(event.getPlayer());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBlockPlace(BlockPlaceEvent event) {
|
||||||
|
callEvent(EventType.PlaceBlock, event.getPlayer(), event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onBlockBreak(BlockBreakEvent event) {
|
||||||
|
callEvent(EventType.BreakBlock, event.getPlayer(), event);
|
||||||
|
}
|
||||||
|
|
||||||
|
@EventHandler
|
||||||
|
public void onPlayerInteract(PlayerInteractEvent event) {
|
||||||
|
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
|
||||||
|
callEvent(EventType.RightClick, event.getPlayer(), event);
|
||||||
|
}
|
||||||
|
if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) {
|
||||||
|
callEvent(EventType.LeftClick, event.getPlayer(), event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,7 +24,7 @@ public class ScriptCommand extends SWCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@LinkedInstance
|
@LinkedInstance
|
||||||
private CustomCommandListener customCommandListener = null;
|
private CustomScriptListener customScriptListener = null;
|
||||||
|
|
||||||
private static List<SWListInv.SWListEntry<SpecialCommand>> swItems = new ArrayList<>();
|
private static List<SWListInv.SWListEntry<SpecialCommand>> swItems = new ArrayList<>();
|
||||||
|
|
||||||
@ -41,9 +41,9 @@ public class ScriptCommand extends SWCommand {
|
|||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
}
|
}
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht (<>) wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht '<>' wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen. Jedes Event kann durch", "§7'var cancel true' gecancelt werden.", "§7Hinter dem Event Namen stehen die Variablen,", "§7welche im Script durch das Event nutztbar sind.", "§7Nutzbare Events sind:", "§eFF", "§ePlaceBlock §8-§7 blockX, blockY, blockZ, blockType", "§eBreakBlock §8-§7 blockX, blockY, blockZ, blockType", "§eRightClick §8-§7 blockInHand", "§eLeftClick §8-§7 blockInHand"), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
@ -80,7 +80,7 @@ public class ScriptCommand extends SWCommand {
|
|||||||
});
|
});
|
||||||
swItems.add(new SWListInv.SWListEntry<>(swItem, specialCommand));
|
swItems.add(new SWListInv.SWListEntry<>(swItem, specialCommand));
|
||||||
});
|
});
|
||||||
for (int i = 0; i < 9 + 4; i++) {
|
for (int i = 0; i < 9 + 3; i++) {
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
}
|
}
|
||||||
@ -104,17 +104,25 @@ public class ScriptCommand extends SWCommand {
|
|||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.OBSIDIAN, "§7Constant §eprotect", Arrays.asList("§etrue§7 wenn Protect angeschaltet ist."), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.OBSIDIAN, "§7Constant §eprotect", Arrays.asList("§etrue§7 wenn Protect angeschaltet ist."), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers."), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers."), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers."), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.NAME_TAG, "§7Constant §ename", Arrays.asList("§eDisplay§7 Name des Spielers."), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.NAME_TAG, "§7Constant §ename", Arrays.asList("§eDisplay§7 Name des Spielers."), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BOOTS, "§7Constant §esneak", Arrays.asList("§etrue§7 wenn der Spieler gerade sneakt."), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BOOTS, "§7Constant §esneak", Arrays.asList("§etrue§7 wenn der Spieler gerade sneakt."), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
for (int i = 0; i < 6 + 2 * 9; i++) {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.DIAMOND_BOOTS, "§7Constant §esprinting", Arrays.asList("§etrue§7 wenn der Spieler gerade rennt."), false, clickType -> {
|
||||||
|
}), null));
|
||||||
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, "§7Constant §eslot", Arrays.asList("§e0-8§7 für den ausgewählten slot.", "§eÜberschreibbar"), false, clickType -> {
|
||||||
|
}), null));
|
||||||
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRASS_BLOCK, "§7Constant §eslotmaterial", Arrays.asList("§eMaterial§7 des Items im Slot"), false, clickType -> {
|
||||||
|
}), null));
|
||||||
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BLOCK, "§7Constant §eoffhandmaterial", Arrays.asList("§eMaterial§7 des Items in oder Off Hand"), false, clickType -> {
|
||||||
|
}), null));
|
||||||
|
for (int i = 0; i < 2 + 2 * 9; i++) {
|
||||||
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> {
|
||||||
}), null));
|
}), null));
|
||||||
}
|
}
|
||||||
@ -134,6 +142,6 @@ public class ScriptCommand extends SWCommand {
|
|||||||
|
|
||||||
@Register({"menu"})
|
@Register({"menu"})
|
||||||
public void menuGUICommand(Player p) {
|
public void menuGUICommand(Player p) {
|
||||||
customCommandListener.openCommandsMenu(p);
|
customScriptListener.openCommandsMenu(p);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,10 @@ public final class ScriptExecutor {
|
|||||||
this(bookMeta, player, new HashMap<>());
|
this(bookMeta, player, new HashMap<>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ScriptExecutor(List<String> pages, Player player) {
|
||||||
|
this(pages, player, new HashMap<>());
|
||||||
|
}
|
||||||
|
|
||||||
public ScriptExecutor(BookMeta bookMeta, Player player, Map<String, Value> localVariables) {
|
public ScriptExecutor(BookMeta bookMeta, Player player, Map<String, Value> localVariables) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
globalVariables = ScriptListener.getGlobalContext(player);
|
globalVariables = ScriptListener.getGlobalContext(player);
|
||||||
|
@ -44,11 +44,11 @@ public class Add implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.LongValue.class) {
|
if (!(v1 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.LongValue.class) {
|
if (!(v2 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -44,11 +44,11 @@ public class Div implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.LongValue.class) {
|
if (!(v1 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.LongValue.class) {
|
if (!(v2 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -44,11 +44,11 @@ public class Mul implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.LongValue.class) {
|
if (!(v1 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.LongValue.class) {
|
if (!(v2 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -44,11 +44,11 @@ public class Sub implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.LongValue.class) {
|
if (!(v1 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.LongValue.class) {
|
if (!(v2 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -44,11 +44,11 @@ public class And implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.BooleanValue.class) {
|
if (!(v1 instanceof Value.BooleanValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.BooleanValue.class) {
|
if (!(v2 instanceof Value.BooleanValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -45,7 +45,7 @@ public class Equal implements SpecialCommand {
|
|||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
Value result;
|
Value result;
|
||||||
if (v1.getClass() != v2.getClass()) {
|
if (!v1.getClass().isInstance(v2.getClass()) && !v2.getClass().isInstance(v1.getClass())) {
|
||||||
result = new Value.BooleanValue(false);
|
result = new Value.BooleanValue(false);
|
||||||
} else if (v1.asString().equals(v2.asString())) {
|
} else if (v1.asString().equals(v2.asString())) {
|
||||||
result = new Value.BooleanValue(true);
|
result = new Value.BooleanValue(true);
|
||||||
|
@ -44,11 +44,11 @@ public class Greater implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.LongValue.class) {
|
if (!(v1 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.LongValue.class) {
|
if (!(v2 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -44,11 +44,11 @@ public class Less implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.LongValue.class) {
|
if (!(v1 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.LongValue.class) {
|
if (!(v2 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ public class Not implements SpecialCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.BooleanValue.class) {
|
if (!(v1 instanceof Value.BooleanValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können genichtet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können genichtet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -44,11 +44,11 @@ public class Or implements SpecialCommand {
|
|||||||
|
|
||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
if (v1.getClass() != Value.BooleanValue.class) {
|
if (!(v1 instanceof Value.BooleanValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.BooleanValue.class) {
|
if (!(v2 instanceof Value.BooleanValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -70,15 +70,15 @@ public class Insert implements SpecialCommand {
|
|||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
|
|
||||||
if (v1.getClass() != Value.StringValue.class) {
|
if (!(v1 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.StringValue.class) {
|
if (!(v2 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v3.getClass() != Value.LongValue.class) {
|
if (!(v3 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ public class Length implements SpecialCommand {
|
|||||||
String resultName = command[1];
|
String resultName = command[1];
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
|
|
||||||
if (v2.getClass() != Value.StringValue.class) {
|
if (!(v2 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -65,11 +65,11 @@ public class Remove implements SpecialCommand {
|
|||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
|
|
||||||
if (v1.getClass() != Value.StringValue.class) {
|
if (!(v1 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.StringValue.class) {
|
if (!(v2 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -70,15 +70,15 @@ public class Replace implements SpecialCommand {
|
|||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
|
|
||||||
if (v1.getClass() != Value.StringValue.class) {
|
if (!(v1 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.StringValue.class) {
|
if (!(v2 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v3.getClass() != Value.StringValue.class) {
|
if (!(v3 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -65,11 +65,11 @@ public class Substring implements SpecialCommand {
|
|||||||
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]);
|
||||||
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]);
|
||||||
|
|
||||||
if (v1.getClass() != Value.StringValue.class) {
|
if (!(v1 instanceof Value.StringValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (v2.getClass() != Value.LongValue.class) {
|
if (!(v2 instanceof Value.LongValue)) {
|
||||||
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden");
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,73 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.bausystem.features.script.command.variable;
|
||||||
|
|
||||||
|
import de.steamwar.bausystem.BauSystem;
|
||||||
|
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
||||||
|
import de.steamwar.bausystem.features.script.SpecialCommand;
|
||||||
|
import de.steamwar.bausystem.features.script.variables.Constants;
|
||||||
|
import de.steamwar.bausystem.linkage.LinkageType;
|
||||||
|
import de.steamwar.bausystem.linkage.Linked;
|
||||||
|
import org.bukkit.Material;
|
||||||
|
|
||||||
|
@Linked(LinkageType.SCRIPT_COMMAND)
|
||||||
|
public class Const implements SpecialCommand {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] description() {
|
||||||
|
return new String[]{
|
||||||
|
"§econst §8<§7Variable§8> §8[§7Value§8(§7s§8)§8]",
|
||||||
|
"",
|
||||||
|
"Schreibt in eine Konstante einen Wert rein, welcher eine Zahl sein kann, ein Boolscher Wert oder ein Text."
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Material material() {
|
||||||
|
return Material.STRUCTURE_BLOCK;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String command() {
|
||||||
|
return "const";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean execute(String[] command, ScriptExecutor scriptExecutor) {
|
||||||
|
if (command.length <= 1) {
|
||||||
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlt und sollte eine Variable sein");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (command.length <= 2) {
|
||||||
|
scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
String varName = command[1];
|
||||||
|
StringBuilder varValue = new StringBuilder();
|
||||||
|
for (int i = 2; i < command.length; i++) {
|
||||||
|
if (varValue.length() != 0) {
|
||||||
|
varValue.append(" ");
|
||||||
|
}
|
||||||
|
varValue.append(command[i]);
|
||||||
|
}
|
||||||
|
Constants.getConstant(varName, scriptExecutor.getPlayer()).fromValue(scriptExecutor.parse(varValue.toString()));
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,6 @@ package de.steamwar.bausystem.features.script.command.variable;
|
|||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
||||||
import de.steamwar.bausystem.features.script.SpecialCommand;
|
import de.steamwar.bausystem.features.script.SpecialCommand;
|
||||||
import de.steamwar.bausystem.features.script.variables.Value;
|
|
||||||
import de.steamwar.bausystem.linkage.LinkageType;
|
import de.steamwar.bausystem.linkage.LinkageType;
|
||||||
import de.steamwar.bausystem.linkage.Linked;
|
import de.steamwar.bausystem.linkage.Linked;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
@ -48,17 +47,7 @@ public class Global implements SpecialCommand {
|
|||||||
}
|
}
|
||||||
varValue.append(command[i]);
|
varValue.append(command[i]);
|
||||||
}
|
}
|
||||||
try {
|
scriptExecutor.getGlobalVariables().putValue(varName, scriptExecutor.parse(varValue.toString()));
|
||||||
long value = Long.parseLong(varValue.toString());
|
|
||||||
scriptExecutor.getLocalVariables().putValue(varName, new Value.LongValue(value));
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
String s = varValue.toString();
|
|
||||||
if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) {
|
|
||||||
scriptExecutor.getGlobalVariables().putValue(varName, new Value.BooleanValue(s.equalsIgnoreCase("true")));
|
|
||||||
} else {
|
|
||||||
scriptExecutor.getGlobalVariables().putValue(varName, new Value.StringValue(s));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package de.steamwar.bausystem.features.script.command.variable;
|
|||||||
import de.steamwar.bausystem.BauSystem;
|
import de.steamwar.bausystem.BauSystem;
|
||||||
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
import de.steamwar.bausystem.features.script.ScriptExecutor;
|
||||||
import de.steamwar.bausystem.features.script.SpecialCommand;
|
import de.steamwar.bausystem.features.script.SpecialCommand;
|
||||||
|
import de.steamwar.bausystem.features.script.variables.Constants;
|
||||||
import de.steamwar.bausystem.features.script.variables.Value;
|
import de.steamwar.bausystem.features.script.variables.Value;
|
||||||
import de.steamwar.bausystem.linkage.LinkageType;
|
import de.steamwar.bausystem.linkage.LinkageType;
|
||||||
import de.steamwar.bausystem.linkage.Linked;
|
import de.steamwar.bausystem.linkage.Linked;
|
||||||
|
@ -8,54 +8,212 @@ import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode;
|
|||||||
import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode;
|
import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode;
|
||||||
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
|
import de.steamwar.bausystem.region.flags.flagvalues.TNTMode;
|
||||||
import lombok.experimental.UtilityClass;
|
import lombok.experimental.UtilityClass;
|
||||||
|
import org.bukkit.Location;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
@UtilityClass
|
@UtilityClass
|
||||||
public class Constants {
|
public class Constants {
|
||||||
|
|
||||||
private final Map<String, Function<Player, Value>> CONSTANTS = new HashMap<>();
|
private final Map<String, Function<Player, Value>> CONSTANTS = new HashMap<>();
|
||||||
|
|
||||||
|
private static class ConstantLongValue extends Value.LongValue {
|
||||||
|
|
||||||
|
private Supplier<Long> longSupplier;
|
||||||
|
private Consumer<Long> longConsumer = ignored -> {};
|
||||||
|
|
||||||
|
public ConstantLongValue(Supplier<Long> longSupplier) {
|
||||||
|
super(longSupplier.get());
|
||||||
|
this.longSupplier = longSupplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConstantLongValue(Supplier<Long> longSupplier, Consumer<Long> longConsumer) {
|
||||||
|
super(longSupplier.get());
|
||||||
|
this.longSupplier = longSupplier;
|
||||||
|
this.longConsumer = longConsumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long asLong() {
|
||||||
|
value = longSupplier.get();
|
||||||
|
return super.asLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean asBoolean() {
|
||||||
|
value = longSupplier.get();
|
||||||
|
return super.asBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String asString() {
|
||||||
|
value = longSupplier.get();
|
||||||
|
return super.asString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromValue(Value value) {
|
||||||
|
super.fromValue(value);
|
||||||
|
longConsumer.accept(this.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ConstantBooleanValue extends Value.BooleanValue {
|
||||||
|
|
||||||
|
private Supplier<Boolean> booleanSupplier;
|
||||||
|
private Consumer<Boolean> booleanConsumer = ignored -> {};
|
||||||
|
|
||||||
|
public ConstantBooleanValue(Supplier<Boolean> booleanSupplier) {
|
||||||
|
super(booleanSupplier.get());
|
||||||
|
this.booleanSupplier = booleanSupplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConstantBooleanValue(Supplier<Boolean> booleanSupplier, Consumer<Boolean> booleanConsumer) {
|
||||||
|
super(booleanSupplier.get());
|
||||||
|
this.booleanSupplier = booleanSupplier;
|
||||||
|
this.booleanConsumer = booleanConsumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long asLong() {
|
||||||
|
value = booleanSupplier.get();
|
||||||
|
return super.asLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean asBoolean() {
|
||||||
|
value = booleanSupplier.get();
|
||||||
|
return super.asBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String asString() {
|
||||||
|
value = booleanSupplier.get();
|
||||||
|
return super.asString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromValue(Value value) {
|
||||||
|
super.fromValue(value);
|
||||||
|
booleanConsumer.accept(this.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ConstantStringValue extends Value.StringValue {
|
||||||
|
|
||||||
|
private Supplier<String> stringSupplier;
|
||||||
|
private Consumer<String> stringConsumer = ignored -> {};
|
||||||
|
|
||||||
|
public ConstantStringValue(Supplier<String> stringSupplier) {
|
||||||
|
super(stringSupplier.get());
|
||||||
|
this.stringSupplier = stringSupplier;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ConstantStringValue(Supplier<String> stringSupplier, Consumer<String> stringConsumer) {
|
||||||
|
super(stringSupplier.get());
|
||||||
|
this.stringSupplier = stringSupplier;
|
||||||
|
this.stringConsumer = stringConsumer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long asLong() {
|
||||||
|
value = stringSupplier.get();
|
||||||
|
return super.asLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean asBoolean() {
|
||||||
|
value = stringSupplier.get();
|
||||||
|
return super.asBoolean();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String asString() {
|
||||||
|
value = stringSupplier.get();
|
||||||
|
return super.asString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void fromValue(Value value) {
|
||||||
|
super.fromValue(value);
|
||||||
|
stringConsumer.accept(this.value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static {
|
static {
|
||||||
CONSTANTS.put("trace", player -> {
|
CONSTANTS.put("trace", player -> {
|
||||||
return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isTracing());
|
return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isTracing);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("autotrace", player -> {
|
CONSTANTS.put("autotrace", player -> {
|
||||||
return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isAutoTrace());
|
return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isAutoTrace);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("tnt", player -> {
|
CONSTANTS.put("tnt", player -> {
|
||||||
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY);
|
return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("tnt-onlytb", player -> {
|
CONSTANTS.put("tnt-onlytb", player -> {
|
||||||
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB);
|
return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("freeze", player -> {
|
CONSTANTS.put("freeze", player -> {
|
||||||
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE);
|
return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("fire", player -> {
|
CONSTANTS.put("fire", player -> {
|
||||||
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW);
|
return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("protect", player -> {
|
CONSTANTS.put("protect", player -> {
|
||||||
return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE);
|
return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("x", player -> {
|
CONSTANTS.put("x", player -> {
|
||||||
return new Value.LongValue(player.getLocation().getBlockX());
|
return new ConstantLongValue(() -> (long) player.getLocation().getBlockX(), aLong -> {
|
||||||
|
Location location = player.getLocation();
|
||||||
|
location.setX((double) aLong);
|
||||||
|
player.teleport(location);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
CONSTANTS.put("y", player -> {
|
CONSTANTS.put("y", player -> {
|
||||||
return new Value.LongValue(player.getLocation().getBlockY());
|
return new ConstantLongValue(() -> (long) player.getLocation().getBlockY(), aLong -> {
|
||||||
|
Location location = player.getLocation();
|
||||||
|
location.setY((double) aLong);
|
||||||
|
player.teleport(location);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
CONSTANTS.put("z", player -> {
|
CONSTANTS.put("z", player -> {
|
||||||
return new Value.LongValue(player.getLocation().getBlockZ());
|
return new ConstantLongValue(() -> (long) player.getLocation().getBlockZ(), aLong -> {
|
||||||
|
Location location = player.getLocation();
|
||||||
|
location.setZ((double) aLong);
|
||||||
|
player.teleport(location);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
CONSTANTS.put("name", player -> {
|
CONSTANTS.put("name", player -> {
|
||||||
return new Value.StringValue(player.getDisplayName());
|
return new ConstantStringValue(player::getDisplayName);
|
||||||
});
|
});
|
||||||
CONSTANTS.put("sneaking", player -> {
|
CONSTANTS.put("sneaking", player -> {
|
||||||
return new Value.BooleanValue(player.isSneaking());
|
return new ConstantBooleanValue(player::isSneaking);
|
||||||
|
});
|
||||||
|
CONSTANTS.put("sprinting", player -> {
|
||||||
|
return new ConstantBooleanValue(player::isSprinting);
|
||||||
|
});
|
||||||
|
CONSTANTS.put("slot", player -> {
|
||||||
|
return new ConstantLongValue(() -> (long) player.getInventory().getHeldItemSlot(), slot -> {
|
||||||
|
if (slot > 8) {
|
||||||
|
slot = 8L;
|
||||||
|
}
|
||||||
|
if (slot < 0) {
|
||||||
|
slot = 0L;
|
||||||
|
}
|
||||||
|
player.getInventory().setHeldItemSlot((int) (long) slot);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
CONSTANTS.put("slotmaterial", player -> {
|
||||||
|
return new ConstantStringValue(() -> player.getInventory().getItemInMainHand().getType().name());
|
||||||
|
});
|
||||||
|
CONSTANTS.put("offhandmaterial", player -> {
|
||||||
|
return new ConstantStringValue(() -> player.getInventory().getItemInOffHand().getType().name());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,5 +228,4 @@ public class Constants {
|
|||||||
public Value getConstant(String variableName, Player player) {
|
public Value getConstant(String variableName, Player player) {
|
||||||
return CONSTANTS.get(variableName).apply(player);
|
return CONSTANTS.get(variableName).apply(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,11 @@ public class Context {
|
|||||||
private Map<String, Value> variables = new HashMap<>();
|
private Map<String, Value> variables = new HashMap<>();
|
||||||
|
|
||||||
public void putValue(String variableName, Value value) {
|
public void putValue(String variableName, Value value) {
|
||||||
variables.put(variableName, value);
|
if (variables.containsKey(variableName)) {
|
||||||
|
variables.get(variableName).fromValue(value);
|
||||||
|
} else {
|
||||||
|
variables.put(variableName, value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeValue(String variableName) {
|
public void removeValue(String variableName) {
|
||||||
|
@ -8,15 +8,13 @@ public interface Value {
|
|||||||
boolean asBoolean();
|
boolean asBoolean();
|
||||||
String asString();
|
String asString();
|
||||||
|
|
||||||
void fromLong(long value);
|
void fromValue(Value value);
|
||||||
void fromBoolean(boolean value);
|
|
||||||
void fromString(String value);
|
|
||||||
|
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@ToString
|
@ToString
|
||||||
class LongValue implements Value {
|
class LongValue implements Value {
|
||||||
|
|
||||||
private long value;
|
protected long value;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long asLong() {
|
public long asLong() {
|
||||||
@ -34,22 +32,8 @@ public interface Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fromLong(long value) {
|
public void fromValue(Value value) {
|
||||||
this.value = value;
|
this.value = value.asLong();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fromBoolean(boolean value) {
|
|
||||||
this.value = value ? 1 : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fromString(String value) {
|
|
||||||
try {
|
|
||||||
this.value = Long.parseLong(value);
|
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
this.value = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,7 +41,7 @@ public interface Value {
|
|||||||
@ToString
|
@ToString
|
||||||
class BooleanValue implements Value {
|
class BooleanValue implements Value {
|
||||||
|
|
||||||
private boolean value;
|
protected boolean value;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long asLong() {
|
public long asLong() {
|
||||||
@ -75,18 +59,8 @@ public interface Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fromLong(long value) {
|
public void fromValue(Value value) {
|
||||||
this.value = value != 0;
|
this.value = value.asBoolean();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fromBoolean(boolean value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fromString(String value) {
|
|
||||||
this.value = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,7 +68,7 @@ public interface Value {
|
|||||||
@ToString
|
@ToString
|
||||||
class StringValue implements Value {
|
class StringValue implements Value {
|
||||||
|
|
||||||
private String value;
|
protected String value;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long asLong() {
|
public long asLong() {
|
||||||
@ -116,18 +90,8 @@ public interface Value {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void fromLong(long value) {
|
public void fromValue(Value value) {
|
||||||
this.value = value + "";
|
this.value = value.asString();
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fromBoolean(boolean value) {
|
|
||||||
this.value = value + "";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void fromString(String value) {
|
|
||||||
this.value = value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
In neuem Issue referenzieren
Einen Benutzer sperren