NaturesAura/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunkProvider.java

49 lines
1.6 KiB
Java
Raw Normal View History

package de.ellpeck.naturesaura.chunk;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2019-10-20 22:30:49 +02:00
import net.minecraft.util.Direction;
2021-12-04 15:40:09 +01:00
import net.minecraft.level.chunk.Chunk;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import net.minecraftforge.common.util.INBTSerializable;
2020-01-21 23:02:39 +01:00
import net.minecraftforge.common.util.LazyOptional;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
2021-12-04 15:40:09 +01:00
public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {
private final Chunk chunk;
2020-10-19 21:26:32 +02:00
private final LazyOptional<IAuraChunk> lazyChunk = LazyOptional.of(this::getAuraChunk);
private IAuraChunk auraChunk;
public AuraChunkProvider(Chunk chunk) {
this.chunk = chunk;
}
private IAuraChunk getAuraChunk() {
if (this.auraChunk == null)
2021-12-04 15:40:09 +01:00
this.auraChunk = new AuraChunk(this.chunk, IAuraType.forLevel(this.chunk.getLevel()));
return this.auraChunk;
}
@Nullable
@Override
2020-01-21 23:02:39 +01:00
public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> capability, @Nullable Direction facing) {
2020-10-19 21:26:32 +02:00
return capability == NaturesAuraAPI.capAuraChunk ? this.lazyChunk.cast() : LazyOptional.empty();
}
@Override
2021-12-04 15:40:09 +01:00
public CompoundTag serializeNBT() {
return this.getAuraChunk().serializeNBT();
}
@Override
2021-12-04 15:40:09 +01:00
public void deserializeNBT(CompoundTag nbt) {
this.getAuraChunk().deserializeNBT(nbt);
}
}