All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.item.component.DebugStickState Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
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.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public record DebugStickState(@NotNull Map state) {
    public static final DebugStickState EMPTY = new DebugStickState(Map.of());

    public static final BinaryTagSerializer NBT_TYPE = BinaryTagSerializer.COMPOUND.map(
            tag -> {
                Map state = new HashMap<>();
                for (Map.Entry entry : tag) {
                    if (!(entry.getValue() instanceof StringBinaryTag property))
                        continue;
                    state.put(entry.getKey(), property.value());
                }
                return new DebugStickState(state);
            },
            state -> {
                CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder();
                for (Map.Entry entry : state.state().entrySet()) {
                    builder.put(entry.getKey(), StringBinaryTag.stringBinaryTag(entry.getValue()));
                }
                return builder.build();
            }
    );

    public DebugStickState {
        state = Map.copyOf(state);
    }

    public @NotNull DebugStickState set(@NotNull String key, @NotNull String value) {
        Map newState = new HashMap<>(state);
        newState.put(key, value);
        return new DebugStickState(newState);
    }

    public @NotNull DebugStickState remove(@NotNull String key) {
        Map newState = new HashMap<>(state);
        newState.remove(key);
        return new DebugStickState(newState);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy