BlockHandler default impl

This commit is contained in:
TheMode 2021-06-13 13:41:01 +02:00
parent c312bf59fc
commit b464b637a4
2 changed files with 7 additions and 21 deletions

View File

@ -24,7 +24,8 @@ public interface BlockHandler {
* @param instance the instance of the block
* @param blockPosition the position of the block
*/
void onPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition);
default void onPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
}
/**
* Called when a block has been destroyed or replaced.
@ -32,7 +33,8 @@ public interface BlockHandler {
* @param instance the instance of the block
* @param blockPosition the position of the block
*/
void onDestroy(@NotNull Instance instance, @NotNull BlockPosition blockPosition);
default 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
@ -43,7 +45,9 @@ public interface BlockHandler {
* @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 BlockPosition blockPosition);
default boolean onInteract(@NotNull Player player, @NotNull Player.Hand hand, @NotNull BlockPosition blockPosition) {
return false;
}
/**
* Defines custom behaviour for entities touching this block.

View File

@ -1,7 +1,5 @@
package demo.block;
import net.minestom.server.entity.Player;
import net.minestom.server.instance.Instance;
import net.minestom.server.instance.block.BlockHandler;
import net.minestom.server.item.ItemStack;
import net.minestom.server.item.Material;
@ -10,7 +8,6 @@ import net.minestom.server.tag.Tag;
import net.minestom.server.tag.TagReadable;
import net.minestom.server.tag.TagSerializer;
import net.minestom.server.tag.TagWritable;
import net.minestom.server.utils.BlockPosition;
import net.minestom.server.utils.NamespaceID;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -60,21 +57,6 @@ public class CampfireHandler implements BlockHandler {
}
});
@Override
public void onPlace(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
}
@Override
public void onDestroy(@NotNull Instance instance, @NotNull BlockPosition blockPosition) {
}
@Override
public boolean onInteract(@NotNull Player player, Player.@NotNull Hand hand, @NotNull BlockPosition blockPosition) {
return false;
}
@Override
public @NotNull Collection<Tag<?>> getBlockEntityTags() {
return List.of(ITEMS);