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) {
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));
}

View File

@ -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<Entity> 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<Entity> consumer) {
Entity entity = this.entityRef.get();
if (entity != null) {
consumer.accept(entity);
}
}
}

View File

@ -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));
}

View File

@ -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));
}

View File

@ -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));
}

View File

@ -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));
}

View File

@ -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 {