commands not working

Discussion in 'Spigot Plugin Development' started by Mhalllz, Jun 2, 2018.

  1. anyone know whats wrong with this code? when I do /apply nothing happens. Yes I registered it in plugin.yml.

    Code (Java):
    package me.MC_Hoxen.staffapplication;

    import mkremins.fanciful.FancyMessage;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    public class Commands extends Main {

        @SuppressWarnings("unused")
        @EventHandler
        public void playerChat(AsyncPlayerChatEvent event) {
            Player player1 = (Player) event.getPlayer();
            final String message = event.getMessage();
        }

        public boolean onCommand(CommandSender theSender, Command cmd,
                String commandLabel, String[] args) {

                Player player = (Player) theSender;

                if (cmd.getName().equalsIgnoreCase("apply")) {

                    player.sendMessage(ChatColor
                            .translateAlternateColorCodes(
                                    '&',
                                    getConfig().getString("Prefix")
                                            + "&eWelcome the the first Step to becoming staff on"
                                            + getConfig().getString("Server-Name")));

                    player.sendMessage(ChatColor
                            .translateAlternateColorCodes(
                                    '&',
                                    "&6You will be asked a series of questions and if your anwsers meet the requirements you will be taken to Step 2"));

                    new FancyMessage("Click here to continue! ").color(
                            ChatColor.GOLD).command("ap2");
                }

                if (cmd.getName().equalsIgnoreCase("ap2")) {

                    int age = Integer.parseInt("message");

                    player.sendMessage("How old are you?");
                    if (age >= 13) {

                        player.sendMessage(ChatColor
                                .translateAlternateColorCodes('&',
                                        "&6Have you played minecraft for over 1 year?"));

                    }
                } else {

                    player.sendMessage(ChatColor.translateAlternateColorCodes('&',
                            "&cSorry, you do not meet the minimum requirements."));

                    return true;

                }

                return true;
        }

    }
     
  2. Use 'commandLabel' instead and why does your class extend 'Main'?
     
    • Agree Agree x 1
  3. this is my Main class

    Code (Text):
    package me.MC_Hoxen.staffapplication;

    import java.util.logging.Logger;

    import org.bukkit.Bukkit;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener{

          Logger myPluginLogger = getLogger();
       
        public void onEnable(){
           
            this.myPluginLogger.info("Staff Application has been successfully enabled!");
            getConfig().options().copyDefaults(true);
            saveConfig();
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
           
        }
       
        public void onDisable() {
           
            this.myPluginLogger.info("Staff Application has been successfully disabled!");
            saveConfig();
           
        }
       
    }
     
     
  4. Show ur config ;)
     
  5. not much there

    Code (Text):
    Prefix: ''
    Server-Name: ''
     
  6. Try to add @Override over your onCommand method.
     
  7. Why do u extends your Main in the Commands class?
    Try to implement CommandExecutor in your Commands class and add getCommand("apply").setExecutor(new Commands()); at onEnabled.
     
  8. nope still nothing, anyone want me to post my project on here so they can try n fix it?
     
  9. Where did you learn this from? this is not how "multi class commands" works...

    - remove the extended main
    - make ur main class instance
    - Implement CommandExecuter in the command class
    - register the command in main onEnable
     
    #10 Ikeetjeop, Jun 2, 2018
    Last edited: Jun 3, 2018
    • Like Like x 1
  10. heres the project
     

    Attached Files:

  11. how does it work?
     
  12. cant just put a random onCommand method anywhere you want. needs to be in a commandexecutor (or whatever the object's name is) instance. and then you have to register it if its not in your javaplugin object
     
  13. DO NOT Make a second instance of a JavaPlugin... Remove extends Main and add implements CommandExecutor then register The Command in the Main class. If you extend Main it will create a second instance of a JavaPlugin wich will lead to bukkit thinking you're trying to make two plugins in one jar
     
    • Funny Funny x 1
  14. ok i did that but how do i define getConfig() in the commands class
     
  15. ok i found out how to get the config but /apply still doesnt work
     
  16. 1. You are trying to implement a command executor and a Listener in one class. But instead extended your Main.

    2. The listener isn't needed. What you want to do instead is to use the Bukkit Conversation API. Here is some links on Bukkit Conversation api
    * https://docs.google.com/document/d/...gTq71ti44AI09OsNM/edit#heading=h.chpxzhljk9wo
    * https://bukkit.org/threads/anyone-know-anything-about-bukkits-conversation-api.87455/
    * https://github.com/BrianMiltonJr/rp-package/tree/master/Rp/src/com/johnwillikers/rp(If you would like to seed a live implementation of conversations. Im not gonna spoonfeed you the code but you can read it)

    3. The only time you can use an if statement without {} would be something like this. And I'm sure it is frowned upon.

    Code (Text):
    if(player.exists())
        Plugin.management.log(player)
    Keep in mind that code would do nothing just reference.

    If we pass our if statement it will run the online under it only. This meaning only one method call. This format of an If Statement isn't intended for multiple method calls, variable adjustments, etc.

    I would recommend learning what the implements and extends modifiers actually mean here: https://stackoverflow.com/questions/10839131/implements-vs-extends-when-to-use-whats-the-difference

    And then I would recommend checking out what an Interface is here: https://docs.oracle.com/javase/tutorial/java/concepts/interface.html
     
  17. All of this was already resolved...
    Read the other Replies before replying
     
  18. Yeah I did and gave him some good links to read on Java in general. Before he gets further into Spigot Plugin development he should understand what Interfaces are when to use extends and implements.
     
  19. He should also learn how constructor works, the are a lots of guides made by spigot's team.
     
    • Winner Winner x 1