Solved Player Spawn

Discussion in 'Spigot Plugin Development' started by Arcader2k, Jul 23, 2018.

  1. Hey guys! I'm trying to implement /spawn and /setspawn into my plugin. problem is, when i do: /spawn, i teleport to the corner of the "Spawnpoint" reguardless if i set the spawn in the center.

    Heres my code:
    Code (Text):

    if(cmd.getName().equalsIgnoreCase("setspawn"))
          {
              String world = p.getWorld().getName();
              double x = p.getLocation().getX();
              double y = p.getLocation().getY();
              double z = p.getLocation().getZ();
              float yaw = p.getLocation().getYaw();
              float pitch = p.getLocation().getPitch();
             
              getConfig().set("Spawn.World", world);
              getConfig().set("Spawn.X", x);
              getConfig().set("Spawn.Y", y);
              getConfig().set("Spawn.Z", z);
              getConfig().set("Spawn.Yaw", yaw);
              getConfig().set("Spawn.Pitch", pitch);
              saveConfig();
             
              p.sendMessage(getConfig().getString("Prefix").replace('&', '§') + " Spawn Set!");
          }
         
    if(cmd.getName().equalsIgnoreCase("spawn"))
          {
              World world = Bukkit.getWorld(getConfig().getString("Spawn.World"));
              double X = getConfig().getInt("Spawn.X");
              double Y = getConfig().getInt("Spawn.Y");
              double Z = getConfig().getInt("Spawn.Z");
              float yaw = (float) getConfig().getDouble("Spawn.Yaw");
              float pitch = (float) getConfig().getDouble("Spawn.Pitch");
             
              Location loc = new Location(world, X, Y, Z, yaw, pitch);
              p.sendMessage(getConfig().getString("Prefix").replace('&', '§') + " Teleporting...");
              p.teleport(loc);
          }
     
     
  2. You are using getInt() in your "spawn" command, so it will always be ".0", which is the corner of a block
    Try changing it to getDouble() so that it returns the same value that was set
     
    • Agree Agree x 1
  3. ^^

    Also, offtopic, I would recommend to add a try-catch block in case the spawn location wasn't set.
    And print some message, for example.
     
  4. when you teleport the player to the spawn, make the x y z an int (like 284 instead of something like 284.884394839)
    and add 0.5 to it. that will make it the middle of the block :)
     
  5. Best way to save and get a location from a file.
    https://www.spigotmc.org/threads/save-get-locations-file-configuration.305907/
     
  6. Try checking what I gave you, it will make your code shorter and more understandable
     
  7. This is what you meant?
    Code (Text):

    getConfig().set("Spawn.World", p.getWorld().getName());
              getConfig().set("Spawn.X", p.getLocation().getX());
              getConfig().set("Spawn.Y", p.getLocation().getY());
              getConfig().set("Spawn.Z", p.getLocation().getZ());
              getConfig().set("Spawn.Yaw", p.getLocation().getYaw());
              getConfig().set("Spawn.Pitch", p.getLocation().getPitch());
              saveConfig();
     
     
  8. No, if you haven't read the Resource, there is only 1 method that does all this.