diff --git a/src/main/java/net/minestom/server/collision/CollisionUtils.java b/src/main/java/net/minestom/server/collision/CollisionUtils.java index 17578bd02..b41cd03d1 100644 --- a/src/main/java/net/minestom/server/collision/CollisionUtils.java +++ b/src/main/java/net/minestom/server/collision/CollisionUtils.java @@ -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()) { diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 515328e8a..af886b94b 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -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(); diff --git a/src/main/java/net/minestom/server/instance/Chunk.java b/src/main/java/net/minestom/server/instance/Chunk.java index 8bf7f8688..83e70c745 100644 --- a/src/main/java/net/minestom/server/instance/Chunk.java +++ b/src/main/java/net/minestom/server/instance/Chunk.java @@ -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; diff --git a/src/main/java/net/minestom/server/instance/Instance.java b/src/main/java/net/minestom/server/instance/Instance.java index c117a88e5..34beae8c5 100644 --- a/src/main/java/net/minestom/server/instance/Instance.java +++ b/src/main/java/net/minestom/server/instance/Instance.java @@ -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(); diff --git a/src/main/java/net/minestom/server/instance/batch/Batch.java b/src/main/java/net/minestom/server/instance/batch/Batch.java index 0a5f84fb8..2f42e8f63 100644 --- a/src/main/java/net/minestom/server/instance/batch/Batch.java +++ b/src/main/java/net/minestom/server/instance/batch/Batch.java @@ -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 extends BlockSetter { +public interface Batch extends Block.Setter { ExecutorService BLOCK_BATCH_POOL = new MinestomThreadPool( MinecraftServer.THREAD_COUNT_BLOCK_BATCH, diff --git a/src/main/java/net/minestom/server/instance/block/Block.java b/src/main/java/net/minestom/server/instance/block/Block.java index b8b6029b4..45979cdc6 100644 --- a/src/main/java/net/minestom/server/instance/block/Block.java +++ b/src/main/java/net/minestom/server/instance/block/Block.java @@ -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. + *

+ * 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. + *

+ * Useful if you are only interested in a block handler or nbt. + */ + CACHED, + /** + * Hints that we only care about the block type. + *

+ * 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 + } + } + } diff --git a/src/main/java/net/minestom/server/instance/block/BlockGetter.java b/src/main/java/net/minestom/server/instance/block/BlockGetter.java deleted file mode 100644 index e3c3a653f..000000000 --- a/src/main/java/net/minestom/server/instance/block/BlockGetter.java +++ /dev/null @@ -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. - *

- * Useful if you are only interested in a block handler or nbt. - */ - CACHED, - /** - * Hints that we only care about the block type. - *

- * 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 - } -} diff --git a/src/main/java/net/minestom/server/instance/block/BlockSetter.java b/src/main/java/net/minestom/server/instance/block/BlockSetter.java deleted file mode 100644 index c61758ea9..000000000 --- a/src/main/java/net/minestom/server/instance/block/BlockSetter.java +++ /dev/null @@ -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. - *

- * 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); - } -}