mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-02 13:31:41 +01:00
ShapeImpl should not depend on the block material
Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
parent
59d5bfe6d1
commit
50a7d7ca81
@ -8,14 +8,12 @@ import net.minestom.server.instance.Chunk;
|
|||||||
import net.minestom.server.instance.Instance;
|
import net.minestom.server.instance.Instance;
|
||||||
import net.minestom.server.instance.WorldBorder;
|
import net.minestom.server.instance.WorldBorder;
|
||||||
import net.minestom.server.instance.block.Block;
|
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 net.minestom.server.utils.chunk.ChunkCache;
|
||||||
import org.jetbrains.annotations.ApiStatus;
|
import org.jetbrains.annotations.ApiStatus;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
|
|
||||||
@ApiStatus.Internal
|
@ApiStatus.Internal
|
||||||
@ApiStatus.Experimental
|
@ApiStatus.Experimental
|
||||||
public final class CollisionUtils {
|
public final class CollisionUtils {
|
||||||
@ -48,9 +46,9 @@ public final class CollisionUtils {
|
|||||||
* @return the result of physics simulation
|
* @return the result of physics simulation
|
||||||
*/
|
*/
|
||||||
public static PhysicsResult handlePhysics(@NotNull Instance instance, @Nullable Chunk chunk,
|
public static PhysicsResult handlePhysics(@NotNull Instance instance, @Nullable Chunk chunk,
|
||||||
@NotNull BoundingBox boundingBox,
|
@NotNull BoundingBox boundingBox,
|
||||||
@NotNull Pos position, @NotNull Vec velocity,
|
@NotNull Pos position, @NotNull Vec velocity,
|
||||||
@Nullable PhysicsResult lastPhysicsResult) {
|
@Nullable PhysicsResult lastPhysicsResult) {
|
||||||
final Block.Getter getter = new ChunkCache(instance, chunk != null ? chunk : instance.getChunkAt(position), Block.STONE);
|
final Block.Getter getter = new ChunkCache(instance, chunk != null ? chunk : instance.getChunkAt(position), Block.STONE);
|
||||||
return BlockCollision.handlePhysics(boundingBox,
|
return BlockCollision.handlePhysics(boundingBox,
|
||||||
velocity, position,
|
velocity, position,
|
||||||
@ -69,8 +67,8 @@ public final class CollisionUtils {
|
|||||||
* @return true is shape is reachable by the given line of sight; false otherwise.
|
* @return true is shape is reachable by the given line of sight; false otherwise.
|
||||||
*/
|
*/
|
||||||
public static boolean isLineOfSightReachingShape(@NotNull Instance instance, @Nullable Chunk chunk,
|
public static boolean isLineOfSightReachingShape(@NotNull Instance instance, @Nullable Chunk chunk,
|
||||||
@NotNull Point start, @NotNull Point end,
|
@NotNull Point start, @NotNull Point end,
|
||||||
@NotNull Shape shape) {
|
@NotNull Shape shape) {
|
||||||
final PhysicsResult result = handlePhysics(instance, chunk,
|
final PhysicsResult result = handlePhysics(instance, chunk,
|
||||||
BoundingBox.ZERO,
|
BoundingBox.ZERO,
|
||||||
Pos.fromPoint(start), Vec.fromPoint(end.sub(start)),
|
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) {
|
public static Shape parseBlockShape(String str, Registry.BlockEntry blockEntry) {
|
||||||
return ShapeImpl.parseBlockFromRegistry(str, block);
|
return ShapeImpl.parseBlockFromRegistry(str, blockEntry);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,10 @@ import it.unimi.dsi.fastutil.doubles.DoubleArrayList;
|
|||||||
import it.unimi.dsi.fastutil.doubles.DoubleList;
|
import it.unimi.dsi.fastutil.doubles.DoubleList;
|
||||||
import net.minestom.server.coordinate.Point;
|
import net.minestom.server.coordinate.Point;
|
||||||
import net.minestom.server.coordinate.Vec;
|
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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.function.Supplier;
|
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
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 static final Pattern PATTERN = Pattern.compile("\\d.\\d{1,3}", Pattern.MULTILINE);
|
||||||
private final BoundingBox[] blockSections;
|
private final BoundingBox[] blockSections;
|
||||||
private final Point relativeStart, relativeEnd;
|
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.blockSections = boundingBoxes;
|
||||||
this.block = block;
|
this.blockEntry = blockEntry;
|
||||||
// Find bounds
|
// Find bounds
|
||||||
{
|
{
|
||||||
double minX = 1, minY = 1, minZ = 1;
|
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);
|
final Matcher matcher = PATTERN.matcher(str);
|
||||||
DoubleList vals = new DoubleArrayList();
|
DoubleList vals = new DoubleArrayList();
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
@ -64,7 +66,7 @@ final class ShapeImpl implements Shape {
|
|||||||
assert bb.minZ() == minZ;
|
assert bb.minZ() == minZ;
|
||||||
boundingBoxes[i] = bb;
|
boundingBoxes[i] = bb;
|
||||||
}
|
}
|
||||||
return new ShapeImpl(boundingBoxes, block);
|
return new ShapeImpl(boundingBoxes, blockEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -86,7 +88,8 @@ final class ShapeImpl implements Shape {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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;
|
boolean hitBlock = false;
|
||||||
for (BoundingBox blockSection : blockSections) {
|
for (BoundingBox blockSection : blockSections) {
|
||||||
// Fast check to see if a collision happens
|
// 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)) {
|
if (RayUtils.SweptAABB(moving, rayStart, rayDirection, blockSection, shapePos, finalResult)) {
|
||||||
finalResult.collidedShapePosition = shapePos;
|
finalResult.collidedShapePosition = shapePos;
|
||||||
finalResult.collidedShape = this;
|
finalResult.collidedShape = this;
|
||||||
finalResult.blockType = block.get().block();
|
finalResult.blockType = block();
|
||||||
}
|
}
|
||||||
hitBlock = true;
|
hitBlock = true;
|
||||||
}
|
}
|
||||||
return hitBlock;
|
return hitBlock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Block block() {
|
||||||
|
Block block = this.block;
|
||||||
|
if (block == null) this.block = block = Block.fromStateId((short) blockEntry.stateId());
|
||||||
|
return block;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -200,7 +200,7 @@ public final class Registry {
|
|||||||
}
|
}
|
||||||
{
|
{
|
||||||
final String string = main.getString("collisionShape");
|
final String string = main.getString("collisionShape");
|
||||||
this.shape = CollisionUtils.parseBlockShape(string, this.materialSupplier);
|
this.shape = CollisionUtils.parseBlockShape(string, this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user