diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java index 781178b4..1f216a94 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/HMCCosmetics.java @@ -26,7 +26,6 @@ import io.github.fisher2911.hmccosmetics.message.Messages; import io.github.fisher2911.hmccosmetics.message.Translation; import io.github.fisher2911.hmccosmetics.task.TaskManager; import io.github.fisher2911.hmccosmetics.user.UserManager; -import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder; import me.mattstudios.mf.base.CommandManager; import me.mattstudios.mf.base.CompletionHandler; import org.bstats.bukkit.Metrics; @@ -75,7 +74,7 @@ public class HMCCosmetics extends JavaPlugin { this.settings = new Settings(this); this.messageHandler = new MessageHandler(this); this.userManager = new UserManager(this); - this.cosmeticManager = new CosmeticManager(new HashMap<>(), new HashMap<>()); + this.cosmeticManager = new CosmeticManager(new HashMap<>(), new HashMap<>(), new HashMap<>()); this.cosmeticsMenu = new CosmeticsMenu(this); this.tokenLoader = new TokenLoader(this); diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/cosmetic/CosmeticManager.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/cosmetic/CosmeticManager.java index bdc0a1b4..aedeef30 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/cosmetic/CosmeticManager.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/cosmetic/CosmeticManager.java @@ -23,10 +23,12 @@ public class CosmeticManager { private final Map tokenMap; private final Map armorItemMap; + private final Map backpackParticleCounts; - public CosmeticManager(final Map tokenMap, final Map armorItemMap) { + public CosmeticManager(final Map tokenMap, final Map armorItemMap, final Map backpackParticleCounts) { this.tokenMap = tokenMap; this.armorItemMap = armorItemMap; + this.backpackParticleCounts = backpackParticleCounts; } @Nullable @@ -82,6 +84,16 @@ public class CosmeticManager { this.armorItemMap.clear(); } + public int getBackpackParticleCount(final String id) { + return this.backpackParticleCounts.getOrDefault(id, 0); + } + + public int getBackpackParticleCount(final ArmorItem armorItem) { + return this.getBackpackParticleCount(armorItem.getId()); + } + + private static final String PARTICLE_COUNT = "particle-count"; + public void load() { this.clearItems(); try { @@ -102,6 +114,10 @@ public class CosmeticManager { final WrappedGuiItem item = ArmorItemSerializer.INSTANCE.deserialize(WrappedGuiItem.class, node); if (item instanceof ArmorItem armorItem) { armorItem.setAction(null); + if (armorItem.getType() == ArmorItem.Type.SELF_BACKPACK) { + final int particleCount = node.node(PARTICLE_COUNT).getInt(0); + this.backpackParticleCounts.put(armorItem.getId(), particleCount); + } this.armorItemMap.put(armorItem.getId(), armorItem); } } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/inventory/PlayerArmor.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/inventory/PlayerArmor.java index 60d18876..c8b1eb1f 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/inventory/PlayerArmor.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/inventory/PlayerArmor.java @@ -52,6 +52,9 @@ public class PlayerArmor { } public ArmorItem setItem(final ArmorItem armorItem) { + if (armorItem.isEmpty() && armorItem.getType() == ArmorItem.Type.BACKPACK) { + this.armorItems.put(ArmorItem.Type.SELF_BACKPACK, armorItem); + } return this.armorItems.put(armorItem.getType(), armorItem); } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java index df9f23db..5fed133a 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/Backpack.java @@ -22,13 +22,13 @@ public class Backpack { private final HMCCosmetics plugin; private final int armorStandID; private final int particleCount = HMCCosmetics.getPlugin(HMCCosmetics.class).getSettings().getCosmeticSettings().getParticleCount(); - private final List IDs = new ArrayList<>(); + private final List particleIDS = new ArrayList<>(); public Backpack(HMCCosmetics plugin, int armorStandID) { this.plugin = plugin; this.armorStandID = armorStandID; for (int i = 0; i < particleCount; i++) { - IDs.add(SpigotReflectionUtil.generateEntityId()); + particleIDS.add(SpigotReflectionUtil.generateEntityId()); } } @@ -41,6 +41,21 @@ public class Backpack { this.spawnForSelf(other, location); } + private void updateIds(BaseUser owner) { + final ArmorItem selfBackpack = owner.getPlayerArmor().getItem(ArmorItem.Type.SELF_BACKPACK); + if (selfBackpack.isEmpty()) return; + final int particleCount = this.plugin.getCosmeticManager().getBackpackParticleCount(selfBackpack); + final int currentSize = this.particleIDS.size(); + if (currentSize == particleCount) return; + if (currentSize < particleCount) { + for (int i = currentSize; i < particleCount; i++) { + this.particleIDS.add(SpigotReflectionUtil.generateEntityId()); + } + return; + } + this.particleIDS.subList(particleCount, currentSize).clear(); + } + private void spawnForOther(BaseUser owner, Player other, Location location) { PacketManager.sendEntitySpawnPacket(location, this.armorStandID, EntityTypes.ARMOR_STAND, other); PacketManager.sendArmorStandMetaContainer(this.armorStandID, other); @@ -48,7 +63,7 @@ public class Backpack { } private void spawnForSelf(Player other, Location location) { - for (final int id : this.IDs) { + for (final int id : this.particleIDS) { PacketManager.sendEntityNotLivingSpawnPacket( location, id, @@ -63,14 +78,14 @@ public class Backpack { public void despawn(Player player) { PacketManager.sendEntityDestroyPacket(this.armorStandID, player); - for (Integer id : this.IDs) { + for (Integer id : this.particleIDS) { PacketManager.sendEntityDestroyPacket(id, player); } } public void despawn() { PacketManager.sendEntityDestroyPacket(this.armorStandID, Bukkit.getOnlinePlayers()); - for (Integer id : this.IDs) { + for (Integer id : this.particleIDS) { PacketManager.sendEntityDestroyPacket(id, Bukkit.getOnlinePlayers()); } } @@ -106,7 +121,6 @@ public class Backpack { itemStack )); - PacketManager.sendEquipmentPacket(equipment, this.armorStandID, other); PacketManager.sendArmorStandMetaContainer(this.armorStandID, other); PacketManager.sendRotationPacket(this.armorStandID, location, false, other); @@ -115,16 +129,16 @@ public class Backpack { PacketManager.sendRidingPacket(owner.getEntityId(), this.armorStandID, other); return; } - for (int i = 0; i < this.IDs.size(); i++) { - final int id = this.IDs.get(i); + for (int i = 0; i < this.particleIDS.size(); i++) { + final int id = this.particleIDS.get(i); PacketManager.sendCloudMetaData(id, other); if (i == 0) { PacketManager.sendRidingPacket(owner.getEntityId(), id, other); } else { - PacketManager.sendRidingPacket(this.IDs.get(i - 1), id, other); + PacketManager.sendRidingPacket(this.particleIDS.get(i - 1), id, other); } } - PacketManager.sendRidingPacket(IDs.get(IDs.size() - 1), this.armorStandID, other); + PacketManager.sendRidingPacket(particleIDS.get(particleIDS.size() - 1), this.armorStandID, other); } } diff --git a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java index dadcc578..7351bbc7 100644 --- a/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java +++ b/common/src/main/java/io/github/fisher2911/hmccosmetics/user/UserManager.java @@ -198,6 +198,9 @@ public class UserManager { final ArmorItem.Type type = armorItem.getType(); if (type == ArmorItem.Type.BALLOON) user.despawnBalloon(); user.setItem(event.getCosmeticItem().getArmorItem()); + if (armorItem.getType() == ArmorItem.Type.BACKPACK && armorItem.isEmpty()) { + user.removeItem(ArmorItem.Type.SELF_BACKPACK); + } if (!sendPacket) { user.setArmorUpdated(false); return;