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

81 lines
2.9 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-10-20 22:30:49 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-01-21 23:02:39 +01:00
import net.minecraft.nbt.INBT;
2019-10-20 22:30:49 +02:00
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.Direction;
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;
2020-01-21 23:02:39 +01:00
import net.minecraftforge.common.util.LazyOptional;
2019-02-17 22:51:05 +01:00
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
@Nullable
@Override
2020-01-21 23:02:39 +01:00
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
return capability == NaturesAuraAPI.capWorldData ? LazyOptional.of(() -> (T) this) : LazyOptional.empty();
2019-02-17 22:51:05 +01:00
}
@Override
2019-10-20 22:30:49 +02:00
public CompoundNBT serializeNBT() {
CompoundNBT compound = new CompoundNBT();
2019-02-17 22:51:05 +01:00
2019-10-20 22:30:49 +02:00
ListNBT storages = new ListNBT();
2019-02-17 22:51:05 +01:00
for (Map.Entry<String, ItemStackHandlerNA> entry : this.enderStorages.entrySet()) {
ItemStackHandlerNA handler = entry.getValue();
if (Helper.isEmpty(handler))
continue;
2019-10-20 22:30:49 +02:00
CompoundNBT storageComp = handler.serializeNBT();
2020-01-21 23:02:39 +01:00
storageComp.putString("name", entry.getKey());
storages.add(storageComp);
2019-02-17 22:51:05 +01:00
}
2020-01-21 23:02:39 +01:00
compound.put("storages", storages);
2019-02-17 22:51:05 +01:00
return compound;
}
@Override
2019-10-20 22:30:49 +02:00
public void deserializeNBT(CompoundNBT compound) {
2019-02-17 22:51:05 +01:00
this.enderStorages.clear();
2020-01-21 23:02:39 +01:00
ListNBT storages = compound.getList("storages", 10);
for (INBT base : storages) {
2019-10-20 22:30:49 +02:00
CompoundNBT storageComp = (CompoundNBT) base;
2019-02-17 22:51:05 +01:00
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
}