Solved Spawn plugin

Discussion in 'Plugin Development' started by rob1998@, May 10, 2012.

Thread Status:
Not open for further replies.
  1. I'm trying to create a Spawn plugin but I'm pretty new to java so I hava a few questions.
    1. what do I have to set in plugin.yml
    2. do I have to make a config?
    3. I get an error from eclipse on this line
    Code:
        public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args ){


    this is my main class:
    Code:
    package com.gmail.grob1998.teleport;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class teleport extends JavaPlugin implements Listener
    {
        public Logger log;
       
        public void onEnable()
        {
            log = getLogger();
           
            getServer().getPluginManager().registerEvents(this, this);
        }
        public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args ){
            if(cmd.getName().equalsIgnoreCase("setSpawn")){
     
    Player p = (Player)sender;
                double x = p.getLocation().getX();
                double y = p.getLocation().getY();
                double z = p.getLocation().getZ();
                String w = p.getWorld().getName();
                getConfig().set("Spawn.world", w);
                getConfig().set("Spawn.x", x);
                getConfig().set("Spawn.y", y);
                getConfig().set("Spawn.z", z);
                saveConfig();
            p.sendMessage("You have saved the Spawn co-ordinates");
           
            if(cmd.getName().equalsIgnoreCase("Spawn")){
    String world = getConfig().getString("Spawn.world");
                        String xcoor = getConfig().getString("Spawn.xcoor");
                        String ycoor = getConfig().getString("Spawn.ycoor");
                        String zcoor = getConfig().getString("Spawn.zcoor");
                        double xd = Double.parseDouble(xcoor);
                        double yd = Double.parseDouble(ycoor);
                        double zd = Double.parseDouble(zcoor);
                        Location Spawn = new Location(Bukkit.getWorld(world),xd,yd,zd);
                        p.teleport(Spawn);
            }
            }
        }
    }
     
  2. Offline

    the_merciless

    Check out these vids. the first few will explain about using configs and plugin.yml



    I think the error may have something to do with the word "final" try removing it

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  3. I have already tried but the error stays
     
  4. Offline

    the_merciless

    what does the error say
     
  5. This is the error
    Code:
    This method must return a result of type boolean
     
  6. Offline

    Komak57

    return false for command-not-found, return true when your command is found, or if you handle everything manually, send the player messages manually and return true.
     
  7. but wich do I have to chose?
     
  8. Offline

    the_merciless

    ah ok you have the }'s in the wrong places you need to close off the first command and also add return true, and return false, at the end

    Code:
    package com.gmail.grob1998.teleport;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class teleport extends JavaPlugin implements Listener
    {
        public Logger log;
     
        public void onEnable()
        {
            log = getLogger();
         
            getServer().getPluginManager().registerEvents(this, this);
        }
        public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args ){
            if(cmd.getName().equalsIgnoreCase("setSpawn")){
     
    Player p = (Player)sender;
                double x = p.getLocation().getX();
                double y = p.getLocation().getY();
                double z = p.getLocation().getZ();
                String w = p.getWorld().getName();
                getConfig().set("Spawn.world", w);
                getConfig().set("Spawn.x", x);
                getConfig().set("Spawn.y", y);
                getConfig().set("Spawn.z", z);
                saveConfig();
            p.sendMessage("You have saved the Spawn co-ordinates");
            return true,
          }
     
            if(cmd.getName().equalsIgnoreCase("Spawn")){
    String world = getConfig().getString("Spawn.world");
                        String xcoor = getConfig().getString("Spawn.xcoor");
                        String ycoor = getConfig().getString("Spawn.ycoor");
                        String zcoor = getConfig().getString("Spawn.zcoor");
                        double xd = Double.parseDouble(xcoor);
                        double yd = Double.parseDouble(ycoor);
                        double zd = Double.parseDouble(zcoor);
                        Location Spawn = new Location(Bukkit.getWorld(world),xd,yd,zd);
                        p.teleport(Spawn);
            return true,
    }return false,
        }
    }
    Something like that
     
  9. I don't have errors anymore, but do I have to make a config or does the plugins that by itselfs?
    and I think I have to add the command to the plugin.yml, or do I have it wrong?
     
  10. Offline

    the_merciless

    Right click on your project name (In the package explorer on the left) go to new then file then name it config.yml , you can leave that empty. do the same for plugin.yml but u need to enter the relevant info in this, this should help you with the plugin.yml
    http://wiki.bukkit.org/Plugin_YAML

    Search "bukkit plugin tutorial" on youtube there are lots of great videos that will explain all this for you. Also you will find alot of what u need to know here, http://wiki.bukkit.org/Plugin_Tutorial

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  11. Ok thanks I'm gonna try

    New problem /setSpawn works but /Spawn not:

    error code:
    Code:
    2012-05-10 20:56:39 [SEVERE] null
    org.bukkit.command.CommandException: Unhandled exception executing command 'spawn' in plugin teleport v1.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:42)
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:166)
        at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:473)
        at net.minecraft.server.NetServerHandler.handleCommand(NetServerHandler.java:821)
        at net.minecraft.server.NetServerHandler.chat(NetServerHandler.java:781)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:764)
        at net.minecraft.server.Packet3Chat.handle(Packet3Chat.java:34)
        at net.minecraft.server.NetworkManager.b(NetworkManager.java:229)
        at net.minecraft.server.NetServerHandler.a(NetServerHandler.java:113)
        at net.minecraft.server.NetworkListenThread.a(NetworkListenThread.java:78)
        at net.minecraft.server.MinecraftServer.w(MinecraftServer.java:551)
        at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:449)
        at net.minecraft.server.ThreadServerApplication.run(SourceFile:492)
    Caused by: java.lang.NullPointerException
        at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:989)
        at java.lang.Double.parseDouble(Double.java:510)
        at com.gmail.grob1998.teleport.teleport.onCommand(teleport.java:46)
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:40)
        ... 12 more
    plugin.yml:
    Code:
    name: teleport
    main: com.gmail.grob1998.teleport.teleport
    version: 1.0
    authors:
      - rob1998
    commands:
      setSpawn:
        description: setSpawn
        usage: /setSpawn
      Spawn:
        description: Spawn
        usage: /Spawn
    config.yml:
    Code:
    
    
    teleport.java:
    Code:
    package com.gmail.grob1998.teleport;
     
    import java.util.logging.Logger;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class teleport extends JavaPlugin implements Listener
    {
        public Logger log;
     
        public void onEnable()
        {
            log = getLogger();
       
            getServer().getPluginManager().registerEvents(this, this);
        }
        public boolean onCommand(final CommandSender sender, Command cmd, String commandLabel, String[] args ){
            if(cmd.getName().equalsIgnoreCase("setSpawn")){
     
    Player p = (Player)sender;
                double x = p.getLocation().getX();
                double y = p.getLocation().getY();
                double z = p.getLocation().getZ();
                String w = p.getWorld().getName();
                getConfig().set("Spawn.world", w);
                getConfig().set("Spawn.x", x);
                getConfig().set("Spawn.y", y);
                getConfig().set("Spawn.z", z);
                saveConfig();
            p.sendMessage("You have saved the Spawn co-ordinates");
            return true;
          }
     
            if(cmd.getName().equalsIgnoreCase("Spawn")){
                Player p = (Player)sender;
    String world = getConfig().getString("Spawn.world");
                        String xcoor = getConfig().getString("Spawn.xcoor");
                        String ycoor = getConfig().getString("Spawn.ycoor");
                        String zcoor = getConfig().getString("Spawn.zcoor");
                        double xd = Double.parseDouble(xcoor);
                        double yd = Double.parseDouble(ycoor);
                        double zd = Double.parseDouble(zcoor);
                        Location Spawn = new Location(Bukkit.getWorld(world),xd,yd,zd);
                        p.teleport(Spawn);
            return true;
    }return false;
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  12. Why do you get the string of the location and then parse it? Why don't you just use getDouble(<path>)?
     
  13. that solves my problem not
     
  14. http://forums.bukkit.org/threads/ho...ubleshoot-your-own-plugins-by-yourself.32457/ ;)

    //EDIT. And for this time: Your config is empty, so
    String xcoor = getConfig().getString("Spawn.xcoor");
    will return null, that's why
    double xd = Double.parseDouble(xcoor);
    gives you a NPE (you can't parse null to a double).
    Simple fix: Change
    String xcoor = getConfig().getString("Spawn.xcoor", "0");
    where 0 is the default... or even better:
    double xd = getConfig().getDouble("Spawn.xcoor", 0.0D);
     
  15. Offline

    Theodossis

    I got it 1 sec

    Take that from my plugin.It got permissions and you can change them if you want :)
    Code:
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            Player player = (Player) sender;
            if (!(sender instanceof Player)) {
                sender.sendMessage(ChatColor.RED + "This command has to be called by other player!");
                return true;
            }
            if (commandLabel.equalsIgnoreCase("setspawn")) {
                if (!player.hasPermission("spawn.setspawn")){
                    player.sendMessage(ChatColor.RED + "You don't have a permission to do that.");
                    return true;
                }
                Location spawn = player.getLocation();
                player.getWorld().setSpawnLocation(spawn.getBlockX(), spawn.getBlockY(), spawn.getBlockZ());
                player.sendMessage(ChatColor.GREEN + "Spawn set");
            } else if (commandLabel.equalsIgnoreCase("spawn")) {
                if (!player.hasPermission("spawnz.spawn")){
                    player.sendMessage(ChatColor.RED + "You don't have a permission to do that.");
                    return true;
                }
                Location spawn = player.getWorld().getSpawnLocation();
                spawn.setX(spawn.getBlockX() + 0.8);
                spawn.setY(spawn.getBlockY());
                spawn.setZ(spawn.getBlockZ() + 0.8);
                player.teleport(spawn);
            } else
                return false; 
            return true;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  16. Does this code require a config?
     
  17. Offline

    xGhOsTkiLLeRx

    Nope. It overrides the world spawn.
    Or teleports the player to the world spawn.
     
  18. Offline

    the_merciless

    The reason my code didnt work for you is because i forgot to change some of the spawn command code. When you save the coords you are saving them in the config as "spawn.x" , "spawn.y" etc

    getConfig().set("Spawn.world", w);
    getConfig().set("Spawn.x", x);
    getConfig().set("Spawn.y", y);

    and when u use the spawn command it looks for "spawn.xcoor" , "spawn.ycoor" etc

    String xcoor = getConfig().getString("Spawn.xcoor");
    String ycoor = getConfig().getString("Spawn.ycoor");
    String zcoor = getConfig().getString("Spawn.zcoor");

    All u have to do is remove the "coor" form everything to match what u saved it as. Sorry for late response u probably got it working by now.
     
  19. Offline

    Shockwave317

    have u tried return:false;
    because u add that in the onCommands brackets
     
Thread Status:
Not open for further replies.

Share This Page