Solved [Help] Errors

Discussion in 'Spigot Plugin Development' started by SwagiWagi, Aug 17, 2016.

  1. Hello everyone!
    I have made this code:
    Code (Text):
    package com.rom.bounce;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Core extends JavaPlugin implements Listener {

        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }

        @Override
        public void onDisable() {
        }

    @EventHandler
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if(label.equalsIgnoreCase("sethome")){
            if(!(sender instanceof Player))
                return true;
            if(args.length == 1){
                Player p = (Player) sender;
                Location l = p.getLocation();
                p.sendMessage(ChatColor.GREEN + "Home has been set.");
                if(label.equalsIgnoreCase("Home")){
                    p.sendMessage(ChatColor.GREEN + "Teleported to home!");
                    p.teleport(l);
                }
            }
        }
        return false;
    }
    }
     
     
  2. I don't see anything wrong with your code, but in console right clicking pastes copied text. Just copy something that is not several lines long and it will stop
     
    • Useful Useful x 1
  3. And when I /Home It does nothing.
     
  4. Could you please view the question again? I have changed it.
     
  5. I don't see the question here. What is the problem?
    Also use cmd.getName() instead of label.getName()
     
  6. Haha.
    @TheGamerPlayz really? Don't see the @ EventHandler, that he registers the class when it's a command, that he uses a command inside of a command?

    Edit:
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Core extends JavaPlugin {

    @Override
    public void onEnable() {
    }

    @Override
    public void onDisable() {
    }

    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    Player p = (Player) sender;
    Location l = null;

    if (label.equalsIgnoreCase("sethome")){
    if (!(sender instanceof Player))
    return true;

    l = p.getLocation();

    p.sendMessage(ChatColor.GREEN + "Home has been set.");
    }

    if (label.equalsIgnoreCase("home")){
    if (!(sender instanceof Player))
    return true;

    if (l == null) {
    p.sendMessage("Home could not be found, set it with /sethome");
    } else {
    p.sendMessage(ChatColor.GREEN + "Teleported to home!");
    p.teleport(l);
    }
    }
    return false;
    }

    }
     
    #6 Krille, Aug 17, 2016
    Last edited: Aug 17, 2016
  7. Yeah, remove that @ EventHandler, the registerEvents(...) method in onEnable() and the listener implementation in your class. You don't need it for commands! Go see this guide, it can be helpful
     
  8. Oh wow yea you are right XD. Remove the EventHandler, implement CommandExecutor, and register the command
     
  9. Not only that, he made a command inside of a command...

    he did
    if (label.blablabla("command 1")) {
    code
    if (label.blablabla("command 2")) {
    code

    }
    }
     
  10. If you put onCommand(..) method in a class that extends JavaPlugin and is registered in plugin.yml (main) you don't need to implements CommandExecutor and register the commands, too.
     
    • Useful Useful x 1
  11. "Home could not be found, set it with /sethome."
     
  12. Set your home then, do /sethome and can I see your code please
     
  13. I already did, I changed the code a bit.
    Code (Text):
    package com.rom.bounce;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Core extends JavaPlugin {

        @Override
        public void onEnable() {
        }

        @Override
        public void onDisable() {
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            Player p = (Player) sender;
            Location l = null;

            if (label.equalsIgnoreCase("sethome")) {
                if (!(sender instanceof Player))
                    return true;
                    l = p.getLocation();
                    p.sendMessage(ChatColor.GREEN + "Home has been set.");
                }

            if (label.equalsIgnoreCase("home")) {
                if (!(sender instanceof Player))
                    return true;

                if (l == null) {
                    p.sendMessage("Home could not be found, set it with /sethome");
                } else {
                    p.sendMessage(ChatColor.GREEN + "Teleported to home!");
                    p.teleport(l);
                }
            }
            return false;
        }

    }
     
  14. Put Location l = null; outside of the onCommand thing and if that doesn't work copy the code I pasted in the spoiler "spoonfeed".
     
    • Useful Useful x 1
  15. Lemme make this ez: learn java.
    yes, I'm that twat. Just do it.
     
    • Like Like x 1
  16. Already did, what was the problem with my Java?
     
  17. Code (Java):
    if (label.equalsIgnoreCase("sethome"))
    {
        if (label.equalsIgnoreCase("home"))
        {
        }
    }
    This is just the same as doing this:
    Code (Java):
    if (label.equalsIgnoreCase("sethome"))
    {
        if (false)
        {
            // lots of functionality
        }
    }
    And anyone who knew java would know that your Location object 'l' would always be null when doing /home.
     
    • Winner Winner x 1
  18. Thank you! Worked!
     
  19. ?
     
  20. Okay, even simpler.
    Your code == this:
    Code (Java):
    if (true) { // WILL GET EXECUTED
         if (false) { setHomeThingie();  } // WON'T EVER GET EXECUTED
    }