Solved Weird command glitch.

Discussion in 'Plugin Help/Development/Requests' started by HighOnSwiftness, Aug 4, 2015.

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

    HighOnSwiftness

    So, I made a quick op and deop command, with neither working.. just need some help.

    Code:
    [04:23:02 INFO]: _eGL issued server command: /deop
    [04:23:02 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'deop
    ' in plugin ExEssentials v0.1
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-00eba53]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:14
    0) ~[craftbukkit.jar:git-Bukkit-00eba53]
            at org.bukkit.craftbukkit.v1_8_R1.CraftServer.dispatchCommand(CraftServe
    r.java:624) ~[craftbukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:1058) [craftbukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.PlayerConnection.a(PlayerConnection.java
    :919) [craftbukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:37) [craft
    bukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.PacketPlayInChat.a(SourceFile:9) [craftb
    ukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.PacketHandleTask.run(SourceFile:13) [cra
    ftbukkit.jar:git-Bukkit-00eba53]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [
    ?:1.8.0_51]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_51]
            at net.minecraft.server.v1_8_R1.MinecraftServer.z(MinecraftServer.java:6
    43) [craftbukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.DedicatedServer.z(DedicatedServer.java:2
    84) [craftbukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.MinecraftServer.y(MinecraftServer.java:5
    98) [craftbukkit.jar:git-Bukkit-00eba53]
            at net.minecraft.server.v1_8_R1.MinecraftServer.run(MinecraftServer.java
    :506) [craftbukkit.jar:git-Bukkit-00eba53]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_51]
    Caused by: java.lang.NullPointerException
            at me.essentials.commands.DeOpCommand.onCommand(DeOpCommand.java:24) ~[?
    :?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-00eba53]
            ... 14 more
    That's the error, here's my deopcommand class.


    Code:
    package me.essentials.commands;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    import me.essentials.Main;
    
    public class DeOpCommand implements CommandExecutor {
       
        public static Main plugin;
    
        public boolean onCommand(CommandSender snd, Command cmd, String lbl, String[] args){
            if(cmd.getName().equalsIgnoreCase("deop")){
    
                if(snd instanceof Player){
                    Player p = (Player) snd;
                   
                   
                    if(args.length == 0){
    
                        p.sendMessage(plugin.wrongargs);
    
                    }else if(args.length == 1){
    
                        if(p.hasPermission("exessentials.op")){
                            @SuppressWarnings("deprecation")
                            Player target = (Player) plugin.getServer().getOfflinePlayer(args[0]);
                           
                            if(target == null){
                               
                                p.sendMessage(ChatColor.DARK_RED + "Player does not exist!");
                               
                            }else{
                               
                                p.sendMessage(ChatColor.GRAY + "DeOpped " + ChatColor.GREEN + target.getName() + ChatColor.GRAY + ".");
                               
                                target.sendMessage(ChatColor.GRAY + "You've been deopped.");
                                target.setOp(false);
                               
                            }
                           
                        }else{
                           
                            p.sendMessage(plugin.nopermission);
                           
                        }
                       
                    }else{
    
    
    
                    }
    
                }else{
    
    
    
                }
    
            }else{
               
                snd.sendMessage(plugin.playersonly);
               
            }
    
            return false;
        }
       
    }
    
    And, here's my main class.

    Code:
    package me.essentials;
    
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import me.essentials.commands.DeOpCommand;
    import me.essentials.commands.OpCommand;
    
    public class Main extends JavaPlugin implements Listener {
       
        public static Main plugin;
       
        public void onEnable(){
            Logger log = Logger.getLogger("Minecraft");
            log.info("Essentials: Enabled, hello!");
            getCommand("op").setExecutor(new OpCommand());
            getCommand("deop").setExecutor(new DeOpCommand());
        }
       
        public void onDisable(){
            Logger log = Logger.getLogger("Minecraft");
            log.info("Essentials: Disabled, goodbye!");
           
        }
       
        public String wrongargs = ChatColor.DARK_RED + "Wrong use of arguments!";
        public String nopermission = ChatColor.DARK_RED + "You do not have permission for this command!";
        public String playersonly = ChatColor.DARK_RED + "Only players may use this command!";
       
    }
    
    Thank you! :)
     
  2. Offline

    megamichiel

    @HighOnSwiftness
    You never initialize the "plugin" field in your CommandExecutor, so it throws an NPE
     
  3. Offline

    bwfcwalshy Retired Staff

    Moved to Bukkit Alternates

    That code...
    Ok, so before you even continue making commands please please fix the bad practices.
    1. Why all the empty else's?
    2. plugin is null because you never initialized it, pass the instance through the constructor.
    3. Don't use java's logger use Bukkit's
    4. Why is plugin static in the main class?
    5. No need for enable and disable messages. Bukkit does it for you.
    6. Your onDisable will now be empty, you can remove it.
    7. Why implement Listener and never use it?
     
  4. Offline

    HighOnSwiftness

    Thanks everyone! I'm so tired and I completely forgot to do this.. thank you!
     
Thread Status:
Not open for further replies.

Share This Page