(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;
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<T extends Entity> implements ICapabilitiesProxyContainer<T> {
@Nullable
@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;

View file

@ -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<NBTTagCompound> {
@SuppressWarnings("unchecked")
DefaultEntityCapabilitiesProxyContainer<Entity> instance = (DefaultEntityCapabilitiesProxyContainer<Entity>) DefaultEntityCapabilitiesProxyContainer.CAPABILITY.getDefaultInstance();
private final Entity entity;
@Nullable
private ICapabilitiesProxyContainer<Entity> container;
Provider(Entity entity) {
this.entity = entity;
}
@Override
public boolean hasCapability(Capability<?> capability, EnumFacing facing) {
return capability == DefaultEntityCapabilitiesProxyContainer.CAPABILITY;
private ICapabilitiesProxyContainer<Entity> 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> T getCapability(Capability<T> capability, EnumFacing facing) {
if (hasCapability(capability, facing)) {
return DefaultEntityCapabilitiesProxyContainer.CAPABILITY.<T>cast(instance.withEntity(entity));
return (T)getContainerObject().withEntity(entity);
}
return null;
@ -32,13 +44,13 @@ class Provider implements ICapabilitySerializable<NBTTagCompound> {
@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);
}
}