Solved An internal error occurred

Discussion in 'Plugin Development' started by Fullzer0, Jan 8, 2019.

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

    Fullzer0

    Hello! I'm new to developing plugins, I tried to change my /gmc command so you could do /gmc [another person] and that worked but the problem is when I do /gmc it doesn't set my own gamemode into creative and says "An internal error occurred while attempting to perform this command" . So everytime I want to go into gamemode creative I do /gmc Fullzer0. Or /gamemode 1. Here is the code:

    Code:
        private main plugin;
    
        public GamemodeCommand(main plugin) {
            this.plugin = plugin;
         
            plugin.getCommand("gmc").setExecutor(this);
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
         
            Player player = (Player) sender;
         
            Player target = Bukkit.getPlayerExact(args[0]);
         
            if (!(sender instanceof Player)) {
                sender.sendMessage(utils.chat(plugin.getConfig().getString("PlayerOnlyCommand.PlayerOnly")));
                return true;
            }
            if(args[0].length() == 0) {
                player.setGameMode(GameMode.CREATIVE);
                player.sendMessage(ChatColor.GREEN + "You're now in Creative Mode!");
            } else {
                String tname = target.getDisplayName();
                String pname = player.getDisplayName();
             
                if(!(target == null)) {
                    target.setGameMode(GameMode.CREATIVE);
                    target.sendMessage(ChatColor.GOLD + pname + ChatColor.GREEN + " Put you into Creative Mode!");
                    player.sendMessage(ChatColor.GREEN + "You've put " + ChatColor.GOLD + tname + ChatColor.GREEN + " into Creative Mode!");
                } else {
                    player.sendMessage(ChatColor.RED + "Your target is currently offline!");
            }
        }     
            return false;
        }
     
    }
    I have rewrote this a couple of times.. I've been working on this for a few hours at least in total out of the days I've been trying to work on it... Probably something really obvious and I'm just blind haha

    edit: Thanks so much! I finally got it working with your help!
     
    Last edited: Jan 10, 2019
  2. Offline

    KarimAKL

    @Fullzer0
    1. You are casting 'sender' to a Player before checking if it's a player first.
    2. You are using 'args[0]' before checking the args length is higher than 0 first.
    I would guess the problem is with point 2.

    EDIT: Also, you are checking the length of 'args[0]', not the 'args' array.
     
  3. Offline

    Fullzer0

    so should I do
    if(args.length == 0)
    instead of
    if(args[0].length == 0)?

    edit: I tried it and it didn't work if there is any other way please reply that
    I think people do "edit" idk im new to forums
     
  4. Offline

    KarimAKL

    @Fullzer0 Correct, to check the amount of arguments typed you do if 'args.length', what is the error and your current code?
     
  5. Offline

    Fullzer0

    There is not error in eclipse it self but it still says an internal error occurred when I do /gmc so I still need to do /gmc Fullzer0
     
  6. Offline

    torpkev

    When you get the error, does it give you the stack trace?

    Showing us the full error text would help a lot
     
    KarimAKL likes this.
  7. Offline

    Fullzer0

    Code:
    [07:07:04] [Server thread/INFO]: Fullzer0 issued server command: /gmc
    [07:07:04] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'gmc' in plugin FullzNetworkCustomCodedEssentials v1.0.2
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_191]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_191]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
        at me.Fullzer0.FullzNetworkEssentials.commands.GamemodeCommand.onCommand(GamemodeCommand.java:30) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        ... 15 more
     
    Last edited by a moderator: Jan 9, 2019
  8. Offline

    KarimAKL

    @Fullzer0 And your current code? I would guess you used 'args[0]' before checking the args length.
     
  9. Offline

    Fullzer0

    That command ran out of this code:

    So, no

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
          
            Player player = (Player) sender;
          
            Player target = Bukkit.getPlayerExact(args[0]);
          
            if (!(sender instanceof Player)) {
              
                return true;
            }
            if(args.length == 0) {
                if(sender instanceof Player) {
                player.setGameMode(GameMode.CREATIVE);
                player.sendMessage(ChatColor.GREEN + "You're now in Creative Mode!");
                }
            } else {
                String tname = target.getDisplayName();
                String pname = player.getDisplayName();
              
                if (target instanceof Player) {
                    target.setGameMode(GameMode.CREATIVE);
                    target.sendMessage(ChatColor.GOLD + pname + ChatColor.GREEN + " Put you into Creative Mode!");
                    player.sendMessage(ChatColor.GREEN + "You've put " + ChatColor.GOLD + tname + ChatColor.GREEN + " into Creative Mode!");
                } else {
                    player.sendMessage(ChatColor.RED + "Your target is currently offline!");
            }
        }      
            return false;
    }
    }
     
  10. Online

    timtower Ninja on the waves Moderator

    @Fullzer0
    Player target = Bukkit.getPlayerExact(args[0]); is before if(args.length == 0) {
     
  11. Offline

    Fullzer0

    Then where should it be?

    NVM I fixed it thanks soo much I've been trying to fix this for so long!
     
  12. Online

    timtower Ninja on the waves Moderator

    The check should be before the target variable.
     
  13. Offline

    Fullzer0

    Ok I got it working but if the target is not online it should send a message to the player saying "Your target is currently offline" but it again it just says "An internal error occurred while attempting to perform this command!"
     
  14. Online

    timtower Ninja on the waves Moderator

  15. Offline

    Fullzer0

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
           
            Player player = (Player) sender;
       
           
            if (!(sender instanceof Player)) {
               
                return true;
            }
            if(args.length == 0) {
               
                if(sender instanceof Player) {
                player.setGameMode(GameMode.CREATIVE);
                player.sendMessage(ChatColor.GREEN + "You're now in" + ChatColor.AQUA + " Creative" + ChatColor.GREEN + " Mode!");
                }
            } else {
                Player target = Bukkit.getPlayerExact(args[0]);
                String tname = target.getDisplayName();
                String pname = player.getDisplayName();
               
                if (target instanceof Player) {
                    target.setGameMode(GameMode.CREATIVE);
                    target.sendMessage(ChatColor.GOLD + pname + ChatColor.GREEN + " Put you into" + ChatColor.AQUA +" Creative" + ChatColor.GREEN + " Mode!");
                    player.sendMessage(ChatColor.GREEN + "You've put " + ChatColor.GOLD + tname + ChatColor.GREEN + " into" + ChatColor.AQUA +  " Creative" + ChatColor.GREEN + " Mode!");
                } else {
                    player.sendMessage(ChatColor.RED + "Your target is currently offline!");
            }
        }       
            return false;
    }
     
  16. Online

    timtower Ninja on the waves Moderator

    @Fullzer0 Get the target, then check if it is null, BEFORE you call any method on it.
     
  17. Offline

    Fullzer0

    I did this:
    Code:
                    if (target == null) {
                    player.sendMessage(ChatColor.RED + "Your target is currently offline!");
                    }
    and it didn't work and it made a "dead code" line under it
     
  18. Online

    timtower Ninja on the waves Moderator

    Post some more code please, entire method
     
  19. Offline

    Fullzer0

    Code:
                Player target = Bukkit.getPlayerExact(args[0]);
                String tname = target.getDisplayName();
                String pname = player.getDisplayName();
               
                if (target instanceof Player) {
                    target.setGameMode(GameMode.CREATIVE);
                    target.sendMessage(ChatColor.GOLD + pname + ChatColor.GREEN + " Put you into" + ChatColor.AQUA +" Creative" + ChatColor.GREEN + " Mode!");
                    player.sendMessage(ChatColor.GREEN + "You've put " + ChatColor.GOLD + tname + ChatColor.GREEN + " into" + ChatColor.AQUA +  " Creative" + ChatColor.GREEN + " Mode!");
                }
                    if (target == null) {
                    player.sendMessage(ChatColor.RED + "Your target is currently offline!");
                    }
            }    
     
  20. Offline

    KarimAKL

    @Fullzer0 Check if 'target' is null just after initializing it. (Before doing anything else with target)

    EDIT: Also if you don't do an else statement after checking if 'target' is null then remember to return true.
     
Thread Status:
Not open for further replies.

Share This Page