I need hel for my Plugin (Permissons)

Discussion in 'Spigot Plugin Development' started by EnglischLehrer, Jul 7, 2018.

  1. Here i want to have a permisson please help i don`t know how i do it:
    Code (Text):

    BanCommands:
    package de.ss.banmanager.commands;

    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import de.ss.banmanager.main.Main;
    import de.ss.banmanager.util.BanManager;
    import de.ss.banmanager.util.BanUnit;

    public class BanCommands implements CommandExecutor {
     
        private Main plugin;
     
        public BanCommands(Main plugin) {
            this.plugin = plugin;
        }
        [USER=32110]@Override[/USER]
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if(cmd.getName().equalsIgnoreCase("ban")) {
                if(args.length >= 2) {
                    String playername = args[0];
                    if(BanManager.isBanned(getUUID(playername))) {
                        sender.sendMessage(plugin.prefix + "§cDieser Spieler ist bereits gebannt!");
                        return true;
                    }
                    String reason = "";
                    for(int i = 1; i < args.length; i++) {
                        reason += args[I] + " ";
                    }
                    BanManager.ban(getUUID(playername), playername, reason, -1);
                    sender.sendMessage(plugin.prefix + "§7Du hast §e" + playername + " §4PERMANENT §7gebannt!");
                    return true;
                }
                sender.sendMessage(plugin.prefix + "§c/ban <Spieler> <Grund>");
                return true;
            }
           
            if(cmd.getName().equalsIgnoreCase("tempban")) {
                if(args.length >= 4) {
                    String playername = args[0];
                    if(BanManager.isBanned(getUUID(playername))) {
                        sender.sendMessage(plugin.prefix + "§cDieser Spieler ist bereits gebannt!");
                        return true;
                    }
                    long value;
                    try {
                        value = Integer.valueOf(args[1]);
                    } catch(NumberFormatException e) {
                        sender.sendMessage(plugin.prefix + "§c<Zahlenwert> muss eine Zahl sein!");
                        return true;
                    }
                    if(value >= 500) {
                        sender.sendMessage(plugin.prefix + "§cDie Zahl muss unter 500 liegen!");
                        return true;
                    }
                    String unitString = args[2];
                    String reason = "";
                    for(int i = 3; i < args.length; i++) {
                        reason += args[I] + " ";
                    }
                    List<String> unitList = BanUnit.getUnitsAsString();
                    if(unitList.contains(unitString.toLowerCase())) {
                        BanUnit unit = BanUnit.getUnit(unitString);
                        long seconds = value * unit.getToSecond();
                        BanManager.ban(getUUID(playername), playername, reason, seconds);
                        sender.sendMessage(plugin.prefix + "§7Du hast §e" + playername + "§7für §c" + value + unit.getName() + "§7gebannt!");
                        return true;
                    }
                    sender.sendMessage(plugin.prefix + "§cDiese <Einheit> existiert nicht!");
                    return true;
                }
                sender.sendMessage(plugin.prefix + "§c/tempban <Spieler> <Zahlenwert> <Einheit> <Grund>");
                return true;
            }
           
            if(cmd.getName().equalsIgnoreCase("check")) {
                if(args.length == 1) {
                    if(args[0].equalsIgnoreCase("list")) {
                        List<String> list = BanManager.getBannedPlayers();
                        if(list.size() == 0) {
                            sender.sendMessage(plugin.prefix + "§cEs sind aktuell keine Spieler gebannt!");
                            return true;
                        }
                        sender.sendMessage(plugin.prefix + "§7---------- §6§lBan-Liste §7----------");
                        for(String allBanned : BanManager.getBannedPlayers()) {
                            sender.sendMessage(plugin.prefix + "§e" + allBanned + " §7(Grund: §r" + BanManager.getReason(getUUID(allBanned)) + "§7)");
                        }
                        return true;
                        }
                        String playername = args[0];
                        sender.sendMessage(plugin.prefix + "§7---------- §6§lBan-Infos §7----------");
                        sender.sendMessage(plugin.prefix + "§eName: §r" + playername);
                        sender.sendMessage(plugin.prefix + "§eGebannt: §r" + (BanManager.isBanned(getUUID(playername)) ? "§aJa!" : "§cNein!"));
                        if(BanManager.isBanned(getUUID(playername))) {
                           sender.sendMessage(plugin.prefix + "§eGrund: §r" + BanManager.getReason(getUUID(playername)));
                           sender.sendMessage(plugin.prefix + "§eVerbleibende Zeit: §r" + BanManager.getRemainingTime(getUUID(playername)));
                        }
                        return true;
                }
                sender.sendMessage(plugin.prefix + "§c/check (list) <Spieler>");
                return true;
            }
           
            if(cmd.getName().equalsIgnoreCase("unban")) {
                if(args.length == 1) {
                    String playername = args[0];
                    if(!BanManager.isBanned(getUUID(playername))) {
                        sender.sendMessage(plugin.prefix + "§cDieser Spieler ist nicht gebannt!");
                        return true;
                    }
                    BanManager.unban(getUUID(playername));
                    sender.sendMessage(plugin.prefix + "§7Du hast §e" + playername + " §7entbannt!");
                    return true;
                }
                sender.sendMessage(plugin.prefix + "§c/unban <Spieler>");
                return true;
            }
            return true;  
        }
       
        @SuppressWarnings("deprecation")
        private String getUUID(String playername) {
            return Bukkit.getOfflinePlayer(playername).getUniqueId().toString();
        }
    }[/I][/I]
     
    #1 EnglischLehrer, Jul 7, 2018
    Last edited: Jul 7, 2018
  2. Sorry for my bad english i am from Germenay :(
     
  3. Please place your code in codeblocks, like this:
    Code (Java):
    foo();
     
  4. Simple add an if statement like this one.
    Code (Java):
    if( Player.hasPermission("yourplugin.command.something")) {
        // You have permission!
    }
    I usually return false when the users does not have the permission/ This so that the code doesn't get too nested, but that's my own preference.
     
    #4 MartenM, Jul 7, 2018
    Last edited: Jul 7, 2018
    • Agree Agree x 1
  5. So?
    Code:
    package de.ss.banmanager.commands;

    import java.util.List;

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

    import de.ss.banmanager.main.Main;
    import de.ss.banmanager.util.BanManager;
    import de.ss.banmanager.util.BanUnit;

    public class BanCommands implements CommandExecutor {

    private Main plugin;

    public BanCommands(Main plugin) {
    this.plugin = plugin;
    }
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if(cmd.getName().equalsIgnoreCase("ban")) {
    if(Player.hasPermission("banmanager.ban")) {
    if(args.length >= 2) {
    String playername = args[0];
    if(BanManager.isBanned(getUUID(playername))) {
    sender.sendMessage(plugin.prefix + "§cDieser Spieler ist bereits gebannt!");
    return true;
    }
    String reason = "";
    for(int i = 1; i < args.length; i++) {
    reason += args + " ";
    }
    BanManager.ban(getUUID(playername), playername, reason, -1);
    sender.sendMessage(plugin.prefix + "§7Du hast §e" + playername + " §4PERMANENT §7gebannt!");
    return true;
    }
    sender.sendMessage(plugin.prefix + "§c/ban <Spieler> <Grund>");
    return true;
    }

    if(cmd.getName().equalsIgnoreCase("tempban")) {
    if(args.length >= 4) {
    String playername = args[0];
    if(BanManager.isBanned(getUUID(playername))) {
    sender.sendMessage(plugin.prefix + "§cDieser Spieler ist bereits gebannt!");
    return true;
    }
    long value;
    try {
    value = Integer.valueOf(args[1]);
    } catch(NumberFormatException e) {
    sender.sendMessage(plugin.prefix + "§c<Zahlenwert> muss eine Zahl sein!");
    return true;
    }
    if(value >= 500) {
    sender.sendMessage(plugin.prefix + "§cDie Zahl muss unter 500 liegen!");
    return true;
    }
    String unitString = args[2];
    String reason = "";
    for(int i = 3; i < args.length; i++) {
    reason += args + " ";
    }
    List<String> unitList = BanUnit.getUnitsAsString();
    if(unitList.contains(unitString.toLowerCase())) {
    BanUnit unit = BanUnit.getUnit(unitString);
    long seconds = value * unit.getToSecond();
    BanManager.ban(getUUID(playername), playername, reason, seconds);
    sender.sendMessage(plugin.prefix + "§7Du hast §e" + playername + "§7für §c" + value + unit.getName() + "§7gebannt!");
    return true;
    }
    sender.sendMessage(plugin.prefix + "§cDiese <Einheit> existiert nicht!");
    return true;
    }
    sender.sendMessage(plugin.prefix + "§c/tempban <Spieler> <Zahlenwert> <Einheit> <Grund>");
    return true;
    }

    if(cmd.getName().equalsIgnoreCase("check")) {
    if(args.length == 1) {
    if(args[0].equalsIgnoreCase("list")) {
    List<String> list = BanManager.getBannedPlayers();
    if(list.size() == 0) {
    sender.sendMessage(plugin.prefix + "§cEs sind aktuell keine Spieler gebannt!");
    return true;
    }
    sender.sendMessage(plugin.prefix + "§7---------- §6§lBan-Liste §7----------");
    for(String allBanned : BanManager.getBannedPlayers()) {
    sender.sendMessage(plugin.prefix + "§e" + allBanned + " §7(Grund: §r" + BanManager.getReason(getUUID(allBanned)) + "§7)");
    }
    return true;
    }
    String playername = args[0];
    sender.sendMessage(plugin.prefix + "§7---------- §6§lBan-Infos §7----------");
    sender.sendMessage(plugin.prefix + "§eName: §r" + playername);
    sender.sendMessage(plugin.prefix + "§eGebannt: §r" + (BanManager.isBanned(getUUID(playername)) ? "§aJa!" : "§cNein!"));
    if(BanManager.isBanned(getUUID(playername))) {
    sender.sendMessage(plugin.prefix + "§eGrund: §r" + BanManager.getReason(getUUID(playername)));
    sender.sendMessage(plugin.prefix + "§eVerbleibende Zeit: §r" + BanManager.getRemainingTime(getUUID(playername)));
    }
    return true;
    }
    sender.sendMessage(plugin.prefix + "§c/check (list) <Spieler>");
    return true;
    }

    if(cmd.getName().equalsIgnoreCase("unban")) {
    if(args.length == 1) {
    String playername = args[0];
    if(!BanManager.isBanned(getUUID(playername))) {
    sender.sendMessage(plugin.prefix + "§cDieser Spieler ist nicht gebannt!");
    return true;
    }
    BanManager.unban(getUUID(playername));
    sender.sendMessage(plugin.prefix + "§7Du hast §e" + playername + " §7entbannt!");
    return true;
    }
    sender.sendMessage(plugin.prefix + "§c/unban <Spieler>");
    return true;
    }
    return true;
    }

    @SuppressWarnings("deprecation")
    private String getUUID(String playername) {
    return Bukkit.getOfflinePlayer(playername).getUniqueId().toString();
    }
    }
     
  6. Can you put in my code pls?The Permisson?
     
  7. Sorry, I basically already gave you the correct code. I am not going to spoonfeed you by putting it into your code, you should be able to that your self.
     
  8. You need to register the permission in your plugin.yml first
    Code (Text):
    main: whatever.package.here.Main
    ...
    commands:
      ...
    permissions:
      fake.permission:
        default: op #other values are false and true, which give it to everyone or no one by default
    It doesn't do anything itself just being in the config, so you have to put that snippet of code that MartenM posted around the beginning of your code, so that cancelling for the reason of not having a permission comes first.
     
  9. Actually you don't need to register it in the plugin.yml

    I don't usually myself. Putting it in the plugin.yml just allows you to add descriptions and easy returns messages when you don't have it and it also allows quick setup of .* permissions.
     
  10. Yeah, I was thinking of noting that it doesn't have to be registered there, but it seems fairly convenient for most devs.
     
  11. Hmmmmmm, I don't understand man: you know how to use a Dependency Injection and how objects work, but you don't know where to put an if statement. Hmmmmmmmm, maybe you copied that code for somewhere?
     

    Attached Files:

    • ed2.png
      ed2.png
      File size:
      180.2 KB
      Views:
      58
    • Funny Funny x 3
  12. Alex my net beans is broken that is the old text and that is eclipse i must wait!
     
  13. JanTuck

    Supporter

    But your name says you are an english teacher.
     
    • Winner Winner x 3
  14. Lol Im not taking about your IDE, I'm saying that the code you showed us isn't yours