Solved Problem trying to change another player's gamemode

Discussion in 'Plugin Development' started by Warpaka, Mar 21, 2021.

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

    Warpaka

    I'm trying to make a plugin that when you type /gmc <player> it changes the gamemode of the player targetted. But when I do it there are a few bugs. For example, when someone is in adventure mode then I change it to creative and then try to change their gamemode back to adventure, it tells me they are in adventure. New to bukkit btw.

    Here's my code :
    Code:
    package 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 org.bukkit.event.Listener;
    
    import net.minecraft.server.v1_8_R3.CommandExecute;
    
    public class Gamemodes extends CommandExecute implements Listener, CommandExecutor {
    
        public String prefix = (ChatColor.BLUE + "Stone" + ChatColor.GRAY + "brick" + ChatColor.GOLD + ">>");
    
        public String cmd1 = "gmc";
        public String cmd2 = "gms";
        public String cmd3 = "gma";
        public String cmd4 = "gmsp";
    
        @Override
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
    
                Player player = (Player) sender;
                if (sender.hasPermission("change.gamemode")) {
                    if (cmd.getName().equalsIgnoreCase(cmd1)) {
                        if (args.length != 0) {
                            if (((Player) player).getGameMode() != GameMode.CREATIVE) {
                            Player target = (Player) Bukkit.getPlayer(args[0]);
                            ((Player) target).setGameMode(GameMode.CREATIVE);
                            player.sendMessage(prefix + ChatColor.GREEN + " Changed " + ChatColor.WHITE + target.getName() + ChatColor.GREEN +  "'s gamemode to creative");
                            return true;
                        }else {
                            player.sendMessage(prefix + ChatColor.RED + " This player is already in creative mode");
                            return true;
                        }
                        } else {
                            if (((Player) player).getGameMode() != GameMode.CREATIVE) {
                                ((Player) player).setGameMode(GameMode.CREATIVE);
                                player.sendMessage(prefix + ChatColor.GREEN + " Changed your gamemode to creative");
                                return true;
                            } else {
                                player.sendMessage(prefix + ChatColor.RED + " You already are in creative mode");
                                return true;
                            }
    
                        }
                    }
    
                    if (cmd.getName().equalsIgnoreCase(cmd2)) {
                        if (args.length != 0) {
                            if (((Player) player).getGameMode() != GameMode.SURVIVAL) {
                            Player target = (Player) Bukkit.getPlayer(args[0]);
                            ((Player) target).setGameMode(GameMode.SURVIVAL);
                            player.sendMessage(prefix + ChatColor.GREEN + " Changed " + ChatColor.WHITE + target.getName() + ChatColor.GREEN +  "'s gamemode to survival");
                            return true;
                        }else {
                            player.sendMessage(prefix + ChatColor.RED + " This player is already in survival mode");
                            return true;
                        }
                        } else {
                            if (((Player) player).getGameMode() != GameMode.SURVIVAL) {
                                ((Player) player).setGameMode(GameMode.SURVIVAL);
                                player.sendMessage(prefix + ChatColor.GREEN + " Changed your gamemode to survival");
                                return true;
                            } else {
                                player.sendMessage(prefix + ChatColor.RED + " You already are in survival mode");
                                return true;
                            }
    
                        }
                    }
    
                    if (cmd.getName().equalsIgnoreCase(cmd3)) {
                        if (args.length != 0) {
                            if (((Player) player).getGameMode() != GameMode.ADVENTURE) {
                            Player target = (Player) Bukkit.getPlayer(args[0]);
                            ((Player) target).setGameMode(GameMode.ADVENTURE);
                            player.sendMessage(prefix + ChatColor.GREEN + " Changed " + ChatColor.WHITE + target.getName() + ChatColor.GREEN +  "'s gamemode to adventure");
                            return true;
                        }else {
                            player.sendMessage(prefix + ChatColor.RED + " This player is already in adventure mode");
                            return true;
                        }
                        } else {
                            if (((Player) player).getGameMode() != GameMode.ADVENTURE) {
                                ((Player) player).setGameMode(GameMode.ADVENTURE);
                                player.sendMessage(prefix + ChatColor.GREEN + " Changed your gamemode to adventure");
                                return true;
                            } else {
                                player.sendMessage(prefix + ChatColor.RED + " You already are in adventure mode");
                                return true;
                            }
    
                        }
                    }
                    if(cmd.getName().equalsIgnoreCase(cmd4)) {
                        if(args.length != 0) {
                            Player target = (Player) Bukkit.getPlayer(args[0]);
                            if(target.getGameMode() != GameMode.SPECTATOR) {
                                target.setGameMode(GameMode.SPECTATOR);
                                player.sendMessage(prefix + ChatColor.GREEN + " Changed " + target.getName() + " to spectator mode");
                                return true;
                            }else {
                                player.sendMessage(prefix + ChatColor.RED + " This player is already in spectator mode");
                                return true;
                            }
                        }else {
                            if(player.getGameMode() != GameMode.SPECTATOR) {
                                player.sendMessage(prefix + ChatColor.GREEN + " Change your gamemode to spectator");
                                player.setGameMode(GameMode.SPECTATOR);
                                return true;
                            }else {
                                player.sendMessage(prefix + ChatColor.RED + " You already are in spectator mode");
                                return true;
                            }
                        }
                    }
                } else {
                    sender.sendMessage(prefix + ChatColor.RED + " You do not have the permission to perform this command");
                    return true;
                }
    
            } else {
                sender.sendMessage("You are not a player");
                return true;
            }
            return false;
        }
    
    }
     
    Last edited by a moderator: Mar 22, 2021
  2. Offline

    KarimAKL

    @Warpaka A few things:
    • Your class extends CommandExecute and implements Listener, you can remove both of those. You just need to implement CommandExecutor for command registration.
    • You are unnecessarily casting Player to Player several times.
    • You can use guard clauses to reduce the amount of nested if and else statements.
    • You should indent your code once when entering a scope and remove an indent when exiting a scope. This is for readability.
    • You should check for possible null values (e.g. The returned value for #getPlayer(String) might be null).
    • Try debugging (print messages to the console), that might give you an explanation as to why your method is not working as intended.
     
    Warpaka and Strahan like this.
  3. Offline

    Warpaka

    Thanks ! Fixed (Too many returns and null)
     
  4. Offline

    Strahan

    That's cool, I wasn't aware there was a term for that :)
     
    KarimAKL likes this.
Thread Status:
Not open for further replies.

Share This Page