From 34b7133f178197b8df17a3428562b5eae9c10731 Mon Sep 17 00:00:00 2001 From: TheMode Date: Wed, 16 Jun 2021 14:04:22 +0200 Subject: [PATCH] Reduce subclass impl --- .../java/net/minestom/server/instance/block/Block.java | 9 +++++++-- .../net/minestom/server/instance/block/BlockTest.java | 7 ------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/minestom/server/instance/block/Block.java b/src/main/java/net/minestom/server/instance/block/Block.java index 00ea534a6..8e234f734 100644 --- a/src/main/java/net/minestom/server/instance/block/Block.java +++ b/src/main/java/net/minestom/server/instance/block/Block.java @@ -10,6 +10,7 @@ import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.Map; +import java.util.Objects; import java.util.function.BiPredicate; /** @@ -27,10 +28,14 @@ public interface Block extends ProtocolObject, TagReadable, BlockConstants { return withProperty(property.getName(), value.toString()); } - @NotNull Block withTag(@NotNull Tag tag, @Nullable T value); - @NotNull Block withNbt(@Nullable NBTCompound compound); + default @NotNull Block withTag(@NotNull Tag tag, @Nullable T value) { + var compound = Objects.requireNonNullElseGet(getNbt(), NBTCompound::new); + tag.write(compound, value); + return withNbt(compound); + } + @NotNull Block withHandler(@Nullable BlockHandler handler); @NotNull String getProperty(@NotNull String property); diff --git a/src/main/java/net/minestom/server/instance/block/BlockTest.java b/src/main/java/net/minestom/server/instance/block/BlockTest.java index 1cd5e2935..8708de7a5 100644 --- a/src/main/java/net/minestom/server/instance/block/BlockTest.java +++ b/src/main/java/net/minestom/server/instance/block/BlockTest.java @@ -41,13 +41,6 @@ class BlockTest implements Block { return Objects.requireNonNull(BlockRegistry.getProperties(this, properties)); } - @Override - public @NotNull Block withTag(@NotNull Tag tag, @Nullable T value) { - var compound = this.compound != null ? this.compound.deepClone() : new NBTCompound(); - tag.write(compound, value); - return new BlockTest(registry, properties, compound, handler); - } - @Override public @NotNull Block withNbt(@Nullable NBTCompound compound) { return new BlockTest(registry, properties, compound, handler);