NaturesAura/src/main/java/de/ellpeck/naturesaura/misc/WorldData.java

85 lines
3.1 KiB
Java
Raw Normal View History

2019-02-17 22:51:05 +01:00
package de.ellpeck.naturesaura.misc;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
2019-02-17 22:51:05 +01:00
import de.ellpeck.naturesaura.Helper;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.misc.IWorldData;
import de.ellpeck.naturesaura.blocks.tiles.ItemStackHandlerNA;
2019-02-18 19:30:35 +01:00
import de.ellpeck.naturesaura.items.ModItems;
import net.minecraft.item.ItemStack;
2019-02-17 22:51:05 +01:00
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Tuple;
import net.minecraft.util.math.Vec3d;
2019-02-17 22:51:05 +01:00
import net.minecraftforge.common.capabilities.Capability;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
public class WorldData implements IWorldData {
private final Map<String, ItemStackHandlerNA> enderStorages = new HashMap<>();
public final ListMultimap<ResourceLocation, Tuple<Vec3d, Integer>> effectPowders = ArrayListMultimap.create();
2019-02-17 22:51:05 +01:00
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == NaturesAuraAPI.capWorldData;
}
@Nullable
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
return capability == NaturesAuraAPI.capWorldData ? (T) this : null;
}
@Override
public NBTTagCompound serializeNBT() {
NBTTagCompound compound = new NBTTagCompound();
NBTTagList storages = new NBTTagList();
for (Map.Entry<String, ItemStackHandlerNA> entry : this.enderStorages.entrySet()) {
ItemStackHandlerNA handler = entry.getValue();
if (Helper.isEmpty(handler))
continue;
NBTTagCompound storageComp = handler.serializeNBT();
storageComp.setString("name", entry.getKey());
storages.appendTag(storageComp);
}
compound.setTag("storages", storages);
return compound;
}
@Override
public void deserializeNBT(NBTTagCompound compound) {
this.enderStorages.clear();
NBTTagList storages = compound.getTagList("storages", 10);
for (NBTBase base : storages) {
NBTTagCompound storageComp = (NBTTagCompound) base;
ItemStackHandlerNA storage = this.getEnderStorage(storageComp.getString("name"));
storage.deserializeNBT(storageComp);
}
}
@Override
public ItemStackHandlerNA getEnderStorage(String name) {
return this.enderStorages.computeIfAbsent(name, n -> new ItemStackHandlerNA(27));
}
2019-02-18 19:30:35 +01:00
@Override
public boolean isEnderStorageLocked(String name) {
ItemStackHandlerNA handler = this.getEnderStorage(name);
for (int i = 0; i < handler.getSlots(); i++) {
ItemStack stack = handler.getStackInSlot(i);
if (!stack.isEmpty() && stack.getItem() == ModItems.TOKEN_TERROR)
return true;
}
return false;
}
2019-02-17 22:51:05 +01:00
}