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;
|
2021-06-17 14:34:55 +02:00
|
|
|
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.
|
|
|
|
*
|
2021-06-17 14:34:55 +02:00
|
|
|
* @param placement the placement details
|
2021-05-24 22:10:16 +02:00
|
|
|
*/
|
2021-06-17 14:34:55 +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.
|
|
|
|
*
|
2021-06-17 14:34:55 +02:00
|
|
|
* @param destroy the destroy details
|
2021-05-24 22:10:16 +02:00
|
|
|
*/
|
2021-06-17 14:34:55 +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).
|
|
|
|
*
|
2021-06-17 14:34:55 +02:00
|
|
|
* @param interaction the interaction details
|
2021-05-24 22:10:16 +02:00
|
|
|
* @return true if this block blocks normal item use, false otherwise
|
|
|
|
*/
|
2021-06-17 14:34:55 +02:00
|
|
|
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.
|
|
|
|
*
|
2021-06-17 14:34:55 +02:00
|
|
|
* @param touch the contact details
|
2021-05-29 00:55:24 +02:00
|
|
|
*/
|
2021-06-17 14:34:55 +02:00
|
|
|
default void handleContact(@NotNull Touch touch) {
|
2021-05-29 00:55:24 +02:00
|
|
|
}
|
|
|
|
|
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();
|
2021-06-17 14:34:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@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-05-24 21:39:30 +02:00
|
|
|
}
|