/net/minecraft/server/BanList.java

https://bitbucket.org/agaricusb/mc-dev · Java · 128 lines · 102 code · 26 blank · 0 comment · 12 complexity · 8e4fa5f3cab1a7a0911cd660ad07a0f1 MD5 · raw file

  1. package net.minecraft.server;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileReader;
  6. import java.io.FileWriter;
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.Iterator;
  12. import java.util.Map;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. public class BanList {
  16. private final InsensitiveStringMap a = new InsensitiveStringMap();
  17. private final File b;
  18. private boolean c = true;
  19. public BanList(File file1) {
  20. this.b = file1;
  21. }
  22. public boolean isEnabled() {
  23. return this.c;
  24. }
  25. public void setEnabled(boolean flag) {
  26. this.c = flag;
  27. }
  28. public Map getEntries() {
  29. this.removeExpired();
  30. return this.a;
  31. }
  32. public boolean isBanned(String s) {
  33. if (!this.isEnabled()) {
  34. return false;
  35. } else {
  36. this.removeExpired();
  37. return this.a.containsKey(s);
  38. }
  39. }
  40. public void add(BanEntry banentry) {
  41. this.a.put(banentry.getName(), banentry);
  42. this.save();
  43. }
  44. public void remove(String s) {
  45. this.a.remove(s);
  46. this.save();
  47. }
  48. public void removeExpired() {
  49. Iterator iterator = this.a.values().iterator();
  50. while (iterator.hasNext()) {
  51. BanEntry banentry = (BanEntry) iterator.next();
  52. if (banentry.hasExpired()) {
  53. iterator.remove();
  54. }
  55. }
  56. }
  57. public void load() {
  58. if (this.b.isFile()) {
  59. BufferedReader bufferedreader;
  60. try {
  61. bufferedreader = new BufferedReader(new FileReader(this.b));
  62. } catch (FileNotFoundException filenotfoundexception) {
  63. throw new Error();
  64. }
  65. String s;
  66. try {
  67. while ((s = bufferedreader.readLine()) != null) {
  68. if (!s.startsWith("#")) {
  69. BanEntry banentry = BanEntry.c(s);
  70. if (banentry != null) {
  71. this.a.put(banentry.getName(), banentry);
  72. }
  73. }
  74. }
  75. } catch (IOException ioexception) {
  76. Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not load ban list", ioexception);
  77. }
  78. }
  79. }
  80. public void save() {
  81. this.save(true);
  82. }
  83. public void save(boolean flag) {
  84. this.removeExpired();
  85. try {
  86. PrintWriter printwriter = new PrintWriter(new FileWriter(this.b, false));
  87. if (flag) {
  88. printwriter.println("# Updated " + (new SimpleDateFormat()).format(new Date()) + " by Minecraft " + "1.4.5");
  89. printwriter.println("# victim name | ban date | banned by | banned until | reason");
  90. printwriter.println();
  91. }
  92. Iterator iterator = this.a.values().iterator();
  93. while (iterator.hasNext()) {
  94. BanEntry banentry = (BanEntry) iterator.next();
  95. printwriter.println(banentry.g());
  96. }
  97. printwriter.close();
  98. } catch (IOException ioexception) {
  99. Logger.getLogger("Minecraft").log(Level.SEVERE, "Could not save ban list", ioexception);
  100. }
  101. }
  102. }