Solved UUID ban plugin

Discussion in 'Plugin Development' started by TheFl4me, Feb 24, 2015.

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

    TheFl4me

    I am currently working on a ban plugin wich is supposed to be UUID compatible but i have a issue:

    EDIT: Ignore the javautil on #119 that was just me trying something out.

    Whenever i ban someone the plugin will create a file with several strings:

    UUID: (the banned players UUID)
    banned: (true/false)
    banned by: (the name of the player the issued the cmd)
    reason: (all args after /ban player)

    all of this works fine exept getting the players UUID. its always showing me: !!java.util.UUID {}
    How do i fix this?

    Another issue i have is if args[2] equals "cheating" it will show a certain msg when the player gets kicked (this works)
    but else if args[2] does not equal "cheating" then it will get all the args after /ban player and display them in the kick/ban msg (this is not working)

    Here are all the classes including the listener

    Ban.class:

    Code:
    package BanManager;
    
    import Main.Main;
    
    import java.io.File;
    import java.io.IOException;
    
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.entity.Player;
    
    public class Ban implements CommandExecutor {
      
        String message = "";
      
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String arg2, String[] args) {
          
            if(sender instanceof ConsoleCommandSender) {
                ConsoleCommandSender p = (ConsoleCommandSender) sender;
                if(cmd.getName().equalsIgnoreCase("ban")) {
                    if(args.length >= 2) {
                        if(args[1].equalsIgnoreCase("cheating")) {
                            try {
                                for(int i = 1; i < args.length; i++){
                                    message = message + args[i] + " ";
                                }
                                Player z = p.getServer().getPlayerExact(args[0]);
                                File file = new File("plugins//KitPvP//banned//"+z.getName()+".yml");
                                if(!file.exists()) {
                                    file.createNewFile();
                                }
                                YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                                cfg.set("UUID", z.getUniqueId());
                                cfg.set("banned", "true");
                                cfg.set("banned by", p.getName());
                                cfg.set("reason", message);
                                message = "";
                                cfg.save(file);
                              
                                Bukkit.getServer().getOnlinePlayers();
                                if(z.getName().equals(args[0])) {
                                    if(z.hasPermission("kitpvp.ban.bypass")) {
                                        p.sendMessage("§cYou cannot ban this player");
                                    }
                                    else {
                                        z.kickPlayer("You have been banned for cheating" + "\n \n§7UUID: "  + z.getUniqueId());
                                        Bukkit.broadcastMessage("§7" + z.getName() + " has been banned");
                                        message = "";
                                    }
                                }                          
                            }
                            catch (IOException e) {
                                p.sendMessage("§4Directory was not found");
                            }
                        }
                        else {
                            try {
                                for(int i = 1; i < args.length; i++){
                                    message = message + args[i] + " ";
                                }
                                Player z = p.getServer().getPlayerExact(args[0]);
                                File file = new File("plugins//KitPvP//banned//"+z.getName()+".yml");
                                if(!file.exists()) {
                                    file.createNewFile();
                                }
                                YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                                cfg.set("UUID", z.getUniqueId());
                                cfg.set("banned", "true");
                                cfg.set("banned by", p.getName());
                                cfg.set("reason", message);
                                message = "";
                                cfg.save(file);
                              
                                Bukkit.getServer().getOnlinePlayers();
                                if(z.getName().equals(args[0])) {
                                    if(z.hasPermission("kitpvp.ban.bypass")) {
                                        p.sendMessage("§cYou cannot ban this player");
                                    }
                                    else {
                                        z.kickPlayer("You have been banned for " + message + "\n \n§7UUID: "  + z.getUniqueId());
                                        Bukkit.broadcastMessage("§7" + z.getName() + " has been banned");
                                        message = "";
                                    }
                                }
                            }
                            catch (IOException e) {
                                p.sendMessage("§4Directory was not found");
                            }
                        }
                    }
                    else {
                        p.sendMessage("§cWrong syntax! Usage: /ban <player> <reason>");
                    }
                }
                return true;
            }
            else {
                Player p = (Player) sender;
                if(cmd.getName().equalsIgnoreCase("ban")) {
                    if(p.hasPermission("kitpvp.ban")) {
                        if(args.length >= 2) {
                            if(args[2].equalsIgnoreCase("cheating")) {
                                try {
                                    for(int i = 1; i < args.length; i++){
                                        message = message + args[i] + " ";
                                    }
                                    Player z = p.getServer().getPlayerExact(args[0]);
                                    File file = new File("plugins//KitPvP//banned//"+z.getName()+".yml");
                                    if(!file.exists()) {
                                        file.createNewFile();
                                    }
                                    YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                                    cfg.set("UUID", javautil());
                                    cfg.set("banned", "true");
                                    cfg.set("banned by", p.getName());
                                    cfg.set("reason", message);
                                    message = "";
                                    cfg.save(file);
                                  
                                    Bukkit.getServer().getOnlinePlayers();
                                    if(z.getName().equals(args[0])) {
                                        if(z.hasPermission("kitpvp.ban.bypass")) {
                                            p.sendMessage("§cYou cannot ban this player");
                                        }
                                        else {
                                            z.kickPlayer("You have been banned for cheating" + "\n \n§7UUID: "  + z.getUniqueId());
                                            Bukkit.broadcastMessage("§7" + z.getName() + " has been banned");
                                            message = "";
                                        }
                                    }                          
                                }
                                catch (IOException e) {
                                    p.sendMessage("§4Directory was not found");
                                }
                            }
                            else {
                                try {
                                    for(int i = 1; i < args.length; i++){
                                        message = message + args[i] + " ";
                                    }
                                    Player z = p.getServer().getPlayerExact(args[0]);
                                    File file = new File("plugins//KitPvP//banned//"+z.getName()+".yml");
                                    if(!file.exists()) {
                                        file.createNewFile();
                                    }
                                    YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
                                    cfg.set("UUID", z.getUniqueId());
                                    cfg.set("banned", "true");
                                    cfg.set("banned by", p.getName());
                                    cfg.set("reason", message);
                                    message = "";
                                    cfg.save(file);
                                  
                                    Bukkit.getServer().getOnlinePlayers();
                                    if(z.getName().equals(args[0])) {
                                        if(z.hasPermission("kitpvp.ban.bypass")) {
                                            p.sendMessage("§cYou cannot ban this player");
                                        }
                                        else {
                                            z.kickPlayer("You have been banned for " + message + "\n \n§7UUID: "  + z.getUniqueId());
                                            Bukkit.broadcastMessage("§7" + z.getName() + " has been banned");
                                            message = "";
                                        }
                                    }
                                }
                                catch (IOException e) {
                                    p.sendMessage("§4Directory was not found");
                                }
                            }
                        }
                        else {
                            p.sendMessage("§cWrong syntax! Usage: /ban <player> <reason>");
                        }
                    }
                    else {
                        p.sendMessage(Main.noperm);
                    }
                }
                return true;
            }
        }
    }
    BanListener.class:

    Code:
    package Listeners;
    
    import java.io.File;
    
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    
    public class BanListener implements Listener {
    
        @EventHandler
        public void onJoin(PlayerJoinEvent e) {
            File file = new File("plugins//KitPvP//banned//"+e.getPlayer().getName()+".yml");
            if(!file.exists()){
                return;
            }
            YamlConfiguration cfg = YamlConfiguration.loadConfiguration(file);
            if(cfg.getString("UUID").equals(e.getPlayer().getUniqueId())) {
                if(cfg.getString("banned").equals("true")) {
                    if(cfg.getString("reason").equals("cheating")) {
                        e.getPlayer().kickPlayer("You have been banned for cheating \n \n§7UUID: " + e.getPlayer().getUniqueId());
                    }
                    else {
                        e.getPlayer().kickPlayer("You have been banned for " + cfg.getString("reason"));
                    }
                }
            }
        }
    }
    Thank you to whoever can help me :)
     
  2. Online

    timtower Administrator Administrator Moderator

    @TheFl4me UUID.toString()
    And try to don't store the data in a file that is using a changing player name
     
  3. Offline

    TheFl4me

    Thanks alot for the quick response :) and another Thank you becuase it worked :D

    is there any chance you could still help me with the 2nd issue? I would really appreciate it :)
     
  4. Online

    timtower Administrator Administrator Moderator

    @TheFl4me Could you perform the command on the console and put the output here?
    And please change your file handling location, you are writing the file and then you check if the player can be banned.
     
  5. Offline

    TheFl4me

    Never mind i managed to fix that one on my own :) Thanks anyways

    P.S.
    and i did as you said with moving the file handling location.
     
Thread Status:
Not open for further replies.

Share This Page