Solved The method getAttachedFace() is undefined for the type Sign

Discussion in 'Plugin Development' started by TheBoy3, Aug 1, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    TheBoy3

    I've been posting problems I've been having like mad about my in dev plugin, but I come here every time I can't find my problem on google.

    Anyways, the title kinda says it all, the method getAttachedFace() is undefined for Sign?

    Here is the code for the event:
    Code:java
    1. public void onPlayerBreakBlock(BlockBreakEvent event) {
    2. Player player = event.getPlayer();
    3. Block block = event.getBlock();
    4. BlockState state = block.getState();
    5. Sign sign;
    6.  
    7. if (state instanceof Sign) {
    8.  
    9. sign = (Sign) state;
    10.  
    11. if (!sign.getLine(0).equals("[MultiKit Shop]")) return;
    12.  
    13. if (!player.getName().equals(sign.getLine(3))) {
    14.  
    15. player.sendMessage(ChatColor.RED + "You may not break that shop!");
    16. event.setCancelled(true);
    17. } else {
    18. player.sendMessage(ChatColor.GREEN + "Shop unregistered.");
    19. }
    20. } else {
    21. BlockFace[] faces = {UP, EAST, NORTH, SOUTH, WEST};
    22. for (BlockFace face : faces) {
    23. Block b = block.getRelative(sign.getAttachedFace()); //Error is here
    24. }
    25. }
    26. }


    Can someone tell me how to fix this, or why this is happening? Thanks again.
     
  2. Offline

    chasechocolate

    Check your imports, you should be importing org.bukkit.block.Sign.
     
    foodyling likes this.
  3. Offline

    TheBoy3

  4. Offline

    chasechocolate

  5. Offline

    Garris0n

    I would think even the normal sign is attached to the DOWN face, so that probably won't work.

    Edit: You could try casting it to attachable, but the Sign class should have the method...
     
  6. Offline

    ZachBora

    You might be forced to check the block data for it's orientation.
     
  7. Offline

    TheBoy3


    I'd do that, but I need to check if the sign is a wallsign first, and I can't use sign.isWallSign(), because org.bukkit.block.Sign doesn't have the method.

    After some looking in the Javadocs, I found out that those methods are actually in org.bukkit.material.Sign because it implements Attachable, but I don't know how to get the methods from the class.
     
  8. Offline

    ZachBora

    TheBoy3 did you try instanceof WallSign ?
     
  9. Offline

    lycano

    Am I missing something?

    org.bukkit.block.Sign does not have getAttachedFace() only org.bukkit.material.Sign does have it. So the compiler is right about that.

    What you want is probably getting the blockface from the MaterialData which you can do by doing

    Code:java
    1.  
    2. BlockFace signFace = sign.getData().getAttachedFace();
    3.  


    If im not mistaken.
     
  10. Offline

    TheBoy3


    I don't understand why I need to try that, because the getAttachedFace() already checks if it is a wall sign, and if it is, it returns BlockFace DOWN.


    Thanks, I just figured that out, but now I'm having another problem with the code:

    Code:java
    1. @EventHandler
    2. public void onPlayerBreakBlock(BlockBreakEvent event) {
    3. Player player = event.getPlayer();
    4. Block block = event.getBlock();
    5. BlockState state = block.getState();
    6. Sign sign;
    7. org.bukkit.material.Sign materialsign;
    8.  
    9. plugin.logger.info("Stage 1"); //Debug line
    10.  
    11. if (state instanceof Sign) {
    12. sign = (Sign) state;
    13. if (!sign.getLine(0).equals("[MultiKit Shop]")) return;
    14.  
    15. if (!player.getName().equals(sign.getLine(3))) {
    16.  
    17. player.sendMessage(ChatColor.RED + "You may not break that shop!");
    18. event.setCancelled(true);
    19. } else {
    20. player.sendMessage(ChatColor.GREEN + "Shop unregistered.");
    21. }
    22. } else {
    23. plugin.logger.info("Stage 2"); //Debug line
    24. BlockFace[] faces = {UP, EAST, NORTH, SOUTH, WEST};
    25. List<Block> blocks = new ArrayList<Block>();
    26. for (BlockFace face : faces) {
    27. blocks.add(block.getRelative(face));
    28. }
    29. for (Block b : blocks) {
    30. if (b.getState() instanceof Sign) {
    31. plugin.logger.info("Stage 3"); //Debug line
    32. sign = (Sign) b.getState();
    33. if (sign.getLine(0).equals("[MultiKit Shop]")) {
    34. plugin.logger.info("Stage 4"); //Debug line
    35. materialsign = (org.bukkit.material.Sign) sign.getData();
    36. if (sign.getBlock().getRelative(materialsign.getAttachedFace()).equals(b)) {
    37. plugin.logger.info("Stage 5"); //Debug line
    38. if (!player.getName().equals(sign.getLine(3))) {
    39. player.sendMessage(ChatColor.RED + "You may not break that shop!");
    40. event.setCancelled(true);
    41. } else {
    42. player.sendMessage(ChatColor.GREEN + "Shop unregistered.");
    43. }
    44. }
    45. }
    46. }
    47. }
    48. }
    49. }


    I doesn't seem to go to stage 5, which means that sign.getBlock().getRelative(materialsign.getAttachedFace()).equals(b) seems to be returning false, can anyone tell me why?

    P.S. This may not be the most efficient code. :)
     
  11. Offline

    Tarestudio

    TheBoy3
    sign.getBlock().getRelative(materialsign.getAttachedFace()) gets the Block the Sign is attached to. b ist the Block of the Sign. So how should the Block of the Sign be equal to the Block next to the Sign?
     
  12. maybe you want to use
    .equals(block)
    sign and b are at the same Location.
     
  13. Offline

    TheBoy3

  14. Offline

    lycano

    TheBoy3 And here is your method cleaned.

    I would however suggest putting the logic into a class.

    You may have different SignTypes in the future and this would save you a lot of logic problems...

    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerBreakBlock(BlockBreakEvent event) {
    4. Player player = event.getPlayer();
    5. Block block = event.getBlock();
    6. BlockState state = block.getState();
    7.  
    8. plugin.logger.info("Stage 1"); //Debug line
    9.  
    10. if (state instanceof Sign) {
    11. Sign sign = (Sign) state;
    12. if (!(sign.getLine(0).equals("[MultiKit Shop]")))
    13. return;
    14.  
    15. if (player.getName().equals(sign.getLine(3))) {
    16. player.sendMessage(ChatColor.GREEN + "Shop unregistered.");
    17. } else {
    18. player.sendMessage(ChatColor.RED + "You may not break that shop!");
    19. event.setCancelled(true);
    20. }
    21.  
    22. return; // stop here and return
    23. }
    24.  
    25. plugin.logger.info("Stage 2"); //Debug line
    26. List<Block> blocks = new ArrayList<Block>(
    27. Arrays.asList(
    28. block.getRelative(BlockFace.UP),
    29. block.getRelative(BlockFace.EAST),
    30. block.getRelative(BlockFace.NORTH),
    31. block.getRelative(BlockFace.SOUTH),
    32. block.getRelative(BlockFace.WEST)
    33. )
    34. );
    35.  
    36. for (Block b : blocks) {
    37. plugin.logger.info("Stage 3"); //Debug line
    38.  
    39. // nothing to do here if the blockstate is not instance of sign
    40. if (!(b.getState() instanceof Sign))
    41. continue;
    42.  
    43. plugin.logger.info("Stage 4"); //Debug line
    44. Sign sign = (Sign) b.getState();
    45. if ((!(sign.getLine(0).equals("[MultiKit Shop]"))) || (sign.getBlock().getRelative(sign.getData().getAttachedFace()).equals(block)))
    46. continue;
    47.  
    48. plugin.logger.info("Stage 5"); //Debug line
    49. if (player.getName().equals(sign.getLine(3))) {
    50. player.sendMessage(ChatColor.GREEN + "Shop unregistered.");
    51. // unregister shop logic START
    52. // ...
    53. // unregister shop logic END
    54. return;
    55. } else {
    56. player.sendMessage(ChatColor.RED + "You may not break that shop!");
    57. event.setCancelled(true);
    58. return; // you can return/break from here and ignore the other blocks as this decision is final
    59. }
    60. }
    61. }
    62.  
     
  15. Offline

    ZachBora

    lycano I don't think the return before the last should be there, what if there is more than 1 shop attached to the block being broken.
     
    lycano likes this.
  16. Offline

    lycano

    ZachBora I don't know if thats required. If thats needed then it should be removed.
     
Thread Status:
Not open for further replies.

Share This Page