From 80e534b14e2ff30686e8a3f823b2d946006f9cd5 Mon Sep 17 00:00:00 2001 From: Jimmy Allen Date: Thu, 18 Mar 2021 11:05:41 +1300 Subject: [PATCH] Fail to teleport if be is obstructed --- src/main/java/nz/jimmy/easyspawn/Bed.java | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/nz/jimmy/easyspawn/Bed.java b/src/main/java/nz/jimmy/easyspawn/Bed.java index 014e2e6..e6f1ba4 100644 --- a/src/main/java/nz/jimmy/easyspawn/Bed.java +++ b/src/main/java/nz/jimmy/easyspawn/Bed.java @@ -4,6 +4,10 @@ import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.*; /** * Bed @@ -21,8 +25,21 @@ public class Bed implements CommandExecutor { Player player = (Player)sender; //Check if player has a bed if(player.getBedSpawnLocation()!=null){ - player.teleport(player.getBedSpawnLocation()); - player.sendMessage("You have been teleported to your bed."); + Location bed = player.getBedSpawnLocation(); + //Check if 2 blocks above are air + World world = bed.getWorld(); + Material mat1 = world.getBlockAt(bed.getBlockX(), + bed.getBlockY()+1, bed.getBlockZ()).getType(); + Material mat2 = world.getBlockAt(bed.getBlockX(), + bed.getBlockY()+2, bed.getBlockZ()).getType(); + + if(mat1 == Material.AIR && mat2 == Material.AIR) { + player.teleport(bed); + player.sendMessage("You have been teleported to your bed."); + } else { + player.sendMessage("Your bed is obstructed."); + } + }else sender.sendMessage("You need to sleep in a bed first."); return true; }