diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java index 97913661..f8d73d37 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java @@ -22,18 +22,29 @@ package de.steamwar.bausystem.features.script; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.core.VersionedCallable; +import net.minecraft.server.v1_15_R1.PacketPlayOutCommands; +import org.bukkit.entity.HumanEntity; +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 java.util.HashMap; +import java.util.Map; + @Linked(LinkageType.LISTENER) public class CustomCommandListener implements Listener { - @EventHandler - public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { - for (ItemStack item : e.getPlayer().getInventory().getContents()) { + private Map> playerMap = new HashMap<>(); + + private void updateInventory(Player p) { + playerMap.remove(p); + for (ItemStack item : p.getInventory().getContents()) { if (item == null || isNoBook(item) || item.getItemMeta() == null) { continue; } @@ -49,14 +60,41 @@ public class CustomCommandListener implements Listener { if (!s.startsWith("#!CMD ")) { continue; } - if (s.substring(6).equals(e.getMessage())) { - e.setCancelled(true); - new ScriptExecutor(bookMeta, e.getPlayer()); - break; - } + playerMap.computeIfAbsent(p, player -> new HashMap<>()).put(s.substring(6), bookMeta); } } + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + updateInventory(event.getPlayer()); + } + + @EventHandler + public void onPlayerQuit(PlayerQuitEvent e) { + playerMap.remove(e.getPlayer()); + } + + @EventHandler + public void onInventoryClose(InventoryCloseEvent e) { + if (e.getPlayer() instanceof Player) { + updateInventory((Player) e.getPlayer()); + } + } + + @EventHandler + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { + Map bookMetaMap = playerMap.get(e.getPlayer()); + if (bookMetaMap == null) { + return; + } + BookMeta bookMeta = bookMetaMap.get(e.getMessage()); + if (bookMeta == null) { + return; + } + e.setCancelled(true); + new ScriptExecutor(bookMeta, e.getPlayer()); + } + private boolean isNoBook(ItemStack item) { return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15)); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/world/GetMaterial.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/world/GetMaterial.java new file mode 100644 index 00000000..2f71b529 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/world/GetMaterial.java @@ -0,0 +1,75 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.script.command.world; + +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.Value; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import org.bukkit.Bukkit; +import org.bukkit.World; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class GetMaterial implements SpecialCommand { + + private final World world = Bukkit.getWorlds().get(0); + + @Override + public String[] description() { + return new String[]{ + "§egetmaterial §8<§7Variable§8> §8<§7Value§8(§7s§8)§8> §8<§7Value§8(§7s§8)§8> §8[§7Value§8(§7s§8)§8]", + "", + "Schreibt das material von einem Block in der Welt in die Variable." + }; + } + + @Override + public String command() { + return "getmaterial"; + } + + @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; + } + if (command.length <= 3) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein"); + return true; + } + if (command.length <= 4) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein"); + return true; + } + + int x = (int) scriptExecutor.getOrItselfValue(command[2]).asLong(); + int y = (int) scriptExecutor.getOrItselfValue(command[3]).asLong(); + int z = (int) scriptExecutor.getOrItselfValue(command[4]).asLong(); + scriptExecutor.getLocalVariables().putValue(command[1], new Value.StringValue(world.getBlockAt(x, y, z).getType().name())); + return false; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/world/SetMaterial.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/world/SetMaterial.java new file mode 100644 index 00000000..d64b9795 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/world/SetMaterial.java @@ -0,0 +1,79 @@ +/* + * 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 . + */ + +package de.steamwar.bausystem.features.script.command.world; + +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; +import org.bukkit.Material; +import org.bukkit.World; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class SetMaterial implements SpecialCommand { + + private final World world = Bukkit.getWorlds().get(0); + + @Override + public String[] description() { + return new String[]{ + "§esetmaterial §8<§7Variable§8> §8<§7Value§8(§7s§8)§8> §8<§7Value§8(§7s§8)§8> §8[§7Value§8(§7s§8)§8]", + "", + "Setzt an einem Block in der Welt das Material der ersten Variable." + }; + } + + @Override + public String command() { + return "setmaterial"; + } + + @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; + } + if (command.length <= 3) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein"); + return true; + } + if (command.length <= 4) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein"); + return true; + } + + int x = (int) scriptExecutor.getOrItselfValue(command[2]).asLong(); + int y = (int) scriptExecutor.getOrItselfValue(command[3]).asLong(); + int z = (int) scriptExecutor.getOrItselfValue(command[4]).asLong(); + try { + world.getBlockAt(x, y, z).setType(Material.valueOf(scriptExecutor.getOrItself(command[1]))); + } catch (Exception e) { + // Ignored + } + return false; + } +}