Solved chest blockface

Discussion in 'Plugin Development' started by Hempfest, Dec 12, 2015.

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

    Hempfest

    Hello im having trouble with my plugin, i have my events registered so thats not the problem. i am trying to implement when you right click a chest it checks if the blockface in each direction is a sign and reads a certain line of the sign to perform or disable a certain action. when i had my earlier code it had worked only for the direction "NORTH". After i cleaned up some code im getting stack trace errors i dont recognize. some help would be much appreciated. i tried seeing if the blockface sign's 2nd line read {Lockit} so if you have a certain permission you can open the chest but if not you are un-authorized to open it. and also would i have to add anything extra for it to work with double chests or would it automatically?


    Code:java
    1. //Making of locked sign event
    2.  
    3. @EventHandler
    4. public void OnSignChange(SignChangeEvent event)
    5. {
    6. Player p = event.getPlayer();
    7. if (event.getPlayer().hasPermission("core.pro"))
    8. if (event.getLine(0).equalsIgnoreCase("lockeddd")) {
    9. p.sendMessage("§eCreating locked chest.");
    10. event.setLine(2, "{Lockit}");
    11. event.setLine(0, null);
    12. } else {
    13. p.sendMessage("§eCreating normal sign");
    14. }
    15. }
    16.  
    17. // Check blockface
    18. @EventHandler
    19. public void onLockedChest(PlayerInteractEvent event) {
    20. if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    21. if (event.getClickedBlock().getState() instanceof Chest) {
    22. Location loc = event.getClickedBlock().getLocation();
    23. Block b1 = loc.getBlock().getRelative(BlockFace.NORTH);
    24. Block b2 = loc.getBlock().getRelative(BlockFace.EAST);
    25. Block b3 = loc.getBlock().getRelative(BlockFace.SOUTH);
    26. Block b4 = loc.getBlock().getRelative(BlockFace.WEST);
    27.  
    28. if (b1.getType() == Material.WALL_SIGN
    29. || b2.getType() == Material.WALL_SIGN
    30. || b3.getType() == Material.WALL_SIGN
    31. || b4.getType() == Material.WALL_SIGN) {
    32. Player player = event.getPlayer();
    33. Sign sign = (Sign)b1.getState();
    34. Sign sign2 = (Sign)b2.getState();
    35. Sign sign3 = (Sign)b3.getState();
    36. Sign sign4 = (Sign)b4.getState();
    37. if ((!player.hasPermission("core.pro")) &&
    38. (sign.getLine(2).equals("{Lockit}") || sign2.getLine(2).equals("{Lockit}") || sign3.getLine(2).equals("{Lockit}") || sign4.getLine(2).equals("{Lockit}")))
    39. {
    40. player.sendMessage(ChatColor.RED + "Only the rank " + ChatColor.AQUA + "PRO" + ChatColor.RED + " can open these chest's.");
    41. event.setCancelled(true);
    42. }
    43. return;
    44. }
    45.  
    46.  
    47. }
    48. // getState end
    49. }
    50. }


    upload_2015-12-12_22-50-10.png
    error when i make a sign but it some how interprets the lines correctly

    upload_2015-12-12_22-51-13.png
    When i have permission and open the chest
     
  2. Offline

    Lightspeed

    Why don't you create a custom meta data that has a list of players and add it to the chest only allowing the players in the meta data to open it? I think this is possible.

    This will save space and time if possible.
     
  3. Offline

    mcdorli

    "blockstate cannot be cast to sign"
    This is your problem: Sign sign = (Sign)b1.getState();
     
    Zombie_Striker likes this.
  4. Offline

    Hempfest

    Possibly, I want the rank pro to be the only rank besides mod and up to have access.

    How do I check the line of a surrounding Wall sign then

    Nvm guys, i managed to fix it! still working on the sign change but for the most part it works, no stack trace errors! even when creating the sign.
    New Code:

    @EventHandler
    public void onLockedChest(PlayerInteractEvent event) {
    if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    if (event.getClickedBlock().getState() instanceof Chest) {
    Location loc = event.getClickedBlock().getLocation();
    Block b1 = loc.getBlock().getRelative(BlockFace.NORTH);
    Block b2 = loc.getBlock().getRelative(BlockFace.EAST);
    Block b3 = loc.getBlock().getRelative(BlockFace.SOUTH);
    Block b4 = loc.getBlock().getRelative(BlockFace.WEST);


    if ((b1 != null) || (b2 != null) || (b3 != null) || (b4 != null)) {
    Player p = event.getPlayer();
    BlockState st =b1.getState();
    if (st instanceof Sign) {
    Sign s = (Sign) st;
    if (!p.hasPermission("core.pro") && s.getLine(2).equals("{Lockit}")) {
    p.sendMessage(ChatColor.RED + "Only the rank " + ChatColor.AQUA + "PRO" + ChatColor.RED + " can open these chest's.");
    event.setCancelled(true);

    }
    }
    BlockState st2 = b2.getState();
    if (st2 instanceof Sign) {
    Sign s = (Sign) st2;
    if (!p.hasPermission("core.pro") && s.getLine(2).equals("{Lockit}")) {
    p.sendMessage(ChatColor.RED + "Only the rank " + ChatColor.AQUA + "PRO" + ChatColor.RED + " can open these chest's.");
    event.setCancelled(true);

    }
    }
    BlockState st3 = b3.getState();
    if (st3 instanceof Sign) {
    Sign s = (Sign) st3;
    if (!p.hasPermission("core.pro") && s.getLine(2).equals("{Lockit}")) {
    p.sendMessage(ChatColor.RED + "Only the rank " + ChatColor.AQUA + "PRO" + ChatColor.RED + " can open these chest's.");
    event.setCancelled(true);

    }
    }
    BlockState st4 = b4.getState();
    if (st4 instanceof Sign) {
    Sign s = (Sign) st4;
    if (!p.hasPermission("core.pro") && s.getLine(2).equals("{Lockit}")) {
    p.sendMessage(ChatColor.RED + "Only the rank " + ChatColor.AQUA + "PRO" + ChatColor.RED + " can open these chest's.");
    event.setCancelled(true);

    }
    }
    return;
    }


    }
    // getState end
    }
    }

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Dec 18, 2015
  5. Offline

    87pen

    @Hempfest
    Use Code tags(spoilers or whatever they are called.) and change title to Solved, thanks!
     
  6. Offline

    Zombie_Striker

  7. Offline

    Hempfest

    Changed to solved, and how to i add my code to a spoiler? im new to using this bukkit page.
     
Thread Status:
Not open for further replies.

Share This Page