From f0e38afb8e67c25364d10eb9848e44c4e0560877 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 7 Jul 2021 20:58:51 +0200 Subject: [PATCH] Add CustomCommandListener Signed-off-by: yoyosource --- .../script/CustomCommandListener.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java new file mode 100644 index 00000000..97913661 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java @@ -0,0 +1,63 @@ +/* + * 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; + +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.core.VersionedCallable; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; + +@Linked(LinkageType.LISTENER) +public class CustomCommandListener implements Listener { + + @EventHandler + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { + for (ItemStack item : e.getPlayer().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; + } + if (s.substring(6).equals(e.getMessage())) { + e.setCancelled(true); + new ScriptExecutor(bookMeta, e.getPlayer()); + break; + } + } + } + + private boolean isNoBook(ItemStack item) { + return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15)); + } +}