From 2ab4dcbc11e9468930c62d8415c69f55e2156877 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Mon, 15 Jun 2020 19:22:00 -0400 Subject: [PATCH] Add option to remove vanishing items from keepinv users (#3328) --- .../essentials/EssentialsEntityListener.java | 53 +++++++++++++++++++ .../com/earth2me/essentials/ISettings.java | 10 ++++ .../src/com/earth2me/essentials/Settings.java | 34 ++++++++++++ Essentials/src/config.yml | 12 +++++ 4 files changed, 109 insertions(+) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsEntityListener.java b/Essentials/src/com/earth2me/essentials/EssentialsEntityListener.java index bad908628..4efa3a56b 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsEntityListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsEntityListener.java @@ -1,8 +1,10 @@ package com.earth2me.essentials; +import com.earth2me.essentials.utils.VersionUtil; import net.ess3.api.IEssentials; import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.*; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -174,6 +176,57 @@ public class EssentialsEntityListener implements Listener { if (user.isAuthorized("essentials.keepinv")) { event.setKeepInventory(true); event.getDrops().clear(); + ISettings.KeepInvPolicy vanish = ess.getSettings().getVanishingItemsPolicy(); + ISettings.KeepInvPolicy bind = ess.getSettings().getBindingItemsPolicy(); + if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_11_2_R01) && (vanish != ISettings.KeepInvPolicy.KEEP || bind != ISettings.KeepInvPolicy.KEEP)) { + for (ItemStack stack : event.getEntity().getInventory()) { + if (stack != null) { + if (stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE)) { + if (vanish == ISettings.KeepInvPolicy.DELETE) { + event.getEntity().getInventory().remove(stack); + } else if (vanish == ISettings.KeepInvPolicy.DROP) { + event.getDrops().add(stack); + event.getEntity().getInventory().remove(stack); + } + } + if (stack.getEnchantments().containsKey(Enchantment.BINDING_CURSE)) { + if (bind == ISettings.KeepInvPolicy.DELETE) { + event.getEntity().getInventory().remove(stack); + } else if (bind == ISettings.KeepInvPolicy.DROP) { + event.getEntity().getInventory().remove(stack); + event.getDrops().add(stack); + } + } + } + } + ItemStack[] armor = event.getEntity().getInventory().getArmorContents(); + for (int i = 0; i < armor.length; i++) { + ItemStack stack = armor[i]; + if (stack != null) { + if (stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE)) { + if (vanish == ISettings.KeepInvPolicy.DELETE) { + armor[i] = null; + } else if (vanish == ISettings.KeepInvPolicy.DROP) { + if (!event.getDrops().contains(stack)) { + event.getDrops().add(stack); + } + armor[i] = null; + } + } + if (stack.getEnchantments().containsKey(Enchantment.BINDING_CURSE)) { + if (bind == ISettings.KeepInvPolicy.DELETE) { + armor[i] = null; + } else if (bind == ISettings.KeepInvPolicy.DROP) { + if (!event.getDrops().contains(stack)) { + event.getDrops().add(stack); + } + armor[i] = null; + } + } + } + } + event.getEntity().getInventory().setArmorContents(armor); + } } } diff --git a/Essentials/src/com/earth2me/essentials/ISettings.java b/Essentials/src/com/earth2me/essentials/ISettings.java index 81365a4a1..7fa544f83 100644 --- a/Essentials/src/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/com/earth2me/essentials/ISettings.java @@ -204,6 +204,16 @@ public interface ISettings extends IConf { boolean areDeathMessagesEnabled(); + KeepInvPolicy getVanishingItemsPolicy(); + + KeepInvPolicy getBindingItemsPolicy(); + + enum KeepInvPolicy { + KEEP, + DELETE, + DROP + } + void setDebug(boolean debug); Set getNoGodWorlds(); diff --git a/Essentials/src/com/earth2me/essentials/Settings.java b/Essentials/src/com/earth2me/essentials/Settings.java index 0582d2bef..5fb42dacf 100644 --- a/Essentials/src/com/earth2me/essentials/Settings.java +++ b/Essentials/src/com/earth2me/essentials/Settings.java @@ -571,6 +571,8 @@ public class Settings implements net.ess3.api.ISettings { nickBlacklist = _getNickBlacklist(); maxProjectileSpeed = _getMaxProjectileSpeed(); removeEffectsOnHeal = _isRemovingEffectsOnHeal(); + vanishingItemPolicy = _getVanishingItemsPolicy(); + bindingItemPolicy = _getBindingItemsPolicy(); } void _lateLoadItemSpawnBlacklist() { @@ -977,6 +979,38 @@ public class Settings implements net.ess3.api.ISettings { return config.getBoolean("death-messages", true); } + private KeepInvPolicy vanishingItemPolicy; + + public KeepInvPolicy _getVanishingItemsPolicy() { + String value = config.getString("vanishing-items-policy", "keep").toLowerCase(Locale.ENGLISH); + try { + return KeepInvPolicy.valueOf(value.toUpperCase(Locale.ENGLISH)); + } catch (IllegalArgumentException e) { + return KeepInvPolicy.KEEP; + } + } + + @Override + public KeepInvPolicy getVanishingItemsPolicy() { + return vanishingItemPolicy; + } + + private KeepInvPolicy bindingItemPolicy; + + public KeepInvPolicy _getBindingItemsPolicy() { + String value = config.getString("binding-items-policy", "keep").toLowerCase(Locale.ENGLISH); + try { + return KeepInvPolicy.valueOf(value.toUpperCase(Locale.ENGLISH)); + } catch (IllegalArgumentException e) { + return KeepInvPolicy.KEEP; + } + } + + @Override + public KeepInvPolicy getBindingItemsPolicy() { + return bindingItemPolicy; + } + private Set noGodWorlds = new HashSet<>(); @Override diff --git a/Essentials/src/config.yml b/Essentials/src/config.yml index dfc48e6c4..d1681dcfd 100644 --- a/Essentials/src/config.yml +++ b/Essentials/src/config.yml @@ -459,6 +459,18 @@ broadcast-afk-message: true # You can disable the death messages of Minecraft here. death-messages: true +# How should essentials handle players with the essentials.keepinv permission who have items with +# curse of vanishing when they die? +# You can set this to "keep" (to keep the item), "drop" (to drop the item), or "delete" (to delete the item). +# Defaults to "keep" +vanishing-items-policy: keep + +# How should essentials handle players with the essentials.keepinv permission who have items with +# curse of binding when they die? +# You can set this to "keep" (to keep the item), "drop" (to drop the item), or "delete" (to delete the item). +# Defaults to "keep" +binding-items-policy: keep + # When players die, should they receive the coordinates they died at? send-info-after-death: false