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

316 lines
8.6 KiB
Java
Raw Normal View History

2021-05-24 21:39:30 +02:00
package net.minestom.server.instance.block;
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.BlockPosition;
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.Collections;
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
2021-05-24 22:10:16 +02:00
* @return true if this block blocks normal item use, false otherwise
*/
default boolean onInteract(@NotNull Interaction interaction) {
2021-06-13 13:41:01 +02:00
return false;
}
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 Collections.emptyList();
}
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)}.
* <p>
* Will in the future rely on sealed classes (https://openjdk.java.net/jeps/409)
* and record pattern for the implementations (https://openjdk.java.net/jeps/405).
*/
@ApiStatus.NonExtendable
interface Placement {
@NotNull Block block();
@NotNull Instance instance();
@NotNull BlockPosition blockPosition();
static @NotNull Placement from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return new Placement() {
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
};
}
}
final class PlayerPlacement implements Placement {
private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Player player;
public PlayerPlacement(Block block, Instance instance, BlockPosition blockPosition, Player player) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
this.player = player;
}
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
public @NotNull Player player() {
return player;
}
}
@ApiStatus.NonExtendable
interface Destroy {
@NotNull Block block();
@NotNull Instance instance();
@NotNull BlockPosition blockPosition();
static @NotNull Destroy from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return new Destroy() {
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
};
}
}
final class PlayerDestroy implements Destroy {
private final Block block;
private final Instance instance;
private final BlockPosition blockPosition;
private final Player player;
public PlayerDestroy(Block block, Instance instance, BlockPosition blockPosition, Player player) {
this.block = block;
this.instance = instance;
this.blockPosition = blockPosition;
this.player = player;
}
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
public @NotNull Player player() {
return player;
}
}
@ApiStatus.NonExtendable
interface Interaction {
@NotNull Block block();
@NotNull Instance instance();
@NotNull BlockPosition blockPosition();
@NotNull Player player();
@NotNull Player.Hand hand();
static @NotNull Interaction from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition,
@NotNull Player player, @NotNull Player.Hand hand) {
return new Interaction() {
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
@Override
public @NotNull Player player() {
return player;
}
@Override
public @NotNull Player.Hand hand() {
return hand;
}
};
}
}
@ApiStatus.NonExtendable
interface Touch {
@NotNull Block block();
@NotNull Instance instance();
@NotNull BlockPosition blockPosition();
@NotNull Entity touching();
static @NotNull Touch from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition,
@NotNull Entity touching) {
return new Touch() {
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
@Override
public @NotNull Entity touching() {
return touching;
}
};
}
}
2021-06-18 14:36:03 +02:00
@ApiStatus.NonExtendable
interface Tick {
@NotNull Block block();
@NotNull Instance instance();
@NotNull BlockPosition blockPosition();
static @NotNull Tick from(@NotNull Block block, @NotNull Instance instance, @NotNull BlockPosition blockPosition) {
return new Tick() {
@Override
public @NotNull Block block() {
return block;
}
@Override
public @NotNull Instance instance() {
return instance;
}
@Override
public @NotNull BlockPosition blockPosition() {
return blockPosition;
}
};
}
}
2021-05-24 21:39:30 +02:00
}