From c5824e41c394e04cbe90db9e9b4b67f5b9a6fefe Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Fri, 31 Dec 2021 11:25:21 -0500 Subject: [PATCH 1/2] remove code from Ancient Athens --- .../craftbukkit/InventoryWorkaround.java | 81 +++++-------------- 1 file changed, 19 insertions(+), 62 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java b/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java index 2abe5e24dea..1e4062cb20f 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java +++ b/Essentials/src/main/java/com/earth2me/essentials/craftbukkit/InventoryWorkaround.java @@ -1,5 +1,6 @@ package com.earth2me.essentials.craftbukkit; +import com.earth2me.essentials.utils.VersionUtil; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -15,15 +16,13 @@ /* * This class can be removed when /~https://github.com/Bukkit/CraftBukkit/pull/193 is accepted to CraftBukkit */ - public final class InventoryWorkaround { /* Spigot 1.9, for whatever reason, decided to merge the armor and main player inventories without providing a way to access the main inventory. There's lots of ugly code in here to work around that. */ private static final int USABLE_PLAYER_INV_SIZE = 36; - // Hot-ish code so cache - private static Boolean hasMainHandSupport = null; + private static final boolean IS_OFFHAND = VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_9_R01); private InventoryWorkaround() { } @@ -201,87 +200,45 @@ public static Map addOversizedItems(final Inventory inventor @SuppressWarnings("deprecation") public static void setItemInMainHand(final Player p, final ItemStack item) { - if (hasMainHandSupport == null) { - try { - p.getInventory().setItemInMainHand(item); - hasMainHandSupport = true; - } catch (final Throwable e) { - p.setItemInHand(item); - hasMainHandSupport = false; - } + if (IS_OFFHAND) { + p.getInventory().setItemInMainHand(item); } else { - if (hasMainHandSupport) { - p.getInventory().setItemInMainHand(item); - } else { - p.setItemInHand(item); - } + p.setItemInHand(item); } } @SuppressWarnings("deprecation") public static void setItemInMainHand(final EntityEquipment invent, final ItemStack item) { - if (hasMainHandSupport == null) { - try { - invent.setItemInMainHand(item); - hasMainHandSupport = true; - } catch (final Throwable e) { - invent.setItemInHand(item); - hasMainHandSupport = false; - } + if (IS_OFFHAND) { + invent.setItemInMainHand(item); } else { - if (hasMainHandSupport) { - invent.setItemInMainHand(item); - } else { - invent.setItemInHand(item); - } + invent.setItemInHand(item); } } @SuppressWarnings("deprecation") public static void setItemInMainHandDropChance(final EntityEquipment invent, final float chance) { - if (hasMainHandSupport == null) { - try { - invent.setItemInMainHandDropChance(chance); - hasMainHandSupport = true; - } catch (final Throwable e) { - invent.setItemInHandDropChance(chance); - hasMainHandSupport = false; - } + if (IS_OFFHAND) { + invent.setItemInMainHandDropChance(chance); } else { - if (hasMainHandSupport) { - invent.setItemInMainHandDropChance(chance); - } else { - invent.setItemInHandDropChance(chance); - } + invent.setItemInHandDropChance(chance); } } public static void setItemInOffHand(final Player p, final ItemStack item) { - // This assumes that all builds that support a main hand also support an off hand. - if (hasMainHandSupport == null || hasMainHandSupport) { - try { - p.getInventory().setItemInOffHand(item); - hasMainHandSupport = true; - } catch (final Throwable e) { - hasMainHandSupport = false; - } + if (IS_OFFHAND) { + p.getInventory().setItemInOffHand(item); } } public static int clearItemInOffHand(final Player p, final ItemStack item) { - // This should be added because if `/clear` itself is not initilized it will return an Error: null. - if (hasMainHandSupport == null || hasMainHandSupport) { - try { - int removedAmount = 0; - if (p.getInventory().getItemInOffHand().getType().equals(item.getType())) { - removedAmount = p.getInventory().getItemInOffHand().getAmount(); - p.getInventory().setItemInOffHand(null); - } - hasMainHandSupport = true; - return removedAmount; - } catch (final Throwable e) { - hasMainHandSupport = false; + if (IS_OFFHAND) { + int removedAmount = 0; + if (p.getInventory().getItemInOffHand().getType().equals(item.getType())) { + removedAmount = p.getInventory().getItemInOffHand().getAmount(); + p.getInventory().setItemInOffHand(null); } + return removedAmount; } return 0; } From 12849b910a00680d059acbf8153eccfad3e4a1dd Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Fri, 31 Dec 2021 11:49:41 -0500 Subject: [PATCH 2/2] Fix keepinv policies ignoring offhand --- .../essentials/EssentialsEntityListener.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/EssentialsEntityListener.java b/Essentials/src/main/java/com/earth2me/essentials/EssentialsEntityListener.java index f0a0068cac9..c8040c660c6 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/EssentialsEntityListener.java +++ b/Essentials/src/main/java/com/earth2me/essentials/EssentialsEntityListener.java @@ -1,5 +1,6 @@ package com.earth2me.essentials; +import com.earth2me.essentials.utils.MaterialUtil; import com.earth2me.essentials.utils.VersionUtil; import net.ess3.api.IEssentials; import org.bukkit.Location; @@ -198,7 +199,7 @@ public void onPlayerDeathInvEvent(final PlayerDeathEvent event) { final ISettings.KeepInvPolicy bind = ess.getSettings().getBindingItemsPolicy(); if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_11_2_R01) && (vanish != ISettings.KeepInvPolicy.KEEP || bind != ISettings.KeepInvPolicy.KEEP)) { for (final ItemStack stack : event.getEntity().getInventory()) { - if (stack != null) { + if (stack != null && !MaterialUtil.isAir(stack.getType())) { if (stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE)) { if (vanish == ISettings.KeepInvPolicy.DELETE) { event.getEntity().getInventory().remove(stack); @@ -217,10 +218,12 @@ public void onPlayerDeathInvEvent(final PlayerDeathEvent event) { } } } + + // Now check armor final ItemStack[] armor = event.getEntity().getInventory().getArmorContents(); for (int i = 0; i < armor.length; i++) { final ItemStack stack = armor[i]; - if (stack != null) { + if (stack != null && !MaterialUtil.isAir(stack.getType())) { if (stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE)) { if (vanish == ISettings.KeepInvPolicy.DELETE) { armor[i] = null; @@ -244,6 +247,24 @@ public void onPlayerDeathInvEvent(final PlayerDeathEvent event) { } } event.getEntity().getInventory().setArmorContents(armor); + + // Now check offhand + if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_9_R01)) { + final ItemStack stack = event.getEntity().getInventory().getItemInOffHand(); + //noinspection ConstantConditions + if (stack != null && !MaterialUtil.isAir(stack.getType())) { + final boolean isVanish = stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE); + final boolean isBind = stack.getEnchantments().containsKey(Enchantment.BINDING_CURSE); + if (isVanish || isBind) { + event.getEntity().getInventory().setItemInOffHand(null); + if ((isVanish && vanish == ISettings.KeepInvPolicy.DROP) || (isBind && bind == ISettings.KeepInvPolicy.DROP)) { + if (!event.getDrops().contains(stack)) { + event.getDrops().add(stack); + } + } + } + } + } } } }