From 13ca7d018c567de4987a35f9fcf554b306a00687 Mon Sep 17 00:00:00 2001 From: XiaoMoMi Date: Wed, 2 Apr 2025 22:04:12 +0800 Subject: [PATCH] fix memory leak & improve speed --- .../bukkit/api/CraftEngineFurniture.java | 10 ++--- .../furniture/BukkitFurnitureManager.java | 38 ++++++++----------- .../plugin/network/PacketConsumers.java | 14 ++----- .../entity/furniture/FurnitureManager.java | 2 +- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java index 04f57f418..436f677b3 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/api/CraftEngineFurniture.java @@ -139,7 +139,7 @@ public class CraftEngineFurniture { */ @Nullable public static LoadedFurniture getLoadedFurnitureByBaseEntity(@NotNull Entity baseEntity) { - return BukkitFurnitureManager.instance().getLoadedFurnitureByBaseEntityId(baseEntity.getEntityId()); + return BukkitFurnitureManager.instance().getLoadedFurnitureByRealEntityId(baseEntity.getEntityId()); } /** @@ -152,7 +152,7 @@ public class CraftEngineFurniture { public static LoadedFurniture getLoadedFurnitureBySeat(@NotNull Entity seat) { Integer baseEntityId = seat.getPersistentDataContainer().get(BukkitFurnitureManager.FURNITURE_SEAT_BASE_ENTITY_KEY, PersistentDataType.INTEGER); if (baseEntityId == null) return null; - return BukkitFurnitureManager.instance().getLoadedFurnitureByBaseEntityId(baseEntityId); + return BukkitFurnitureManager.instance().getLoadedFurnitureByRealEntityId(baseEntityId); } /** @@ -163,7 +163,7 @@ public class CraftEngineFurniture { */ public static boolean remove(@NotNull Entity furniture) { if (!isFurniture(furniture)) return false; - LoadedFurniture loadedFurniture = BukkitFurnitureManager.instance().getLoadedFurnitureByBaseEntityId(furniture.getEntityId()); + LoadedFurniture loadedFurniture = BukkitFurnitureManager.instance().getLoadedFurnitureByRealEntityId(furniture.getEntityId()); if (loadedFurniture == null) return false; loadedFurniture.destroy(); return true; @@ -181,7 +181,7 @@ public class CraftEngineFurniture { boolean dropLoot, boolean playSound) { if (!isFurniture(furniture)) return false; - LoadedFurniture loadedFurniture = BukkitFurnitureManager.instance().getLoadedFurnitureByBaseEntityId(furniture.getEntityId()); + LoadedFurniture loadedFurniture = BukkitFurnitureManager.instance().getLoadedFurnitureByRealEntityId(furniture.getEntityId()); if (loadedFurniture == null) return false; remove(loadedFurniture, (net.momirealms.craftengine.core.entity.player.Player) null, dropLoot, playSound); return true; @@ -201,7 +201,7 @@ public class CraftEngineFurniture { boolean dropLoot, boolean playSound) { if (!isFurniture(furniture)) return false; - LoadedFurniture loadedFurniture = BukkitFurnitureManager.instance().getLoadedFurnitureByBaseEntityId(furniture.getEntityId()); + LoadedFurniture loadedFurniture = BukkitFurnitureManager.instance().getLoadedFurnitureByRealEntityId(furniture.getEntityId()); if (loadedFurniture == null) return false; remove(loadedFurniture, player, dropLoot, playSound); return true; diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java index d2d48310d..5f3e8a199 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/entity/furniture/BukkitFurnitureManager.java @@ -41,9 +41,8 @@ public class BukkitFurnitureManager implements FurnitureManager { private final Map byId = new HashMap<>(); - private final Map furnitureByBaseEntityId = new ConcurrentHashMap<>(256, 0.5f); + private final Map furnitureByRealEntityId = new ConcurrentHashMap<>(256, 0.5f); private final Map furnitureByEntityId = new ConcurrentHashMap<>(512, 0.5f); - private final Map furnitureByCollisionEntitiesId = new ConcurrentHashMap<>(256, 0.5f); // Event listeners private final Listener dismountListener; private final FurnitureEventListener furnitureEventListener; @@ -78,7 +77,7 @@ public class BukkitFurnitureManager implements FurnitureManager { SoundData data = furniture.settings().sounds().placeSound(); location.getWorld().playSound(location, data.id().toString(), SoundCategory.BLOCKS, data.volume(), data.pitch()); } - return getLoadedFurnitureByBaseEntityId(furnitureEntity.getEntityId()); + return getLoadedFurnitureByRealEntityId(furnitureEntity.getEntityId()); } @Override @@ -258,13 +257,13 @@ public class BukkitFurnitureManager implements FurnitureManager { } @Override - public boolean isFurnitureBaseEntity(int entityId) { - return this.furnitureByBaseEntityId.containsKey(entityId); + public boolean isFurnitureRealEntity(int entityId) { + return this.furnitureByRealEntityId.containsKey(entityId); } @Nullable - public LoadedFurniture getLoadedFurnitureByBaseEntityId(int entityId) { - return this.furnitureByBaseEntityId.get(entityId); + public LoadedFurniture getLoadedFurnitureByRealEntityId(int entityId) { + return this.furnitureByRealEntityId.get(entityId); } @Nullable @@ -272,23 +271,18 @@ public class BukkitFurnitureManager implements FurnitureManager { return this.furnitureByEntityId.get(entityId); } - @Nullable - public LoadedFurniture getLoadedFurnitureByCollisionEntityId(int entityId) { - return this.furnitureByCollisionEntitiesId.get(entityId); - } - - public boolean isFurnitureCollisionEntity(int entityId) { - return this.furnitureByCollisionEntitiesId.containsKey(entityId); - } - protected void handleBaseFurnitureUnload(Entity entity) { int id = entity.getEntityId(); - LoadedFurniture furniture = this.furnitureByBaseEntityId.remove(id); + LoadedFurniture furniture = this.furnitureByRealEntityId.remove(id); if (furniture != null) { furniture.destroySeats(); for (int sub : furniture.entityIds()) { this.furnitureByEntityId.remove(sub); } + for (CollisionEntity collision : furniture.collisionEntities()) { + this.furnitureByRealEntityId.remove(FastNMS.INSTANCE.method$Entity$getId(collision)); + collision.destroy(); + } } } @@ -301,7 +295,7 @@ public class BukkitFurnitureManager implements FurnitureManager { Optional optionalFurniture = getFurniture(key); if (optionalFurniture.isEmpty()) return; CustomFurniture customFurniture = optionalFurniture.get(); - LoadedFurniture previous = this.furnitureByBaseEntityId.get(display.getEntityId()); + LoadedFurniture previous = this.furnitureByRealEntityId.get(display.getEntityId()); if (previous != null) return; Location location = entity.getLocation(); if (FastNMS.INSTANCE.isPreventingStatusUpdates(location.getWorld(), location.getBlockX() >> 4, location.getBlockZ() >> 4)) { @@ -323,7 +317,7 @@ public class BukkitFurnitureManager implements FurnitureManager { Optional optionalFurniture = getFurniture(key); if (optionalFurniture.isPresent()) { CustomFurniture customFurniture = optionalFurniture.get(); - LoadedFurniture previous = this.furnitureByBaseEntityId.get(display.getEntityId()); + LoadedFurniture previous = this.furnitureByRealEntityId.get(display.getEntityId()); if (previous != null) return; addNewFurniture(display, customFurniture, getAnchorType(entity, customFurniture)); return; @@ -355,13 +349,13 @@ public class BukkitFurnitureManager implements FurnitureManager { private synchronized LoadedFurniture addNewFurniture(ItemDisplay display, CustomFurniture furniture, AnchorType anchorType) { LoadedFurniture loadedFurniture = new LoadedFurniture(display, furniture, anchorType); - this.furnitureByBaseEntityId.put(loadedFurniture.baseEntityId(), loadedFurniture); + this.furnitureByRealEntityId.put(loadedFurniture.baseEntityId(), loadedFurniture); for (int entityId : loadedFurniture.entityIds()) { this.furnitureByEntityId.put(entityId, loadedFurniture); } for (CollisionEntity collisionEntity : loadedFurniture.collisionEntities()) { int collisionEntityId = FastNMS.INSTANCE.method$Entity$getId(collisionEntity); - this.furnitureByCollisionEntitiesId.put(collisionEntityId, loadedFurniture); + this.furnitureByRealEntityId.put(collisionEntityId, loadedFurniture); } loadedFurniture.initializeColliders(); return loadedFurniture; @@ -377,7 +371,7 @@ public class BukkitFurnitureManager implements FurnitureManager { Integer baseFurniture = vehicle.getPersistentDataContainer().get(FURNITURE_SEAT_BASE_ENTITY_KEY, PersistentDataType.INTEGER); if (baseFurniture == null) return; vehicle.remove(); - LoadedFurniture furniture = getLoadedFurnitureByBaseEntityId(baseFurniture); + LoadedFurniture furniture = getLoadedFurnitureByRealEntityId(baseFurniture); if (furniture == null) { return; } diff --git a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java index 45121029f..424b361e6 100644 --- a/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java +++ b/bukkit/src/main/java/net/momirealms/craftengine/bukkit/plugin/network/PacketConsumers.java @@ -618,7 +618,7 @@ public class PacketConsumers { } else if (entityType == Reflections.instance$EntityType$ITEM_DISPLAY) { // Furniture int entityId = (int) Reflections.field$ClientboundAddEntityPacket$entityId.get(packet); - LoadedFurniture furniture = BukkitFurnitureManager.instance().getLoadedFurnitureByBaseEntityId(entityId); + LoadedFurniture furniture = BukkitFurnitureManager.instance().getLoadedFurnitureByRealEntityId(entityId); if (furniture != null) { user.furnitureView().computeIfAbsent(furniture.baseEntityId(), k -> new ArrayList<>()).addAll(furniture.fakeEntityIds()); user.sendPacket(furniture.spawnPacket((Player) user.platformPlayer()), false); @@ -629,7 +629,7 @@ public class PacketConsumers { } else if (entityType == Reflections.instance$EntityType$SHULKER) { // Cancel collider entity packet int entityId = (int) Reflections.field$ClientboundAddEntityPacket$entityId.get(packet); - LoadedFurniture furniture = BukkitFurnitureManager.instance().getLoadedFurnitureByCollisionEntityId(entityId); + LoadedFurniture furniture = BukkitFurnitureManager.instance().getLoadedFurnitureByRealEntityId(entityId); if (furniture != null) { event.setCancelled(true); } @@ -642,10 +642,7 @@ public class PacketConsumers { public static final TriConsumer SYNC_ENTITY_POSITION = (user, event, packet) -> { try { int entityId = (int) Reflections.field$ClientboundEntityPositionSyncPacket$id.get(packet); - if (BukkitFurnitureManager.instance().isFurnitureBaseEntity(entityId)) { - event.setCancelled(true); - } - if (BukkitFurnitureManager.instance().isFurnitureCollisionEntity(entityId)) { + if (BukkitFurnitureManager.instance().isFurnitureRealEntity(entityId)) { event.setCancelled(true); } } catch (Exception e) { @@ -656,10 +653,7 @@ public class PacketConsumers { public static final TriConsumer MOVE_ENTITY = (user, event, packet) -> { try { int entityId = (int) Reflections.field$ClientboundMoveEntityPacket$entityId.get(packet); - if (BukkitFurnitureManager.instance().isFurnitureBaseEntity(entityId)) { - event.setCancelled(true); - } - if (BukkitFurnitureManager.instance().isFurnitureCollisionEntity(entityId)) { + if (BukkitFurnitureManager.instance().isFurnitureRealEntity(entityId)) { event.setCancelled(true); } } catch (Exception e) { diff --git a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureManager.java b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureManager.java index 100a41b5c..965337964 100644 --- a/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureManager.java +++ b/core/src/main/java/net/momirealms/craftengine/core/entity/furniture/FurnitureManager.java @@ -33,5 +33,5 @@ public interface FurnitureManager extends Reloadable, ConfigSectionParser { Optional getFurniture(Key id); - boolean isFurnitureBaseEntity(int entityId); + boolean isFurnitureRealEntity(int entityId); }