Inconsistent Location saving as String

Discussion in 'Spigot Plugin Development' started by janie177, Jun 28, 2016.

  1. Hey,

    I have been coming across a strange thing for a couple of months now. For a couple of plugins I have, I convert locations to String using a util class I made. Then I save this string to the config and load it again to convert back to location.

    I mostly do this to respawn chests in set locations. The thing that happens though, is that sometimes (like 1% of the time) the chest seems to respawn in the wrong spot. Meaning it will either be one block too high, or one block to the side.

    It's been happening very rarely in my worlds for a couple of months. Sometimes I suddenly come across two chests next to each other, of which only one is a set spawn location.

    I was wondering if anyone else has experienced similar problems, where a location will be inconsistent sometimes. Like when using coordinates to create a new location such as: x = 17.43498 y= 85.348787 z= -398.343543, and the location being a different block sometimes.

    Also yes, I realize this could just be coincidence and someone placing those extra chests. This is unlikely though as very few people have build access in that particular server. I just want to make sure if this is not something that has been happening to others before I start a witchhunt.
     
  2. Store getBlock X, Y, Z as it is rounded aka the actual block location.
     
  3. Show us the util class
     
  4. http://pastebin.com/raw/BnNUd0Gu

    This is the methods I use. I'm really just wondering how the same input could lead to a different output in very rare occasions. It doesn't make sense to me, yet chests keep appearing in the wrong location from time to time, while the coordinates in the config were the same and resulted in the correct location before.
     
  5.  
  6. Though sometimes I will want to have the exact spot when I use this for other things than chests. I use it for boss spawning too for example, or my custom spawn plugins. I really wanna keep the ability to be able to set a location on the center of a block. Besides I still fail to see how entering the exact same numbers 100 times would result in a location 99 times, and a different one once.
     
  7. Code (Text):
        public static Location stringToLocation(String s, Boolean getDecimal) {
            String[] split = s.split(",");
            double x = Double.parseDouble(split[0]);
            double y = Double.parseDouble(split[1]);
            double z = Double.parseDouble(split[2]);
            World w;

            try {
                w = Bukkit.getWorld(split[3]);
            } catch (Exception ignored) {
                Bukkit.getLogger().info("[MGLib] World could not be found while converting location from string.");
                return null;
            }
            if (getDecimal){
            return new Location(w, x, y, z);
            )
            else {
                 Location l = new Location(w, x, y, z);
                 return new Location(w, l.getBlockX, l.getBlockY, l.getBlockZ);
            }
        }
    this should work if you want the block location for placing blocks use getDecimal false else true
     
  8. The decimal part is redundent just always store getBlock X, Y, Z
     
  9. This still doesn't explain to me why (or even if) block locations with decimals are inconsistent. I appreciate your help, though I would really like to know the reason why something does not work too. Thank you for helping though, I'll just add a boolean to it for decimal :D
     
  10. Normally it's not "inconsistent". Try figuring out how to reproduce the bug then we have better chances to find out the cause.

    Gesendet von meinem Nexus 5 mit Tapatalk