Block Break Event not working

Discussion in 'Plugin Development' started by Gamerdude157, Nov 4, 2014.

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

    Gamerdude157

    hey guys i made a chest lock plugin and it worked great until i realized that you must listen for block break event(see if someone is trying to break it AND see if the user is breaking it therefore removing it from the config) but it wont work when someone tries to delete their own chest. if a stranger tries it gives him the you cant break message, but when an owner breaks it the config wont save. I did config.save() and im getting no console errors. What is happening!
    Code:java
    1. @EventHandler
    2. public void onBreak(BlockBreakEvent e) throws IOException {
    3. Player p = e.getPlayer();
    4. Block b = e.getBlock();
    5. if (b.getType().equals(Material.CHEST)) {
    6. BlockFace face = BlockFace.NORTH_EAST;
    7. for (BlockFace c : new BlockFace[]{BlockFace.NORTH,BlockFace.SOUTH,BlockFace.EAST,BlockFace.WEST}) {
    8. if (b.getRelative(c,1).getType().equals(Material.CHEST)) {
    9. face = c;
    10. break;
    11. }
    12. }
    13. if (face == BlockFace.NORTH_EAST) {
    14. if (config.isSet(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ() + ".owner")) {
    15. if (!config.getString(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ() + ".owner").equals(UUIDS.getPlayerUUID(p.getName()))) {
    16. e.setCancelled(true);
    17. p.sendMessage(msg + " This chest has been locked by " + UUIDS.getPlayerName(config.getString(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ() + ".owner")));
    18. }else{
    19. config.set(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ(), null);
    20. config.save(file);
    21. }
    22. }
    23. }else{
    24. if (config.isSet(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ() + ".owner")) {
    25. if (!config.getString(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ() + ".owner").equals(UUIDS.getPlayerUUID(p.getName()))) {
    26. p.sendMessage(msg + " This chest has been locked by " + UUIDS.getPlayerName(config.getString(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ() + ".owner")));
    27. e.setCancelled(true);
    28. }else{
    29. config.set(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ(), null);
    30. config.save(file);
    31. }
    32. }
    33. else if (config.isSet(b.getWorld().getName() + "." + b.getRelative(face,1).getX() + b.getRelative(face,1).getY() + b.getRelative(face,1).getZ() + ".owner")) {
    34. if (!config.getString(b.getWorld().getName() + "." + b.getRelative(face,1).getX() + b.getRelative(face,1).getY() + b.getRelative(face,1).getZ() + ".owner").equals(UUIDS.getPlayerUUID(p.getName()))) {
    35. e.setCancelled(true);
    36. p.sendMessage(msg + " This chest has been locked by " + UUIDS.getPlayerName(config.getString(b.getWorld().getName() + "." + b.getRelative(face,1).getX() + b.getRelative(face,1).getY() + b.getRelative(face,1).getZ() + ".owner")));
    37. }else{
    38. config.set(b.getWorld().getName() + "." + b.getRelative(face,1).getX() + b.getRelative(face,1).getY() + b.getRelative(face,1).getZ(), null);
    39. config.save(file);
    40. }
    41. }
    42. }
    43. }
    44. else if (b.getType().equals(Material.FURNACE)) {
    45. if (config.isSet(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ() + ".owner")) {
    46. }
    47. }
    48. }
     
  2. Offline

    Dudemister1999

    Gamerdude157 Phew, dirty code. I'd do the following:

    1 - Use UUIDs, compatible between users.
    2 - Instead of all the config.isSet's, just do something like this:
    2.a - Use this class I created to store Locations: http://pastebin.com/YJLEbPYS
    2.b - Use this config example to be able to support more than chest locking
    Code:java
    1.  
    2. Config Example:
    3.  
    4. <uuid>
    5. world,0,10,123: CHEST
    6.  

    3 - As far as your issue, use my above examples and check if the config has <playerUUID>.<location>. If he does, let him break it. If not, cancel the event and tell him the block is protected by Bukkit.getOfflinePlayer(UUID);
     
  3. Offline

    Gamerdude157

    So what exactly is the problem Dudemister1999 becuase i am using uuids so whats happening
     
  4. Offline

    Dudemister1999

    Gamerdude157 So like you said, owners of blocks cannot break them. So if you store locations in configs like I showed above, you can just check if the config has the key <UUID>.<LOCATION>, then allow the block to be broken.
     
  5. Offline

    Gamerdude157

    no Dudemister1999 I said that NON-OWNERS can not break them. But the config is set up correctly and the path is right but for some reason when the owner breaks the block the config doesn't save like i set it to. Read my code once more

    hello?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  6. Offline

    Dudemister1999

    Gamerdude157 I was agreeing with your statement. "So like you said, owners of blocks cannot break them." > "So, you had said that owners of blocks cannot break the owned blocks" (Translation)
     
  7. Offline

    Gamerdude157

    ok whatever Dudemister1999 but why isn't the block being removed from the config? That is the actual problem. No errors at ALL just nothing
     
  8. Offline

    Dudemister1999

    Gamerdude157 I just noticed something:

    Code:java
    1. config.set(b.getWorld().getName() + "." + b.getX() + b.getY() + b.getZ(), null);


    That will create something like world.1051854, how would you figure out where the positions are?
     
  9. Offline

    Gamerdude157

    what do you mean Dudemister1999 figure out where the positions are? I save them like that on purpose

    hello Dudemister1999 ?

    hello?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  10. Offline

    Dudemister1999

    Gamerdude157 You can only bump in 12+ hours. (Unless it was changed to 24, like I've been hearing from time to time). Try to wait a while, people aren't hear to answer your every question immediately. As for your issue, it's most likely something obvious I missed. To be honest, the dirty(ish) code makes it hard to read, so I can't immediately identify the issue.
     
  11. Offline

    Gamerdude157

    ok it has been 16 hours
    bUmP
     
  12. Offline

    Watto

    Gamerdude157

    He means your getX, getY and getZ aren't spaced, so for example x: 20 y: 50 and z: 150 will convert into 2050150 and you have absolutely no way to differentiate between each coordinate.
     
  13. Offline

    Gamerdude157

    OMG :O I AM SO STUPID :p
    What would you reccomend for me to do then Watto and/or Dudemister1999
     
  14. Offline

    Dudemister1999

  15. Offline

    Watto

    Gamerdude157

    Space them
    getX() + " " + getY() + " " + getZ()
     
Thread Status:
Not open for further replies.

Share This Page