Development Assistance Command not working

Discussion in 'Plugin Help/Development/Requests' started by SwingDude, Dec 13, 2014.

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

    SwingDude

    Hi,
    I am making a plugin that handles chat channels (and don't just say "x plugin already does that". I know there are plugins that do this. I just wanted to do it to learn HashMaps and onCommand()). However, it will only show the usage when I use the command (which implies that yes, I have registered it in plugin.yml). I put it in an event when it was less developed, and it indicated that I had an NPE. I have changed my code since then, though. I still believe that my problem is an NPE. I believe it is a problem with the getConfig() statements, as it isn't printing the channelAmount int when programmed to.
    Code:
    package com.gmail.nlspector.chatchannels;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class ChatChannel extends JavaPlugin implements Listener {
    
        int i = 1;
        int channelAmount;
        String channelNames;
        @Override
        public void onEnable(){
            getLogger().info("Cool! You are using ChatChannels");
            getServer().getPluginManager().registerEvents(this, this);
            saveDefaultConfig();
            getConfig();
            channelAmount = getConfig().getInt("amount");
        }
       
        public Map<String, String> playerChannel = new HashMap<>();
       
        public boolean onCommand(Command cmd, CommandSender sender, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("channel")){
                try {
                if(sender instanceof Player){
                    if(args.length != 0){   
                        String selectedChannel = args[0];
                        for(i = 1; i < (channelAmount + 1); i = i + 1){
                            String thisLoopChannel = this.getConfig().getString("channels." + Integer.toString(i));
                            if(selectedChannel.equals(thisLoopChannel)){
                                String switchedPlayer = sender.getName();
                                playerChannel.put(switchedPlayer, selectedChannel);
                                sender.sendMessage("You have been switched to the" + selectedChannel + "channel");
                                return true;
                            } else if(i == channelAmount){
                                sender.sendMessage("That isn't a channel!");
                            }
                        }
                    } else {
                        sender.sendMessage("Too little arguments!");
                    }
                }
                } catch (Exception e){
                    getLogger().severe(e.getMessage());
                }
            }        
            return false;
        }
       
    }
    
     
  2. Offline

    HeadGam3z

    Is there a stack-trace?
     
  3. Offline

    SwingDude

    There is no stack-trace, which is why I posted this here.
    @HeadGam3z

    Found the problem. Apparently I messed up the onCommand:
    Code:
    public void onCommand(Command cmd, CommandSender sender, String label, String[] args){
    //some code here
    }
    should have been:
    Code:
    @Override
    public void onCommand(CommandSender sender, Command cmd, String label, String[] args){
    //some code here
    }
    *facepalm*

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
Thread Status:
Not open for further replies.

Share This Page