Use per-block handler

This commit is contained in:
TheMode 2021-06-12 13:57:59 +02:00
parent a2bd0f7906
commit ab51b50072
3 changed files with 20 additions and 20 deletions

View File

@ -564,7 +564,7 @@ public class Entity implements Viewable, Tickable, EventHandler, DataContainer,
final Block block = finalChunk.getBlock(blockPosition); final Block block = finalChunk.getBlock(blockPosition);
final BlockHandler handler = block.getHandler(); final BlockHandler handler = block.getHandler();
if (handler != null) { if (handler != null) {
drag = handler.getDrag(instance, block, blockPosition); drag = handler.getDrag(instance, blockPosition);
} else { } else {
// Default ground drag // Default ground drag
drag = 0.5f; 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 // checks that we are actually in the block, and not just here because of a rounding error
if (boundingBox.intersect(tmpPosition)) { if (boundingBox.intersect(tmpPosition)) {
// TODO: replace with check with custom block bounding box // TODO: replace with check with custom block bounding box
handler.handleContact(instance, block, tmpPosition, this); handler.handleContact(instance, tmpPosition, this);
} }
} }
} }

View File

@ -147,8 +147,8 @@ public class InstanceContainer extends Instance {
} }
setAlreadyChanged(blockPosition, block); setAlreadyChanged(blockPosition, block);
final Block previousBlock = chunk.getBlock(blockPosition); final BlockHandler previousHandler = chunk.getBlock(blockPosition)
final BlockHandler previousHandler = previousBlock.getHandler(); .getHandler();
// Change id based on neighbors // Change id based on neighbors
block = executeBlockPlacementRule(block, blockPosition); block = executeBlockPlacementRule(block, blockPosition);
@ -164,12 +164,12 @@ public class InstanceContainer extends Instance {
if (previousHandler != null) { if (previousHandler != null) {
// Previous destroy // Previous destroy
previousHandler.onDestroy(this, previousBlock, blockPosition); previousHandler.onDestroy(this, blockPosition);
} }
final BlockHandler handler = block.getHandler(); final BlockHandler handler = block.getHandler();
if (handler != null) { if (handler != null) {
// New placement // New placement
handler.onPlace(this, block, blockPosition); handler.onPlace(this, blockPosition);
} }
} }
} }

View File

@ -7,25 +7,29 @@ import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.NamespaceID; import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull; 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. * Called when a block has been placed.
* *
* @param instance the instance of the block * @param instance the instance of the block
* @param block the block
* @param blockPosition the position of 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. * Called when a block has been destroyed or replaced.
* *
* @param instance the instance of the block * @param instance the instance of the block
* @param block the block
* @param blockPosition the position of 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 * 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 player the player interacting
* @param hand the hand used to interact * @param hand the hand used to interact
* @param block the block
* @param blockPosition the position of this block * @param blockPosition the position of this block
* @return true if this block blocks normal item use, false otherwise * @return true if this block blocks normal item use, false otherwise
*/ */
boolean onInteract(@NotNull Player player, @NotNull Player.Hand hand, public abstract boolean onInteract(@NotNull Player player, @NotNull Player.Hand hand, @NotNull BlockPosition blockPosition);
@NotNull Block block, @NotNull BlockPosition blockPosition);
/** /**
* Gets the drag of this block. * Gets the drag of this block.
@ -46,23 +48,21 @@ public interface BlockHandler {
* Has to be between 0 and 1. * Has to be between 0 and 1.
* *
* @param instance the instance of the block * @param instance the instance of the block
* @param block the block
* @param blockPosition the block position * @param blockPosition the block position
* @return the drag to apply * @return the drag to apply
*/ */
default float getDrag(@NotNull Instance instance, @NotNull Block block, @NotNull BlockPosition blockPosition) { public float getDrag(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return 0.5f; return block.registry().friction();
} }
/** /**
* Defines custom behaviour for entities touching this block. * Defines custom behaviour for entities touching this block.
* *
* @param instance the instance * @param instance the instance
* @param block the block
* @param position the position at which the block is * @param position the position at which the block is
* @param touching the entity currently touching the block * @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 * @return the namespace id of this handler
*/ */
@NotNull NamespaceID getNamespaceId(); public abstract @NotNull NamespaceID getNamespaceId();
} }