Solved Problems with decimals

Discussion in 'Spigot Plugin Help' started by PartyMC, Feb 18, 2020.

  1. Hi! Im currently working on creating my own simple spawn command. Everything works, but when I set the spawn and the coordinates, no decimals show up. This means that players dont get teleported to the middle of the block. I want the coordinates to be saved with 1 decimal. This is my current code:

    Code (Text):
    package me.PartyProNL.PartyParks.Commands;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.PartyProNL.PartyParks.Main;
    import me.PartyProNL.PartyParks.Util.Color;

    public class SpawnCommand implements CommandExecutor{
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String string, String[] args) {

            if(cmd.getName().equalsIgnoreCase("setspawn")) {
               
                if(sender instanceof Player) {
                   
                    Player p = (Player)sender;

                        String world = p.getWorld().getName();
                        int x = p.getLocation().getBlockX();
                        int y = p.getLocation().getBlockY();
                        int z = p.getLocation().getBlockZ();
                        float pitch = p.getLocation().getPitch();
                        float yaw = p.getLocation().getYaw();
                        Main.plugin.getConfig().set("spawn.x", x);
                        Main.plugin.getConfig().set("spawn.y", y);
                        Main.plugin.getConfig().set("spawn.z", z);
                        Main.plugin.getConfig().set("spawn.pitch", pitch);
                        Main.plugin.getConfig().set("spawn.yaw", yaw);
                        Main.plugin.getConfig().set("spawn.world", world);
                       
                        Main.plugin.getConfig().options().copyDefaults(true);
                        Main.plugin.saveConfig();
                   
                        p.sendMessage(Color.color("&aSuccesfully set spawn to &f" + x + "x " + y + "y " + z + "z&a in world &f" + world));
                       
                    return true;
                   
                }
                else {
                    Bukkit.getServer().getConsoleSender().sendMessage("This command can only be executed by a player!");
                    return true;
                }
               
            }
           
            if(cmd.getName().equalsIgnoreCase("spawn")) {
                if(sender instanceof Player) {
                    Player p = (Player)sender;
                   
                    try {
                       
                        World world = Bukkit.getServer().getWorld(Main.plugin.getConfig().getString("spawn.world"));
                        int x = Main.plugin.getConfig().getInt("spawn.x");
                        int y = Main.plugin.getConfig().getInt("spawn.y");
                        int z = Main.plugin.getConfig().getInt("spawn.z");
                        Location spawn = new Location(world, x, y, z);
                        spawn.setYaw( (float) Main.plugin.getConfig().getDouble("spawn.yaw"));
                        spawn.setPitch( (float) Main.plugin.getConfig().getDouble("spawn.pitch"));
                        p.teleport(spawn);
                       
                    } catch(NullPointerException e) {
                        p.sendMessage(Color.color("&cThe spawn has not been set yet!"));
                    }
                   
                    return true;
                }
               
                else {
                    Bukkit.getServer().getConsoleSender().sendMessage("This command can only be executed by a player!");
                    return true;
                }
            }
           
            return false;
        }

    }
    Like you see, I am setting the coordinates of the player to the config. But it doesnt save any decimals. How can I save them?
     
  2. Rather than using getLocation().getBlockX(), try getLocation().getX();

    That will return a double which will have a decimal. You will also need to save the value into the config and read it as a double / float as ints cannot have decimals.
     
  3. This fixed it, did not know Ints did not have decimals. Thanks alot!
     
  4. SteelPhoenix

    Moderator

    Because Location is ConfigurationSerializable, you can just use set(path, location) and getLocation(String path) (or (Location) get(String path) in older versions).
     
    • Informative Informative x 2
    • Agree Agree x 1