diff --git a/src/main/java/com/darkona/adventurebackpack/block/BlockSleepingBag.java b/src/main/java/com/darkona/adventurebackpack/block/BlockSleepingBag.java index 8a8e7075..cf5ed88f 100644 --- a/src/main/java/com/darkona/adventurebackpack/block/BlockSleepingBag.java +++ b/src/main/java/com/darkona/adventurebackpack/block/BlockSleepingBag.java @@ -10,7 +10,6 @@ import net.minecraft.entity.player.EntityPlayer; import net.minecraft.init.Blocks; import net.minecraft.item.Item; -import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.ChatComponentTranslation; import net.minecraft.util.ChunkCoordinates; import net.minecraft.util.Direction; @@ -36,11 +35,6 @@ public class BlockSleepingBag extends BlockDirectional { private static final int[][] footBlockToHeadBlockMap = new int[][] { { 0, 1 }, { -1, 0 }, { 0, -1 }, { 1, 0 } }; - private static final String TAG_STORED_SPAWN = "storedSpawn"; - private static final String TAG_SPAWN_POS_X = "posX"; - private static final String TAG_SPAWN_POS_Y = "posY"; - private static final String TAG_SPAWN_POS_Z = "posZ"; - @SideOnly(Side.CLIENT) private IIcon[] endIcons; @@ -92,34 +86,34 @@ public static void packPortableSleepingBag(EntityPlayer player) { } } - public static void storeOriginalSpawn(EntityPlayer player, NBTTagCompound tag) { + public static void storeOriginalSpawn(EntityPlayer player) { ChunkCoordinates spawn = player.getBedLocation(player.worldObj.provider.dimensionId); - if (spawn != null) { - NBTTagCompound storedSpawn = new NBTTagCompound(); - storedSpawn.setInteger(TAG_SPAWN_POS_X, spawn.posX); - storedSpawn.setInteger(TAG_SPAWN_POS_Y, spawn.posY); - storedSpawn.setInteger(TAG_SPAWN_POS_Z, spawn.posZ); - tag.setTag(TAG_STORED_SPAWN, storedSpawn); + final BackpackProperty props = BackpackProperty.get(player); + + if (spawn != null && props != null) { + props.setStoredSpawn(spawn); LogHelper.info( "Stored spawn data for " + player .getDisplayName() + ": " + spawn + " dimID: " + player.worldObj.provider.dimensionId); } else { - LogHelper.warn("Cannot store spawn data for " + player.getDisplayName()); + LogHelper.warn("Cannot store spawn data for " + player.getDisplayName() + ", because it is non-existent"); } } - public static void restoreOriginalSpawn(EntityPlayer player, NBTTagCompound tag) { - if (tag.hasKey(TAG_STORED_SPAWN)) { - NBTTagCompound storedSpawn = tag.getCompoundTag(TAG_STORED_SPAWN); - ChunkCoordinates coords = new ChunkCoordinates( - storedSpawn.getInteger(TAG_SPAWN_POS_X), - storedSpawn.getInteger(TAG_SPAWN_POS_Y), - storedSpawn.getInteger(TAG_SPAWN_POS_Z)); - player.setSpawnChunk(coords, false, player.worldObj.provider.dimensionId); - tag.removeTag(TAG_STORED_SPAWN); - LogHelper.info( - "Restored spawn data for " + player - .getDisplayName() + ": " + coords + " dimID: " + player.worldObj.provider.dimensionId); + public static void restoreOriginalSpawn(EntityPlayer player) { + final BackpackProperty props = BackpackProperty.get(player); + + if (props != null) { + final ChunkCoordinates oldSpawn = props.getStoredSpawn(); + if (oldSpawn != null) { + player.setSpawnChunk(oldSpawn, false, player.worldObj.provider.dimensionId); + LogHelper.info( + "Restored spawn data for " + player.getDisplayName() + + ": " + + oldSpawn + + " dimID: " + + player.worldObj.provider.dimensionId); + } } else { LogHelper.warn("No spawn data to restore for " + player.getDisplayName()); } @@ -185,18 +179,22 @@ public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer p // and the bed location isn't set until then, normally. if (isSleepingInPortableBag(player)) { - storeOriginalSpawn(player, Wearing.getWearingBackpackInv(player).getExtendedProperties()); - player.setSpawnChunk(new ChunkCoordinates(x, y, z), true, player.dimension); + storeOriginalSpawn(player); } else { - player.setSpawnChunk(new ChunkCoordinates(x, y, z), true, player.dimension); LogHelper.info("Looking for a campfire nearby..."); ChunkCoordinates campfire = CoordsUtils .findBlock3D(world, x, y, z, ModBlocks.blockCampFire, 8, 2); if (campfire != null) { LogHelper.info("Campfire Found, saving coordinates. " + campfire); BackpackProperty.get(player).setCampFire(campfire); + } else { + LogHelper.info("No campfire found. Keeping spawnpoint at previous location"); + storeOriginalSpawn(player); + BackpackProperty.get(player).setCampFire(null); } } + player.setSpawnChunk(new ChunkCoordinates(x, y, z), true, player.dimension); + return true; } else { if (enumstatus == EntityPlayer.EnumStatus.NOT_POSSIBLE_NOW) { diff --git a/src/main/java/com/darkona/adventurebackpack/handlers/PlayerEventHandler.java b/src/main/java/com/darkona/adventurebackpack/handlers/PlayerEventHandler.java index d667f223..3a976fae 100644 --- a/src/main/java/com/darkona/adventurebackpack/handlers/PlayerEventHandler.java +++ b/src/main/java/com/darkona/adventurebackpack/handlers/PlayerEventHandler.java @@ -274,15 +274,23 @@ public void playerWokeUp(PlayerWakeUpEvent event) { ChunkCoordinates bedLocation = player.getBedLocation(player.dimension); if (bedLocation != null && player.worldObj.getBlock(bedLocation.posX, bedLocation.posY, bedLocation.posZ) == ModBlocks.blockSleepingBag) { - // If the player wakes up in one of those super confortable SleepingBags (tm) (Patent Pending) + // If the player wakes up in one of those super comfortable SleepingBags (tm) (Patent Pending) if (BlockSleepingBag.isSleepingInPortableBag(player)) { BlockSleepingBag.packPortableSleepingBag(player); BackpackProperty.get(player).setWakingUpInPortableBag(true); LogHelper.info("Player just woke up in a portable sleeping bag."); } else { - BackpackProperty.get(player).setForceCampFire(true); - LogHelper.info( - "Player just woke up in a sleeping bag, forcing respawn in the last lighted campfire, if there's any"); + BackpackProperty props = BackpackProperty.get(player); + if (props != null) { + BackpackProperty.get(player).setWakingUpInDeployedBag(true); + if (props.getCampFire() != null) { + props.setForceCampFire(true); + LogHelper.info( + "Player just woke up in a deployed sleeping bag, forcing respawn near the last lit campfire"); + } else { + LogHelper.info("Player just woke up in a deployed sleeping bag away from any campfire"); + } + } } } else { // If it's a regular bed or whatever @@ -293,19 +301,25 @@ public void playerWokeUp(PlayerWakeUpEvent event) { @SubscribeEvent public void tickPlayer(TickEvent.PlayerTickEvent event) { EntityPlayer player = event.player; - if (player != null && !player.isDead && Wearing.isWearingWearable(player)) { - if (event.phase == TickEvent.Phase.START) { - BackpackProperty.get(player).executeWearableUpdateProtocol(); - } - if (event.phase == TickEvent.Phase.END) { - if (!player.worldObj.isRemote) { - if (BackpackProperty.get(player).isWakingUpInPortableBag() && Wearing.isWearingBackpack(player)) { - BlockSleepingBag.restoreOriginalSpawn( - player, - Wearing.getWearingBackpackInv(player).getExtendedProperties()); - BackpackProperty.get(player).setWakingUpInPortableBag(false); + if (player != null && !player.isDead) { + if (Wearing.isWearingWearable(player)) { + if (event.phase == TickEvent.Phase.START) { + BackpackProperty.get(player).executeWearableUpdateProtocol(); + } + if (event.phase == TickEvent.Phase.END) { + if (!player.worldObj.isRemote) { + if (BackpackProperty.get(player).isWakingUpInPortableBag() + && Wearing.isWearingBackpack(player)) { + BlockSleepingBag.restoreOriginalSpawn(player); + BackpackProperty.get(player).setWakingUpInPortableBag(false); + } } } + } else if (event.phase == TickEvent.Phase.END && !player.worldObj.isRemote) { + if (BackpackProperty.get(player).isWakingUpInDeployedBag()) { + BlockSleepingBag.restoreOriginalSpawn(player); + BackpackProperty.get(player).setWakingUpInDeployedBag(false); + } } } if (player != null && !player.isDead && player instanceof EntityPlayer) { diff --git a/src/main/java/com/darkona/adventurebackpack/playerProperties/BackpackProperty.java b/src/main/java/com/darkona/adventurebackpack/playerProperties/BackpackProperty.java index 16d1734d..0bf9b7f3 100644 --- a/src/main/java/com/darkona/adventurebackpack/playerProperties/BackpackProperty.java +++ b/src/main/java/com/darkona/adventurebackpack/playerProperties/BackpackProperty.java @@ -16,14 +16,17 @@ public class BackpackProperty implements IExtendedEntityProperties { private static final String PROPERTY_NAME = "abp.property"; + private static final String TAG_STORED_SPAWN = "storedSpawn"; private EntityPlayer player; private ItemStack wearable = null; + private ChunkCoordinates storedSpawn = null; private ChunkCoordinates campFire = null; private boolean forceCampFire = false; private int dimension = 0; private boolean isWakingUpInPortableBag = false; + private boolean isWakingUpInDeployedBag = false; public void setWakingUpInPortableBag(boolean b) { this.isWakingUpInPortableBag = b; @@ -33,6 +36,14 @@ public boolean isWakingUpInPortableBag() { return this.isWakingUpInPortableBag; } + public void setWakingUpInDeployedBag(boolean b) { + this.isWakingUpInDeployedBag = b; + } + + public boolean isWakingUpInDeployedBag() { + return this.isWakingUpInDeployedBag; + } + public static void sync(EntityPlayer player) { if (player instanceof EntityPlayerMP) { syncToNear((EntityPlayerMP) player); @@ -72,6 +83,13 @@ public static BackpackProperty get(EntityPlayer player) { @Override public void saveNBTData(NBTTagCompound compound) { if (wearable != null) compound.setTag("wearable", wearable.writeToNBT(new NBTTagCompound())); + if (storedSpawn != null) { + NBTTagCompound spawn = new NBTTagCompound(); + spawn.setInteger("posX", storedSpawn.posX); + spawn.setInteger("posY", storedSpawn.posY); + spawn.setInteger("posZ", storedSpawn.posZ); + compound.setTag(TAG_STORED_SPAWN, spawn); + } if (campFire != null) { compound.setInteger("campFireX", campFire.posX); compound.setInteger("campFireY", campFire.posY); @@ -87,6 +105,14 @@ public void loadNBTData(NBTTagCompound compound) { setWearable( compound.hasKey("wearable") ? ItemStack.loadItemStackFromNBT(compound.getCompoundTag("wearable")) : null); + if (compound.hasKey(TAG_STORED_SPAWN)) { + NBTTagCompound spawn = compound.getCompoundTag(TAG_STORED_SPAWN); + setStoredSpawn( + new ChunkCoordinates( + spawn.getInteger("posX"), + spawn.getInteger("posY"), + spawn.getInteger("posZ"))); + } setCampFire( new ChunkCoordinates( compound.getInteger("campFireX"), @@ -110,6 +136,14 @@ public ItemStack getWearable() { return wearable; } + public void setStoredSpawn(ChunkCoordinates coords) { + storedSpawn = coords; + } + + public ChunkCoordinates getStoredSpawn() { + return storedSpawn; + } + public void setCampFire(ChunkCoordinates cf) { campFire = cf; }