Adding location of sign to the config

Discussion in 'Plugin Development' started by mouz_, Dec 14, 2015.

Thread Status:
Not open for further replies.
  1. Hi, I have problems with adding the location of sign to the config and updating it. I had location in the config, but it was deleting my whole config. (http://pastebin.com/La3xhjSm)

    On enable I have something like this to update the signs:
    Code:
            locations = (List<Location>) config.get("signs");
           if (locations == null) {
                locations = new ArrayList<Location>();
           }
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(this, new Runnable() {
                @Override
               public void run() {
                   for (Location loc: locations) {
                       if (loc.getBlock().getType().equals(Material.WALL_SIGN)) {
                            Sign sign = (Sign) loc.getBlock().getState();
    then there is code to update lines of the sign.

    I'm adding the sign like that:
    Code:
        public void onSignChange(final SignChangeEvent e) {
            Player p = e.getPlayer();
            if (!p.hasPermission("castlewars.admin")) {
                return;
            }
            final FileConfiguration config = this.getConfig();
            Block sign = e.getBlock();
            locations.add(sign.getLocation());
            config.set("signs", locations);
            config.options().copyDefaults(true);
            this.saveConfig();
        }
    How to add location of signs to the config, loop through and update them?
     
  2. Offline

    Zombie_Striker

    Why does this need to be final?

    Code:
        - !!org.bukkit.Location
          ^
    This is the problem. When saving the location variable, something broke. You might need to clear out the config (or that one path) and see if it still happens.
     
  3. Offline

    mythbusterma

    @Zombie_Striker

    In general, things should be final if you don't intend on changing them. It's good practise.


    @mouz_

    If you're not running 1.8, you can't serialize locations like that. Besides, you then just copy the defaults over right after, which clears the config. If you actually read the documentation, you would know this.
     
  4. Offline

    mcdorli

    "How to add location of signs to the config"
    Save the x, y and z values, and the worlds name separately like this
    Code:
    location1:
      x: 123
      y: 72
      z: 183
      world: world
    
    "loop through and update them"
    http://wiki.bukkit.org/Configuration_API_Reference
     
  5. I created something like that:
    - getting the location of sign:
    Code:
        public String getSignLocation(final Block sign) {
            final Location loc = sign.getLocation();
            final String location = String.valueOf(sign.getWorld().getName()) + "/" + (int)loc.getX() + "/" + (int)loc.getY() + "/" + (int)loc.getZ() + "/" + loc.getYaw() + "/" + loc.getPitch();
            return location;
        }
    - adding sign to the config:
    Code:
        @EventHandler
        public void onSignChange(final SignChangeEvent e) {
            Player p = e.getPlayer();
            if (!p.hasPermission("castlewars.admin")) {
                return;
            }
            final FileConfiguration config = this.getConfig();
            Block sign = e.getBlock();
            locations.add(sign.getLocation());
            config.set("signs", (Object)this.getSignLocation(sign));
            config.options().copyDefaults(true);
            this.saveConfig();
        }
    How to get locations from the config and loop through them? @mcdorli I didn't found anything about getting the locations.
     
  6. Offline

    Mrs. bwfctower

    @mouz_ MemorySection#getKeys(boolean deep)

    Why are you wrapping a String with String#valueOf?
    And why are you casting the coords to integers?
    And no need to store the yaw or pitch, they are irrelevant when it comes to blocks.

    Don't you want to cancel the event?
    You're going to want to store a new location, instead of overwriting the old one, right?
     
  7. Well, there is no event, so you can't cancel it.

    How to store a new location? I have this error: http://scr.hu/1gy8/zxvpj when I'm trying to do
    Code:
    locations.add((Object)this.getSignLocation(sign));
     
  8. Offline

    timtower Administrator Administrator Moderator

    @mouz_ Why are you casting it to an object?
     
  9. Offline

    Mrs. bwfctower

    No event? What's this then?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @mouz_ Why are you using a string there? The locations list are Locations, you don't need the strings till the onDisable.
     
  11. So what to do?
     
  12. Offline

    timtower Administrator Administrator Moderator

    @mouz_ You add locations to your list. Not strings.
    Strings are for saving in the config.
     
  13. So I add the location to the list, then what?
    Code:
    locations.add(sign.getLocation());
     
  14. Offline

    Mrs. bwfctower

    @mouz_ Then in your onDisable method you save them (as strings) to the config.
     
  15. How to get strings from locations?
     
  16. Offline

    timtower Administrator Administrator Moderator

    @mouz_
    Funny how you already posted it.
     
  17. \

    (final Block sign)
    http://scr.hu/1gy8/7a7ao
    On disable:
    Code:
            for (Location loc: locations) {
                config.set("signs", this.getSignLocation(loc));
            }
     
  18. Offline

    timtower Administrator Administrator Moderator

    @mouz_ Are you really not able to change 2 variables around? Really?
     
  19. Oh sorry, I forgot that I don't need it in the SignChangeEvent.
    Code:
        public void onDisable() {
            final FileConfiguration config = this.getConfig();
            for (Location loc: locations) {
                config.set("signs", this.getSignLocation(loc));
            }
    Code:
        public void onEnable() {
            final FileConfiguration config = this.getConfig();
            locations = (List<Location>) config.get("signs");
            if (locations == null) {
                locations = new ArrayList<Location>();
            }
    Code:
        @EventHandler
        public void onSignChange(final SignChangeEvent e) {
            Player p = e.getPlayer();
            if (!p.hasPermission("castlewars.admin")) {
                return;
            }
            final FileConfiguration config = this.getConfig();
            Block sign = e.getBlock();
            locations.add(sign.getLocation());
            config.set("signs", locations);
            config.options().copyDefaults(true);
            this.saveConfig();
        }
    That's all?
     
  20. Offline

    timtower Administrator Administrator Moderator

    @mouz_ Nope.
    1. You are overriding 1 sign every time in the onDisable as you save on the exact same place every time.
    2. Your loading method won't work as Bukkit doesn't magically know that a list of strings can be converted to a list of locations.
    3. You are doing stuff with the config on the event, no need for that.
     
  21. 1. What do I do? Add it to the list or what?
    2. How to get locations from strings in the config?
    3. Deleted.
     
  22. Offline

    timtower Administrator Administrator Moderator

    @mouz_ 1. Get a string list, add all signs using your method to that, save that list.
    2. Do the reverse of 1
     
  23. 1. On disable:
    Code:
            for (Location loc: locations) {
                strings = new ArrayList<String>();
                strings.add(this.getSignLocation(loc));
                config.set("signs", strings);
            }
    2. I don't know how to use my method for string list.
    Code:
    final List<String> signs = (List<String>)config.getStringList("signs");
     
  24. Offline

    mcdorli

    Do you still don't feel, that only spoonfeeding would help here?
    You don' need that cast there, bukkit does it for you
    Now you have a string list ("wich shouldn't be final btw") with the signs locations. Now, you can create location from them, and use those locations to whatever you would like to

    Or just use bukkit 1.8.8. The locations are serializable there. Not the best solution, but better than nothing.
     
  25. Offline

    timtower Administrator Administrator Moderator

    Are you helping then or doing the work for him.
    @mouz_ You need to loop through the values, you don't do them all at once.
     
  26. You don't have to "spoonfeed", just try to help. :) How to loop trough them?

    Code:
            for (Location loc: locations) {
                strings = new ArrayList<String>();
                strings.add(this.getSignLocation(loc));
            }
            config.set("signs", strings);
    ?
     
  27. Offline

    timtower Administrator Administrator Moderator

    @mouz_ The loop is good, but line 2 should be for the loop, not in or after.
     
  28. Code:
            strings = new ArrayList<String>();
            for (Location loc: locations) {
                strings.add(this.getSignLocation(loc));
            }
            config.set("signs", strings);
    Is it good?
     
  29. Offline

    timtower Administrator Administrator Moderator

    @mouz_ You are indeed getting there.
     
Thread Status:
Not open for further replies.

Share This Page