diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java index a2bddc40..c941049a 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/config/Settings.java @@ -48,6 +48,7 @@ public class Settings { private static final String COSMETIC_ADD_ENCHANTS_CHESTPLATE_PATH = "chest-add-enchantments"; private static final String COSMETIC_ADD_ENCHANTS_LEGGINGS_PATH = "leggings-add-enchantments"; private static final String COSMETIC_ADD_ENCHANTS_BOOTS_PATH = "boots-add-enchantments"; + private static final String COSMETIC_DESTROY_LOOSE_COSMETIC_PATH = "destroy-loose-cosmetics"; private static String defaultMenu; private static String dyeMenuName; @@ -71,6 +72,7 @@ public class Settings { private static boolean addBootsEnchants; private static boolean emoteDamageLeave; private static boolean emoteInvincible; + private static boolean destroyLooseCosmetics; private static int lookDownPitch; private static int viewDistance; private static int tickPeriod; @@ -105,6 +107,7 @@ public class Settings { cosmeticEmoteBlockCheck = cosmeticSettings.node(COSMETIC_EMOTE_CHECK_PATH).getBoolean(true); emoteDamageLeave = cosmeticSettings.node(COSMETIC_EMOTE_DAMAGE_PATH).getBoolean(false); emoteInvincible = cosmeticSettings.node(COSMETIC_EMOTE_INVINCIBLE_PATH).getBoolean(false); + destroyLooseCosmetics = cosmeticSettings.node(COSMETIC_DESTROY_LOOSE_COSMETIC_PATH).getBoolean(false); addHelmetEnchants = cosmeticSettings.node(COSMETIC_ADD_ENCHANTS_HELMET_PATH).getBoolean(false); addChestplateEnchants = cosmeticSettings.node(COSMETIC_ADD_ENCHANTS_CHESTPLATE_PATH).getBoolean(false); addLeggingEnchants = cosmeticSettings.node(COSMETIC_ADD_ENCHANTS_LEGGINGS_PATH).getBoolean(false); @@ -272,6 +275,10 @@ public class Settings { return worldGuardMoveCheck; } + public static boolean isDestroyLooseCosmetics() { + return destroyLooseCosmetics; + } + public static boolean getShouldAddEnchants(EquipmentSlot slot) { switch (slot) { case HEAD -> { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java index 3e8be70e..b3eb7ef6 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java @@ -34,7 +34,6 @@ import org.bukkit.entity.Pose; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.*; -import org.bukkit.event.inventory.ClickType; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.player.*; import org.bukkit.inventory.EquipmentSlot; @@ -62,12 +61,20 @@ public class PlayerGameListener implements Listener { @EventHandler public void onPlayerClick(@NotNull InventoryClickEvent event) { - if (event.getClick() != ClickType.SHIFT_LEFT && event.getClick() != ClickType.SHIFT_RIGHT) return; + // || !event.getClickedInventory().getType().equals(InventoryType.PLAYER) + if (event.getClick().isShiftClick()) return; + MessagesUtil.sendDebugMessages("inventoryclickevent"); //if (event.getSlotType() != InventoryType.SlotType.ARMOR) return; CosmeticUser user = CosmeticUsers.getUser(event.getWhoClicked().getUniqueId()); if (user == null) return; ItemStack item = event.getCurrentItem(); if (item == null) return; + + if (Settings.isDestroyLooseCosmetics() && InventoryUtils.isCosmeticItem(event.getCurrentItem())) { + MessagesUtil.sendDebugMessages("remvoe item"); + event.getWhoClicked().getInventory().removeItem(event.getCurrentItem()); + } + EquipmentSlot slot = getArmorSlot(item.getType()); if (slot == null) return; CosmeticSlot cosmeticSlot = InventoryUtils.BukkitCosmeticSlot(slot); diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java index 0fbe3658..91037043 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -18,13 +18,11 @@ import com.hibiscusmc.hmccosmetics.util.InventoryUtils; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import com.hibiscusmc.hmccosmetics.util.PlayerUtils; import com.hibiscusmc.hmccosmetics.util.packets.PacketManager; -import org.bukkit.Bukkit; -import org.bukkit.Color; -import org.bukkit.Location; -import org.bukkit.Material; +import org.bukkit.*; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.*; +import org.bukkit.persistence.PersistentDataType; import org.bukkit.scheduler.BukkitTask; import java.util.*; @@ -212,6 +210,8 @@ public class CosmeticUser { mapMeta.setColor(color); } } + itemMeta.getPersistentDataContainer().set(InventoryUtils.getKey(), PersistentDataType.STRING, "true"); + item.setItemMeta(itemMeta); } return item; diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java index c4ce0717..ff09d492 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/InventoryUtils.java @@ -1,8 +1,12 @@ package com.hibiscusmc.hmccosmetics.util; import com.comphenix.protocol.wrappers.EnumWrappers; +import com.hibiscusmc.hmccosmetics.HMCCosmeticsPlugin; import com.hibiscusmc.hmccosmetics.cosmetic.CosmeticSlot; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.EquipmentSlot; +import org.bukkit.inventory.ItemStack; +import org.bukkit.persistence.PersistentDataType; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -137,4 +141,14 @@ public class InventoryUtils { } } } + + public static boolean isCosmeticItem(ItemStack itemStack) { + itemStack = itemStack.clone(); + if (!itemStack.hasItemMeta()) return false; + return itemStack.getItemMeta().getPersistentDataContainer().has(getKey(), PersistentDataType.STRING); + } + + public static NamespacedKey getKey() { + return new NamespacedKey(HMCCosmeticsPlugin.getInstance(), "cosmetic"); + } } diff --git a/common/src/main/resources/config.yml b/common/src/main/resources/config.yml index 171559b7..7e54110f 100644 --- a/common/src/main/resources/config.yml +++ b/common/src/main/resources/config.yml @@ -33,6 +33,10 @@ cosmetic-settings: leggings-add-enchantments: false # If the plugin should keep enchants on leggings. This is useful as some enchantments are client side only. boots-add-enchantments: false # If the plugin should keep enchants on boots. This is useful as some enchantments are client side only. + # This attempts to destroy cosmetics that get loose in the wild, such as through a player entering creative mode. + # Most servers who don't use creative mode and have properly set up the plugin should have no need for this and can leave it disabled. + destroy-loose-cosmetics: false + # view distance in blocks that other players will see the backpack cosmetic # setting this to lower than the server player view distance should fix the # bug where players see random backpacks. Put -1 to ignore and send packets to everyone.