(maybe) Fixed null pointer crash. This code now looks EVEN WORSE!!!

This commit is contained in:
Sollace 2019-01-26 10:50:03 +02:00
parent 2534053305
commit 655d12922d
2 changed files with 52 additions and 11 deletions

View file

@ -1,5 +1,7 @@
package com.minelittlepony.unicopia.forgebullshit; package com.minelittlepony.unicopia.forgebullshit;
import javax.annotation.Nullable;
import com.minelittlepony.unicopia.player.IOwned; import com.minelittlepony.unicopia.player.IOwned;
import com.minelittlepony.unicopia.player.IPlayer; import com.minelittlepony.unicopia.player.IPlayer;
import com.minelittlepony.unicopia.player.IRaceContainer; import com.minelittlepony.unicopia.player.IRaceContainer;
@ -12,8 +14,35 @@ import net.minecraftforge.common.capabilities.CapabilityInject;
class DefaultEntityCapabilitiesProxyContainer<T extends Entity> implements ICapabilitiesProxyContainer<T> { class DefaultEntityCapabilitiesProxyContainer<T extends Entity> implements ICapabilitiesProxyContainer<T> {
@Nullable
@CapabilityInject(ICapabilitiesProxyContainer.class) @CapabilityInject(ICapabilitiesProxyContainer.class)
public static final Capability<ICapabilitiesProxyContainer<?>> CAPABILITY = null; public static Capability<ICapabilitiesProxyContainer<?>> CAPABILITY = null;
@SuppressWarnings("unchecked")
static boolean updateAndCompare(Capability<?> capability) {
if (CAPABILITY == null && capability != null) {
if (capability.getDefaultInstance() instanceof ICapabilitiesProxyContainer) {
CAPABILITY = (Capability<ICapabilitiesProxyContainer<?>>)capability;
}
}
return capability() == capability;
}
static <T extends Entity> ICapabilitiesProxyContainer<T> newInstance() {
if (capability() == null) {
return null;
}
return capability().cast(capability().getDefaultInstance());
}
@Nullable
static Capability<ICapabilitiesProxyContainer<?>> capability() {
if (CAPABILITY == null) {
new RuntimeException("Warning: Capability is null").printStackTrace();
}
return CAPABILITY;
}
private IRaceContainer<T> container; private IRaceContainer<T> container;

View file

@ -1,5 +1,7 @@
package com.minelittlepony.unicopia.forgebullshit; package com.minelittlepony.unicopia.forgebullshit;
import javax.annotation.Nullable;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.EnumFacing; import net.minecraft.util.EnumFacing;
@ -7,24 +9,34 @@ import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilitySerializable; import net.minecraftforge.common.capabilities.ICapabilitySerializable;
class Provider implements ICapabilitySerializable<NBTTagCompound> { class Provider implements ICapabilitySerializable<NBTTagCompound> {
@SuppressWarnings("unchecked")
DefaultEntityCapabilitiesProxyContainer<Entity> instance = (DefaultEntityCapabilitiesProxyContainer<Entity>) DefaultEntityCapabilitiesProxyContainer.CAPABILITY.getDefaultInstance();
private final Entity entity; private final Entity entity;
@Nullable
private ICapabilitiesProxyContainer<Entity> container;
Provider(Entity entity) { Provider(Entity entity) {
this.entity = entity; this.entity = entity;
} }
@Override private ICapabilitiesProxyContainer<Entity> getContainerObject() {
public boolean hasCapability(Capability<?> capability, EnumFacing facing) { if (container == null) {
return capability == DefaultEntityCapabilitiesProxyContainer.CAPABILITY; container = DefaultEntityCapabilitiesProxyContainer.newInstance();
}
return container;
} }
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
return DefaultEntityCapabilitiesProxyContainer.updateAndCompare(capability);
}
@SuppressWarnings("unchecked")
@Override @Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) { public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
if (hasCapability(capability, facing)) { if (hasCapability(capability, facing)) {
return DefaultEntityCapabilitiesProxyContainer.CAPABILITY.<T>cast(instance.withEntity(entity)); return (T)getContainerObject().withEntity(entity);
} }
return null; return null;
@ -32,13 +44,13 @@ class Provider implements ICapabilitySerializable<NBTTagCompound> {
@Override @Override
public NBTTagCompound serializeNBT() { public NBTTagCompound serializeNBT() {
return (NBTTagCompound) DefaultEntityCapabilitiesProxyContainer.CAPABILITY.getStorage() return (NBTTagCompound) DefaultEntityCapabilitiesProxyContainer.capability().getStorage()
.writeNBT(DefaultEntityCapabilitiesProxyContainer.CAPABILITY, instance.withEntity(entity), null); .writeNBT(DefaultEntityCapabilitiesProxyContainer.capability(), getContainerObject().withEntity(entity), null);
} }
@Override @Override
public void deserializeNBT(NBTTagCompound nbt) { public void deserializeNBT(NBTTagCompound nbt) {
DefaultEntityCapabilitiesProxyContainer.CAPABILITY.getStorage() DefaultEntityCapabilitiesProxyContainer.capability().getStorage()
.readNBT(DefaultEntityCapabilitiesProxyContainer.CAPABILITY, instance.withEntity(entity), null, nbt); .readNBT(DefaultEntityCapabilitiesProxyContainer.capability(), getContainerObject().withEntity(entity), null, nbt);
} }
} }