Minestom/src/main/java/net/minestom/server/instance/block/BlockHandler.java

310 lines
8.3 KiB
Java
Raw Normal View History

2021-05-24 21:39:30 +02:00
package net.minestom.server.instance.block;
import net.minestom.server.coordinate.Point;
2021-05-29 00:55:24 +02:00
import net.minestom.server.entity.Entity;
2021-05-24 22:10:16 +02:00
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
2021-06-12 19:17:50 +02:00
import net.minestom.server.tag.Tag;
2021-05-24 22:10:16 +02:00
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.ApiStatus;
2021-05-24 22:10:16 +02:00
import org.jetbrains.annotations.NotNull;
2021-06-12 19:17:50 +02:00
import java.util.Collection;
import java.util.List;
2021-06-25 19:10:41 +02:00
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
2021-06-12 19:17:50 +02:00
2021-06-12 16:02:35 +02:00
/**
* Interface used to provide block behavior. Set with {@link Block#withHandler(BlockHandler)}.
* <p>
* Implementations are expected to be thread safe.
*/
2021-06-12 15:06:52 +02:00
public interface BlockHandler {
2021-05-24 22:10:16 +02:00
/**
* Called when a block has been placed.
*
* @param placement the placement details
2021-05-24 22:10:16 +02:00
*/
default void onPlace(@NotNull Placement placement) {
2021-06-13 13:41:01 +02:00
}
2021-05-24 22:10:16 +02:00
/**
* Called when a block has been destroyed or replaced.
*
* @param destroy the destroy details
2021-05-24 22:10:16 +02:00
*/
default void onDestroy(@NotNull Destroy destroy) {
2021-06-13 13:41:01 +02:00
}
2021-05-24 22:10:16 +02:00
/**
* Handles interactions with this block. Can also block normal item use (containers should block when opening the
* menu, this prevents the player from placing a block when opening it for instance).
*
* @param interaction the interaction details
* @return true to let the block interaction happens, false to cancel
2021-05-24 22:10:16 +02:00
*/
default boolean onInteract(@NotNull Interaction interaction) {
return true;
2021-06-13 13:41:01 +02:00
}
2021-05-24 22:10:16 +02:00
2021-05-29 00:55:24 +02:00
/**
* Defines custom behaviour for entities touching this block.
*
* @param touch the contact details
2021-05-29 00:55:24 +02:00
*/
2021-06-17 15:11:19 +02:00
default void onTouch(@NotNull Touch touch) {
2021-05-29 00:55:24 +02:00
}
2021-06-18 14:36:03 +02:00
default void tick(@NotNull Tick tick) {
}
default boolean isTickable() {
return false;
}
2021-06-12 19:17:50 +02:00
default @NotNull Collection<Tag<?>> getBlockEntityTags() {
return List.of();
2021-06-12 19:17:50 +02:00
}
default byte getBlockEntityAction() {
return -1;
}
2021-05-24 22:10:16 +02:00
/**
* Gets the id of this handler.
* <p>
* Used to write the block entity in the anvil world format.
*
* @return the namespace id of this handler
*/
2021-06-12 15:06:52 +02:00
@NotNull NamespaceID getNamespaceId();
/**
* Represents an object forwarded to {@link #onPlace(Placement)}.
*/
2021-10-22 01:55:55 +02:00
sealed class Placement permits PlayerPlacement {
private final Block block;
private final Instance instance;
private final Point blockPosition;
@ApiStatus.Internal
public Placement(Block block, Instance instance, Point blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
}
public @NotNull Block getBlock() {
return block;
}
public @NotNull Instance getInstance() {
return instance;
}
public @NotNull Point getBlockPosition() {
return blockPosition;
}
}
final class PlayerPlacement extends Placement {
private final Player player;
private final Player.Hand hand;
private final BlockFace blockFace;
private final float cursorX, cursorY, cursorZ;
@ApiStatus.Internal
public PlayerPlacement(Block block, Instance instance, Point blockPosition,
Player player, Player.Hand hand, BlockFace blockFace, float cursorX, float cursorY, float cursorZ) {
super(block, instance, blockPosition);
this.player = player;
this.hand = hand;
this.blockFace = blockFace;
this.cursorX = cursorX;
this.cursorY = cursorY;
this.cursorZ = cursorZ;
}
public @NotNull Player getPlayer() {
return player;
}
public @NotNull Player.Hand getHand() {
return hand;
}
public @NotNull BlockFace getBlockFace() {
return blockFace;
}
public float getCursorX() {
return cursorX;
}
public float getCursorY() {
return cursorY;
}
public float getCursorZ() {
return cursorZ;
}
}
2021-10-22 01:55:55 +02:00
sealed class Destroy permits PlayerDestroy {
private final Block block;
private final Instance instance;
private final Point blockPosition;
@ApiStatus.Internal
public Destroy(Block block, Instance instance, Point blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
}
public @NotNull Block getBlock() {
return block;
}
public @NotNull Instance getInstance() {
return instance;
}
public @NotNull Point getBlockPosition() {
return blockPosition;
}
}
final class PlayerDestroy extends Destroy {
private final Player player;
@ApiStatus.Internal
public PlayerDestroy(Block block, Instance instance, Point blockPosition, Player player) {
super(block, instance, blockPosition);
this.player = player;
}
public @NotNull Player getPlayer() {
return player;
}
}
2021-10-22 01:55:55 +02:00
final class Interaction {
private final Block block;
private final Instance instance;
private final Point blockPosition;
private final Player player;
private final Player.Hand hand;
@ApiStatus.Internal
public Interaction(Block block, Instance instance, Point blockPosition, Player player, Player.Hand hand) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
this.player = player;
this.hand = hand;
}
public @NotNull Block getBlock() {
return block;
}
public @NotNull Instance getInstance() {
return instance;
}
public @NotNull Point getBlockPosition() {
return blockPosition;
}
public @NotNull Player getPlayer() {
return player;
}
public @NotNull Player.Hand getHand() {
return hand;
}
}
2021-10-22 01:55:55 +02:00
final class Touch {
private final Block block;
private final Instance instance;
private final Point blockPosition;
private final Entity touching;
@ApiStatus.Internal
public Touch(Block block, Instance instance, Point blockPosition, Entity touching) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
this.touching = touching;
}
public @NotNull Block getBlock() {
return block;
}
public @NotNull Instance getInstance() {
return instance;
}
public @NotNull Point getBlockPosition() {
return blockPosition;
}
public @NotNull Entity getTouching() {
return touching;
}
}
2021-10-22 01:55:55 +02:00
final class Tick {
private final Block block;
private final Instance instance;
private final Point blockPosition;
@ApiStatus.Internal
public Tick(Block block, Instance instance, Point blockPosition) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
}
public @NotNull Block getBlock() {
return block;
}
public @NotNull Instance getInstance() {
return instance;
}
2021-06-18 14:36:03 +02:00
public @NotNull Point getBlockPosition() {
return blockPosition;
2021-06-18 14:36:03 +02:00
}
}
2021-06-25 19:10:41 +02:00
/**
* Handler used for loaded blocks with unknown namespace
* in order to do not lose the information while saving, and for runtime debugging purpose.
*/
2021-09-10 15:50:38 +02:00
@ApiStatus.Internal
2021-09-10 18:08:39 +02:00
final class Dummy implements BlockHandler {
2021-06-25 19:10:41 +02:00
private static final Map<String, BlockHandler> DUMMY_CACHE = new ConcurrentHashMap<>();
public static @NotNull BlockHandler get(@NotNull String namespace) {
2021-09-10 15:50:38 +02:00
return DUMMY_CACHE.computeIfAbsent(namespace, Dummy::new);
2021-06-25 19:10:41 +02:00
}
2021-09-10 18:08:39 +02:00
private final NamespaceID namespace;
2021-09-10 15:50:38 +02:00
private Dummy(String name) {
2021-09-10 18:08:39 +02:00
namespace = NamespaceID.from(name);
}
@Override
public @NotNull NamespaceID getNamespaceId() {
return namespace;
2021-06-25 19:10:41 +02:00
}
}
2021-05-24 21:39:30 +02:00
}