From ab51b50072cbefc852c64efcc8efe33c83214ecb Mon Sep 17 00:00:00 2001 From: TheMode Date: Sat, 12 Jun 2021 13:57:59 +0200 Subject: [PATCH] Use per-block handler --- .../net/minestom/server/entity/Entity.java | 4 +-- .../server/instance/InstanceContainer.java | 8 +++--- .../server/instance/block/BlockHandler.java | 28 +++++++++---------- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/main/java/net/minestom/server/entity/Entity.java b/src/main/java/net/minestom/server/entity/Entity.java index 140268796..fdcc7dd62 100644 --- a/src/main/java/net/minestom/server/entity/Entity.java +++ b/src/main/java/net/minestom/server/entity/Entity.java @@ -564,7 +564,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer, final Block block = finalChunk.getBlock(blockPosition); final BlockHandler handler = block.getHandler(); if (handler != null) { - drag = handler.getDrag(instance, block, blockPosition); + drag = handler.getDrag(instance, blockPosition); } else { // Default ground drag drag = 0.5f; @@ -621,7 +621,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer, // checks that we are actually in the block, and not just here because of a rounding error if (boundingBox.intersect(tmpPosition)) { // TODO: replace with check with custom block bounding box - handler.handleContact(instance, block, tmpPosition, this); + handler.handleContact(instance, tmpPosition, this); } } } diff --git a/src/main/java/net/minestom/server/instance/InstanceContainer.java b/src/main/java/net/minestom/server/instance/InstanceContainer.java index 9bea531da..c21ae2738 100644 --- a/src/main/java/net/minestom/server/instance/InstanceContainer.java +++ b/src/main/java/net/minestom/server/instance/InstanceContainer.java @@ -147,8 +147,8 @@ public class InstanceContainer extends Instance { } setAlreadyChanged(blockPosition, block); - final Block previousBlock = chunk.getBlock(blockPosition); - final BlockHandler previousHandler = previousBlock.getHandler(); + final BlockHandler previousHandler = chunk.getBlock(blockPosition) + .getHandler(); // Change id based on neighbors block = executeBlockPlacementRule(block, blockPosition); @@ -164,12 +164,12 @@ public class InstanceContainer extends Instance { if (previousHandler != null) { // Previous destroy - previousHandler.onDestroy(this, previousBlock, blockPosition); + previousHandler.onDestroy(this, blockPosition); } final BlockHandler handler = block.getHandler(); if (handler != null) { // New placement - handler.onPlace(this, block, blockPosition); + handler.onPlace(this, blockPosition); } } } diff --git a/src/main/java/net/minestom/server/instance/block/BlockHandler.java b/src/main/java/net/minestom/server/instance/block/BlockHandler.java index 008c612c2..bb357386b 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockHandler.java +++ b/src/main/java/net/minestom/server/instance/block/BlockHandler.java @@ -7,25 +7,29 @@ import net.minestom.server.utils.BlockPosition; import net.minestom.server.utils.NamespaceID; import org.jetbrains.annotations.NotNull; -public interface BlockHandler { +public abstract class BlockHandler { + + private final Block block; + + public BlockHandler(Block block) { + this.block = block; + } /** * Called when a block has been placed. * * @param instance the instance of the block - * @param block the block * @param blockPosition the position of the block */ - void onPlace(@NotNull Instance instance, @NotNull Block block, @NotNull BlockPosition blockPosition); + public abstract void onPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition); /** * Called when a block has been destroyed or replaced. * * @param instance the instance of the block - * @param block the block * @param blockPosition the position of the block */ - void onDestroy(@NotNull Instance instance, @NotNull Block block, @NotNull BlockPosition blockPosition); + public abstract void onDestroy(@NotNull Instance instance, @NotNull BlockPosition blockPosition); /** * Handles interactions with this block. Can also block normal item use (containers should block when opening the @@ -33,12 +37,10 @@ public interface BlockHandler { * * @param player the player interacting * @param hand the hand used to interact - * @param block the block * @param blockPosition the position of this block * @return true if this block blocks normal item use, false otherwise */ - boolean onInteract(@NotNull Player player, @NotNull Player.Hand hand, - @NotNull Block block, @NotNull BlockPosition blockPosition); + public abstract boolean onInteract(@NotNull Player player, @NotNull Player.Hand hand, @NotNull BlockPosition blockPosition); /** * Gets the drag of this block. @@ -46,23 +48,21 @@ public interface BlockHandler { * Has to be between 0 and 1. * * @param instance the instance of the block - * @param block the block * @param blockPosition the block position * @return the drag to apply */ - default float getDrag(@NotNull Instance instance, @NotNull Block block, @NotNull BlockPosition blockPosition) { - return 0.5f; + public float getDrag(@NotNull Instance instance, @NotNull BlockPosition blockPosition) { + return block.registry().friction(); } /** * Defines custom behaviour for entities touching this block. * * @param instance the instance - * @param block the block * @param position the position at which the block is * @param touching the entity currently touching the block */ - default void handleContact(@NotNull Instance instance, @NotNull Block block, @NotNull BlockPosition position, @NotNull Entity touching) { + public void handleContact(@NotNull Instance instance, @NotNull BlockPosition position, @NotNull Entity touching) { } /** @@ -72,5 +72,5 @@ public interface BlockHandler { * * @return the namespace id of this handler */ - @NotNull NamespaceID getNamespaceId(); + public abstract @NotNull NamespaceID getNamespaceId(); }