Make BlockSetter/Getter inner interfaces inside Block

Signed-off-by: TheMode <themode@outlook.fr>
This commit is contained in:
TheMode 2021-11-12 18:07:42 +01:00
parent cd8ea97977
commit ba77a9ea10
8 changed files with 69 additions and 80 deletions

View File

@ -7,7 +7,6 @@ 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.instance.block.BlockGetter;
import net.minestom.server.utils.chunk.ChunkUtils;
import org.jetbrains.annotations.NotNull;
@ -121,7 +120,7 @@ public class CollisionUtils {
// Collision at chunk border
return true;
}
final Block block = chunk.getBlock(newCorner, BlockGetter.Condition.TYPE);
final Block block = chunk.getBlock(newCorner, Block.Getter.Condition.TYPE);
// TODO: block collision boxes
// TODO: for the moment, always consider a full block
if (block != null && block.isSolid()) {

View File

@ -25,7 +25,6 @@ import net.minestom.server.instance.EntityTracker;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.InstanceManager;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockGetter;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.network.packet.server.CachedPacket;
import net.minestom.server.network.packet.server.ServerPacket;
@ -658,7 +657,7 @@ public class Entity implements Viewable, Tickable, TagHandler, PermissionHandler
final Chunk chunk = ChunkUtils.retrieve(instance, currentChunk, x, z);
if (!ChunkUtils.isLoaded(chunk))
continue;
final Block block = chunk.getBlock(x, y, z, BlockGetter.Condition.CACHED);
final Block block = chunk.getBlock(x, y, z, Block.Getter.Condition.CACHED);
if (block == null)
continue;
final BlockHandler handler = block.handler();

View File

@ -7,8 +7,6 @@ import net.minestom.server.coordinate.Vec;
import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFColumnarSpace;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockGetter;
import net.minestom.server.instance.block.BlockSetter;
import net.minestom.server.network.packet.server.play.ChunkDataPacket;
import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagHandler;
@ -34,7 +32,7 @@ import java.util.UUID;
* You generally want to avoid storing references of this object as this could lead to a huge memory leak,
* you should store the chunk coordinates instead.
*/
public abstract class Chunk implements BlockGetter, BlockSetter, Viewable, Tickable, TagHandler {
public abstract class Chunk implements Block.Getter, Block.Setter, Viewable, Tickable, TagHandler {
public static final int CHUNK_SIZE_X = 16;
public static final int CHUNK_SIZE_Z = 16;
public static final int CHUNK_SECTION_SIZE = 16;

View File

@ -15,7 +15,9 @@ import net.minestom.server.entity.Player;
import net.minestom.server.entity.pathfinding.PFInstanceSpace;
import net.minestom.server.event.GlobalHandles;
import net.minestom.server.event.instance.InstanceTickEvent;
import net.minestom.server.instance.block.*;
import net.minestom.server.instance.block.Block;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.instance.block.BlockManager;
import net.minestom.server.network.packet.server.play.BlockActionPacket;
import net.minestom.server.network.packet.server.play.TimeUpdatePacket;
import net.minestom.server.tag.Tag;
@ -49,7 +51,7 @@ import java.util.stream.Collectors;
* you need to be sure to signal the {@link UpdateManager} of the changes using
* {@link UpdateManager#signalChunkLoad(Chunk)} and {@link UpdateManager#signalChunkUnload(Chunk)}.
*/
public abstract class Instance implements BlockGetter, BlockSetter, Tickable, TagHandler, PacketGroupingAudience {
public abstract class Instance implements Block.Getter, Block.Setter, Tickable, TagHandler, PacketGroupingAudience {
protected static final BlockManager BLOCK_MANAGER = MinecraftServer.getBlockManager();
protected static final UpdateManager UPDATE_MANAGER = MinecraftServer.getUpdateManager();

View File

@ -2,7 +2,7 @@ package net.minestom.server.instance.batch;
import net.minestom.server.MinecraftServer;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.BlockSetter;
import net.minestom.server.instance.block.Block;
import net.minestom.server.thread.MinestomThreadPool;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -27,7 +27,7 @@ import java.util.concurrent.ExecutorService;
* @see AbsoluteBlockBatch
* @see RelativeBlockBatch
*/
public interface Batch<C> extends BlockSetter {
public interface Batch<C> extends Block.Setter {
ExecutorService BLOCK_BATCH_POOL = new MinestomThreadPool(
MinecraftServer.THREAD_COUNT_BLOCK_BATCH,

View File

@ -1,5 +1,8 @@
package net.minestom.server.instance.block;
import net.minestom.server.coordinate.Point;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.batch.Batch;
import net.minestom.server.registry.ProtocolObject;
import net.minestom.server.registry.Registry;
import net.minestom.server.tag.Tag;
@ -10,6 +13,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.function.BiPredicate;
/**
@ -195,4 +199,60 @@ public sealed interface Block extends ProtocolObject, TagReadable, Blocks permit
Comparator STATE = (b1, b2) -> b1.stateId() == b2.stateId();
}
/**
* Represents an element which can place blocks at position.
* <p>
* Notably used by {@link Instance}, {@link Batch}.
*/
interface Setter {
void setBlock(int x, int y, int z, @NotNull Block block);
default void setBlock(@NotNull Point blockPosition, @NotNull Block block) {
setBlock(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), block);
}
}
interface Getter {
@UnknownNullability Block getBlock(int x, int y, int z, @NotNull Condition condition);
default @UnknownNullability Block getBlock(@NotNull Point point, @NotNull Condition condition) {
return getBlock(point.blockX(), point.blockY(), point.blockZ(), condition);
}
default @NotNull Block getBlock(int x, int y, int z) {
return Objects.requireNonNull(getBlock(x, y, z, Condition.NONE));
}
default @NotNull Block getBlock(@NotNull Point point) {
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 {
/**
* Returns a block no matter what.
* {@link Block#AIR} being the default result.
*/
NONE,
/**
* Hints that the method should return only if the block is cached.
* <p>
* Useful if you are only interested in a block handler or nbt.
*/
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
}
}
}

View File

@ -1,50 +0,0 @@
package net.minestom.server.instance.block;
import net.minestom.server.coordinate.Point;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnknownNullability;
import java.util.Objects;
public interface BlockGetter {
@UnknownNullability Block getBlock(int x, int y, int z, @NotNull Condition condition);
default @UnknownNullability Block getBlock(@NotNull Point point, @NotNull Condition condition) {
return getBlock(point.blockX(), point.blockY(), point.blockZ(), condition);
}
default @NotNull Block getBlock(int x, int y, int z) {
return Objects.requireNonNull(getBlock(x, y, z, Condition.NONE));
}
default @NotNull Block getBlock(@NotNull Point point) {
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 {
/**
* Returns a block no matter what.
* {@link Block#AIR} being the default result.
*/
NONE,
/**
* Hints that the method should return only if the block is cached.
* <p>
* Useful if you are only interested in a block handler or nbt.
*/
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
}
}

View File

@ -1,19 +0,0 @@
package net.minestom.server.instance.block;
import net.minestom.server.coordinate.Point;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.batch.Batch;
import org.jetbrains.annotations.NotNull;
/**
* Represents an element which can place blocks at position.
* <p>
* Notably used by {@link Instance}, {@link Batch}.
*/
public interface BlockSetter {
void setBlock(int x, int y, int z, @NotNull Block block);
default void setBlock(@NotNull Point blockPosition, @NotNull Block block) {
setBlock(blockPosition.blockX(), blockPosition.blockY(), blockPosition.blockZ(), block);
}
}