diff --git a/src/main/java/cr0s/warpdrive/event/PlayerHandler.java b/src/main/java/cr0s/warpdrive/event/PlayerHandler.java index 34e7bf96..1feafa3c 100644 --- a/src/main/java/cr0s/warpdrive/event/PlayerHandler.java +++ b/src/main/java/cr0s/warpdrive/event/PlayerHandler.java @@ -16,10 +16,14 @@ import javax.annotation.Nonnull; import java.util.ArrayList; import net.minecraft.block.Block; +import net.minecraft.block.BlockBed; +import net.minecraft.block.BlockHorizontal; import net.minecraft.block.state.IBlockState; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.text.TextComponentTranslation; import net.minecraftforge.event.entity.player.EntityItemPickupEvent; import net.minecraftforge.event.entity.player.PlayerEvent; @@ -87,6 +91,44 @@ public class PlayerHandler { // check for maintenance and member access final IBlockState blockState = event.getWorld().getBlockState(blockPos); doCancelEventForNonMembers(event, blockPos, blockState); + + // check for bed usage while aboard ships, allowing players to set their spawn point with the bed + if ( event.getEntityPlayer() != null + && event.getEntityPlayer().inventory != null + && blockState.getBlock().equals(Blocks.BED) + && !event.getWorld().isRemote + && (blockPos.getX() != 0 || blockPos.getY() != 0 || blockPos.getZ() != 0) ) { + final ArrayList globalRegions = GlobalRegionManager.getContainers(EnumGlobalRegionType.SHIP, event.getWorld(), blockPos); + if (!globalRegions.isEmpty()) {// (aboard a ship) + // get the head location + BlockPos blockPosBed = blockPos; + if (blockState.getValue(BlockBed.PART) != BlockBed.EnumPartType.HEAD) { + blockPosBed = blockPos.offset(blockState.getValue(BlockHorizontal.FACING)); + final IBlockState blockStateHead = event.getWorld().getBlockState(blockPosBed); + if (blockState.getBlock() != blockStateHead.getBlock()) { + blockPosBed = blockPos; + } + } + + final BlockPos blockPosBedOriginal = event.getEntityPlayer().getBedLocation(event.getWorld().provider.getDimension()); + event.getEntityPlayer().setSpawnChunk(blockPosBed.toImmutable(), false, event.getWorld().provider.getDimension()); + final BlockPos blockPosBedUpdated = event.getEntityPlayer().getBedLocation(event.getWorld().provider.getDimension()); + if ( blockPosBedOriginal == null + || ( blockPosBedUpdated != null + && !blockPosBedUpdated.equals(blockPosBedOriginal) ) ) { + Commons.addChatMessage(event.getEntityPlayer(), + new TextComponentTranslation("commands.spawnpoint.success", + event.getEntityPlayer().getDisplayName(), + blockPosBedUpdated.getX(), + blockPosBedUpdated.getY(), + blockPosBedUpdated.getZ() )); + } + // cancel the event outside the overworld to prevent an explosion + if (event.getWorld().provider.getDimension() != 0) { + event.setCanceled(true); + } + } + } } private void doCancelEventForNonMembers(@Nonnull final PlayerEvent event,