net.minestom.server.component.DataComponentMap 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.component;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.utils.Unit;
import net.minestom.server.utils.nbt.BinaryTagSerializer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.function.Function;
import java.util.function.IntFunction;
/**
* Represents any container of {@link DataComponent}s.
*
* This type is capable of storing a patch of added and removed components on top of a 'prototype', or existing
* set of components. See {@link #diff(DataComponentMap, DataComponentMap)}.
*/
@ApiStatus.Experimental
public sealed interface DataComponentMap extends DataComponent.Holder permits DataComponentMapImpl {
@NotNull DataComponentMap EMPTY = new DataComponentMapImpl(new Int2ObjectArrayMap<>(0));
static @NotNull DataComponentMap.Builder builder() {
return new DataComponentMapImpl.BuilderImpl(new Int2ObjectArrayMap<>());
}
static @NotNull DataComponentMap.PatchBuilder patchBuilder() {
return new DataComponentMapImpl.PatchBuilderImpl(new Int2ObjectArrayMap<>());
}
/**
* Creates a network type for the given component type. For internal use only, get the value from the target component class.
*/
@ApiStatus.Internal
static @NotNull BinaryTagSerializer nbtType(
@NotNull IntFunction> idToType,
@NotNull Function> nameToType
) {
return new DataComponentMapImpl.NbtType(idToType, nameToType, false);
}
/**
* Creates a network type for the given component type. For internal use only, get the value from the target component class.
*/
@ApiStatus.Internal
static @NotNull NetworkBuffer.Type patchNetworkType(@NotNull IntFunction> idToType) {
return new DataComponentMapImpl.PatchNetworkType(idToType);
}
/**
* Creates a network type for the given component type. For internal use only, get the value from the target component class.
*/
@ApiStatus.Internal
static @NotNull BinaryTagSerializer patchNbtType(
@NotNull IntFunction> idToType,
@NotNull Function> nameToType
) {
return new DataComponentMapImpl.NbtType(idToType, nameToType, true);
}
static @NotNull DataComponentMap diff(@NotNull DataComponentMap prototype, @NotNull DataComponentMap patch) {
final DataComponentMapImpl patchImpl = (DataComponentMapImpl) patch;
if (patchImpl.components().isEmpty()) return EMPTY;
final DataComponentMapImpl protoImpl = (DataComponentMapImpl) prototype;
final Int2ObjectArrayMap