summaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/funnyteleporters/ColouredChestState.java
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-11-20 19:21:03 +0100
committerLinnea Gräf <nea@nea.moe>2024-11-20 19:21:03 +0100
commit2e43b831af08beaed320f74cd9a1e537397135d3 (patch)
tree3fca59ae68b391fa4d59c801fb084de8a4d9e0ed /src/main/java/moe/nea/funnyteleporters/ColouredChestState.java
parent14f272f5db13d56de538b71df79f2dccbec0475f (diff)
downloadfunny-teleporters-2e43b831af08beaed320f74cd9a1e537397135d3.tar.gz
funny-teleporters-2e43b831af08beaed320f74cd9a1e537397135d3.tar.bz2
funny-teleporters-2e43b831af08beaed320f74cd9a1e537397135d3.zip
Add item sharing
Diffstat (limited to 'src/main/java/moe/nea/funnyteleporters/ColouredChestState.java')
-rw-r--r--src/main/java/moe/nea/funnyteleporters/ColouredChestState.java83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/main/java/moe/nea/funnyteleporters/ColouredChestState.java b/src/main/java/moe/nea/funnyteleporters/ColouredChestState.java
new file mode 100644
index 0000000..9e18b16
--- /dev/null
+++ b/src/main/java/moe/nea/funnyteleporters/ColouredChestState.java
@@ -0,0 +1,83 @@
+package moe.nea.funnyteleporters;
+
+import com.mojang.serialization.Codec;
+import com.mojang.serialization.codecs.RecordCodecBuilder;
+import net.minecraft.inventory.Inventories;
+import net.minecraft.item.ItemStack;
+import net.minecraft.nbt.NbtCompound;
+import net.minecraft.registry.RegistryWrapper;
+import net.minecraft.server.MinecraftServer;
+import net.minecraft.util.DyeColor;
+import net.minecraft.util.Identifier;
+import net.minecraft.util.collection.DefaultedList;
+import net.minecraft.util.dynamic.Codecs;
+import net.minecraft.world.PersistentState;
+import net.minecraft.world.World;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.StringJoiner;
+
+import static moe.nea.funnyteleporters.FunnyRegistry.MODID;
+
+public class ColouredChestState extends PersistentState {
+
+ public Map<List<DyeColor>, DefaultedList<ItemStack>> inventories = new HashMap<>();
+
+ private static String joinDyeIndex(List<DyeColor> dyes) {
+ StringJoiner j = new StringJoiner(";");
+ dyes.forEach(it -> j.add(it.getName()));
+ return j.toString();
+ }
+
+ private static List<DyeColor> splitDyeIndex(String string) {
+ List<DyeColor> dyes = new ArrayList<>(3);
+ for (String s : string.split(";")) {
+ dyes.add(DyeColor.byName(s, DyeColor.BLACK));
+ }
+ return Collections.unmodifiableList(dyes);
+ }
+
+ public static final String ColouredChestInventories = "ColouredChestInventories";
+
+ @Override
+ public NbtCompound writeNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
+ var compound = new NbtCompound();
+ inventories.forEach((dyeColors, itemStacks) ->
+ compound.put(joinDyeIndex(dyeColors), Inventories.writeNbt(new NbtCompound(), itemStacks, registryLookup))
+ );
+ nbt.put(ColouredChestInventories, compound);
+ return nbt;
+ }
+
+ public static ColouredChestState createFromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup registryLookup) {
+ var state = new ColouredChestState();
+ var compound = nbt.getCompound(ColouredChestInventories);
+ compound.getKeys().forEach(key -> {
+ var list = DefaultedList.ofSize(27, ItemStack.EMPTY);
+ Inventories.readNbt(compound.getCompound(key), list, registryLookup);
+ state.inventories.put(splitDyeIndex(key), list);
+ }
+ );
+ return state;
+ }
+
+ public DefaultedList<ItemStack> getDelegate(List<DyeColor> dyeColors) {
+ return inventories.computeIfAbsent(dyeColors, ignored -> DefaultedList.ofSize(27, ItemStack.EMPTY));
+ }
+
+ public static Type<ColouredChestState> TYPE = new Type<>(ColouredChestState::new, ColouredChestState::createFromNbt, null);
+
+ public static Identifier identifier = Identifier.of(MODID, "coloured_chests");
+
+ public static ColouredChestState getServerState(MinecraftServer server) {
+ var persistentStateWorld = Objects.requireNonNull(server.getWorld(World.OVERWORLD)).getPersistentStateManager(); // Force all data saving to be done in the overworld
+ var state = persistentStateWorld.getOrCreate(TYPE, identifier.toString());
+ state.markDirty(); // Always save
+ return state;
+ }
+}