Solved Command Not Working

Discussion in 'Plugin Development' started by GRocksMc, Feb 25, 2017.

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

    GRocksMc

    I'm making a plugin that allows you to change someone's name in the tab list using /tabname <playername> <new tabname>

    Code:
    package com.GRocks.ApplicationPlugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class TabName extends JavaPlugin {
    
        @Override
        public void onEnable(){
    
        }
    
        @Override
        public void onDisable(){
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                if(cmd.getName().equalsIgnoreCase("tabname")){
                    if(sender instanceof Player){
                        Player targetplayer = Bukkit.getPlayerExact(args[0]);
                        targetplayer.setPlayerListName(args[1]);
                        sender.sendMessage(ChatColor.GRAY + targetplayer.getName() + "'s name is now" + targetplayer.getDisplayName() + ".");
                        return true;
                    
                    }
                }
                return false;
        }
        }
                    
    I just don't know where the problem is. Doing /tabname | does nothing.
     
  2. Offline

    mine-care

    No need to overwirte those if you aren't using them!

    Well a couple of things here...
    • What if the arguments provided are less than 2 for instance? if i do /tabname without arguments? Then this code will error... Better check that the ammount of arguments is correct before anything else.

    • getPlayerExact may return null if the string provided is not a valid player, therefore you must do a null check to avoid errors.

    • As for 'not doing anything' is an error thrown? Is any message sent back to you ingame? Have you debuged the code?
     
  3. Offline

    GRocksMc

    Changed code to
    Code:
    package com.GRocks.ApplicationPlugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.md_5.bungee.api.ChatColor;
    
    public class TabName extends JavaPlugin {
    
        public void onEnable(){
    
        }
    
        public void onDisable(){
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
                if(cmd.getLabel().equalsIgnoreCase("tabname")){
                    if(args.length == 0){
                        sender.sendMessage(ChatColor.RED + "Syntax: <Player_Name> <New_TabName>");
                    }
                    else if(args.length == 1){
                        sender.sendMessage(ChatColor.RED + "Syntax: <Player_Name> <New_TabName>");
                    }
                    else if(args.length > 1){
                        Player targetplayer = Bukkit.getPlayerExact(args[0]);
                        targetplayer.setPlayerListName(args[1]);
                        sender.sendMessage(ChatColor.GRAY + targetplayer.getName() + "'s name is now" + targetplayer.getDisplayName() + ".");
                        return true;
                    }
                }
                return false;
        }
        }
                    
    Is there something else I could do without having to do a null check? If not, how would I do it. The syntax message isn't even showing up, so I don't think the problem is where we think it is.

    And by 'not doing anything' I mean that nothing happens. Not even an error or help message.

    EDIT: I figured out what the problem is. I have 2 classes with their own commands, but the other one was set as the main class in the plugin.yml . I set my TabName class as the main one and now it works. How do I get them both to work?
     
    Last edited: Feb 25, 2017
  4. Offline

    Zombie_Striker

    @GRocksMc
    Register the commands.

    Also, you want cmd.getName(), not label. Label does not support aliases. And you never nullcheck target player. If the player is not online/does not exists, this will throw an NPE. Nullcheck before getting the player.
     
  5. Offline

    GRocksMc

    Okay, I did what you said @Zombie_Striker . Everything is working fine. How do I save the tabnames of the players in a .yml file and then load them when a player joins?
     
  6. Offline

    Zombie_Striker

    @GRocksMc
    1. If you have done anything with configs, you should know how to save Strings. These strings will be the names of all the players that the player should see
    2. When a player joins, load the Yaml. Grab the strings from the config, and clear all the players that are not in config. If it does not matter if the player is online or if they are offline, for loop through all the strings. If the player with that name is not online, add it to the tablist.
     
  7. Offline

    GRocksMc

    @Zombie_Striker

    I have made it now so when you set a tabname, it saves it in the config.yml. How do I get it to load the name when the player get online and set the tabname?
     
  8. Offline

    Zombie_Striker

    @GRocksMc
    1. PlayerJoinEvent
    2. Get the name from the config, using the path you provided when you saved the names.
    3. You then can use my Tablist Util to edit the tablist.
     
  9. Offline

    GRocksMc

    @Zombie_Striker

    I can't seem to figure out how to get the tabname that is saved, back from the config and use it.
     
    Last edited: Feb 25, 2017
  10. Offline

    Zombie_Striker

    @GRocksMc
    Post your code. It is essentially taking the same line you used for saving the tabname, but changing "set" to "get"
     
  11. Offline

    GRocksMc

    @Zombie_Striker

    Code:
    @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            String savedTabName = getConfig().getString("TabNames.Players." + player.getName());
            player.setPlayerListName(savedTabName);
        }
        }
    config.yml:
    Code:
    TabNames:
      Players:
        GRocksMc: Fred
    
    and why does this message have to wait for moderator approval. It just makes the process longer.
     
    Last edited: Feb 25, 2017
  12. Offline

    Zombie_Striker

    @GRocksMc
    Exactly. It takes longer for you so that we don't get bots, and that all members follow the rules.

    That is it. Are there any problems with your current code?
     
  13. Offline

    GRocksMc

    When someone with an assigned tabname join the server, it doesn't assign their tabname and they just have their default name.
     
  14. Offline

    mine-care

  15. Offline

    Zombie_Striker

    @GRocksMc
    Have you debugged your code? Does the event ever get called? Is savedname null/ what does it equal?
     
  16. Offline

    GRocksMc

    @Zombie_Striker
    I have never debugged and don't know how.

    String savedTabName = getConfig().getString("TabNames.Players." + player.getName());

    Thank for reminding me @mine-care . Would you happen to know anything on the subject to help?
     
  17. Offline

    Zombie_Striker

    @GRocksMc
    For deubgging, all you're trying to do is verify that something works the way it should. In this case, you want to make sure that the event gets called. You can easily do this by just adding a line to send a message in the event. If you see the message, that means the event does get called. If not, you most likely forgot to register the class.

    Also, is savedTabName null? Null check it.
     
  18. Offline

    GRocksMc

    @Zombie_Striker
    How should I null check, and what do I have it do if it is null? What do I do if it shows up null?

    Besides null checking, how do I register the TabNameEvents class in my Main class (it's actually called main.) Right now, this is what I have in my Main class.
    Code:
    package com.GRocks.ApplicationPlugin;
    
    import org.apache.logging.log4j.core.config.plugins.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
    
    
        private static Plugin plugin;
    
        @Override
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
            plugin = (Plugin) this;
                pm.registerEvents(new TabNameEvents(), this);
                getCommand("apply").setExecutor(new Apply());
                getCommand("tabname").setExecutor(new TabName());
          
        }
    
        public static Plugin getPlugin() {
            return plugin;
        }
    }
    
    I can't figure out how to register the TabNameEvents in the main class. This isn't working.
    Code:
    package com.GRocks.ApplicationPlugin;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class TabNameEvents extends JavaPlugin implements Listener {
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            //event.getPlayer().sendMessage("hey");
            Player player = event.getPlayer();
            String savedTabName = getConfig().getString("TabNames.Players." + player.getName());
            player.setPlayerListName(savedTabName);
        }
    }
    
    The console keep telling me that something is wrong with line 9. Can I not extends JavaPlugin and implements Listener? Okay, I know I can't so then how do I use the stuff from JavaPlugin such as getConfig() and setConfig()?
     
    Last edited: Feb 25, 2017
  19. Offline

    Zombie_Striker

    @GRocksMc
    You can only have one class that extends JavaPlugin. Make sure only Main extends JavaPlugin.

    To use things from the main class Do not use static and instead pass the instance of the main class through the other class' s constructor.
     
  20. Offline

    GRocksMc

    @Zombie_Striker
    I found a thread that shows me what to do in this situation,
    Main.java Class:
    Code:
    public class Main extends JavaPlugin {
        @Override
        public void onEnable(){
            PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(new TabNameEvents(), this);
                getCommand("apply").setExecutor(new Apply());
                getCommand("tabname").setExecutor(new TabName(null));
        }
    }
    
    
    TabName.java Class:
    Code:
    public class TabName implements CommandExecutor{
    Plugin test = Bukkit.getPluginManager().getPlugin("GamerVerse");
        Main plugin;
    
        public TabName(Main instance) {
        plugin = instance;
        }
        public void onEnable(){
            getConfig().options().copyDefaults(true);
            saveConfig();
        }
        }
    Eclipse is still showing errors "The method getConfig() is undefined for the type TabName" and "The method saveConfig() is undefined for the type TabName." How do I registerEvents in the Main class, I can't seem to figure that out. Because if you look, it says null, what do I do there? Also, when I do Bukkit.getPluginManager().getPlugin("gamerverse"): what is the "GamervVerse" part supposed to be.

    [EDIT]
    I figured out that I just had to use "test" in front of getConfig() and saveConfig(). Now, I have another problem, how do I make it so if the targetplayer isn't online that it would send the sender a message.
     
    Last edited: Feb 26, 2017
  21. Offline

    Zombie_Striker

    @GRocksMc
    What do you mean "targetplayer" and "sender"?
     
  22. Offline

    GRocksMc

    That's what I called them in my code
    Code:
    package com.GRocks.ApplicationPlugin;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import net.md_5.bungee.api.ChatColor;
    
    import org.bukkit.plugin.Plugin;
    
    public class TabName implements CommandExecutor{
    Plugin main = Bukkit.getPluginManager().getPlugin("GamerVerse");
        Main plugin;
      
        public TabName(Main instance) {
        plugin = instance;
        }
        public void onEnable(){
            main.getConfig().options().copyDefaults(true);
            main.saveConfig();
        }
    
        public void onDisable(){
       
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
           
            if(sender.hasPermission("gamerverse.command.tabname")){
                if(cmd.getName().equalsIgnoreCase("tabname")){
                    if(args.length == 0){
                        sender.sendMessage(ChatColor.RED + "Syntax: /tabname <Player_Name> <New_TabName>");
                    }
                    else if(args.length == 1){
                        sender.sendMessage(ChatColor.RED + "Syntax: /tabname <Player_Name> <New_TabName>");
                    }
                    else if(args.length > 1){
                        String coloredTabName = ChatColor.translateAlternateColorCodes('&', args[1]);
                        String savedTabName = args[1];
                        Player targetplayer = Bukkit.getPlayerExact(args[0]);
                        targetplayer.setPlayerListName(coloredTabName);
                        sender.sendMessage(targetplayer.getName() + ChatColor.GRAY + "'s tabname is now " + ChatColor.RESET + targetplayer.getPlayerListName() + ".");
                        targetplayer.sendMessage("Your tabname is now " + ChatColor.RESET + targetplayer.getPlayerListName() + ".");
                        main.getConfig().set("TabNames.Players." + targetplayer.getName(), savedTabName);
                        main.saveConfig();
                        return true;
                       
                    }
                }
            }
                return false;
        }
    }
     
  23. Offline

    Zombie_Striker

    @GRocksMc
    If targetplayer is not online, targetplayer will be null. Null check targetplayer before you use it.
     
    mine-care likes this.
  24. Offline

    GRocksMc

  25. Offline

    Zombie_Striker

    @GRocksMc
    If your problem has been solved, mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page