minecraft-source/src/net/minecraft/world/level/storage/CommandStorage.java

92 lines
3.2 KiB
Java

package net.minecraft.world.level.storage;
import net.minecraft.nbt.Tag;
import java.util.Iterator;
import net.minecraft.world.level.saveddata.SavedData;
import java.util.stream.Stream;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import com.google.common.collect.Maps;
import java.util.Map;
public class CommandStorage {
private final Map<String, Container> namespaces;
private final DimensionDataStorage storage;
public CommandStorage(final DimensionDataStorage cri) {
this.namespaces = Maps.newHashMap();
this.storage = cri;
}
private Container newStorage(final String string1, final String string2) {
final Container a4 = new Container(string2);
this.namespaces.put(string1, a4);
return a4;
}
public CompoundTag get(final ResourceLocation sm) {
final String string3 = sm.getNamespace();
final String string4 = createId(string3);
final Container a5 = this.storage.<Container>get(() -> this.newStorage(string3, string4), string4);
return (a5 != null) ? a5.get(sm.getPath()) : new CompoundTag();
}
public void set(final ResourceLocation sm, final CompoundTag jt) {
final String string4 = sm.getNamespace();
final String string5 = createId(string4);
this.storage.<Container>computeIfAbsent(() -> this.newStorage(string4, string5), string5).put(sm.getPath(), jt);
}
public Stream<ResourceLocation> keys() {
return this.namespaces.entrySet().stream().<ResourceLocation>flatMap(entry -> entry.getValue().getKeys((String)entry.getKey()));
}
private static String createId(final String string) {
return "command_storage_" + string;
}
static class Container extends SavedData {
private final Map<String, CompoundTag> storage;
public Container(final String string) {
super(string);
this.storage = Maps.newHashMap();
}
@Override
public void load(final CompoundTag jt) {
final CompoundTag jt2 = jt.getCompound("contents");
for (final String string5 : jt2.getAllKeys()) {
this.storage.put(string5, jt2.getCompound(string5));
}
}
@Override
public CompoundTag save(final CompoundTag jt) {
final CompoundTag jt4 = new CompoundTag();
this.storage.forEach((string, jt3) -> jt4.put(string, jt3.copy()));
jt.put("contents", jt4);
return jt;
}
public CompoundTag get(final String string) {
final CompoundTag jt3 = this.storage.get(string);
return (jt3 != null) ? jt3 : new CompoundTag();
}
public void put(final String string, final CompoundTag jt) {
if (jt.isEmpty()) {
this.storage.remove(string);
}
else {
this.storage.put(string, jt);
}
this.setDirty();
}
public Stream<ResourceLocation> getKeys(final String string) {
return this.storage.keySet().stream().<ResourceLocation>map(string2 -> new ResourceLocation(string, string2));
}
}
}