package com.xfestudio.mydimensionlord.platform;
import com.mojang.serialization.*;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.*;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.saveddata.SavedDataType;
import net.minecraft.world.level.storage.*;
import net.minecraftforge.server.ServerLifecycleHooks;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
public final class PlatformData {
private PlatformData() {}
public static HolderLookup.Provider registries() {
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
return server == null ? RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY) : server.registryAccess();
}
public static HolderLookup.Provider registries(BlockEntity entity) {
return entity.getLevel() == null ? registries() : entity.getLevel().registryAccess();
}
public static CompoundTag saveStack(ItemStack stack) {
return (CompoundTag) ItemStack.OPTIONAL_CODEC.encodeStart(registries().createSerializationContext(NbtOps.INSTANCE), stack).getOrThrow();
}
public static ItemStack readStack(CompoundTag tag) {
return ItemStack.OPTIONAL_CODEC.parse(registries().createSerializationContext(NbtOps.INSTANCE), tag).getOrThrow();
}
public static CompoundTag saveBlockWithId(BlockEntity entity) {
var output = TagValueOutput.createWithContext(net.minecraft.util.ProblemReporter.DISCARDING, registries(entity));
entity.saveWithId(output);
return output.buildResult();
}
public static CompoundTag blockEntityTag(ItemStack stack) {
var data = stack.get(net.minecraft.core.component.DataComponents.BLOCK_ENTITY_DATA);
return data == null ? new CompoundTag() : data.copyTagWithoutId();
}
public static <T extends PortableSavedData> T savedData(DimensionDataStorage storage, String name,
Supplier<T> factory, Function<CompoundTag, T> loader) {
return storage.computeIfAbsent(new SavedDataType<>(name, factory,
CompoundTag.CODEC.xmap(loader, data -> data.save(new CompoundTag()))));
}
private static final MapCodec<CompoundTag> TAG_MAP = new MapCodec<>() {
@Override public <T> DataResult<CompoundTag> decode(DynamicOps<T> ops, MapLike<T> input) {
return CompoundTag.CODEC.parse(ops, ops.createMap(input.entries()));
}
@Override public <T> RecordBuilder<T> encode(CompoundTag input, DynamicOps<T> ops, RecordBuilder<T> prefix) {
input.forEach((key, tag) -> prefix.add(key, NbtOps.INSTANCE.convertTo(ops, tag)));
return prefix;
}
@Override public <T> Stream<T> keys(DynamicOps<T> ops) { return Stream.empty(); }
};
public static CompoundTag readTag(ValueInput input) { return input.read(TAG_MAP).orElseGet(CompoundTag::new); }
public static void writeTag(ValueOutput output, CompoundTag tag) { output.store(TAG_MAP, tag); }
}
package com.xfestudio.mydimensionlord.platform;
import com.mojang.serialization.*;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.*;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.saveddata.SavedDataType;
import net.minecraft.world.level.storage.*;
import net.minecraftforge.server.ServerLifecycleHooks;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
public final class PlatformData {
private PlatformData() {}
public static HolderLookup.Provider registries() {
MinecraftServer server = ServerLifecycleHooks.getCurrentServer();
return server == null ? RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY) : server.registryAccess();
}
public static HolderLookup.Provider registries(BlockEntity entity) {
return entity.getLevel() == null ? registries() : entity.getLevel().registryAccess();
}
public static CompoundTag saveStack(ItemStack stack) {
return (CompoundTag) ItemStack.OPTIONAL_CODEC.encodeStart(registries().createSerializationContext(NbtOps.INSTANCE), stack).getOrThrow();
}
public static ItemStack readStack(CompoundTag tag) {
return ItemStack.OPTIONAL_CODEC.parse(registries().createSerializationContext(NbtOps.INSTANCE), tag).getOrThrow();
}
public static CompoundTag saveBlockWithId(BlockEntity entity) {
var output = TagValueOutput.createWithContext(net.minecraft.util.ProblemReporter.DISCARDING, registries(entity));
entity.saveWithId(output);
return output.buildResult();
}
public static CompoundTag blockEntityTag(ItemStack stack) {
var data = stack.get(net.minecraft.core.component.DataComponents.BLOCK_ENTITY_DATA);
return data == null ? new CompoundTag() : data.copyTagWithoutId();
}
public static <T extends PortableSavedData> T savedData(DimensionDataStorage storage, String name,
Supplier<T> factory, Function<CompoundTag, T> loader) {
return storage.computeIfAbsent(new SavedDataType<>(name, factory,
CompoundTag.CODEC.xmap(loader, data -> data.save(new CompoundTag()))));
}
private static final MapCodec<CompoundTag> TAG_MAP = new MapCodec<>() {
@Override public <T> DataResult<CompoundTag> decode(DynamicOps<T> ops, MapLike<T> input) {
return CompoundTag.CODEC.parse(ops, ops.createMap(input.entries()));
}
@Override public <T> RecordBuilder<T> encode(CompoundTag input, DynamicOps<T> ops, RecordBuilder<T> prefix) {
input.forEach((key, tag) -> prefix.add(key, NbtOps.INSTANCE.convertTo(ops, tag)));
return prefix;
}
@Override public <T> Stream<T> keys(DynamicOps<T> ops) { return Stream.empty(); }
};
public static CompoundTag readTag(ValueInput input) { return input.read(TAG_MAP).orElseGet(CompoundTag::new); }
public static void writeTag(ValueOutput output, CompoundTag tag) { output.store(TAG_MAP, tag); }
}