Code review

This commit is contained in:
Шандуренко Константин Владимирович 2021-08-31 15:55:24 +03:00
parent 34ec59dc68
commit 32b33d6bf9

View File

@ -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 new Vec(getMinX(), getMinY(), getMinZ()),
@NotNull List<Vec> producePoints() { new Vec(getMaxX(), getMinY(), getMinZ()),
return List.of( new Vec(getMaxX(), getMinY(), getMaxZ()),
new Vec(getMinX(), getMinY(), getMinZ()), new Vec(getMinX(), getMinY(), getMaxZ())
new Vec(getMaxX(), getMinY(), getMinZ()), ));
new Vec(getMaxX(), getMinY(), getMaxZ()), private final CachedFace topFace = new CachedFace(() -> List.of(
new Vec(getMinX(), getMinY(), getMaxZ()) new Vec(getMinX(), getMaxY(), getMinZ()),
); new Vec(getMaxX(), getMaxY(), getMinZ()),
} new Vec(getMaxX(), getMaxY(), getMaxZ()),
}; new Vec(getMinX(), getMaxY(), getMaxZ())
private final CachedFace topFace = new CachedFace() { ));
@Override private final CachedFace leftFace = new CachedFace(() -> List.of(
@NotNull List<Vec> producePoints() { new Vec(getMinX(), getMinY(), getMinZ()),
return List.of( new Vec(getMinX(), getMaxY(), getMinZ()),
new Vec(getMinX(), getMaxY(), getMinZ()), new Vec(getMinX(), getMaxY(), getMaxZ()),
new Vec(getMaxX(), getMaxY(), getMinZ()), new Vec(getMinX(), getMinY(), getMaxZ())
new Vec(getMaxX(), getMaxY(), getMaxZ()), ));
new Vec(getMinX(), getMaxY(), getMaxZ()) private final CachedFace rightFace = new CachedFace(() -> List.of(
); new Vec(getMaxX(), getMinY(), getMinZ()),
} new Vec(getMaxX(), getMaxY(), getMinZ()),
}; new Vec(getMaxX(), getMaxY(), getMaxZ()),
private final CachedFace leftFace = new CachedFace() { new Vec(getMaxX(), getMinY(), getMaxZ())
@Override ));
@NotNull List<Vec> producePoints() { private final CachedFace frontFace = new CachedFace(() -> List.of(
return List.of( new Vec(getMinX(), getMinY(), getMinZ()),
new Vec(getMinX(), getMinY(), getMinZ()), new Vec(getMaxX(), getMinY(), getMinZ()),
new Vec(getMinX(), getMaxY(), getMinZ()), new Vec(getMaxX(), getMaxY(), getMinZ()),
new Vec(getMinX(), getMaxY(), getMaxZ()), new Vec(getMinX(), getMaxY(), getMinZ())
new Vec(getMinX(), getMinY(), getMaxZ()) ));
); private final CachedFace backFace = new CachedFace(() -> List.of(
} new Vec(getMinX(), getMinY(), getMaxZ()),
}; new Vec(getMaxX(), getMinY(), getMaxZ()),
private final CachedFace rightFace = new CachedFace() { new Vec(getMaxX(), getMaxY(), getMaxZ()),
@Override new Vec(getMinX(), getMaxY(), getMaxZ())
@NotNull List<Vec> producePoints() { ));
return List.of(
new Vec(getMaxX(), getMinY(), getMinZ()),
new Vec(getMaxX(), getMaxY(), getMinZ()),
new Vec(getMaxX(), getMaxY(), getMaxZ()),
new Vec(getMaxX(), getMinY(), getMaxZ())
);
}
};
private final CachedFace frontFace = new CachedFace() {
@Override
@NotNull List<Vec> producePoints() {
return List.of(
new Vec(getMinX(), getMinY(), getMinZ()),
new Vec(getMaxX(), getMinY(), getMinZ()),
new Vec(getMaxX(), getMaxY(), getMinZ()),
new Vec(getMinX(), getMaxY(), getMinZ())
);
}
};
private final CachedFace backFace = new CachedFace() {
@Override
@NotNull List<Vec> producePoints() {
return List.of(
new Vec(getMinX(), getMinY(), getMaxZ()),
new Vec(getMaxX(), getMinY(), getMaxZ()),
new Vec(getMaxX(), 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;