2019-04-14 11:22:11 +02:00
|
|
|
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;
|
2019-04-14 11:22:11 +02:00
|
|
|
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;
|
2019-04-14 11:22:11 +02:00
|
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
|
2021-12-04 15:40:09 +01:00
|
|
|
public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable<CompoundTag> {
|
2019-04-14 11:22:11 +02:00
|
|
|
|
|
|
|
private final Chunk chunk;
|
2020-10-19 21:26:32 +02:00
|
|
|
private final LazyOptional<IAuraChunk> lazyChunk = LazyOptional.of(this::getAuraChunk);
|
2019-04-14 11:22:11 +02:00
|
|
|
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()));
|
2019-04-14 11:22:11 +02:00
|
|
|
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();
|
2019-04-14 11:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public CompoundTag serializeNBT() {
|
2019-04-14 11:22:11 +02:00
|
|
|
return this.getAuraChunk().serializeNBT();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2021-12-04 15:40:09 +01:00
|
|
|
public void deserializeNBT(CompoundTag nbt) {
|
2019-04-14 11:22:11 +02:00
|
|
|
this.getAuraChunk().deserializeNBT(nbt);
|
|
|
|
}
|
|
|
|
}
|