Solved Spawn teleport not working

Discussion in 'Plugin Development' started by Randomguy, Oct 29, 2015.

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

    Randomguy

    Hi, I have this code:
    Code:
    if(commandLabel.equalsIgnoreCase("SetSpawn"))
            {
                if(p.hasPermission("spawnplugin.setspawn") || p.isOp())
                {
                    double x = p.getLocation().getX();
                    double y = p.getLocation().getY();
                    double z = p.getLocation().getZ();
                   
                    this.getConfig().set("spawn x", x);
                    this.getConfig().set("spawn y", y);
                    this.getConfig().set("spawn z", z);
                   
                    p.sendMessage(ChatColor.YELLOW + "" + ChatColor.GOLD + "Set spawn successfully!");
                }
                else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
               
                return true;
            }
            if(commandLabel.equalsIgnoreCase("Spawn"))
            {
                if(p.hasPermission("spawnplugin.spawn") || p.isOp())
                {
                        p.getLocation().setX(this.getConfig().getInt("spawn x"));
                        p.getLocation().setY(this.getConfig().getInt("spawn y"));
                        p.getLocation().setZ(this.getConfig().getInt("spawn z"));
                       
                        p.sendMessage(ChatColor.GREEN + "Whoosh");
                }
                else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
    It will say whoosh and set spawn successfully but it won't teleport the player... I don't think the x, y, and z are registering to the config correctly. Am i doing something wrong? Sorry if this is a stupid mistake, I've not coded in bukkit for awhile...
     
  2. Offline

    Ruptur

    @Randomguy
    Please use command.getName() instead of commandLabel

    Its best to set location thats in its own section.

    For example
    Code:
    spawn:
      x: 1
      y: 2
      z: 3
    You are currently setting only x, y, z coords to config (which isnt 'wrong') but its better to also set the world, picth and yaw since you are setting a spawn point.
    A much faster way of doing this is using location.serialize() because it saves you time.

    Eg
    Code:
    // The location you need to save, could be the player's location that  runs the command
    Location loc = something;
    
    // Save the location info a section called 'myspawn' in config
    getConfig().set("my spawn", loc.serialize()); 
    Config then would look something like
    Code:
    my spawn:
      x: 323.0
      y: 3.0
      z: 56.0
      world: A world
      pitch: 34.0
      yaw: 90.0
    With this then you can grab the location from the config easily and in less lines.
    To grab a location from a section in a config you can use Location.deserialize(map)

    An example of this:
    Code:
    Location fromConfig;
    ConfigurationSection section = getConfig().getConfigurationSection("my spawn");
    
    // Add a check here to make sure section isnt null (the spawn hasnt been set)
    
    // Get the keys and values of the section and deserialize to a location
    fromConfig = Location.deserialize(section.getValues(false));
    
    // Do something with the location
    Hope i helped :)
     
  3. Offline

    Randomguy

    Ok thanks!
     
  4. Offline

    Randomguy

    It sort of worked... I'm making a spawn plugin... Setting the spawn worked, but I get an error when I try to go to the spawn. I can show the error in the server, its on the
    Code:
    fromConfig = Location.deserialize(section.getValues(false));
    The error is saying I can't get a string from a world... IK what's wrong but I'm not sure how to fix it.
    Error:
    Code:
    [13:37:32 INFO]: ZachAttack1170 issued server command: /setspawn
    [13:37:35 INFO]: ZachAttack1170 issued server command: /spawn
    [13:37:35 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin SpawnPlugin v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit.jar:git-Bukkit-994b2aa]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit.jar:git-Bukkit-994b2aa]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:621) ~[craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1079) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:939) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit.jar:git-Bukkit-994b2aa]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_51]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_51]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SystemUtils.java:19) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:676) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:632) [craftbukkit.jar:git-Bukkit-994b2aa]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:540) [craftbukkit.jar:git-Bukkit-994b2aa]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_51]
    Caused by: java.lang.ClassCastException: org.bukkit.craftbukkit.v1_8_R3.CraftWorld cannot be cast to java.lang.String
        at org.bukkit.Location.deserialize(Location.java:593) ~[craftbukkit.jar:git-Bukkit-994b2aa]
        at com.Zach.SpawnPluginMain.MainClass.onCommand(MainClass.java:54) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit.jar:git-Bukkit-994b2aa]
        ... 15 more
    Code:
    Code:
    if(commandLabel.equalsIgnoreCase("Spawn"))
            {
                if(p.hasPermission("spawnplugin.spawn") || p.isOp())
                {
                    ConfigurationSection spawn = getConfig().getConfigurationSection("spawn");
                    
                    if(spawn == null) p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "A spawn has not been set yet! Type /SetSpawn to set a spawn or talk to a server administrator.");
                    
                    Location loc = Location.deserialize(spawn.getValues(false)); //error throwing line
                    
                    p.getLocation().setYaw(loc.getYaw());
                    p.getLocation().setWorld(loc.getWorld()); 
                    p.getLocation().setPitch(loc.getPitch());
                    p.getLocation().setX(loc.getX());
                    p.getLocation().setY(loc.getY());
                    p.getLocation().setZ(loc.getZ());
                       
                    p.sendMessage(ChatColor.GREEN + "Whoosh");
                }
                else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
               
                return true;
            }
    If i type /SetSpawn the config doesn't change, but everything works fine and there is no error...
    Code for SetSpawn:
    Code:
    if(commandLabel.equalsIgnoreCase("SetSpawn"))
            {
                if(p.hasPermission("spawnplugin.setspawn") || p.isOp())
                {
                    ConfigurationSection spawn = getConfig().getConfigurationSection("spawn");
                   
                    spawn.set("x", p.getLocation().getX());
                    spawn.set("y", p.getLocation().getY());
                    spawn.set("z", p.getLocation().getZ());
                    spawn.set("world", p.getLocation().getWorld());
                    spawn.set("yaw", p.getLocation().getYaw());
                    spawn.set("pitch", p.getLocation().getPitch());
                   
                    p.sendMessage(ChatColor.YELLOW + "" + ChatColor.GOLD + "Set spawn successfully!");
                }
                else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
               
                return true;
            }
    It may look like I am a beginner and I am... I know Java pretty well, it's just bukkit that I'm not really good with... All help appreciated! :)
     
  5. Offline

    87pen

  6. Offline

    Randomguy

    Thanks! But how would you do this with the world? getConfig().setWorld(...);

    @87pen

    Edit: I ended up doing this:

    Code:
    if(commandLabel.equalsIgnoreCase("SetSpawn"))
                {
                    if(p.hasPermission("spawnplugin.setspawn") || p.isOp())
                    {
                        ConfigurationSection spawn = getConfig().getConfigurationSection("spawn");
                       
                        spawn.set("x", p.getLocation().getX());
                        spawn.set("y", p.getLocation().getY());
                        spawn.set("z", p.getLocation().getZ());
                        spawn.set("world", p.getLocation().getWorld().getName());
                        spawn.set("yaw", p.getLocation().getYaw());
                        spawn.set("pitch", p.getLocation().getPitch());
                        saveConfig();
                       
                        p.sendMessage(ChatColor.YELLOW + "" + ChatColor.GOLD + "Set spawn successfully!");
                    }
                    else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
                   
                    return true;
                }
                if(commandLabel.equalsIgnoreCase("Spawn"))
                {
                    if(p.hasPermission("spawnplugin.spawn") || p.isOp())
                    {
                        ConfigurationSection spawn = getConfig().getConfigurationSection("spawn");
                        
                        if(spawn == null) p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "A spawn has not been set yet! Type /SetSpawn to set a spawn or talk to a server administrator.");
                        
                        p.getLocation().setX(getConfig().getDouble("spawn.x"));
                        p.getLocation().setY(getConfig().getDouble("spawn.y"));
                        p.getLocation().setZ(getConfig().getDouble("spawn.z"));
                        p.getLocation().setYaw((float) getConfig().getDouble("spawn.yaw"));
                        p.getLocation().setPitch((float) getConfig().getDouble("spawn.pitch"));
                        p.getLocation().setWorld(Bukkit.getWorld(this.getConfig().getString("spawn.world")));
                           
                        p.sendMessage(ChatColor.GREEN + "Whoosh");
                    }
                    else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
                   
                    return true;
                }
    The only problem is it doesn't teleport the player... no errors or anything... it just prints whoosh when you type /spawn
     
    Last edited: Oct 31, 2015
  7. Offline

    87pen

    @Randomguy Is it working now?, if it is please mark the thread as solved. Make sure to change getConfig().getString("spawn.x") to getConfig().getDouble("spawn.x") because you are trying to receive a double.
     
  8. Offline

    Randomguy

    IK that I did that... It doesn't work... I'm not sure what to get the world as? If I do getConfig().get("spawn.world"); and cast it it will literally be something like this:
    Code:
    Spawn:
       x:
       y:
       etc.
       world:
          spawn_animals: true
          pvp: true
          etc.
    Also it doesn't teleport the player...

    Read the comment above... I'm not sure how to get world... So i tried something don't know if it worked...

    Thanks for helping everyone! I got it working! This is the code:

    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[])
        {   
            if(sender instanceof Player)
            {   
                Player p = (Player) sender;
               
                if(commandLabel.equalsIgnoreCase("SetSpawn"))
                {
                    if(p.hasPermission("spawnplugin.setspawn") || p.isOp())
                    {
                        ConfigurationSection spawn = getConfig().getConfigurationSection("spawn");
                       
                        spawn.set("x", p.getLocation().getX());
                        spawn.set("y", p.getLocation().getY());
                        spawn.set("z", p.getLocation().getZ());
                        spawn.set("world", p.getLocation().getWorld().getName());
                        spawn.set("yaw", p.getLocation().getYaw());
                        spawn.set("pitch", p.getLocation().getPitch());
                        saveConfig();
                       
                        p.sendMessage(ChatColor.YELLOW + "" + ChatColor.GOLD + "Set spawn successfully!");
                    }
                    else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
                   
                    return true;
                }
                if(commandLabel.equalsIgnoreCase("Spawn"))
                {   
                    if(p.hasPermission("spawnplugin.spawn") || p.isOp())
                    {          
                        Location loc = p.getLocation();
                        
                        loc.setX(getConfig().getDouble("spawn.x"));
                        loc.setY(getConfig().getDouble("spawn.y"));
                        loc.setZ(getConfig().getDouble("spawn.z"));
                        loc.setYaw((float) getConfig().getDouble("spawn.yaw"));
                        loc.setPitch((float) getConfig().getDouble("spawn.pitch"));
                        loc.setWorld(Bukkit.getWorld(this.getConfig().getString("spawn.world")));
             
                        p.teleport(loc);           
                        p.sendMessage(ChatColor.GREEN + "Whoosh");
                    }
                    else p.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "You do not have permission for this command!");
                   
                    return true;
                }
    I set a location then teleported the player to the location...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 1, 2015
  9. Offline

    87pen

    @Randomguy if you want me to see it tahg me.
    Code:
          getConfig().set("floria.world", p.getLocation().getWorld().getName());
          getConfig().set("floria.x", Double.valueOf(p.getLocation().getX()));
          getConfig().set("floria.y", Double.valueOf(p.getLocation().getY()));
          getConfig().set("floria.z", Double.valueOf(p.getLocation().getZ()));
          saveConfig();
    and thennnnnnn
    Code:
          World w = Bukkit.getServer().getWorld(getConfig().getString("floria.world"));
          double x = getConfig().getDouble("floria.x");
          double y = getConfig().getDouble("floria.y");
          double z = getConfig().getDouble("floria.z");
          p.teleport(new Location(w, x, y, z));
          p.sendMessage(ChatColor.GREEN + "Welcome to Floria!");
    https://hub.spigotmc.org/javadocs/bukkit/
     
Thread Status:
Not open for further replies.

Share This Page