package nl.kallestruik.riseofblocks.registry; import net.minecraft.core.BlockPos; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.level.saveddata.SavedData; import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.Map; import java.util.Set; public class FlagRegistry extends SavedData { private static final String DATA_NAME = "flag_registry"; private final HashMap flagPositions = new HashMap<>(); private static final Factory FACTORY = new Factory(FlagRegistry::new, FlagRegistry::load); public static FlagRegistry get(MinecraftServer server) { return server.getLevel(ServerLevel.OVERWORLD).getDataStorage().computeIfAbsent( FACTORY, DATA_NAME ); } public FlagPosition get(String teamName) { return flagPositions.get(teamName); } public Set> getAll() { return flagPositions.entrySet(); } public void put(String teamName, FlagPosition pos) { flagPositions.put(teamName, pos); setDirty(); } public void remove(String teamName) { flagPositions.remove(teamName); setDirty(); } @Override public @NotNull CompoundTag save(@NotNull CompoundTag compoundTag, HolderLookup.@NotNull Provider provider) { ListTag list = new ListTag(); for (String teamName : flagPositions.keySet()) { CompoundTag entry = new CompoundTag(); entry.putString("team", teamName); FlagPosition flagPos = flagPositions.get(teamName); entry.putInt("x", flagPos.pos.getX()); entry.putInt("y", flagPos.pos.getY()); entry.putInt("z", flagPos.pos.getZ()); entry.putString("dimension", flagPos.dimension.toString()); list.add(entry); } compoundTag.put("entries", list); return compoundTag; } public static FlagRegistry load(CompoundTag tag, HolderLookup.Provider provider) { FlagRegistry registry = new FlagRegistry(); ListTag list = tag.getList("entries", ListTag.TAG_COMPOUND); for (Tag t : list) { CompoundTag entry = (CompoundTag) t; String teamName = entry.getString("team"); int x = entry.getInt("x"); int y = entry.getInt("y"); int z = entry.getInt("z"); ResourceLocation dimension = ResourceLocation.parse(entry.getString("dimension")); FlagPosition flagPos = new FlagPosition(new BlockPos(x, y, z), dimension); registry.flagPositions.put(teamName, flagPos); } return registry; } public static class FlagPosition { private BlockPos pos; private ResourceLocation dimension; public FlagPosition(BlockPos pos, ResourceLocation dimension) { this.pos = pos; this.dimension = dimension; } public BlockPos getPos() { return pos; } public void setPos(BlockPos pos) { this.pos = pos; } public ResourceLocation getDimension() { return dimension; } public void setDimension(ResourceLocation dimension) { this.dimension = dimension; } @Override public String toString() { return "FlagPosition{" + "pos=" + pos + ", dimension=" + dimension + '}'; } } }