ShapeImpl should not depend on the block material

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2022-04-13 12:07:43 +02:00
parent 59d5bfe6d1
commit 50a7d7ca81
3 changed files with 27 additions and 20 deletions

View File

@ -8,14 +8,12 @@ import net.minestom.server.instance.Chunk;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.WorldBorder;
import net.minestom.server.instance.block.Block;
import net.minestom.server.item.Material;
import net.minestom.server.registry.Registry;
import net.minestom.server.utils.chunk.ChunkCache;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Supplier;
@ApiStatus.Internal
@ApiStatus.Experimental
public final class CollisionUtils {
@ -48,9 +46,9 @@ public final class CollisionUtils {
* @return the result of physics simulation
*/
public static PhysicsResult handlePhysics(@NotNull Instance instance, @Nullable Chunk chunk,
@NotNull BoundingBox boundingBox,
@NotNull Pos position, @NotNull Vec velocity,
@Nullable PhysicsResult lastPhysicsResult) {
@NotNull BoundingBox boundingBox,
@NotNull Pos position, @NotNull Vec velocity,
@Nullable PhysicsResult lastPhysicsResult) {
final Block.Getter getter = new ChunkCache(instance, chunk != null ? chunk : instance.getChunkAt(position), Block.STONE);
return BlockCollision.handlePhysics(boundingBox,
velocity, position,
@ -69,8 +67,8 @@ public final class CollisionUtils {
* @return true is shape is reachable by the given line of sight; false otherwise.
*/
public static boolean isLineOfSightReachingShape(@NotNull Instance instance, @Nullable Chunk chunk,
@NotNull Point start, @NotNull Point end,
@NotNull Shape shape) {
@NotNull Point start, @NotNull Point end,
@NotNull Shape shape) {
final PhysicsResult result = handlePhysics(instance, chunk,
BoundingBox.ZERO,
Pos.fromPoint(start), Vec.fromPoint(end.sub(start)),
@ -114,7 +112,7 @@ public final class CollisionUtils {
};
}
public static Shape parseBlockShape(String str, Supplier<Material> block) {
return ShapeImpl.parseBlockFromRegistry(str, block);
public static Shape parseBlockShape(String str, Registry.BlockEntry blockEntry) {
return ShapeImpl.parseBlockFromRegistry(str, blockEntry);
}
}

View File

@ -4,10 +4,10 @@ import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
import it.unimi.dsi.fastutil.doubles.DoubleList;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.item.Material;
import net.minestom.server.instance.block.Block;
import net.minestom.server.registry.Registry;
import org.jetbrains.annotations.NotNull;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -15,11 +15,13 @@ final class ShapeImpl implements Shape {
private static final Pattern PATTERN = Pattern.compile("\\d.\\d{1,3}", Pattern.MULTILINE);
private final BoundingBox[] blockSections;
private final Point relativeStart, relativeEnd;
private final Supplier<Material> block;
private ShapeImpl(BoundingBox[] boundingBoxes, Supplier<Material> block) {
private final Registry.BlockEntry blockEntry;
private Block block;
private ShapeImpl(BoundingBox[] boundingBoxes, Registry.BlockEntry blockEntry) {
this.blockSections = boundingBoxes;
this.block = block;
this.blockEntry = blockEntry;
// Find bounds
{
double minX = 1, minY = 1, minZ = 1;
@ -39,7 +41,7 @@ final class ShapeImpl implements Shape {
}
}
static ShapeImpl parseBlockFromRegistry(String str, Supplier<Material> block) {
static ShapeImpl parseBlockFromRegistry(String str, Registry.BlockEntry blockEntry) {
final Matcher matcher = PATTERN.matcher(str);
DoubleList vals = new DoubleArrayList();
while (matcher.find()) {
@ -64,7 +66,7 @@ final class ShapeImpl implements Shape {
assert bb.minZ() == minZ;
boundingBoxes[i] = bb;
}
return new ShapeImpl(boundingBoxes, block);
return new ShapeImpl(boundingBoxes, blockEntry);
}
@Override
@ -86,7 +88,8 @@ final class ShapeImpl implements Shape {
}
@Override
public boolean intersectBoxSwept(@NotNull Point rayStart, @NotNull Point rayDirection, @NotNull Point shapePos, @NotNull BoundingBox moving, @NotNull SweepResult finalResult) {
public boolean intersectBoxSwept(@NotNull Point rayStart, @NotNull Point rayDirection,
@NotNull Point shapePos, @NotNull BoundingBox moving, @NotNull SweepResult finalResult) {
boolean hitBlock = false;
for (BoundingBox blockSection : blockSections) {
// Fast check to see if a collision happens
@ -97,10 +100,16 @@ final class ShapeImpl implements Shape {
if (RayUtils.SweptAABB(moving, rayStart, rayDirection, blockSection, shapePos, finalResult)) {
finalResult.collidedShapePosition = shapePos;
finalResult.collidedShape = this;
finalResult.blockType = block.get().block();
finalResult.blockType = block();
}
hitBlock = true;
}
return hitBlock;
}
private Block block() {
Block block = this.block;
if (block == null) this.block = block = Block.fromStateId((short) blockEntry.stateId());
return block;
}
}

View File

@ -200,7 +200,7 @@ public final class Registry {
}
{
final String string = main.getString("collisionShape");
this.shape = CollisionUtils.parseBlockShape(string, this.materialSupplier);
this.shape = CollisionUtils.parseBlockShape(string, this);
}
}