From 1118be11534551d1d46acc780de7c7df07161aa1 Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 19:24:12 -0500 Subject: [PATCH 1/8] fix: set velocity after setting the location otherwise the velocity would be set to zero after teleporting the balloon --- .../hmccosmetics/cosmetic/types/CosmeticBalloonType.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java index 72d4f465..746e3f18 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBalloonType.java @@ -79,8 +79,8 @@ public void update(@NotNull CosmeticUser user) { } Vector velocity = newLocation.toVector().subtract(currentLocation.toVector()); - userBalloonManager.setVelocity(velocity.multiply(1.1)); userBalloonManager.setLocation(newLocation); + userBalloonManager.setVelocity(velocity.multiply(1.1)); HMCCPacketManager.sendTeleportPacket(userBalloonManager.getPufferfishBalloonId(), newLocation, false, viewer); HMCCPacketManager.sendLeashPacket(userBalloonManager.getPufferfishBalloonId(), entity.getEntityId(), viewer); From d932732d9b7bb7b9ac89c23f1c064654c2d24a33 Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 19:24:30 -0500 Subject: [PATCH 2/8] feat: add getVelocity() method to UserBalloonManager --- .../hmccosmetics/user/manager/UserBalloonManager.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBalloonManager.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBalloonManager.java index 89b24202..a8e19a94 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBalloonManager.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBalloonManager.java @@ -180,6 +180,10 @@ public void setLocation(Location location) { this.getModelEntity().teleport(location); } + public Vector getVelocity() { + return getModelEntity().getVelocity(); + } + public void setVelocity(Vector vector) { this.getModelEntity().setVelocity(vector); } From 720437212b9a5c2e28eb13f53bb80a8bf3ac2d42 Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 19:26:50 -0500 Subject: [PATCH 3/8] perf: do not double call Map get (microoptimization) --- .../com/hibiscusmc/hmccosmetics/user/CosmeticUser.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 70f78d0a..5bb2fa0a 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -301,11 +301,10 @@ public Set getSlotsWithCosmetics() { @Override public void updateCosmetic(@NotNull CosmeticSlot slot) { - if (getCosmetic(slot) == null) { - return; + Cosmetic cosmetic = playerCosmetics.get(slot); + if (cosmetic != null) { + cosmetic.update(this); } - getCosmetic(slot).update(this); - return; } public void updateCosmetic(Cosmetic cosmetic) { From 545f258f9046e49f3e2d4d5f458552bf69d081f7 Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 19:30:00 -0500 Subject: [PATCH 4/8] perf: avoid creating an ImmutableCollection copy every time updateCosmetics() is called micro-optimizations, also we avoid calling Map#get again and using cosmetic.update(this) instead --- .../java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 5bb2fa0a..ef241735 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -315,7 +315,7 @@ public void updateCosmetic() { MessagesUtil.sendDebugMessages("updateCosmetic (All) - start"); HashMap items = new HashMap<>(); - for (Cosmetic cosmetic : getCosmetics()) { + for (Cosmetic cosmetic : playerCosmetics.values()) { if (cosmetic instanceof CosmeticArmorType armorType) { if (getUserEmoteManager().isPlayingEmote() || isInWardrobe()) return; if (!(getEntity() instanceof HumanEntity humanEntity)) return; @@ -327,7 +327,7 @@ public void updateCosmetic() { items.put(HMCCInventoryUtils.getEquipmentSlot(armorType.getSlot()), armorType.getItem(this)); } else { - updateCosmetic(cosmetic.getSlot()); + cosmetic.update(this); } } if (items.isEmpty() || getEntity() == null) return; From f5a01c010418abf1d7ddc250694338ed0045ba4e Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 19:46:02 -0500 Subject: [PATCH 5/8] perf: remove unnecessary null-check --- .../com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java | 1 - 1 file changed, 1 deletion(-) 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 f72f6cc9..5e32c589 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/listener/PlayerGameListener.java @@ -700,7 +700,6 @@ private EquipmentSlot getArmorSlot(final Material material) { for (final EquipmentSlot slot : EquipmentSlot.values()) { final Set armorItems = ARMOR_ITEMS.get(slot); if (armorItems == null) continue; - if (material == null) continue; if (armorItems.contains(material)) return slot; } return null; From 76429ed0412bf1478b85e410e00c43bf044f14d8 Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 19:48:30 -0500 Subject: [PATCH 6/8] perf: avoid creating unnecessary collection copies --- .../java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 ef241735..fd14d3f6 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/CosmeticUser.java @@ -194,7 +194,7 @@ protected void tick() { this.updateCosmetic(); - if(isHidden() && !getUserEmoteManager().isPlayingEmote() && !getCosmetics().isEmpty()) { + if(isHidden() && !getUserEmoteManager().isPlayingEmote() && !playerCosmetics.isEmpty()) { MessagesUtil.sendActionBar(getPlayer(), "hidden-cosmetics"); } } @@ -632,7 +632,7 @@ public Color getCosmeticColor(CosmeticSlot slot) { public List getDyeableSlots() { ArrayList dyableSlots = new ArrayList<>(); - for (Cosmetic cosmetic : getCosmetics()) { + for (Cosmetic cosmetic : playerCosmetics.values()) { if (cosmetic.isDyable()) dyableSlots.add(cosmetic.getSlot()); } From 9e1a30025fbdac122f595527fdfe76a3978bc619 Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 19:57:21 -0500 Subject: [PATCH 7/8] perf: cache some variables in CosmeticBackpackType#update --- .../cosmetic/types/CosmeticBackpackType.java | 36 +++++++++++-------- .../user/manager/UserBackpackManager.java | 2 +- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java index 8ec50c7c..6dca010e 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/cosmetic/types/CosmeticBackpackType.java @@ -3,6 +3,8 @@ import com.hibiscusmc.hmccosmetics.config.Settings; import com.hibiscusmc.hmccosmetics.cosmetic.Cosmetic; import com.hibiscusmc.hmccosmetics.user.CosmeticUser; +import com.hibiscusmc.hmccosmetics.user.manager.UserBackpackManager; +import com.hibiscusmc.hmccosmetics.user.manager.UserEntity; import com.hibiscusmc.hmccosmetics.util.MessagesUtil; import com.hibiscusmc.hmccosmetics.util.packets.HMCCPacketManager; import lombok.Getter; @@ -39,10 +41,11 @@ public CosmeticBackpackType(String id, ConfigurationNode config) { @Override public void update(@NotNull CosmeticUser user) { - Entity entity = Bukkit.getEntity(user.getUniqueId()); + Entity entity = user.getEntity(); if (entity == null) return; - Location loc = entity.getLocation().clone().add(0, 2, 0); + Location entityLocation = entity.getLocation(); + Location loc = entityLocation.clone().add(0, 2, 0); if (user.isInWardrobe() || !user.isBackpackSpawned()) return; if (user.isHidden()) { @@ -50,22 +53,27 @@ public void update(@NotNull CosmeticUser user) { user.despawnBackpack(); return; } - List outsideViewers = user.getUserBackpackManager().getEntityManager().refreshViewers(loc); - user.getUserBackpackManager().getEntityManager().teleport(loc); - user.getUserBackpackManager().getEntityManager().setRotation((int) loc.getYaw(), isFirstPersonCompadible()); + UserBackpackManager backpackManager = user.getUserBackpackManager(); + UserEntity entityManager = backpackManager.getEntityManager(); + int firstArmorStandId = backpackManager.getFirstArmorStandId(); - HMCCPacketManager.sendEntitySpawnPacket(user.getEntity().getLocation(), user.getUserBackpackManager().getFirstArmorStandId(), EntityType.ARMOR_STAND, UUID.randomUUID(), outsideViewers); - HMCCPacketManager.sendArmorstandMetadata(user.getUserBackpackManager().getFirstArmorStandId(), outsideViewers); - PacketManager.equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, user.getUserCosmeticItem(this, getItem()), outsideViewers); + List outsideViewers = entityManager.refreshViewers(loc); + + entityManager.teleport(loc); + entityManager.setRotation((int) loc.getYaw(), isFirstPersonCompadible()); + + HMCCPacketManager.sendEntitySpawnPacket(entityLocation, firstArmorStandId, EntityType.ARMOR_STAND, UUID.randomUUID(), outsideViewers); + HMCCPacketManager.sendArmorstandMetadata(firstArmorStandId, outsideViewers); + PacketManager.equipmentSlotUpdate(firstArmorStandId, EquipmentSlot.HEAD, user.getUserCosmeticItem(this, getItem()), outsideViewers); // If true, it will send the riding packet to all players. If false, it will send the riding packet only to new players - if (Settings.isBackpackForceRidingEnabled()) HMCCPacketManager.sendRidingPacket(entity.getEntityId(), user.getUserBackpackManager().getFirstArmorStandId(), user.getUserBackpackManager().getEntityManager().getViewers()); - else HMCCPacketManager.sendRidingPacket(entity.getEntityId(), user.getUserBackpackManager().getFirstArmorStandId(), outsideViewers); + if (Settings.isBackpackForceRidingEnabled()) HMCCPacketManager.sendRidingPacket(entity.getEntityId(), firstArmorStandId, entityManager.getViewers()); + else HMCCPacketManager.sendRidingPacket(entity.getEntityId(), firstArmorStandId, outsideViewers); if (!user.isInWardrobe() && isFirstPersonCompadible() && user.getPlayer() != null) { List owner = List.of(user.getPlayer()); - ArrayList particleCloud = user.getUserBackpackManager().getAreaEffectEntityId(); + ArrayList particleCloud = backpackManager.getAreaEffectEntityId(); for (int i = 0; i < particleCloud.size(); i++) { if (i == 0) { HMCCPacketManager.sendRidingPacket(entity.getEntityId(), particleCloud.get(i), owner); @@ -73,16 +81,16 @@ public void update(@NotNull CosmeticUser user) { HMCCPacketManager.sendRidingPacket(particleCloud.get(i - 1), particleCloud.get(i) , owner); } } - HMCCPacketManager.sendRidingPacket(particleCloud.get(particleCloud.size() - 1), user.getUserBackpackManager().getFirstArmorStandId(), owner); + HMCCPacketManager.sendRidingPacket(particleCloud.get(particleCloud.size() - 1), firstArmorStandId, owner); if (!user.isHidden()) { //if (loc.getPitch() < -70) NMSHandlers.getHandler().equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, new ItemStack(Material.AIR), owner); //else NMSHandlers.getHandler().equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, firstPersonBackpack, owner); - PacketManager.equipmentSlotUpdate(user.getUserBackpackManager().getFirstArmorStandId(), EquipmentSlot.HEAD, user.getUserCosmeticItem(this, firstPersonBackpack), owner); + PacketManager.equipmentSlotUpdate(firstArmorStandId, EquipmentSlot.HEAD, user.getUserCosmeticItem(this, firstPersonBackpack), owner); } //MessagesUtil.sendDebugMessages("First Person Backpack Update[owner=" + user.getUniqueId() + ",player_location=" + loc + "]!", Level.INFO); } - user.getUserBackpackManager().showBackpack(); + backpackManager.showBackpack(); } public boolean isFirstPersonCompadible() { diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBackpackManager.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBackpackManager.java index 133929e9..51d3b7d4 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBackpackManager.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/user/manager/UserBackpackManager.java @@ -27,7 +27,7 @@ public class UserBackpackManager { @Getter private boolean backpackHidden; @Getter - private int invisibleArmorStand; + private final int invisibleArmorStand; private ArrayList particleCloud = new ArrayList<>(); @Getter private final CosmeticUser user; From 49eb1d61ef4a098a1f406129cc9de585920e9c1a Mon Sep 17 00:00:00 2001 From: yusshu Date: Mon, 10 Mar 2025 20:02:55 -0500 Subject: [PATCH 8/8] perf: remove repeated logic when getting location viewers PacketManager#getViewers already performs the distance <= 0 check --- .../hmccosmetics/util/packets/HMCCPacketManager.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/HMCCPacketManager.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/HMCCPacketManager.java index f145afa5..641d2157 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/HMCCPacketManager.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/util/packets/HMCCPacketManager.java @@ -389,13 +389,7 @@ public static void sendMovePacket( */ @NotNull public static List getViewers(@NotNull Location location) { - ArrayList viewers = new ArrayList<>(); - if (Settings.getViewDistance() <= 0) { - viewers.addAll(location.getWorld().getPlayers()); - } else { - viewers.addAll(PacketManager.getViewers(location, Settings.getViewDistance())); - } - return viewers; + return PacketManager.getViewers(location, Settings.getViewDistance()); } public static void sendPacket(Player player, PacketContainer packet) {