![JAR search and dependency download from the Maven repository](/logo.png)
net.minestom.server.item.component.ItemBlockState Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of minestom-snapshots Show documentation
Show all versions of minestom-snapshots Show documentation
1.20.4 Lightweight Minecraft server
package net.minestom.server.item.component;
import net.kyori.adventure.nbt.BinaryTag;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.StringBinaryTag;
import net.minestom.server.instance.block.Block;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public record ItemBlockState(@NotNull Map properties) {
public static final ItemBlockState EMPTY = new ItemBlockState(Map.of());
public static final NetworkBuffer.Type NETWORK_TYPE = new NetworkBuffer.Type<>() {
@Override
public void write(@NotNull NetworkBuffer buffer, ItemBlockState value) {
buffer.write(NetworkBuffer.VAR_INT, value.properties.size());
for (Map.Entry entry : value.properties.entrySet()) {
buffer.write(NetworkBuffer.STRING, entry.getKey());
buffer.write(NetworkBuffer.STRING, entry.getValue());
}
}
@Override
public ItemBlockState read(@NotNull NetworkBuffer buffer) {
int size = buffer.read(NetworkBuffer.VAR_INT);
Map properties = new HashMap<>(size);
for (int i = 0; i < size; i++) {
properties.put(buffer.read(NetworkBuffer.STRING), buffer.read(NetworkBuffer.STRING));
}
return new ItemBlockState(properties);
}
};
public static final BinaryTagSerializer NBT_TYPE = BinaryTagSerializer.COMPOUND.map(
tag -> {
Map properties = new HashMap<>(tag.size());
for (Map.Entry entry : tag) {
if (!(entry.getValue() instanceof StringBinaryTag str)) continue;
properties.put(entry.getKey(), str.value());
}
return new ItemBlockState(properties);
},
value -> {
CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
for (Map.Entry entry : value.properties.entrySet()) {
builder.put(entry.getKey(), StringBinaryTag.stringBinaryTag(entry.getValue()));
}
return builder.build();
}
);
public ItemBlockState {
properties = Map.copyOf(properties);
}
public @NotNull ItemBlockState with(@NotNull String key, @NotNull String value) {
Map newProperties = new HashMap<>(properties);
newProperties.put(key, value);
return new ItemBlockState(newProperties);
}
public @NotNull Block apply(@NotNull Block block) {
for (Map.Entry entry : properties.entrySet()) {
if (block.getProperty(entry.getKey()) == null)
continue; // Ignore properties not present on this block
block = block.withProperty(entry.getKey(), entry.getValue());
}
return block;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy