Make Entity nullable in Metadata (#596)

This commit is contained in:
Moulberry 2022-01-23 16:34:30 +08:00 committed by GitHub
parent c24bf3d13d
commit 54af9a7c96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 65 additions and 40 deletions

View File

@ -21,12 +21,16 @@ public class AgeableMobMeta extends PathfinderMobMeta {
if (isBaby() == value) { if (isBaby() == value) {
return; return;
} }
BoundingBox bb = this.entity.getBoundingBox(); this.consumeEntity((entity) -> {
if (value) { BoundingBox bb = entity.getBoundingBox();
setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); if (value) {
} else { double width = bb.getWidth() / 2;
setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 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)); super.metadata.setIndex(OFFSET, Metadata.Boolean(value));
} }

View File

@ -4,6 +4,10 @@ import net.kyori.adventure.text.Component;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Metadata; import net.minestom.server.entity.Metadata;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.ref.WeakReference;
import java.util.function.Consumer;
public class EntityMeta { public class EntityMeta {
public static final byte OFFSET = 0; 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 HAS_GLOWING_EFFECT_BIT = 0x40;
private final static byte FLYING_WITH_ELYTRA_BIT = (byte) 0x80; private final static byte FLYING_WITH_ELYTRA_BIT = (byte) 0x80;
protected final Entity entity; private final WeakReference<Entity> entityRef;
protected final Metadata metadata; protected final Metadata metadata;
public EntityMeta(@NotNull Entity entity, @NotNull Metadata metadata) { public EntityMeta(@Nullable Entity entity, @NotNull Metadata metadata) {
this.entity = entity; this.entityRef = new WeakReference<>(entity);
this.metadata = metadata; this.metadata = metadata;
} }
@ -180,12 +184,11 @@ public class EntityMeta {
setMask(index, mask); setMask(index, mask);
} }
protected void setBoundingBox(double x, double y, double z) { protected void consumeEntity(Consumer<Entity> consumer) {
this.entity.setBoundingBox(x, y, z); Entity entity = this.entityRef.get();
} if (entity != null) {
consumer.accept(entity);
protected void setBoundingBox(double width, double height) { }
setBoundingBox(width, height, width);
} }
} }

View File

@ -21,12 +21,16 @@ public class PiglinMeta extends BasePiglinMeta {
if (isBaby() == value) { if (isBaby() == value) {
return; return;
} }
BoundingBox bb = this.entity.getBoundingBox(); this.consumeEntity((entity) -> {
if (value) { BoundingBox bb = entity.getBoundingBox();
setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); if (value) {
} else { double width = bb.getWidth() / 2;
setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 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)); super.metadata.setIndex(OFFSET, Metadata.Boolean(value));
} }

View File

@ -21,12 +21,16 @@ public class ZoglinMeta extends MonsterMeta {
if (isBaby() == value) { if (isBaby() == value) {
return; return;
} }
BoundingBox bb = this.entity.getBoundingBox(); this.consumeEntity((entity) -> {
if (value) { BoundingBox bb = entity.getBoundingBox();
setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); if (value) {
} else { double width = bb.getWidth() / 2;
setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 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)); super.metadata.setIndex(OFFSET, Metadata.Boolean(value));
} }

View File

@ -22,12 +22,16 @@ public class ZombieMeta extends MonsterMeta {
if (isBaby() == value) { if (isBaby() == value) {
return; return;
} }
BoundingBox bb = this.entity.getBoundingBox(); this.consumeEntity((entity) -> {
if (value) { BoundingBox bb = entity.getBoundingBox();
setBoundingBox(bb.getWidth() / 2, bb.getHeight() / 2); if (value) {
} else { double width = bb.getWidth() / 2;
setBoundingBox(bb.getWidth() * 2, bb.getHeight() * 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)); super.metadata.setIndex(OFFSET, Metadata.Boolean(value));
} }

View File

@ -1,5 +1,6 @@
package net.minestom.server.entity.metadata.other; package net.minestom.server.entity.metadata.other;
import net.minestom.server.collision.BoundingBox;
import net.minestom.server.entity.Entity; import net.minestom.server.entity.Entity;
import net.minestom.server.entity.Metadata; import net.minestom.server.entity.Metadata;
import net.minestom.server.entity.metadata.MobMeta; import net.minestom.server.entity.metadata.MobMeta;
@ -18,8 +19,10 @@ public class SlimeMeta extends MobMeta {
} }
public void setSize(int value) { public void setSize(int value) {
float boxSize = 0.51000005f * value; this.consumeEntity((entity) -> {
setBoundingBox(boxSize, boxSize); float boxSize = 0.51000005f * value;
entity.setBoundingBox(boxSize, boxSize, boxSize);
});
super.metadata.setIndex(OFFSET, Metadata.VarInt(value)); super.metadata.setIndex(OFFSET, Metadata.VarInt(value));
} }

View File

@ -1,5 +1,6 @@
package net.minestom.server.entity.metadata.water.fish; 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.Entity;
import net.minestom.server.entity.Metadata; import net.minestom.server.entity.Metadata;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -23,11 +24,13 @@ public class PufferfishMeta extends AbstractFishMeta {
} }
private void updateBoundingBox(State state) { private void updateBoundingBox(State state) {
switch (state) { this.consumeEntity((entity) -> {
case UNPUFFED -> setBoundingBox(.35D, .35D); switch (state) {
case SEMI_PUFFED -> setBoundingBox(.5D, .5D); case UNPUFFED -> entity.setBoundingBox(.35D, .35D, .35D);
default -> setBoundingBox(.7D, .7D); case SEMI_PUFFED -> entity.setBoundingBox(.5D, .5D, .5D);
} default -> entity.setBoundingBox(.7D, .7D, .7D);
}
});
} }
public enum State { public enum State {