My gamemode command does not work

Discussion in 'Spigot Plugin Help' started by commandROD, Mar 2, 2020.

  1. I created a /gm command, which makes gamemode a lot easier with custom messages.
    I created the gamemode.java file, did everything correctly (I think) and it just doesn't work!
    Can someone help me?
    Here is the code:

    Edit: I forgot to mention, the command register but I get "An internal error occurred while attempting to perform this command" when I execute the command.

    Code (Java):
    package me.commandrod.krovivalcore.commands;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import me.commandrod.krovivalcore.utils.Utils;

    public class Gamemode implements CommandExecutor {

        @SuppressWarnings("null")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player p = (Player) sender;
            Player t = Bukkit.getPlayer(args[0]);
            if (cmd.getName().equalsIgnoreCase("gm")) {
            if (sender instanceof Player) {
                if (p.hasPermission("core.gamemode")) {
                if (args.length == 0) {
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.RED + "Wrong usage! Missing arguments. /gm <number/letter> [player]");
                } else if (args.length >= 3){
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.RED + "Wrong usage! Too many arguments. /gm <number/letter> [player]");
                } else {
                // Creative
                if (args[0].equals("1")) {
                    p.setGameMode(GameMode.CREATIVE);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                if (args[0].equals("c")) {
                    p.setGameMode(GameMode.CREATIVE);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                // Survival
                if (args[0].equals("0")) {
                    p.setGameMode(GameMode.SURVIVAL);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                if (args[0].equals("s")) {
                    p.setGameMode(GameMode.SURVIVAL);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                // Adventure
                if (args[0].equals("a")) {
                    p.setGameMode(GameMode.ADVENTURE);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                if (args[0].equals("2")) {
                    p.setGameMode(GameMode.ADVENTURE);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                // Spec
                if (args[0].equals("spec")) {
                    p.setGameMode(GameMode.SPECTATOR);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                if (args[0].equals("3")) {
                    p.setGameMode(GameMode.SPECTATOR);
                    p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                    }
                }
            }
                if (sender instanceof Player) {
                if (p.hasPermission("core.gamemode.others")) {
                    if (args.length == 2) {
                        if (t == null) {
                            sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.RED + "The player " + ChatColor.DARK_AQUA + t.getName() + ChatColor.RED + " is not online.");
                        } else {
                            if (args[0].equals("1")) {
                                t.setGameMode(GameMode.CREATIVE);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Changed " + ChatColor.GREEN + t.getName() + ChatColor.DARK_AQUA + "gamemode to " + ChatColor.GREEN + t.getGameMode());
                                }
                            if (args[0].equals("c")) {
                                t.setGameMode(GameMode.CREATIVE);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Changed " + ChatColor.GREEN + t.getName() + ChatColor.DARK_AQUA + "gamemode to " + ChatColor.GREEN + t.getGameMode());
                                }
                            // Survival
                            if (args[0].equals("0")) {
                                t.setGameMode(GameMode.SURVIVAL);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Changed " + ChatColor.GREEN + t.getName() + ChatColor.DARK_AQUA + "gamemode to " + ChatColor.GREEN + t.getGameMode());
                                }
                            if (args[0].equals("s")) {
                                t.setGameMode(GameMode.SURVIVAL);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Changed " + ChatColor.GREEN + t.getName() + ChatColor.DARK_AQUA + "gamemode to " + ChatColor.GREEN + t.getGameMode());
                                }
                            // Adventure
                            if (args[0].equals("a")) {
                                t.setGameMode(GameMode.ADVENTURE);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                }
                            if (args[0].equals("2")) {
                                t.setGameMode(GameMode.ADVENTURE);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Changed " + ChatColor.GREEN + t.getName() + ChatColor.DARK_AQUA + "gamemode to " + ChatColor.GREEN + t.getGameMode());
                                }
                            // Spec
                            if (args[0].equals("spec")) {
                                t.setGameMode(GameMode.SPECTATOR);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Changed " + ChatColor.GREEN + t.getName() + ChatColor.DARK_AQUA + "gamemode to " + ChatColor.GREEN + t.getGameMode());
                                }
                            if (args[0].equals("3")) {
                                t.setGameMode(GameMode.SPECTATOR);
                                t.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                                sender.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Changed " + ChatColor.GREEN + t.getName() + ChatColor.DARK_AQUA + "gamemode to " + ChatColor.GREEN + t.getGameMode());
                                    }
                                }
                            }
                        }
                    } else {
                 
                    }
                } else {
                System.out.println("You need to be a player to perform this command!");
                }
            }
            return true;
     
        }
    }
     
  2. Another problem:
    If I do /gm 1 [player] (with numbers [1]) for example, it doesn't work.
    But if I use /gm c [player] it doesn't work.
    Also it doesn't work on other players except for me.
     
  3. If you got an internal error message, your console would have spit out a stack trace, can we see that as well?

    Also, you can make your code a lot easier to read and deal with by reducing all those nested if statements, for example

    Code (Java):
    instead of...

    if (sender instance of Player) {

      if... {

      }

    }

    ..try

    if (!(sender instance of Player)) {

    //sender.sendMessage(must be a player blah blah)
    return true;

    }

    ... code
    EDIT: you don't need to check if the command is "gm", as the entire method will only run when "gm" is executed as specified in your onEnable and in your plugin.yml when registering your command
     
    #3 Wick__, Mar 2, 2020
    Last edited: Mar 2, 2020
  4. Sure
    https://prnt.sc/ra7u1s
     
  5. your running a command for a player from console?

    https://prnt.sc/ra7u1s

    you made sure that it was a player sender by using if (sender instanceof Player) { so then you cant run the command from console

    remove that line to be able to run the command from console

    thats why your getting the error parsing console command
     
  6. That is not the problem I ran the command from a player.
     
  7. Well the error in the above stack trace is because you ran the command from the console, can you provide the stack trace of when you try as a player ?
     
  8. https://prnt.sc/ra8hde
     
  9. Code (Java):
    Player t = Bukkit.getPlayer(args[0]);
    This will throw an error every time you do /gm without any arguments, you shouldn't blindly declare a variable using an element in a list without checking first. So I'd pop that line after your length check of at least 1.

    EDIT: sorry, same goes for casting to player, you cant tell your IDE "hey the sender is a player" without checking if its actually a player, this will throw an error everytime its ran from the console
     
  10. It worked, but I still have the problem with the Targeted player.
     
  11. What problem?

    Also, after you set their game mode and send them a message, you should return true right after, as the following code should not run, and could potentially cause problems.

    Additional tip! Whenever you copy-paste code, there's a solid chance that you're not taking the best approach.

    Code (Java):
    //your approach

                        if (args[0].equals("0")) {
                            p.setGameMode(GameMode.SURVIVAL);
                            p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                        }
                        if (args[0].equals("s")) {
                            p.setGameMode(GameMode.SURVIVAL);
                            p.sendMessage(Utils.chat("&7[&3&lCore&7] ") + ChatColor.DARK_AQUA + "Your gamemode is now " + ChatColor.GREEN + p.getGameMode() + ".");
                        }

    //slightly better approach

                if (args[0].equalsIgnoreCase("s") || args[0].equalsIgnoreCase("0")) {

                    player.setGameMode(GameMode.SURVIVAL);
                    player.sendMessage(Utils.chat("&7[&3&lCore&7] &bYour gamemode is now  &a") + p.getGameMode() + ".");
                    return true;

                }
     
  12. dude if ur problem has been fixed could u change the prefix to solved
     
  13. I know but I didn’t test it lol
    I can’t right now i will test it later