From b9cd266d0eaa892391925216831b60b555602dde Mon Sep 17 00:00:00 2001 From: Akiranya Date: Thu, 9 Sep 2021 17:05:49 +0800 Subject: [PATCH] Fixed Soulbound bug There are some cases where the player's inventory is not empty right after respawning. For example, ItemsAdder gives a death map right after the player respawns. In this case, EcoEnchants won't give back soulbound items to the player, causing the items just disappeared. I fix this by checking if there is any soulbound items right after respawning, instead of simply checking whether the player's inventory is empty or not. More robust logic to handle such case is expected. My solution is just a workaround. --- .../enchantments/ecoenchants/special/Soulbound.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/special/Soulbound.java b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/special/Soulbound.java index 26ec8e35..6bd59dcb 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/special/Soulbound.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/special/Soulbound.java @@ -5,7 +5,7 @@ import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.meta.EnchantmentType; import com.willfp.ecoenchants.enchantments.util.EnchantChecks; import com.willfp.ecoenchants.enchantments.util.EnchantmentUtils; -import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -97,9 +97,10 @@ public class Soulbound extends EcoEnchant { player.setMetadata("soulbound-items", this.getPlugin().getMetadataValueFactory().create(soulboundItems)); } - public boolean hasEmptyInventory(@NotNull final Player player) { + public boolean hasSoulboundItems(@NotNull final Player player) { + final NamespacedKey soulbound = this.getPlugin().getNamespacedKeyFactory().create("soulbound"); for (ItemStack itemStack : player.getInventory().getContents()) { - if (itemStack != null && itemStack.getType() != Material.AIR) { + if (itemStack != null && itemStack.getItemMeta().getPersistentDataContainer().has(soulbound, PersistentDataType.INTEGER)) { return false; } } @@ -111,7 +112,7 @@ public class Soulbound extends EcoEnchant { Player player = event.getPlayer(); this.getPlugin().getScheduler().runLater(() -> { - if (!hasEmptyInventory(player)) { + if (!hasSoulboundItems(player)) { return; } @@ -142,6 +143,7 @@ public class Soulbound extends EcoEnchant { @EventHandler(priority = EventPriority.HIGHEST) public void onDeath(@NotNull final PlayerDeathEvent event) { - event.getDrops().removeIf(itemStack -> itemStack.getItemMeta().getPersistentDataContainer().has(this.getPlugin().getNamespacedKeyFactory().create("soulbound"), PersistentDataType.INTEGER)); + final NamespacedKey soulbound = this.getPlugin().getNamespacedKeyFactory().create("soulbound"); + event.getDrops().removeIf(itemStack -> itemStack.getItemMeta().getPersistentDataContainer().has(soulbound, PersistentDataType.INTEGER)); } }