From 655d12922dffc1258168479e4ba5f5ad0e0ebc72 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 26 Jan 2019 10:50:03 +0200 Subject: [PATCH] (maybe) Fixed null pointer crash. This code now looks EVEN WORSE!!! --- ...faultEntityCapabilitiesProxyContainer.java | 31 +++++++++++++++++- .../unicopia/forgebullshit/Provider.java | 32 +++++++++++++------ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/forgebullshit/DefaultEntityCapabilitiesProxyContainer.java b/src/main/java/com/minelittlepony/unicopia/forgebullshit/DefaultEntityCapabilitiesProxyContainer.java index 03552c76..99e9c9c1 100644 --- a/src/main/java/com/minelittlepony/unicopia/forgebullshit/DefaultEntityCapabilitiesProxyContainer.java +++ b/src/main/java/com/minelittlepony/unicopia/forgebullshit/DefaultEntityCapabilitiesProxyContainer.java @@ -1,5 +1,7 @@ package com.minelittlepony.unicopia.forgebullshit; +import javax.annotation.Nullable; + import com.minelittlepony.unicopia.player.IOwned; import com.minelittlepony.unicopia.player.IPlayer; import com.minelittlepony.unicopia.player.IRaceContainer; @@ -12,8 +14,35 @@ import net.minecraftforge.common.capabilities.CapabilityInject; class DefaultEntityCapabilitiesProxyContainer implements ICapabilitiesProxyContainer { + @Nullable @CapabilityInject(ICapabilitiesProxyContainer.class) - public static final Capability> CAPABILITY = null; + public static Capability> CAPABILITY = null; + + @SuppressWarnings("unchecked") + static boolean updateAndCompare(Capability capability) { + if (CAPABILITY == null && capability != null) { + if (capability.getDefaultInstance() instanceof ICapabilitiesProxyContainer) { + CAPABILITY = (Capability>)capability; + } + } + + return capability() == capability; + } + + static ICapabilitiesProxyContainer newInstance() { + if (capability() == null) { + return null; + } + return capability().cast(capability().getDefaultInstance()); + } + + @Nullable + static Capability> capability() { + if (CAPABILITY == null) { + new RuntimeException("Warning: Capability is null").printStackTrace(); + } + return CAPABILITY; + } private IRaceContainer container; diff --git a/src/main/java/com/minelittlepony/unicopia/forgebullshit/Provider.java b/src/main/java/com/minelittlepony/unicopia/forgebullshit/Provider.java index 6e512805..6efee5f9 100644 --- a/src/main/java/com/minelittlepony/unicopia/forgebullshit/Provider.java +++ b/src/main/java/com/minelittlepony/unicopia/forgebullshit/Provider.java @@ -1,5 +1,7 @@ package com.minelittlepony.unicopia.forgebullshit; +import javax.annotation.Nullable; + import net.minecraft.entity.Entity; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.EnumFacing; @@ -7,24 +9,34 @@ import net.minecraftforge.common.capabilities.Capability; import net.minecraftforge.common.capabilities.ICapabilitySerializable; class Provider implements ICapabilitySerializable { - @SuppressWarnings("unchecked") - DefaultEntityCapabilitiesProxyContainer instance = (DefaultEntityCapabilitiesProxyContainer) DefaultEntityCapabilitiesProxyContainer.CAPABILITY.getDefaultInstance(); private final Entity entity; + @Nullable + private ICapabilitiesProxyContainer container; + Provider(Entity entity) { this.entity = entity; } - @Override - public boolean hasCapability(Capability capability, EnumFacing facing) { - return capability == DefaultEntityCapabilitiesProxyContainer.CAPABILITY; + private ICapabilitiesProxyContainer getContainerObject() { + if (container == null) { + container = DefaultEntityCapabilitiesProxyContainer.newInstance(); + } + + return container; } + @Override + public boolean hasCapability(Capability capability, EnumFacing facing) { + return DefaultEntityCapabilitiesProxyContainer.updateAndCompare(capability); + } + + @SuppressWarnings("unchecked") @Override public T getCapability(Capability capability, EnumFacing facing) { if (hasCapability(capability, facing)) { - return DefaultEntityCapabilitiesProxyContainer.CAPABILITY.cast(instance.withEntity(entity)); + return (T)getContainerObject().withEntity(entity); } return null; @@ -32,13 +44,13 @@ class Provider implements ICapabilitySerializable { @Override public NBTTagCompound serializeNBT() { - return (NBTTagCompound) DefaultEntityCapabilitiesProxyContainer.CAPABILITY.getStorage() - .writeNBT(DefaultEntityCapabilitiesProxyContainer.CAPABILITY, instance.withEntity(entity), null); + return (NBTTagCompound) DefaultEntityCapabilitiesProxyContainer.capability().getStorage() + .writeNBT(DefaultEntityCapabilitiesProxyContainer.capability(), getContainerObject().withEntity(entity), null); } @Override public void deserializeNBT(NBTTagCompound nbt) { - DefaultEntityCapabilitiesProxyContainer.CAPABILITY.getStorage() - .readNBT(DefaultEntityCapabilitiesProxyContainer.CAPABILITY, instance.withEntity(entity), null, nbt); + DefaultEntityCapabilitiesProxyContainer.capability().getStorage() + .readNBT(DefaultEntityCapabilitiesProxyContainer.capability(), getContainerObject().withEntity(entity), null, nbt); } }