From b10545994ac16bc487b5d43147ceec4467b82696 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 14 Apr 2019 11:22:11 +0200 Subject: [PATCH] lazy load chunk capabilities so that OptiFine doesn't kill itself --- .../api/aura/chunk/IAuraChunk.java | 3 +- .../ellpeck/naturesaura/chunk/AuraChunk.java | 15 ------ .../naturesaura/chunk/AuraChunkProvider.java | 51 +++++++++++++++++++ .../naturesaura/events/CommonEvents.java | 10 +--- 4 files changed, 54 insertions(+), 25 deletions(-) create mode 100644 src/main/java/de/ellpeck/naturesaura/chunk/AuraChunkProvider.java diff --git a/src/main/java/de/ellpeck/naturesaura/api/aura/chunk/IAuraChunk.java b/src/main/java/de/ellpeck/naturesaura/api/aura/chunk/IAuraChunk.java index 05ba2fd1..29918515 100644 --- a/src/main/java/de/ellpeck/naturesaura/api/aura/chunk/IAuraChunk.java +++ b/src/main/java/de/ellpeck/naturesaura/api/aura/chunk/IAuraChunk.java @@ -6,7 +6,6 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; -import net.minecraftforge.common.capabilities.ICapabilityProvider; import net.minecraftforge.common.util.INBTSerializable; import java.util.function.BiConsumer; @@ -19,7 +18,7 @@ import java.util.function.BiConsumer; * It is not intended for API users to create custom implementation of this * class. */ -public interface IAuraChunk extends ICapabilityProvider, INBTSerializable { +public interface IAuraChunk extends INBTSerializable { /** * The default amount of Aura that a chunk has stored */ diff --git a/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java index 79c28e01..6b722be3 100644 --- a/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java +++ b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunk.java @@ -11,18 +11,14 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; -import net.minecraft.util.EnumFacing; import net.minecraft.util.ResourceLocation; import net.minecraft.util.Tuple; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import net.minecraft.world.chunk.Chunk; -import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import org.apache.commons.lang3.mutable.MutableInt; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -196,17 +192,6 @@ public class AuraChunk implements IAuraChunk { } } - @Override - public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { - return capability == NaturesAuraAPI.capAuraChunk; - } - - @Nullable - @Override - public T getCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { - return capability == NaturesAuraAPI.capAuraChunk ? (T) this : null; - } - @Override public NBTTagCompound serializeNBT() { NBTTagList list = new NBTTagList(); diff --git a/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunkProvider.java b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunkProvider.java new file mode 100644 index 00000000..48a6b6c6 --- /dev/null +++ b/src/main/java/de/ellpeck/naturesaura/chunk/AuraChunkProvider.java @@ -0,0 +1,51 @@ +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; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.util.EnumFacing; +import net.minecraft.world.chunk.Chunk; +import net.minecraftforge.common.capabilities.Capability; +import net.minecraftforge.common.capabilities.ICapabilityProvider; +import net.minecraftforge.common.util.INBTSerializable; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; + +public class AuraChunkProvider implements ICapabilityProvider, INBTSerializable { + + private final Chunk chunk; + private IAuraChunk auraChunk; + + public AuraChunkProvider(Chunk chunk) { + this.chunk = chunk; + } + + private IAuraChunk getAuraChunk() { + if (this.auraChunk == null) + this.auraChunk = new AuraChunk(this.chunk, IAuraType.forWorld(this.chunk.getWorld())); + return this.auraChunk; + } + + @Override + public boolean hasCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { + return capability == NaturesAuraAPI.capAuraChunk; + } + + @Nullable + @Override + public T getCapability(@Nonnull Capability capability, @Nullable EnumFacing facing) { + return capability == NaturesAuraAPI.capAuraChunk ? (T) this.getAuraChunk() : null; + } + + @Override + public NBTTagCompound serializeNBT() { + return this.getAuraChunk().serializeNBT(); + } + + @Override + public void deserializeNBT(NBTTagCompound nbt) { + this.getAuraChunk().deserializeNBT(nbt); + } +} diff --git a/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java b/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java index 62bb75a0..0898288a 100644 --- a/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java +++ b/src/main/java/de/ellpeck/naturesaura/events/CommonEvents.java @@ -5,8 +5,8 @@ import de.ellpeck.naturesaura.ModConfig; import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.aura.chunk.IAuraChunk; -import de.ellpeck.naturesaura.api.aura.type.IAuraType; import de.ellpeck.naturesaura.chunk.AuraChunk; +import de.ellpeck.naturesaura.chunk.AuraChunkProvider; import de.ellpeck.naturesaura.misc.WorldData; import de.ellpeck.naturesaura.packet.PacketHandler; import net.minecraft.util.ResourceLocation; @@ -28,13 +28,7 @@ public class CommonEvents { @SubscribeEvent public void onChunkCapsAttach(AttachCapabilitiesEvent event) { Chunk chunk = event.getObject(); - World world = chunk.getWorld(); - if (world == null) { - NaturesAura.LOGGER.warn("Chunk with null world found. This is most likely due to OptiFine doing stuff that should really be disallowed. I don't know if this will cause any further issues, but hopefully not."); - return; - } - IAuraType type = IAuraType.forWorld(world); - event.addCapability(new ResourceLocation(NaturesAura.MOD_ID, "aura"), new AuraChunk(chunk, type)); + event.addCapability(new ResourceLocation(NaturesAura.MOD_ID, "aura"), new AuraChunkProvider(chunk)); } @SubscribeEvent