What should I use to getBlock behind a block? It is not working good: Code: if (e.getClickedBlock().getRelative(-1, 0, 0).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood button"); return; } else if (e.getClickedBlock().getRelative(+1, 0, 0).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood button"); return; } else if (e.getClickedBlock().getRelative(0, 0, -1).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood button"); return; } else if (e.getClickedBlock().getRelative(0, 0, +1).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood button"); return; }
Since I have already done something similar to this myself, I will provide you with the directional part (it's also all over the forums...): Code: int direction = (int) location.getYaw(); int pitch = (int) location.getPitch(); if(direction < 0) { direction += 360; direction = (direction + 45) / 90; }else { direction = (direction + 45) / 90; } if(pitch > 37) { //Player is looking up, I think.. }else if(pitch < -16) { //Player is looking down, I think.. }else if(direction % 2 == 0) { //Player is looking north or south. }else if(direction % 2 == 1) { //Player is looking east or west. } The north/south/east/west part may need to be changed a bit. Also, just use "block.getRelative(BlockFace.[direction])".
@Urag Erm... There is no method to magically get the block behind a block. You need to get the players direction first! Using the way I showed, you can determine which way the player is looking. Once you get the direction they are looking, simply get the relative block by doing "block.getRelative(BlockFace.[direction])". For example, if the player is looking down, the block behind is "block.getRelative(BlockFace.DOWN)".
@Shortninja66 What's wrong here? Only west works. Code: if (direction < 180 & direction > 0){ if (e.getClickedBlock().getRelative(BlockFace.WEST).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood job! west"); } } if (direction < 90 & direction > -90){ if (e.getClickedBlock().getRelative(BlockFace.NORTH).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood job! north"); } } if (direction > -180 & direction < -0){ if (e.getClickedBlock().getRelative(BlockFace.EAST).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood job! east"); } } if (direction < 90 & direction < -90){ if (e.getClickedBlock().getRelative(BlockFace.SOUTH).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aGood job! south"); } }
Ahh.. I see now, you do not care about up or down directions You can use this code from sk89q: Code: public String getCardinalDirection(Player player) { double rotation = (player.getLocation().getYaw() - 90) % 360; if (rotation < 0) { rotation += 360.0; } if (0 <= rotation && rotation < 22.5) { return "north"; } else if (22.5 <= rotation && rotation < 67.5) { return "north"; } else if (67.5 <= rotation && rotation < 112.5) { return "east"; } else if (112.5 <= rotation && rotation < 157.5) { return "south"; } else if (157.5 <= rotation && rotation < 202.5) { return "south"; } else if (202.5 <= rotation && rotation < 247.5) { return "south"; } else if (247.5 <= rotation && rotation < 292.5) { return "west"; } else if (292.5 <= rotation && rotation < 337.5) { return "north"; } else if (337.5 <= rotation && rotation < 360.0) { return "north"; } else { return null; } }
Code: } else if (337.5 <= rotation && rotation < 360.0) { if(getCardinalDirection(p).equalsIgnoreCase("north")){ p.sendMessage("§aNorth"); return "north"; } } Does not work. Eclipse underlines: - getCardinalDirection - return "north";
@Urag You clearly do not understand how methods work.. The code I sent is to be left alone! Just put that method in the same class as what you are working in and then do "if(getCardinalDirection(player).equalsIgnoreCase("north"))".
@Shortninja66 Code: package me.urag.randomteleport; import org.bukkit.Material; import org.bukkit.block.BlockFace; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; public class ButtonListener implements Listener { public String getCardinalDirection(Player player) { double rotation = (player.getLocation().getYaw() - 90) % 360; if (rotation < 0) { rotation += 360.0; } if (0 <= rotation && rotation < 22.5) { return "north"; } else if (22.5 <= rotation && rotation < 67.5) { return "north"; } else if (67.5 <= rotation && rotation < 112.5) { return "east"; } else if (112.5 <= rotation && rotation < 157.5) { return "south"; } else if (157.5 <= rotation && rotation < 202.5) { return "south"; } else if (202.5 <= rotation && rotation < 247.5) { return "south"; } else if (247.5 <= rotation && rotation < 292.5) { return "west"; } else if (292.5 <= rotation && rotation < 337.5) { return "north"; } else if (337.5 <= rotation && rotation < 360.0) { return "north"; } else { return null; } } @EventHandler public void onPlayerInteract(PlayerInteractEvent e){ if (!(e.getAction() == Action.RIGHT_CLICK_BLOCK)) return; if (e.getClickedBlock().getType() == Material.STONE_BUTTON){ if(getCardinalDirection(e.getPlayer()).equalsIgnoreCase("north")){ if (e.getClickedBlock().getRelative(BlockFace.NORTH).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aNorth"); } } if(getCardinalDirection(e.getPlayer()).equalsIgnoreCase("east")){ if (e.getClickedBlock().getRelative(BlockFace.NORTH).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aEast"); } } if(getCardinalDirection(e.getPlayer()).equalsIgnoreCase("south")){ if (e.getClickedBlock().getRelative(BlockFace.NORTH).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aSouth"); } } if(getCardinalDirection(e.getPlayer()).equalsIgnoreCase("west")){ if (e.getClickedBlock().getRelative(BlockFace.NORTH).getType() == Material.SPONGE){ e.getPlayer().sendMessage("§aWest"); } } } } } ============================================================================== Like dat?