diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/world/InventoryListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/world/InventoryListener.java new file mode 100644 index 00000000..a4e2490d --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/world/InventoryListener.java @@ -0,0 +1,71 @@ +/* + * 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.world; + +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import de.steamwar.core.Core; +import org.bukkit.Material; +import org.bukkit.attribute.Attribute; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +@Linked(LinkageType.LISTENER) +public class InventoryListener implements Listener { + + @EventHandler + public void onInventoryClick(InventoryClickEvent e) { + ItemStack stack = e.getCursor(); + if (stack == null || !stack.hasItemMeta()) { + return; + } + assert stack.getItemMeta() != null; + if (stack.getItemMeta().hasEnchants()) { + for (Enchantment en : Enchantment.values()) { + if (stack.getEnchantmentLevel(en) > en.getMaxLevel()) { + stack.removeEnchantment(en); + } + } + } + + Material material = stack.getType(); + if (material == Material.POTION || material == Material.SPLASH_POTION || material == Material.LINGERING_POTION) { + stack.setType(Material.MILK_BUCKET); + } + + if (Core.getVersion() < 15) { + e.setCurrentItem(stack); + return; + } + + if (stack.getItemMeta().hasAttributeModifiers()) { + ItemMeta meta = stack.getItemMeta(); + for (Attribute a : Attribute.values()) { + meta.removeAttributeModifier(a); + } + stack.setItemMeta(meta); + } + e.setCurrentItem(stack); + } +}