Save coordinates in an array?

Discussion in 'Plugin Development' started by NeguinDaFarofa, Aug 15, 2015.

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

    NeguinDaFarofa

    How do I save what's inside of a config (for coordinates) in a list?

    Command:
    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            Player p = (Player) sender;
            if (cmd.getName().equalsIgnoreCase("setpos")) {
                if (!p.hasPermission("escadona.admin")) {
                    p.sendMessage("§4§lVoce nao possui permissao!");
                    return true;
                }
                if (args.length < 1) {
                    p.sendMessage("§4§lComando nao encontrado! Acho que voce quis executar /setpos [1/2]");
                }
                else if ((args.length == 1) && (args[0].equalsIgnoreCase("1"))) {
                    plugin.getConfig().set("spawns." + args[0] + ".x", p.getLocation().getX());
                    plugin.getConfig().set("spawns." + args[0] + ".y", p.getLocation().getY());
                    plugin.getConfig().set("spawns." + args[0] + ".z", p.getLocation().getZ());
                    plugin.getConfig().set("spawns." + args[0] + ".yaw", p.getLocation().getYaw());
                    plugin.getConfig().set("spawns." + args[0] + ".pitch", p.getLocation().getPitch());
                    plugin.saveConfig();
                    p.sendMessage("§a§lPosicao §2§l1 §a§lmarcada!");
                    p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
                }
                else if ((args.length == 1) && (args[0].equalsIgnoreCase("2"))) {
                    plugin.getConfig().set("spawns." + args[0] + ".x", p.getLocation().getX());
                    plugin.getConfig().set("spawns." + args[0] + ".y", p.getLocation().getY());
                    plugin.getConfig().set("spawns." + args[0] + ".z", p.getLocation().getZ());
                    plugin.getConfig().set("spawns." + args[0] + ".yaw", p.getLocation().getYaw());
                    plugin.getConfig().set("spawns." + args[0] + ".pitch", p.getLocation().getPitch());
                    plugin.saveConfig();
                    p.sendMessage("§a§lPosicao §2§l2 §a§lmarcada!");
                    p.playSound(p.getLocation(), Sound.NOTE_PLING, 10, 1);
                }
                else {
                    p.sendMessage("§4§lIsso nao existe!");
                }
            }
            return false;
        }
    Config:
    Code:
    spawns:
      '1':
        x: 98.551801477632
        y: 173.875
        z: 108.45002072968907
        yaw: 0.15041712
        pitch: 4.4999256
      '2':
        x: 100.57317019583716
        y: 173.875
        z: 108.29259125468619
        yaw: 0.60043335
        pitch: 4.4999104
     
  2. Offline

    khave

    I'd create a custom data class that houses the x, y, z, pitch, and yaw and then just put that into a List.
     
  3. Offline

    NeguinDaFarofa

    Code:
     public void setLocation( String name , Location location ) {
      if (location == null) {
       create( name );
       return;
      }
      set( name + ".world" , location.getWorld().getName() );
      set( name + ".x" , location.getX() );
      set( name + ".y" , location.getY() );
      set( name + ".z" , location.getZ() );
      set( name + ".yaw" , location.getYaw() );
      set( name + ".pitch" , location.getPitch() );
      saveConfig();
    }
    
    public boolean hasLocation( String name ) {
    
      return has( name + ".world" ) && has( name + ".x" ) && has( name + ".y" )
       && has( name + ".z" ) && has( name + ".yaw" ) && has( name + ".pitch" );
    }
    
    public Location getLocation( String name ) {
    
      if ( hasLocation( name ) ) {
       World world = Bukkit.getWorld( ( String ) get( name + ".world" ) );
       double x = ( double ) get( name + ".x" );
       double y = ( double ) get( name + ".y" );
       double z = ( double ) get( name + ".z" );
       float yaw = ( float ) getConfig().getDouble( name + ".yaw" );
       float pitch = ( float ) getConfig().getDouble( name + ".pitch" );
       return new Location( world , x , y , z , yaw , pitch );
      }
      return null;
    }
    Like this?
     
    Last edited: Aug 15, 2015
  4. Offline

    Krumb069

    You can save them as string : x/y/z/yaw/pitch/world
    Then you can do split for getting location
     
  5. Offline

    567legodude

    @NeguinDaFarofa You can save a location as a string like @Krumb069 said.
    There are 2 methods you can use to do that:
    Code:
    public static String locToString(Location loc) {
        return loc.getWorld().getName() + ";" + loc.getX() + ";" + loc.getY() + ";" + loc.getZ() + ";" + loc.getPitch() + ";" + loc.getYaw();
    }
    
    public static Location stringToLoc(String s) {
        String[] part = s.split(Pattern.quote(";"));
        Location loc = new Location(Bukkit.getWorld(part[0]), Double.parseDouble(part[1]), Double.parseDouble(part[2]), Double.parseDouble(part[3]));
        loc.setPitch(Float.parseFloat(part[4]));
        loc.setYaw(Float.parseFloat(part[5]));
        return loc;
    }
     
    Krumb069 likes this.
  6. Offline

    FakeSwinThry

    And use printf() to set limit of numbers after dot. printf("%.3f;%.3f ...", x, y ...)
     
  7. Offline

    mythbusterma

  8. Offline

    FakeSwinThry

    Yeah, this is java. String.format(...)
     
Thread Status:
Not open for further replies.

Share This Page