lazy load chunk capabilities so that OptiFine doesn't kill itself

This commit is contained in:
Ellpeck 2019-04-14 11:22:11 +02:00
parent 9d1b0a9f65
commit b10545994a
4 changed files with 54 additions and 25 deletions

View file

@ -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<NBTTagCompound> {
public interface IAuraChunk extends INBTSerializable<NBTTagCompound> {
/**
* The default amount of Aura that a chunk has stored
*/

View file

@ -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> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
return capability == NaturesAuraAPI.capAuraChunk ? (T) this : null;
}
@Override
public NBTTagCompound serializeNBT() {
NBTTagList list = new NBTTagList();

View file

@ -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<NBTTagCompound> {
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> T getCapability(@Nonnull Capability<T> 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);
}
}

View file

@ -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<Chunk> 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