Solved Custom Ban System Help

Discussion in 'Plugin Development' started by BucketOfFun, Nov 10, 2012.

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

    BucketOfFun

    I'm trying to make a custom ban system for a plugin, but I can't exactly get the "keeping people out of the server" part to work.
    Phil2812 Built off the principle of your inventory serializer.
    Code:
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
    if(cmd.getName().equalsIgnoreCase("pcb")){
    String punishment = args[0];
    Player target = Bukkit.getServer().getPlayer(args[1]);
    if(punishment.equalsIgnoreCase("ban")){
    StringBuilder msg = new StringBuilder(args[2]);
    for(int arg = 3; arg < args.length; arg++){
    msg.append(" ").append(args[arg]);
    }
     
    String m = msg.toString();
     
    if(target == null){
    File dir = new File("plugins/Pixel Control/Ban History");
    dir.mkdirs();
    File file = new File(dir + "/BannedPlayers.txt");
    try{
    FileWriter fr = new FileWriter(file);
    BufferedWriter br = new BufferedWriter(fr, 32768);
    br.newLine();
    br.write(":@n" + args[1] + ":@r" + m + ";");
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    String n = target.getName();
     
    Bukkit.getServer().broadcastMessage(ChatColor.BLUE + "[PNBans]: " + ChatColor.RED + n + ChatColor.BLUE + " was banned - " + ChatColor.RED + m);
    File dir = new File("plugins/Pixel Control/Ban History");
    dir.mkdirs();
    File file = new File(dir + "/BannedPlayers.txt");
    if(!file.exists()){
    try{
    PCFileManager.createBanFile();
    FileWriter fr = new FileWriter(file);
    BufferedWriter br = new BufferedWriter(fr, 32768);
    br.newLine();
    br.write(":@n" + n + ":@r" + m + ";");
    br.close();
    fr.close();
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    else{
    try{
    FileWriter fr = new FileWriter(file);
    BufferedWriter br = new BufferedWriter(fr, 32768);
    br.newLine();
    br.write(":n@" + n + ":r@" + m + ";");
    br.close();
    fr.close();
    }catch(IOException e){
    e.printStackTrace();
    }
    }
    target.kickPlayer("You have been banned - " + m);
    sender.sendMessage("Successfully banned " + n);
    return true;
    }
                      return false;
                }
     
    @EventHandler
    public void checkIfBanned(PlayerLoginEvent e, String list) throws IOException{
    File dir = new File("plugins/Pixel Control/Ban History");
    dir.mkdirs();
    File file = new File(dir + "/Banned Players.txt");
    try {
    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr, 32768);
    list = br.readLine();
    if(file.exists()){
    String[] history = list.split(";");
    for (int i = 1; i < history.length; i++){
    String[] split = history[i].split("@");
    for (String check : split)
              {
                  String[] player = check.split(":");
                  if (player[0].equals("p"))
                  {
                      String p = player[1];
                      if(player[0].equals("r")){
                      StringBuilder reason = new StringBuilder(player[1]);
          for(int arg = 2; arg < player.length; arg++){
          reason.append(" ").append(player[arg]);
          }
                      String r = reason.toString();
                      if(e.getPlayer().getName().equalsIgnoreCase(p)){
                      e.setResult(Result.KICK_OTHER);
                      e.setKickMessage("Banned - " + r + ". Appeal at http://pixelsville.enjin.com/banappeal");
                      }
                      }
                  }
          }
    }
    }
    } catch (FileNotFoundException ex) {
            pc.getLogger().warning("BAN FILE MISSING! DAFUQ HAPPENED???");
    }
    }
    Wow, why is it that code always posts weird? If you want to see this on pastebin, just let me know.
     
  2. Offline

    gomeow

    Well, what's it supposed to do?
     
    Assult likes this.
  3. Offline

    cman1885

    Try to not get passed String list. And don't use throws, put a try/catch. And make sure you register events.
     
  4. Offline

    BucketOfFun

    It's supposed to:
    1. Take the name of the person banned, add it after :n@, and take the reason they were banned and add it after : r @ (Without the spaces, the rage emote was getting in the way) on the same line. This is the part it does.
    2. When a player logs in, it searches the file to see if it can find their name, and if it can, kicks them and sets the kick message to the reason they were banned paired with their name + the link for ban appeals. This is the part that I can't get to work, the player can just join back in. No errors or anything. (Yes the event is registered :p)

    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  5. Offline

    BucketOfFun

    Bump. Anybody?
     
  6. Offline

    TwistedMexi

    Do you get any kind of errors in your console?
     
  7. Offline

    fireblast709

    BucketOfFun some cleanup of your code, using YAML files and Bukkit's YamlConfiguration class :3
    Code:java
    1. public YamlConfiguration yc;
    2.  
    3. public void onEnable()
    4. {
    5. File dir = new File("plugins/Pixel Control/Ban History");
    6. dir.mkdirs();
    7. File file = new File(dir.getPath() + "/Banned Players.yml");
    8. yc = YamlConfiguration.loadConfiguration(file);
    9. if(yc == null)
    10. {
    11. pc.getLogger().warning("BAN FILE MISSING! DAFUQ HAPPENED???");
    12. }
    13.  
    14. Bukkit.getPluginManager().registerEvents(this, this);
    15. }
    16.  
    17. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args)
    18. {
    19. if(cmd.getName().equalsIgnoreCase("pcb"))
    20. {
    21. String punishment = args[0];
    22. Player target = Bukkit.getServer().getPlayer(args[1]);
    23. if(punishment.equalsIgnoreCase("ban"))
    24. {
    25. if(yc == null)
    26. {
    27. sender.sendMessage("Could not ban player: ban file does not exist");
    28. return true;
    29. }
    30. StringBuilder msg = new StringBuilder(args[2]);
    31. for(int arg = 3; arg < args.length; arg++)
    32. {
    33. msg.append(" ").append(args[arg]);
    34. }
    35.  
    36. String m = msg.toString();
    37.  
    38. String n;
    39. if(target == null) // For offline players
    40. {
    41. n = args[1];
    42. }
    43. else
    44. {
    45. n = target.getName();
    46. }
    47.  
    48. yc.set("banned."+n, m);
    49.  
    50. Bukkit.getServer().broadcastMessage(ChatColor.BLUE + "[PNBans]: " + ChatColor.RED + n + ChatColor.BLUE + " was banned - " + ChatColor.RED + m);
    51.  
    52.  
    53. target.kickPlayer("You have been banned - " + m);
    54. sender.sendMessage("Successfully banned " + n);
    55. return true;
    56. }
    57. return false;
    58. }
    59. }
    60.  
    61. @EventHandler
    62. public void checkIfBanned(PlayerLoginEvent e)
    63. {
    64. if(yc != null)
    65. {
    66. String name = e.getPlayer().getName();
    67. String reason = yc.getString("banned."name, "notbanned");
    68. if(!reason.equals("notbanned")) // Uh-oh, you are banned with a reason
    69. {
    70. e.setResult(Result.KICK_OTHER);
    71. e.setKickMessage("Banned - " + reason + ". Appeal at [url]http://pixelsville.enjin.com/banappeal[/url]");
    72. }
    73. }
    74. else
    75. {
    76. pc.getLogger().warning("Ban file missing, skipping check");
    77. }
    78. }

    Should work now (if the kicks you used work at least). The bans file looks like this now:
    Code:
    # playername: reason
    banned:
      player1: cuz I banned you
      player2: and this is the reason
    And do not forget to let the main class implement Listener!
     
  8. Offline

    BucketOfFun

    fireblast709
    Nothing writes to the .yml file when I type the command :oops:
     
  9. Offline

    gomeow

    Did you save it?
     
  10. Offline

    BucketOfFun

    *Facepalm* Thanks guys!
     
  11. Offline

    gomeow

    Your welcome
     
  12. Offline

    fireblast709

    no problem :3
     
Thread Status:
Not open for further replies.

Share This Page