Fix keepinv policies ignoring offhand (#4725)

This commit is contained in:
Josh Roy 2022-02-13 15:37:25 -05:00 committed by GitHub
parent 63cbf7e2da
commit f00c2dcf92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 64 deletions

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials; package com.earth2me.essentials;
import com.earth2me.essentials.utils.MaterialUtil;
import com.earth2me.essentials.utils.VersionUtil; import com.earth2me.essentials.utils.VersionUtil;
import net.ess3.api.IEssentials; import net.ess3.api.IEssentials;
import org.bukkit.Location; import org.bukkit.Location;
@ -198,7 +199,7 @@ public class EssentialsEntityListener implements Listener {
final ISettings.KeepInvPolicy bind = ess.getSettings().getBindingItemsPolicy(); final ISettings.KeepInvPolicy bind = ess.getSettings().getBindingItemsPolicy();
if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_11_2_R01) && (vanish != ISettings.KeepInvPolicy.KEEP || bind != ISettings.KeepInvPolicy.KEEP)) { if (VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_11_2_R01) && (vanish != ISettings.KeepInvPolicy.KEEP || bind != ISettings.KeepInvPolicy.KEEP)) {
for (final ItemStack stack : event.getEntity().getInventory()) { 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 (stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE)) {
if (vanish == ISettings.KeepInvPolicy.DELETE) { if (vanish == ISettings.KeepInvPolicy.DELETE) {
event.getEntity().getInventory().remove(stack); event.getEntity().getInventory().remove(stack);
@ -217,10 +218,12 @@ public class EssentialsEntityListener implements Listener {
} }
} }
} }
// Now check armor
final ItemStack[] armor = event.getEntity().getInventory().getArmorContents(); final ItemStack[] armor = event.getEntity().getInventory().getArmorContents();
for (int i = 0; i < armor.length; i++) { for (int i = 0; i < armor.length; i++) {
final ItemStack stack = armor[i]; final ItemStack stack = armor[i];
if (stack != null) { if (stack != null && !MaterialUtil.isAir(stack.getType())) {
if (stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE)) { if (stack.getEnchantments().containsKey(Enchantment.VANISHING_CURSE)) {
if (vanish == ISettings.KeepInvPolicy.DELETE) { if (vanish == ISettings.KeepInvPolicy.DELETE) {
armor[i] = null; armor[i] = null;
@ -244,6 +247,24 @@ public class EssentialsEntityListener implements Listener {
} }
} }
event.getEntity().getInventory().setArmorContents(armor); 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);
}
}
}
}
}
} }
} }
} }

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.craftbukkit; package com.earth2me.essentials.craftbukkit;
import com.earth2me.essentials.utils.VersionUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -15,15 +16,13 @@ import java.util.Map;
/* /*
* This class can be removed when https://github.com/Bukkit/CraftBukkit/pull/193 is accepted to CraftBukkit * This class can be removed when https://github.com/Bukkit/CraftBukkit/pull/193 is accepted to CraftBukkit
*/ */
public final class InventoryWorkaround { public final class InventoryWorkaround {
/* /*
Spigot 1.9, for whatever reason, decided to merge the armor and main player inventories without providing a way 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. 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; private static final int USABLE_PLAYER_INV_SIZE = 36;
// Hot-ish code so cache private static final boolean IS_OFFHAND = VersionUtil.getServerBukkitVersion().isHigherThanOrEqualTo(VersionUtil.v1_9_R01);
private static Boolean hasMainHandSupport = null;
private InventoryWorkaround() { private InventoryWorkaround() {
} }
@ -201,87 +200,45 @@ public final class InventoryWorkaround {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static void setItemInMainHand(final Player p, final ItemStack item) { public static void setItemInMainHand(final Player p, final ItemStack item) {
if (hasMainHandSupport == null) { if (IS_OFFHAND) {
try {
p.getInventory().setItemInMainHand(item);
hasMainHandSupport = true;
} catch (final Throwable e) {
p.setItemInHand(item);
hasMainHandSupport = false;
}
} else {
if (hasMainHandSupport) {
p.getInventory().setItemInMainHand(item); p.getInventory().setItemInMainHand(item);
} else { } else {
p.setItemInHand(item); p.setItemInHand(item);
} }
} }
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static void setItemInMainHand(final EntityEquipment invent, final ItemStack item) { public static void setItemInMainHand(final EntityEquipment invent, final ItemStack item) {
if (hasMainHandSupport == null) { if (IS_OFFHAND) {
try {
invent.setItemInMainHand(item);
hasMainHandSupport = true;
} catch (final Throwable e) {
invent.setItemInHand(item);
hasMainHandSupport = false;
}
} else {
if (hasMainHandSupport) {
invent.setItemInMainHand(item); invent.setItemInMainHand(item);
} else { } else {
invent.setItemInHand(item); invent.setItemInHand(item);
} }
} }
}
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public static void setItemInMainHandDropChance(final EntityEquipment invent, final float chance) { public static void setItemInMainHandDropChance(final EntityEquipment invent, final float chance) {
if (hasMainHandSupport == null) { if (IS_OFFHAND) {
try {
invent.setItemInMainHandDropChance(chance);
hasMainHandSupport = true;
} catch (final Throwable e) {
invent.setItemInHandDropChance(chance);
hasMainHandSupport = false;
}
} else {
if (hasMainHandSupport) {
invent.setItemInMainHandDropChance(chance); invent.setItemInMainHandDropChance(chance);
} else { } else {
invent.setItemInHandDropChance(chance); invent.setItemInHandDropChance(chance);
} }
} }
}
public static void setItemInOffHand(final Player p, final ItemStack item) { 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 (IS_OFFHAND) {
if (hasMainHandSupport == null || hasMainHandSupport) {
try {
p.getInventory().setItemInOffHand(item); p.getInventory().setItemInOffHand(item);
hasMainHandSupport = true;
} catch (final Throwable e) {
hasMainHandSupport = false;
}
} }
} }
public static int clearItemInOffHand(final Player p, final ItemStack 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 (IS_OFFHAND) {
if (hasMainHandSupport == null || hasMainHandSupport) {
try {
int removedAmount = 0; int removedAmount = 0;
if (p.getInventory().getItemInOffHand().getType().equals(item.getType())) { if (p.getInventory().getItemInOffHand().getType().equals(item.getType())) {
removedAmount = p.getInventory().getItemInOffHand().getAmount(); removedAmount = p.getInventory().getItemInOffHand().getAmount();
p.getInventory().setItemInOffHand(null); p.getInventory().setItemInOffHand(null);
} }
hasMainHandSupport = true;
return removedAmount; return removedAmount;
} catch (final Throwable e) {
hasMainHandSupport = false;
}
} }
return 0; return 0;
} }