mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-08 01:17:47 +01:00
Simplify block implementation
This commit is contained in:
parent
4f4afbbe6d
commit
b541ef4a74
@ -12,7 +12,6 @@ import org.jetbrains.annotations.Unmodifiable;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.BiPredicate;
|
||||
|
||||
/**
|
||||
@ -55,18 +54,7 @@ public interface Block extends ProtocolObject, TagReadable, BlockConstants {
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new block with different nbt data.
|
||||
*
|
||||
* @param compound the new block nbt, null to remove
|
||||
* @return a new block with different nbt
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
@NotNull Block withNbt(@Nullable NBTCompound compound);
|
||||
|
||||
/**
|
||||
* Creates a new block with a tag value modified.
|
||||
* <p>
|
||||
* Equivalent to getting {@link #nbt()}, applying the tag and calling {@link #withNbt(NBTCompound)}.
|
||||
* Creates a new block with a tag modified.
|
||||
*
|
||||
* @param tag the tag to modify
|
||||
* @param value the tag value, null to remove
|
||||
@ -74,12 +62,20 @@ public interface Block extends ProtocolObject, TagReadable, BlockConstants {
|
||||
* @return a new block with the modified tag
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
default <T> @NotNull Block withTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
var compound = Objects.requireNonNullElseGet(nbt(), NBTCompound::new);
|
||||
tag.write(compound, value);
|
||||
return withNbt(compound);
|
||||
<T> @NotNull Block withTag(@NotNull Tag<T> tag, @Nullable T value);
|
||||
|
||||
/**
|
||||
* Creates a new block with different nbt data.
|
||||
*
|
||||
* @param compound the new block nbt, null to remove
|
||||
* @return a new block with different nbt
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
default @NotNull Block withNbt(@Nullable NBTCompound compound) {
|
||||
return withTag(Tag.NBT, compound);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new block with the specified {@link BlockHandler handler}.
|
||||
*
|
||||
@ -97,7 +93,9 @@ public interface Block extends ProtocolObject, TagReadable, BlockConstants {
|
||||
* @return the block nbt, null if not present
|
||||
*/
|
||||
@Contract(pure = true)
|
||||
@Nullable NBTCompound nbt();
|
||||
default @Nullable NBTCompound nbt() {
|
||||
return getTag(Tag.NBT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the block handler.
|
||||
|
@ -11,6 +11,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
class BlockImpl implements Block {
|
||||
@ -53,9 +54,10 @@ class BlockImpl implements Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Block withNbt(@Nullable NBTCompound compound) {
|
||||
final var clonedNbt = compound != null ? compound.deepClone() : null;
|
||||
final var cachedNbt = NBT_CACHE.get(clonedNbt, c -> clonedNbt);
|
||||
public @NotNull <T> Block withTag(@NotNull Tag<T> tag, @Nullable T value) {
|
||||
var compound = Objects.requireNonNullElseGet(nbt(), NBTCompound::new);
|
||||
tag.write(compound, value);
|
||||
final var cachedNbt = NBT_CACHE.get(compound, c -> compound);
|
||||
return new BlockImpl(registry, properties, cachedNbt, handler);
|
||||
}
|
||||
|
||||
@ -64,12 +66,6 @@ class BlockImpl implements Block {
|
||||
return new BlockImpl(registry, properties, nbt, handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable NBTCompound nbt() {
|
||||
// TODO return immutable compound without clone
|
||||
return nbt != null ? nbt.deepClone() : null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable BlockHandler handler() {
|
||||
return handler;
|
||||
|
Loading…
Reference in New Issue
Block a user