Solved setting a respawn location from a config.

Discussion in 'Plugin Development' started by soulofw0lf, Apr 29, 2013.

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

    soulofw0lf

    I'm trying to go through my config, find the shortest distance between a current location and a set of locations saved in the config, and respawn the player at the closest destination. saving the destinations to the config is working fine, setting up some sendMessages to test reading the locations worked fine, but as soon as i try to go through all the locations to find the closest distance it acts really weird, i have no clue where it's getting the numbers from at all for the location it sets as the respawn, and i'm getting no errors at all! here's my listener

    Code:
    public class RespawnListener implements Listener {
    RPGSpawns Rpgs;
    public RespawnListener(RPGSpawns rpgs){
    this.Rpgs = rpgs;
    }
    @EventHandler(priority = EventPriority.HIGHEST)
    public void onPlayerRespawn(PlayerRespawnEvent event){
    Player k = event.getPlayer();
    Location loc = k.getLocation();
    World thisworld = loc.getWorld();
    String world = thisworld.getName();
    Double smallest = null;
    Location newspawn = loc;
    for(String key : this.Rpgs.getConfig().getConfigurationSection(world).getKeys(false)){
    String pitch = this.Rpgs.getConfig().getString(world + "." + key + ".Pitch");
    String yaw = this.Rpgs.getConfig().getString(world + "." + key + ".Yaw");
    Float newpitch = Float.parseFloat(pitch);
    Float newyaw = Float.parseFloat(yaw);
    Location newloc = new Location(thisworld,this.Rpgs.getConfig().getDouble(world + "." + key + ".X"),this.Rpgs.getConfig().getDouble(world + "." + key + ".Y"),this.Rpgs.getConfig().getDouble(world + "." + key + ".Z"),newyaw,newpitch);
    Double dist = loc.distanceSquared(newloc);
    if ((smallest) != null){
    if (smallest >= dist){
    newspawn = newloc;
    }
    } else {
    smallest = dist;
    }
     
     
    }
    event.setRespawnLocation(newspawn);
    }
    }
    and here's my config

    Code:
    Cretin's_Chasm:
      spawn1:
        X: -7.292492299772318
        Y: 45.0
        Z: 96.47696902656669
        Pitch: 6.5163665
        Yaw: 182.54614
      spawn2:
        X: 0.012764183102335569
        Y: 53.0
        Z: 279.6506220638138
        Pitch: 14.386572
        Yaw: 262.03925
      spawn3:
        X: 110.80657419730137
        Y: 51.0
        Z: -148.95243237086805
        Pitch: 1.8941553
        Yaw: 42.17383
      spawn4:
        X: 21.06041893896721
        Y: 73.0
        Z: -334.072087534114
        Pitch: 4.267683
        Yaw: 350.6172
    any help at all would be greatly appreciated!

    EDIT: SOLVED code used to solved the problem has been replaced in the code section above for anyone else having the same problem

    Updated to reflect changes in the way i was trying to do it, still can't get it to work at all. any help would be appreciated.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 1, 2016
Thread Status:
Not open for further replies.

Share This Page