diff --git a/src/main/java/net/minestom/server/entity/metadata/AgeableMobMeta.java b/src/main/java/net/minestom/server/entity/metadata/AgeableMobMeta.java index 1467bff49..a9c875a7e 100644 --- a/src/main/java/net/minestom/server/entity/metadata/AgeableMobMeta.java +++ b/src/main/java/net/minestom/server/entity/metadata/AgeableMobMeta.java @@ -21,12 +21,16 @@ public class AgeableMobMeta extends PathfinderMobMeta { if (isBaby() == value) { return; } - BoundingBox bb = this.entity.getBoundingBox(); - if (value) { - setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); - } else { - setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 2); - } + this.consumeEntity((entity) -> { + BoundingBox bb = entity.getBoundingBox(); + if (value) { + double width = bb.getWidth() / 2; + entity.setBoundingBox(width, bb.getHeight() / 2, width); + } else { + double width = bb.getWidth() * 2; + entity.setBoundingBox(width, bb.getHeight() * 2, width); + } + }); super.metadata.setIndex(OFFSET, Metadata.Boolean(value)); } diff --git a/src/main/java/net/minestom/server/entity/metadata/EntityMeta.java b/src/main/java/net/minestom/server/entity/metadata/EntityMeta.java index a34add2c6..2746fd7c3 100644 --- a/src/main/java/net/minestom/server/entity/metadata/EntityMeta.java +++ b/src/main/java/net/minestom/server/entity/metadata/EntityMeta.java @@ -4,6 +4,10 @@ import net.kyori.adventure.text.Component; import net.minestom.server.entity.Entity; import net.minestom.server.entity.Metadata; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.ref.WeakReference; +import java.util.function.Consumer; public class EntityMeta { public static final byte OFFSET = 0; @@ -17,11 +21,11 @@ public class EntityMeta { private final static byte HAS_GLOWING_EFFECT_BIT = 0x40; private final static byte FLYING_WITH_ELYTRA_BIT = (byte) 0x80; - protected final Entity entity; + private final WeakReference entityRef; protected final Metadata metadata; - public EntityMeta(@NotNull Entity entity, @NotNull Metadata metadata) { - this.entity = entity; + public EntityMeta(@Nullable Entity entity, @NotNull Metadata metadata) { + this.entityRef = new WeakReference<>(entity); this.metadata = metadata; } @@ -180,12 +184,11 @@ public class EntityMeta { setMask(index, mask); } - protected void setBoundingBox(double x, double y, double z) { - this.entity.setBoundingBox(x, y, z); - } - - protected void setBoundingBox(double width, double height) { - setBoundingBox(width, height, width); + protected void consumeEntity(Consumer consumer) { + Entity entity = this.entityRef.get(); + if (entity != null) { + consumer.accept(entity); + } } } diff --git a/src/main/java/net/minestom/server/entity/metadata/monster/PiglinMeta.java b/src/main/java/net/minestom/server/entity/metadata/monster/PiglinMeta.java index ae5b318ac..65e25d905 100644 --- a/src/main/java/net/minestom/server/entity/metadata/monster/PiglinMeta.java +++ b/src/main/java/net/minestom/server/entity/metadata/monster/PiglinMeta.java @@ -21,12 +21,16 @@ public class PiglinMeta extends BasePiglinMeta { if (isBaby() == value) { return; } - BoundingBox bb = this.entity.getBoundingBox(); - if (value) { - setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); - } else { - setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 2); - } + this.consumeEntity((entity) -> { + BoundingBox bb = entity.getBoundingBox(); + if (value) { + double width = bb.getWidth() / 2; + entity.setBoundingBox(width, bb.getHeight() / 2, width); + } else { + double width = bb.getWidth() * 2; + entity.setBoundingBox(width, bb.getHeight() * 2, width); + } + }); super.metadata.setIndex(OFFSET, Metadata.Boolean(value)); } diff --git a/src/main/java/net/minestom/server/entity/metadata/monster/ZoglinMeta.java b/src/main/java/net/minestom/server/entity/metadata/monster/ZoglinMeta.java index 9aa81aa0a..561d2b37a 100644 --- a/src/main/java/net/minestom/server/entity/metadata/monster/ZoglinMeta.java +++ b/src/main/java/net/minestom/server/entity/metadata/monster/ZoglinMeta.java @@ -21,12 +21,16 @@ public class ZoglinMeta extends MonsterMeta { if (isBaby() == value) { return; } - BoundingBox bb = this.entity.getBoundingBox(); - if (value) { - setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); - } else { - setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 2); - } + this.consumeEntity((entity) -> { + BoundingBox bb = entity.getBoundingBox(); + if (value) { + double width = bb.getWidth() / 2; + entity.setBoundingBox(width, bb.getHeight() / 2, width); + } else { + double width = bb.getWidth() * 2; + entity.setBoundingBox(width, bb.getHeight() * 2, width); + } + }); super.metadata.setIndex(OFFSET, Metadata.Boolean(value)); } diff --git a/src/main/java/net/minestom/server/entity/metadata/monster/zombie/ZombieMeta.java b/src/main/java/net/minestom/server/entity/metadata/monster/zombie/ZombieMeta.java index d074c6d4e..5a20f3555 100644 --- a/src/main/java/net/minestom/server/entity/metadata/monster/zombie/ZombieMeta.java +++ b/src/main/java/net/minestom/server/entity/metadata/monster/zombie/ZombieMeta.java @@ -22,12 +22,16 @@ public class ZombieMeta extends MonsterMeta { if (isBaby() == value) { return; } - BoundingBox bb = this.entity.getBoundingBox(); - if (value) { - setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); - } else { - setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 2); - } + this.consumeEntity((entity) -> { + BoundingBox bb = entity.getBoundingBox(); + if (value) { + double width = bb.getWidth() / 2; + entity.setBoundingBox(width, bb.getHeight() / 2, width); + } else { + double width = bb.getWidth() * 2; + entity.setBoundingBox(width, bb.getHeight() * 2, width); + } + }); super.metadata.setIndex(OFFSET, Metadata.Boolean(value)); } diff --git a/src/main/java/net/minestom/server/entity/metadata/other/SlimeMeta.java b/src/main/java/net/minestom/server/entity/metadata/other/SlimeMeta.java index 2217ca64e..aefe938a3 100644 --- a/src/main/java/net/minestom/server/entity/metadata/other/SlimeMeta.java +++ b/src/main/java/net/minestom/server/entity/metadata/other/SlimeMeta.java @@ -1,5 +1,6 @@ package net.minestom.server.entity.metadata.other; +import net.minestom.server.collision.BoundingBox; import net.minestom.server.entity.Entity; import net.minestom.server.entity.Metadata; import net.minestom.server.entity.metadata.MobMeta; @@ -18,8 +19,10 @@ public class SlimeMeta extends MobMeta { } public void setSize(int value) { - float boxSize = 0.51000005f * value; - setBoundingBox(boxSize, boxSize); + this.consumeEntity((entity) -> { + float boxSize = 0.51000005f * value; + entity.setBoundingBox(boxSize, boxSize, boxSize); + }); super.metadata.setIndex(OFFSET, Metadata.VarInt(value)); } diff --git a/src/main/java/net/minestom/server/entity/metadata/water/fish/PufferfishMeta.java b/src/main/java/net/minestom/server/entity/metadata/water/fish/PufferfishMeta.java index 2920830b6..8f0e3b584 100644 --- a/src/main/java/net/minestom/server/entity/metadata/water/fish/PufferfishMeta.java +++ b/src/main/java/net/minestom/server/entity/metadata/water/fish/PufferfishMeta.java @@ -1,5 +1,6 @@ package net.minestom.server.entity.metadata.water.fish; +import net.minestom.server.collision.BoundingBox; import net.minestom.server.entity.Entity; import net.minestom.server.entity.Metadata; import org.jetbrains.annotations.NotNull; @@ -23,11 +24,13 @@ public class PufferfishMeta extends AbstractFishMeta { } private void updateBoundingBox(State state) { - switch (state) { - case UNPUFFED -> setBoundingBox(.35D, .35D); - case SEMI_PUFFED -> setBoundingBox(.5D, .5D); - default -> setBoundingBox(.7D, .7D); - } + this.consumeEntity((entity) -> { + switch (state) { + case UNPUFFED -> entity.setBoundingBox(.35D, .35D, .35D); + case SEMI_PUFFED -> entity.setBoundingBox(.5D, .5D, .5D); + default -> entity.setBoundingBox(.7D, .7D, .7D); + } + }); } public enum State {