need help with my gamemode plugin

Discussion in 'Spigot Plugin Development' started by _DutchGamernl_, Apr 26, 2017.

  1. hello.
    i made an gamemode plugin. but now as i do /gm 1 . i see an no permission message for changing other players their gamemode ( that I made) and my gamemode is at the same time changed to creative.
    but how to get rid of that message as i want to change only my gamemode without the gamemode change others permission?
    here's my build:

    Code (Text):
    package main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener {

        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }

        @Override
            public boolean onCommand(CommandSender s, Command cmd, String CommandLabel, String[] args) {
            if (s instanceof Player) {
                Player p = (Player) s;
                if (cmd.getName().equalsIgnoreCase("gamemode")) {
                    if (!p.hasPermission("gamemode.change")) {
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "you don't have permissions!");
                        return true;
                    }
                }
                if (args.length == 0) {
                    s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "use /gm <gamemode> [player]");
                    return true;
                }
                {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("1") || args[0].equalsIgnoreCase("c") || args[0].equalsIgnoreCase("creative") || args[0].equalsIgnoreCase("crea")) {
                            GM.changeGamemode(GM.CREATIVE, p);
                        }else if (args[0].equalsIgnoreCase("2") || args[0].equalsIgnoreCase("a") || args[0].equalsIgnoreCase("adventure")) {
                            GM.changeGamemode(GM.ADVENTURE, p);
                        }else if (args[0].equalsIgnoreCase("3") || args[0].equalsIgnoreCase("spec") || args[0].equalsIgnoreCase("sp") || args[0].equalsIgnoreCase("spectator")) {
                            GM.changeGamemode(GM.SPECTATOR, p);
                        }else if (args[0].equalsIgnoreCase("0") || args[0].equalsIgnoreCase("s") || args[0].equalsIgnoreCase("sur") || args[0].equalsIgnoreCase("survival")) {
                            GM.changeGamemode(GM.SURVIVAL, p);
                        }
                    }
                }
                {
                    if (!p.hasPermission("gamemode.change.others")) {
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "you don't have permissions to change other players gamemode!");
                        return true;
                    }
                    if (args.length == 2) {
                        Player target = Bukkit.getServer().getPlayer(args[1]);
                        if (target == null) {
                            s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "can't find that player!");
                            return true;
                        }
                        if (args[0].equalsIgnoreCase("1") || args[0].equalsIgnoreCase("c") || args[0].equalsIgnoreCase("creative") || args[0].equalsIgnoreCase("crea")) {
                            GM.changeGamemode(GM.CREATIVE, target);
                        }else if (args[0].equalsIgnoreCase("2") || args[0].equalsIgnoreCase("a") || args[0].equalsIgnoreCase("adventure")) {
                            GM.changeGamemode(GM.ADVENTURE, target);
                        }else if (args[0].equalsIgnoreCase("3") || args[0].equalsIgnoreCase("spec") || args[0].equalsIgnoreCase("sp") || args[0].equalsIgnoreCase("spectator")) {
                            GM.changeGamemode(GM.SPECTATOR, target);
                        }else if (args[0].equalsIgnoreCase("0") || args[0].equalsIgnoreCase("s") || args[0].equalsIgnoreCase("sur") || args[0].equalsIgnoreCase("survival")) {
                            GM.changeGamemode(GM.SURVIVAL, target);
                        }
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.AQUA + "you changed the gamemode of: " + ChatColor.RED + target.getDisplayName());
                        target.sendMessage("§a[§bGameMode§a] " + ChatColor.GOLD + "your gamemode is changed by " + ChatColor.RED + s.getName());
                        }
                }
            }
            return false;
        }
    }
    i can't see what i doing wrong.
    why is that message showing up?

    EDIT:
    i think i got it but i test it right now. i think that my plugin.yml was a little bit bugged because i made there an all pemission in and i deleted that now
     
    #1 _DutchGamernl_, Apr 26, 2017
    Last edited: Apr 26, 2017
  2. Not sure what your issue was, but is it fixed now?
     
  3. no. it was not the gamemode.* permission in the plugin.yml . but i get 2 messages: 1 is that my own gamemode is changed and the other one is that i got no permissions to change other players their gamemode.
     
  4. So you want to only see that your gamemode was changed, and not that you lack permissions?
     
  5. yes that's what i want. and as i do /gm 1 [player] then i want that message
     
  6. Your code doesn't make sense... you need to do it step-by-step in logical order.

    Code (Text):
    if (cmd.getName().equalsIgnoreCase("gamemode")) {
                    if (!p.hasPermission("gamemode.change")) {
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "you don't have permissions!");
                        return true;
                    }
                }
                if (args.length == 0) {
                    s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "use /gm <gamemode> [player]");
                    return true;
                }
    That code checks for argument length AFTER you've closed the statement under the "gamemode" command. It should be the other way around.
     
  7. the code does work but i have 2 messages when i do /gm 1. without the player
    Code (Text):
    package main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener {
     
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @Override
            public boolean onCommand(CommandSender s, Command cmd, String CommandLabel, String[] args) {
            if (s instanceof Player) {
                Player p = (Player) s;
                if (cmd.getName().equalsIgnoreCase("gamemode")) {
                    if (!p.hasPermission("gamemode.change.own")) {
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "you don't have permissions!");
                        return true;
                    }
                }
                if (args.length == 0) {
                    s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "use /gm <gamemode> [player]");
                    return true;
                }
                {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("1") || args[0].equalsIgnoreCase("c") || args[0].equalsIgnoreCase("creative") || args[0].equalsIgnoreCase("crea")) {
                            GM.changeGamemode(GM.CREATIVE, p);  
                        }else if (args[0].equalsIgnoreCase("2") || args[0].equalsIgnoreCase("a") || args[0].equalsIgnoreCase("adventure")) {
                            GM.changeGamemode(GM.ADVENTURE, p);
                        }else if (args[0].equalsIgnoreCase("3") || args[0].equalsIgnoreCase("spec") || args[0].equalsIgnoreCase("sp") || args[0].equalsIgnoreCase("spectator")) {
                            GM.changeGamemode(GM.SPECTATOR, p);
                        }else if (args[0].equalsIgnoreCase("0") || args[0].equalsIgnoreCase("s") || args[0].equalsIgnoreCase("sur") || args[0].equalsIgnoreCase("survival")) {
                            GM.changeGamemode(GM.SURVIVAL, p);
                        }  
                    }
                }
                {
                    if (!p.hasPermission("gamemode.change.others")) {
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "you don't have permissions to change other players gamemode!");
                     
                    }
                    if (args.length == 2) {
                        Player target = Bukkit.getServer().getPlayer(args[1]);
                        if (target == null) {
                            s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "can't find that player!");
                            return true;
                        }
                        if (args[0].equalsIgnoreCase("1") || args[0].equalsIgnoreCase("c") || args[0].equalsIgnoreCase("creative") || args[0].equalsIgnoreCase("crea")) {
                            GM.changeGamemode(GM.CREATIVE, target);  
                        }else if (args[0].equalsIgnoreCase("2") || args[0].equalsIgnoreCase("a") || args[0].equalsIgnoreCase("adventure")) {
                            GM.changeGamemode(GM.ADVENTURE, target);
                        }else if (args[0].equalsIgnoreCase("3") || args[0].equalsIgnoreCase("spec") || args[0].equalsIgnoreCase("sp") || args[0].equalsIgnoreCase("spectator")) {
                            GM.changeGamemode(GM.SPECTATOR, target);
                        }else if (args[0].equalsIgnoreCase("0") || args[0].equalsIgnoreCase("s") || args[0].equalsIgnoreCase("sur") || args[0].equalsIgnoreCase("survival")) {
                            GM.changeGamemode(GM.SURVIVAL, target);
                        }
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.AQUA + "you changed the gamemode of: " + ChatColor.RED + target.getDisplayName());
                        target.sendMessage("§a[§bGameMode§a] " + ChatColor.GOLD + "your gamemode is changed by " + ChatColor.RED + s.getName());
                        }
                }
            }
            return false;
        }
    }
    EDIT:
    what do you mean exacly? i am an noob at programming of plugins btw
     
  8. Can you provide us your plugin.yml?
     
  9. plugin.yml:

    Code (Text):
    name: gamemode
    version: 1
    main: main.main

    commands:
       gamemode:
         description: set your gamemode
         aliases: [gm]

    permissions:
       gamemode.change.own:
         description: set the gamemode of yourself
         default: false
       gamemode.change.others:
         description: set other players gamemode
         default: false
    build:

    Code (Text):
    package main;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class main extends JavaPlugin implements Listener {
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
       
        @Override
            public boolean onCommand(CommandSender s, Command cmd, String CommandLabel, String[] args) {
            if (s instanceof Player) {
                Player p = (Player) s;
                if (cmd.getName().equalsIgnoreCase("gamemode") || cmd.getName().equalsIgnoreCase("gm")) {
                    if (!p.hasPermission("gamemode.change.own")) {
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "you don't have permissions!");
                        return true;
                    }
                }
                if (args.length == 0) {
                    s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "use /gm <0,1,2,3 >[player]");
                    return true;
                }
                {
                    if (args.length == 1) {
                        if (args[0].equalsIgnoreCase("1") || args[0].equalsIgnoreCase("c") || args[0].equalsIgnoreCase("creative") || args[0].equalsIgnoreCase("crea")) {
                            GM.changeGamemode(GM.CREATIVE, p);  
                        }else if (args[0].equalsIgnoreCase("2") || args[0].equalsIgnoreCase("a") || args[0].equalsIgnoreCase("adventure")) {
                            GM.changeGamemode(GM.ADVENTURE, p);
                        }else if (args[0].equalsIgnoreCase("3") || args[0].equalsIgnoreCase("spec") || args[0].equalsIgnoreCase("sp") || args[0].equalsIgnoreCase("spectator")) {
                            GM.changeGamemode(GM.SPECTATOR, p);
                        }else if (args[0].equalsIgnoreCase("0") || args[0].equalsIgnoreCase("s") || args[0].equalsIgnoreCase("sur") || args[0].equalsIgnoreCase("survival")) {
                            GM.changeGamemode(GM.SURVIVAL, p);
                        }  
                    }
                    if (!p.hasPermission("gamemode.change.others")) {
                        s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "you don't have permissions!");
                        return true;
                    }
                }
                if (args.length == 2) {
                    Player target = Bukkit.getServer().getPlayer(args[1]);
                       if (target == null) {
                           s.sendMessage("§a[§bGameMode§a] " + ChatColor.RED + "can't find that player!");
                           return true;
                       }
                       if (args[0].equalsIgnoreCase("1") || args[0].equalsIgnoreCase("c")) {
                           GM.changeGamemode(GM.CREATIVE, target);  
                       }else if (args[0].equalsIgnoreCase("2") || args[0].equalsIgnoreCase("a")) {
                           GM.changeGamemode(GM.ADVENTURE, target);
                       }else if (args[0].equalsIgnoreCase("3") || args[0].equalsIgnoreCase("spec") || args[0].equalsIgnoreCase("sp")) {
                           GM.changeGamemode(GM.SPECTATOR, target);
                       }else if (args[0].equalsIgnoreCase("0") || args[0].equalsIgnoreCase("s")) {
                           GM.changeGamemode(GM.SURVIVAL, target);
                       }
                       s.sendMessage("§a[§bGameMode§a] " + ChatColor.AQUA + "you changed the gamemode of: " + ChatColor.RED + target.getDisplayName());
                       target.sendMessage("§a[§bGameMode§a] " + ChatColor.GOLD + "your gamemode is changed by " + ChatColor.RED + s.getName());
                }
            }
            return false;
        }
    }
     
  10. 1. You dont have any events, no need to implement Listener and registering the class(unless you have a listener inside the class which you didnt upload)
    2. Clean up your code, You have made it weak and messy.


    Look up some tutorials on how to setup your plugin.
     
  11. I can't find good tutorials....
     
  12. To clean up you're code in eclipse IDE use ctrl+shift+f
     
  13. i found an solution on another forum. it's has to be under the args.length == 2 line the second permission