mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-04 23:47:59 +01:00
Code review
This commit is contained in:
parent
34ec59dc68
commit
32b33d6bf9
@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* See https://wiki.vg/Entity_metadata#Mobs_2
|
* See https://wiki.vg/Entity_metadata#Mobs_2
|
||||||
@ -19,72 +20,42 @@ public class BoundingBox {
|
|||||||
private final Entity entity;
|
private final Entity entity;
|
||||||
private final double x, y, z;
|
private final double x, y, z;
|
||||||
|
|
||||||
private final CachedFace bottomFace = new CachedFace() {
|
private final CachedFace bottomFace = new CachedFace(() -> List.of(
|
||||||
@Override
|
|
||||||
@NotNull List<Vec> producePoints() {
|
|
||||||
return List.of(
|
|
||||||
new Vec(getMinX(), getMinY(), getMinZ()),
|
new Vec(getMinX(), getMinY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMinY(), getMinZ()),
|
new Vec(getMaxX(), getMinY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMinY(), getMaxZ()),
|
new Vec(getMaxX(), getMinY(), getMaxZ()),
|
||||||
new Vec(getMinX(), getMinY(), getMaxZ())
|
new Vec(getMinX(), getMinY(), getMaxZ())
|
||||||
);
|
));
|
||||||
}
|
private final CachedFace topFace = new CachedFace(() -> List.of(
|
||||||
};
|
|
||||||
private final CachedFace topFace = new CachedFace() {
|
|
||||||
@Override
|
|
||||||
@NotNull List<Vec> producePoints() {
|
|
||||||
return List.of(
|
|
||||||
new Vec(getMinX(), getMaxY(), getMinZ()),
|
new Vec(getMinX(), getMaxY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMaxY(), getMinZ()),
|
new Vec(getMaxX(), getMaxY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMaxY(), getMaxZ()),
|
new Vec(getMaxX(), getMaxY(), getMaxZ()),
|
||||||
new Vec(getMinX(), getMaxY(), getMaxZ())
|
new Vec(getMinX(), getMaxY(), getMaxZ())
|
||||||
);
|
));
|
||||||
}
|
private final CachedFace leftFace = new CachedFace(() -> List.of(
|
||||||
};
|
|
||||||
private final CachedFace leftFace = new CachedFace() {
|
|
||||||
@Override
|
|
||||||
@NotNull List<Vec> producePoints() {
|
|
||||||
return List.of(
|
|
||||||
new Vec(getMinX(), getMinY(), getMinZ()),
|
new Vec(getMinX(), getMinY(), getMinZ()),
|
||||||
new Vec(getMinX(), getMaxY(), getMinZ()),
|
new Vec(getMinX(), getMaxY(), getMinZ()),
|
||||||
new Vec(getMinX(), getMaxY(), getMaxZ()),
|
new Vec(getMinX(), getMaxY(), getMaxZ()),
|
||||||
new Vec(getMinX(), getMinY(), getMaxZ())
|
new Vec(getMinX(), getMinY(), getMaxZ())
|
||||||
);
|
));
|
||||||
}
|
private final CachedFace rightFace = new CachedFace(() -> List.of(
|
||||||
};
|
|
||||||
private final CachedFace rightFace = new CachedFace() {
|
|
||||||
@Override
|
|
||||||
@NotNull List<Vec> producePoints() {
|
|
||||||
return List.of(
|
|
||||||
new Vec(getMaxX(), getMinY(), getMinZ()),
|
new Vec(getMaxX(), getMinY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMaxY(), getMinZ()),
|
new Vec(getMaxX(), getMaxY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMaxY(), getMaxZ()),
|
new Vec(getMaxX(), getMaxY(), getMaxZ()),
|
||||||
new Vec(getMaxX(), getMinY(), getMaxZ())
|
new Vec(getMaxX(), getMinY(), getMaxZ())
|
||||||
);
|
));
|
||||||
}
|
private final CachedFace frontFace = new CachedFace(() -> List.of(
|
||||||
};
|
|
||||||
private final CachedFace frontFace = new CachedFace() {
|
|
||||||
@Override
|
|
||||||
@NotNull List<Vec> producePoints() {
|
|
||||||
return List.of(
|
|
||||||
new Vec(getMinX(), getMinY(), getMinZ()),
|
new Vec(getMinX(), getMinY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMinY(), getMinZ()),
|
new Vec(getMaxX(), getMinY(), getMinZ()),
|
||||||
new Vec(getMaxX(), getMaxY(), getMinZ()),
|
new Vec(getMaxX(), getMaxY(), getMinZ()),
|
||||||
new Vec(getMinX(), getMaxY(), getMinZ())
|
new Vec(getMinX(), getMaxY(), getMinZ())
|
||||||
);
|
));
|
||||||
}
|
private final CachedFace backFace = new CachedFace(() -> List.of(
|
||||||
};
|
|
||||||
private final CachedFace backFace = new CachedFace() {
|
|
||||||
@Override
|
|
||||||
@NotNull List<Vec> producePoints() {
|
|
||||||
return List.of(
|
|
||||||
new Vec(getMinX(), getMinY(), getMaxZ()),
|
new Vec(getMinX(), getMinY(), getMaxZ()),
|
||||||
new Vec(getMaxX(), getMinY(), getMaxZ()),
|
new Vec(getMaxX(), getMinY(), getMaxZ()),
|
||||||
new Vec(getMaxX(), getMaxY(), getMaxZ()),
|
new Vec(getMaxX(), getMaxY(), getMaxZ()),
|
||||||
new Vec(getMinX(), getMaxY(), getMaxZ())
|
new Vec(getMinX(), getMaxY(), getMaxZ())
|
||||||
);
|
));
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a {@link BoundingBox} linked to an {@link Entity} and with a specific size.
|
* Creates a {@link BoundingBox} linked to an {@link Entity} and with a specific size.
|
||||||
@ -444,18 +415,21 @@ public class BoundingBox {
|
|||||||
X, Y, Z
|
X, Y, Z
|
||||||
}
|
}
|
||||||
|
|
||||||
private abstract class CachedFace {
|
private class CachedFace {
|
||||||
|
|
||||||
private final AtomicReference<@NotNull PositionedPoints> reference = new AtomicReference<>(new PositionedPoints());
|
private final AtomicReference<@NotNull PositionedPoints> reference = new AtomicReference<>(new PositionedPoints());
|
||||||
|
private final Supplier<@NotNull List<Vec>> faceProducer;
|
||||||
|
|
||||||
abstract @NotNull List<Vec> producePoints();
|
private CachedFace(Supplier<@NotNull List<Vec>> faceProducer) {
|
||||||
|
this.faceProducer = faceProducer;
|
||||||
|
}
|
||||||
|
|
||||||
@NotNull List<Vec> get() {
|
@NotNull List<Vec> get() {
|
||||||
return reference.updateAndGet(value -> {
|
return reference.updateAndGet(value -> {
|
||||||
Pos entityPosition = entity.getPosition();
|
Pos entityPosition = entity.getPosition();
|
||||||
if (value.lastPosition == null || !value.lastPosition.samePoint(entityPosition)) {
|
if (value.lastPosition == null || !value.lastPosition.samePoint(entityPosition)) {
|
||||||
value.lastPosition = entityPosition;
|
value.lastPosition = entityPosition;
|
||||||
value.points = producePoints();
|
value.points = faceProducer.get();
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}).points;
|
}).points;
|
||||||
|
Loading…
Reference in New Issue
Block a user