2019-02-17 22:51:05 +01:00
|
|
|
package de.ellpeck.naturesaura.misc;
|
|
|
|
|
2019-02-21 12:27:54 +01:00
|
|
|
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;
|
2020-01-26 00:43:12 +01:00
|
|
|
import net.minecraft.nbt.LongNBT;
|
2019-10-20 22:30:49 +02:00
|
|
|
import net.minecraft.util.Direction;
|
2019-02-21 12:27:54 +01:00
|
|
|
import net.minecraft.util.ResourceLocation;
|
|
|
|
import net.minecraft.util.Tuple;
|
2020-01-26 00:43:12 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2019-02-21 12:27:54 +01:00
|
|
|
import net.minecraft.util.math.Vec3d;
|
2019-02-17 22:51:05 +01:00
|
|
|
import net.minecraftforge.common.capabilities.Capability;
|
2020-01-26 00:43:12 +01:00
|
|
|
import net.minecraftforge.common.util.Constants;
|
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;
|
2020-01-26 00:43:12 +01:00
|
|
|
import java.util.*;
|
2019-02-17 22:51:05 +01:00
|
|
|
|
|
|
|
public class WorldData implements IWorldData {
|
|
|
|
private final Map<String, ItemStackHandlerNA> enderStorages = new HashMap<>();
|
2019-02-21 12:27:54 +01:00
|
|
|
public final ListMultimap<ResourceLocation, Tuple<Vec3d, Integer>> effectPowders = ArrayListMultimap.create();
|
2020-01-26 00:43:12 +01:00
|
|
|
public final List<BlockPos> recentlyConvertedMossStones = new ArrayList<>();
|
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
|
|
|
|
2020-01-26 00:43:12 +01:00
|
|
|
ListNBT moss = new ListNBT();
|
|
|
|
for (BlockPos pos : this.recentlyConvertedMossStones)
|
|
|
|
moss.add(new LongNBT(pos.toLong()));
|
|
|
|
compound.put("converted_moss", moss);
|
|
|
|
|
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-26 00:43:12 +01:00
|
|
|
for (INBT base : compound.getList("storages", 10)) {
|
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);
|
|
|
|
}
|
2020-01-26 00:43:12 +01:00
|
|
|
|
|
|
|
this.recentlyConvertedMossStones.clear();
|
|
|
|
for (INBT base : compound.getList("converted_moss", Constants.NBT.TAG_LONG))
|
|
|
|
this.recentlyConvertedMossStones.add(BlockPos.fromLong(((LongNBT) base).getLong()));
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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
|
|
|
}
|