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

103 lines
3.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;
2021-12-04 15:40:09 +01:00
import de.ellpeck.naturesaura.api.misc.ILevelData;
import de.ellpeck.naturesaura.blocks.tiles.BlockEntitySpawnLamp;
2021-12-04 19:17:21 +01:00
import de.ellpeck.naturesaura.blocks.tiles.ItemStackHandlerNA;
import de.ellpeck.naturesaura.chunk.AuraChunk;
2019-02-18 19:30:35 +01:00
import de.ellpeck.naturesaura.items.ModItems;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
2021-12-04 19:17:21 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-04 19:17:21 +01:00
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.LongTag;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Tuple;
2021-12-04 19:17:21 +01:00
import net.minecraft.world.phys.Vec3;
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.*;
2019-02-17 22:51:05 +01:00
2021-12-04 15:40:09 +01:00
public class LevelData implements ILevelData {
2021-12-04 19:17:21 +01:00
public final ListMultimap<ResourceLocation, Tuple<Vec3, Integer>> effectPowders = ArrayListMultimap.create();
public final Long2ObjectOpenHashMap<AuraChunk> auraChunksWithSpots = new Long2ObjectOpenHashMap<>();
public final List<BlockPos> recentlyConvertedMossStones = new ArrayList<>();
2021-12-04 15:40:09 +01:00
public final Set<BlockEntitySpawnLamp> spawnLamps = new HashSet<>();
private final Map<String, ItemStackHandlerNA> enderStorages = new HashMap<>();
2021-12-04 15:40:09 +01:00
private final LazyOptional<LevelData> lazyThis = LazyOptional.of(() -> this);
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) {
2021-12-23 13:27:52 +01:00
return capability == NaturesAuraAPI.CAP_LEVEL_DATA ? this.lazyThis.cast() : LazyOptional.empty();
2019-02-17 22:51:05 +01:00
}
@Override
2021-12-04 15:40:09 +01:00
public CompoundTag serializeNBT() {
2021-12-15 16:30:22 +01:00
var compound = new CompoundTag();
2019-02-17 22:51:05 +01:00
2021-12-15 16:30:22 +01:00
var storages = new ListTag();
for (var entry : this.enderStorages.entrySet()) {
var handler = entry.getValue();
2019-02-17 22:51:05 +01:00
if (Helper.isEmpty(handler))
continue;
2021-12-15 16:30:22 +01:00
var 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
2021-12-15 16:30:22 +01:00
var moss = new ListTag();
for (var pos : this.recentlyConvertedMossStones)
2021-12-04 19:17:21 +01:00
moss.add(LongTag.valueOf(pos.asLong()));
compound.put("converted_moss", moss);
2019-02-17 22:51:05 +01:00
return compound;
}
@Override
2021-12-04 15:40:09 +01:00
public void deserializeNBT(CompoundTag compound) {
2019-02-17 22:51:05 +01:00
this.enderStorages.clear();
2021-12-15 16:30:22 +01:00
for (var base : compound.getList("storages", 10)) {
var storageComp = (CompoundTag) base;
var storage = this.getEnderStorage(storageComp.getString("name"));
2019-02-17 22:51:05 +01:00
storage.deserializeNBT(storageComp);
}
this.recentlyConvertedMossStones.clear();
2021-12-15 16:30:22 +01:00
for (var base : compound.getList("converted_moss", Tag.TAG_LONG))
2021-12-04 19:17:21 +01:00
this.recentlyConvertedMossStones.add(BlockPos.of(((LongTag) base).getAsLong()));
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) {
2021-12-15 16:30:22 +01:00
var handler = this.getEnderStorage(name);
for (var i = 0; i < handler.getSlots(); i++) {
var stack = handler.getStackInSlot(i);
2019-02-18 19:30:35 +01:00
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
}