Fix World Capabilities NPE when there are no attached capabilities.

This commit is contained in:
LexManos 2016-08-14 01:48:41 -07:00
parent bfddcfb4fc
commit 0e286f880e
4 changed files with 24 additions and 20 deletions

View file

@ -26,7 +26,7 @@ public class WorldCapabilityData extends WorldSavedData
public void readFromNBT(NBTTagCompound nbt)
{
this.capNBT = nbt;
if(serializable != null)
if (serializable != null)
{
serializable.deserializeNBT(this.capNBT);
this.capNBT = null;
@ -36,7 +36,9 @@ public class WorldCapabilityData extends WorldSavedData
@Override
public NBTTagCompound writeToNBT(NBTTagCompound nbt)
{
return nbt = serializable.serializeNBT();
if (serializable != null)
nbt = serializable.serializeNBT();
return nbt;
}
@Override
@ -48,7 +50,7 @@ public class WorldCapabilityData extends WorldSavedData
public void setCapabilities(WorldProvider provider, INBTSerializable<NBTTagCompound> capabilities)
{
this.serializable = capabilities;
if(this.capNBT != null)
if (this.capNBT != null)
{
serializable.deserializeNBT(this.capNBT);
this.capNBT = null;

View file

@ -489,7 +489,7 @@ public class ForgeEventFactory
{
MinecraftForge.EVENT_BUS.post(new PotionBrewEvent.Post(brewingItemStacks));
}
public static void onPlayerBrewedPotion(EntityPlayer player, ItemStack stack)
{
MinecraftForge.EVENT_BUS.post(new PlayerBrewedPotionEvent(player, stack));
@ -525,7 +525,8 @@ public class ForgeEventFactory
return gatherCapabilities(new AttachCapabilitiesEvent.Item(item, stack), parent);
}
public static CapabilityDispatcher gatherCapabilities(World world, ICapabilityProvider parent) {
public static CapabilityDispatcher gatherCapabilities(World world, ICapabilityProvider parent)
{
return gatherCapabilities(new AttachCapabilitiesEvent.World(world), parent);
}

View file

@ -74,8 +74,11 @@ public class NoBedSleepingTest
public static class EventHandler
{
@SubscribeEvent
public void onEntityConstruct(AttachCapabilitiesEvent evt)
public void onEntityConstruct(AttachCapabilitiesEvent.Entity evt)
{
if (!(evt.getEntity() instanceof EntityPlayer))
return;
evt.addCapability(new ResourceLocation(MODID, "IExtraSleeping"), new ICapabilitySerializable<NBTPrimitive>()
{
IExtraSleeping inst = SLEEP_CAP.getDefaultInstance();

View file

@ -1,16 +1,9 @@
package net.minecraftforge.test;
import java.util.Map;
import java.util.Set;
import com.google.common.collect.Maps;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagIntArray;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.Capability.IStorage;
@ -44,7 +37,8 @@ public class WorldCapabilityRainTimerTest {
@SubscribeEvent
public void attatchTimer(AttachCapabilitiesEvent.World event)
{
event.addCapability(new ResourceLocation(MODID, "rainTimer"), new RainTimerProvider());
if (!event.getWorld().isRemote && !event.getWorld().provider.getHasNoSky())
event.addCapability(new ResourceLocation(MODID, "rainTimer"), new RainTimerProvider());
}
@SubscribeEvent
@ -55,18 +49,22 @@ public class WorldCapabilityRainTimerTest {
IRainTimer timer = event.world.getCapability(TIMER_CAP, null);
if(event.phase == TickEvent.Phase.END)
if (timer == null)
return;
if (event.phase == TickEvent.Phase.END)
timer.onTick();
if(event.phase == TickEvent.Phase.START)
if (event.phase == TickEvent.Phase.START)
{
if(!event.world.provider.getHasNoSky() && event.world.isRaining())
if(event.world.isRaining())
{
if(timer.getDuration() == 0)
if (timer.getDuration() == 0)
timer.refreshTimer(1000, 0);
else if(timer.isTimerReachedDuration())
else if (timer.isTimerReachedDuration())
event.world.provider.resetRainAndThunder();
} else
}
else
timer.refreshTimer(0, 0);
}
}