mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-30 16:28:00 +01:00
(maybe) Fixed null pointer crash. This code now looks EVEN WORSE!!!
This commit is contained in:
parent
2534053305
commit
655d12922d
2 changed files with 52 additions and 11 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue