Search in sources :

Example 1 with PlayerBedLeaveEvent

use of org.bukkit.event.player.PlayerBedLeaveEvent in project Glowstone by GlowstoneMC.

the class GlowPlayer method leaveBed.

/**
 * This player leaves their bed causing them to quit sleeping.
 *
 * @param setSpawn Whether to set the bed spawn of the player
 */
public void leaveBed(boolean setSpawn) {
    Preconditions.checkState(bed != null, "Player is not in bed");
    GlowBlock head = BlockBed.getHead(bed);
    GlowBlock foot = BlockBed.getFoot(bed);
    // Determine exit location
    Block exitBlock = BlockBed.getExitLocation(head, foot);
    if (exitBlock == null) {
        // If no empty blocks were found fallback to block above bed
        exitBlock = head.getRelative(BlockFace.UP);
    }
    // Set their spawn (normally omitted if their bed gets destroyed instead of them leaving it)
    if (setSpawn) {
        setBedSpawnLocation(head.getLocation());
    }
    // Empty the bed
    BlockBed.setOccupied(head, foot, false);
    bed = null;
    sleeping = false;
    // And eject the player
    // Use center of block
    Location exitLocation = exitBlock.getLocation().add(0.5, 0.1, 0.5);
    setRawLocation(exitLocation, false);
    teleported = true;
    // Call event
    EventFactory.getInstance().callEvent(new PlayerBedLeaveEvent(this, head, setSpawn));
    playAnimationToSelf(EntityAnimation.LEAVE_BED);
    playAnimation(EntityAnimation.LEAVE_BED);
// TODO: Set bed metadata (LivingEntity)
}
Also used : GlowBlock(net.glowstone.block.GlowBlock) GlowBlock(net.glowstone.block.GlowBlock) Block(org.bukkit.block.Block) PlayerBedLeaveEvent(org.bukkit.event.player.PlayerBedLeaveEvent) Location(org.bukkit.Location)

Aggregations

GlowBlock (net.glowstone.block.GlowBlock)1 Location (org.bukkit.Location)1 Block (org.bukkit.block.Block)1 PlayerBedLeaveEvent (org.bukkit.event.player.PlayerBedLeaveEvent)1