Add hint in BlockGetter to only retrieve a block type at a position (ignore the handler + nbt)

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-09-18 16:18:41 +02:00
parent dda90a6dfe
commit a3ff3b25c4
3 changed files with 25 additions and 9 deletions

View File

@ -7,6 +7,7 @@ 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.instance.block.BlockGetter;
import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.utils.chunk.ChunkUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -121,10 +122,10 @@ public class CollisionUtils {
// Collision at chunk border // Collision at chunk border
return true; return true;
} }
final Block block = chunk.getBlock(newCorner); final Block block = chunk.getBlock(newCorner, BlockGetter.Condition.TYPE);
// TODO: block collision boxes // TODO: block collision boxes
// TODO: for the moment, always consider a full block // TODO: for the moment, always consider a full block
if (block.isSolid()) { if (block != null && block.isSolid()) {
corners.set(cornerIndex, new Vec( corners.set(cornerIndex, new Vec(
Math.abs(axis.x()) > 10e-16 ? newCorner.blockX() - axis.x() * sign : originalCorner.x(), Math.abs(axis.x()) > 10e-16 ? newCorner.blockX() - axis.x() * sign : originalCorner.x(),
Math.abs(axis.y()) > 10e-16 ? newCorner.blockY() - axis.y() * sign : originalCorner.y(), Math.abs(axis.y()) > 10e-16 ? newCorner.blockY() - axis.y() * sign : originalCorner.y(),

View File

@ -101,10 +101,12 @@ public class DynamicChunk extends Chunk {
@Override @Override
public @Nullable Block getBlock(int x, int y, int z, @NotNull Condition condition) { public @Nullable Block getBlock(int x, int y, int z, @NotNull Condition condition) {
// Verify if the block object is present // Verify if the block object is present
final Block entry = !entries.isEmpty() ? if (condition != Condition.TYPE) {
entries.get(ChunkUtils.getBlockIndex(x, y, z)) : null; final Block entry = !entries.isEmpty() ?
if (entry != null || condition == Condition.CACHED) { entries.get(ChunkUtils.getBlockIndex(x, y, z)) : null;
return entry; if (entry != null || condition == Condition.CACHED) {
return entry;
}
} }
// Retrieve the block from state id // Retrieve the block from state id
final Section section = getOptionalSection(y); final Section section = getOptionalSection(y);

View File

@ -1,6 +1,7 @@
package net.minestom.server.instance.block; package net.minestom.server.instance.block;
import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Point;
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;
@ -21,6 +22,11 @@ public interface BlockGetter {
return Objects.requireNonNull(getBlock(point, Condition.NONE)); return Objects.requireNonNull(getBlock(point, Condition.NONE));
} }
/**
* Represents a hint to retrieve blocks more efficiently.
* Implementing interfaces do not have to honor this.
*/
@ApiStatus.Experimental
enum Condition { enum Condition {
/** /**
* Returns a block no matter what. * Returns a block no matter what.
@ -28,10 +34,17 @@ public interface BlockGetter {
*/ */
NONE, NONE,
/** /**
* Returns a block only if it has a handler or nbt. * Hints that the method should return only if the block is cached.
* <p> * <p>
* Should be more performant than {@link #NONE}. * Useful if you are only interested in a block handler or nbt.
*/ */
CACHED CACHED,
/**
* Hints that we only care about the block type.
* <p>
* Useful if you need to retrieve registry information about the block.
* Be aware that the returned block may not return the proper handler/nbt.
*/
TYPE
} }
} }