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;
|
2020-02-15 13:07:43 +01:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntitySpawnLamp;
|
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;
|
2020-09-22 03:17:02 +02:00
|
|
|
import net.minecraft.util.math.vector.Vector3d;
|
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-02-15 13:07:43 +01:00
|
|
|
import java.util.*;
|
2019-02-17 22:51:05 +01:00
|
|
|
|
|
|
|
public class WorldData implements IWorldData {
|
2020-09-22 03:17:02 +02:00
|
|
|
public final ListMultimap<ResourceLocation, Tuple<Vector3d, Integer>> effectPowders = ArrayListMultimap.create();
|
2020-01-26 00:43:12 +01:00
|
|
|
public final List<BlockPos> recentlyConvertedMossStones = new ArrayList<>();
|
2020-02-07 15:22:30 +01:00
|
|
|
private final Map<String, ItemStackHandlerNA> enderStorages = new HashMap<>();
|
2020-02-15 13:07:43 +01:00
|
|
|
public final Set<TileEntitySpawnLamp> spawnLamps = new HashSet<>();
|
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)
|
2020-01-28 18:08:56 +01:00
|
|
|
moss.add(LongNBT.valueOf(pos.toLong()));
|
2020-01-26 00:43:12 +01:00
|
|
|
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;
|
|
|
|
}
|
2020-03-14 03:10:56 +01:00
|
|
|
|
|
|
|
public void addMossStone(BlockPos pos) {
|
|
|
|
this.recentlyConvertedMossStones.add(pos);
|
2020-03-14 13:58:45 +01:00
|
|
|
if (this.recentlyConvertedMossStones.size() > 512)
|
2020-03-14 03:10:56 +01:00
|
|
|
this.recentlyConvertedMossStones.remove(0);
|
|
|
|
}
|
2019-02-17 22:51:05 +01:00
|
|
|
}
|