Blacklisting Locations

Discussion in 'Plugin Development' started by hanahouhanah, Jul 24, 2014.

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

    hanahouhanah

    I'm trying to blacklist locations (x and y) then save it to a file.
    I really do believe I'm taking this in the wrong direction, though, and I am also receiving a NullPointerException for whatever reason.

    This is what I got so far, any ideas?

    Code:java
    1. FileConfiguration blocked = YamlConfiguration.loadConfiguration(plugin.blocks);
    2. Location loc = e.getClickedBlock().getLocation();
    3. if(loc != null)
    4. {
    5. if(blocked != null)
    6. {
    7. if(blocked.getList("blocks.x").contains(loc.getBlockX()) && blocked.getList("blocks.z").contains(loc.getBlockZ())) //NPE here
    8. {
    9. blocked.getList("blocks.x").remove(loc.getBlockX());
    10. blocked.getList("blocks.z").remove(loc.getBlockZ());
    11. p.sendMessage("[" + ChatColor.GOLD + "AntiAbuse" + ChatColor.WHITE + "]" + ChatColor.RED + "Block removed from blacklist.");
    12. try {
    13. blocked.save(plugin.blocks);
    14. } catch (IOException e1) {
    15. e1.printStackTrace();
    16. }
    17. }
    18. else
    19. {
    20. blocked.getList("blocks.x").add(loc.getBlockX(), null);
    21. blocked.getList("blocks.z").add(loc.getBlockZ(), null);
    22. p.sendMessage("[" + ChatColor.GOLD + "AntiAbuse" + ChatColor.WHITE + "]" + ChatColor.RED + "Block added to blacklist.");
    23. try {
    24. blocked.save(plugin.blocks);
    25. } catch (IOException e1) {
    26. e1.printStackTrace();
    27. }
    28. }
    29. }
    30. if(p.getItemInHand().getType() == Material.CACTUS)
    31. {
    32.  
    33. }
    34. }
     
  2. Offline

    _Filip

    What do you mean by "blacklisting"?
     
  3. Offline

    hanahouhanah

    TheSpherret Such as if a player tries to do something in that spot, they get denied.
    But I guess the real question is "how would I go about adding a bunch of locations to a list without it messing up"
     
  4. Offline

    _Filip

    hanahouhanah What kind of list is it? A list in the YAML or a Java List?
     
  5. Offline

    hanahouhanah

  6. Offline

    _Filip

    hanahouhanah With your current code, whenever you create a new blacklisted location, it overwrites the old one, am I right?
     
  7. Offline

    hanahouhanah

    TheSpherret That's what I was figuring would happen, but I haven't got to testing what would happen with the updated YAML because for some reason, I'm getting an NPE @

    Code:java
    1. if(blocked.getList("blocks.x").contains(loc.getBlockX()) && blocked.getList("blocks.z").contains(loc.getBlockZ()))

    Everything is spelled correctly, as my yaml looks like this:
    Code:
    blocks:
      x: {}
      z: {}
    
    and is created like this:
    Code:java
    1. try
    2. {
    3. blocks.createNewFile();
    4. FileConfiguration blockss = YamlConfiguration.loadConfiguration(blocks);
    5. blockss.createSection("blocks.x");
    6. blockss.createSection("blocks.z");
    7. blockss.save(blocks);
    8. }
    9. catch (IOException e)
    10. {
    11. e.printStackTrace();
    12. }
     
  8. Offline

    _Filip

    The problem is, there is no list under blocks.x or blocks.z, it's empty, you have to initialize it first.
     
  9. Offline

    hanahouhanah

    TheSpherret How might I go about doing this? I never had to initialize it before unless I did something different a while back.
     
  10. Offline

    _Filip

    hanahouhanah There is no List in that section at this time, so when you try to use the get method it returns null.
    You have to use the set method to set it to a list first.
     
  11. Offline

    hanahouhanah

    TheSpherret
    The config is now generated to look like this:
    Code:
    blocks:
      x: ''
    block:
      z: ''
    
    Set up as:
    Code:java
    1. try
    2. {
    3. blocks.createNewFile();
    4. FileConfiguration blockss = YamlConfiguration.loadConfiguration(blocks);
    5. blockss.createSection("blocks").set("x", "");
    6. blockss.createSection("block").set("z", "");
    7. blockss.save(blocks);
    8. }
    9. catch (IOException e)
    10. {
    11. e.printStackTrace();
    12. }


    But I still get an NPE on the same line:
    Code:java
    1. FileConfiguration blocked = YamlConfiguration.loadConfiguration(plugin.blocks);
    2. Location loc = e.getClickedBlock().getLocation();
    3. if(blocked.getList("blocks.x").contains(loc.getBlockX()) && blocked.getList("block.z").contains(loc.getBlockZ())) //NPE here
    4. {
    5. blocked.getList("blocks.x").remove(loc.getBlockX());
    6. blocked.getList("block.z").remove(loc.getBlockZ());
    7. p.sendMessage("[" + ChatColor.GOLD + "AntiAbuse" + ChatColor.WHITE + "]" + ChatColor.RED + "Block removed from blacklist.");
    8. try {
    9. blocked.save(plugin.blocks);
    10. } catch (IOException e1) {
    11. e1.printStackTrace();
    12. }
    13. }
    14. else
    15. {
    16. blocked.getList("blocks.x").add(loc.getBlockX(), null);
    17. blocked.getList("block.z").add(loc.getBlockZ(), null);
    18. p.sendMessage("[" + ChatColor.GOLD + "AntiAbuse" + ChatColor.WHITE + "]" + ChatColor.RED + "Block added to blacklist.");
    19. try {
    20. blocked.save(plugin.blocks);
    21. } catch (IOException e1) {
    22. e1.printStackTrace();
    23. }
    24. }
     
Thread Status:
Not open for further replies.

Share This Page