PageRenderTime 40ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/worldguard-legacy/src/main/java/com/sk89q/worldguard/blacklist/Blacklist.java

https://gitlab.com/igserfurtmcschulserver/CustomWorldGuard
Java | 292 lines | 178 code | 45 blank | 69 comment | 40 complexity | 8b762c3b9fab5dde8c7f0f7d1b611919 MD5 | raw file
  1. /*
  2. * WorldGuard, a suite of tools for Minecraft
  3. * Copyright (C) sk89q <http://www.sk89q.com>
  4. * Copyright (C) WorldGuard team and contributors
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU Lesser General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.sk89q.worldguard.blacklist;
  20. import com.sk89q.worldguard.blacklist.action.Action;
  21. import com.sk89q.worldguard.blacklist.action.ActionType;
  22. import com.sk89q.worldguard.blacklist.event.BlacklistEvent;
  23. import com.sk89q.worldguard.blacklist.event.EventType;
  24. import com.sk89q.worldguard.blacklist.target.TargetMatcher;
  25. import com.sk89q.worldguard.blacklist.target.TargetMatcherParseException;
  26. import com.sk89q.worldguard.blacklist.target.TargetMatcherParser;
  27. import com.sk89q.guavabackport.cache.CacheBuilder;
  28. import com.sk89q.guavabackport.cache.CacheLoader;
  29. import com.sk89q.guavabackport.cache.LoadingCache;
  30. import com.sk89q.worldguard.bukkit.commands.CommandUtils;
  31. import org.bukkit.ChatColor;
  32. import java.io.BufferedReader;
  33. import java.io.File;
  34. import java.io.FileReader;
  35. import java.io.IOException;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.concurrent.TimeUnit;
  39. import java.util.logging.Level;
  40. import java.util.logging.Logger;
  41. public abstract class Blacklist {
  42. private static final Logger log = Logger.getLogger(Blacklist.class.getCanonicalName());
  43. private MatcherIndex index = MatcherIndex.getEmptyInstance();
  44. private final BlacklistLoggerHandler blacklistLogger = new BlacklistLoggerHandler();
  45. private BlacklistEvent lastEvent;
  46. private boolean useAsWhitelist;
  47. private LoadingCache<String, TrackedEvent> repeatingEventCache = CacheBuilder.newBuilder()
  48. .maximumSize(1000)
  49. .expireAfterAccess(30, TimeUnit.SECONDS)
  50. .build(new CacheLoader<String, TrackedEvent>() {
  51. @Override
  52. public TrackedEvent load(String s) throws Exception {
  53. return new TrackedEvent();
  54. }
  55. });
  56. public Blacklist(boolean useAsWhitelist) {
  57. this.useAsWhitelist = useAsWhitelist;
  58. }
  59. /**
  60. * Returns whether the list is empty.
  61. *
  62. * @return whether the blacklist is empty
  63. */
  64. public boolean isEmpty() {
  65. return index.isEmpty();
  66. }
  67. /**
  68. * Get the number of individual items that have blacklist entries.
  69. *
  70. * @return The number of items in the blacklist
  71. */
  72. public int getItemCount() {
  73. return index.size();
  74. }
  75. /**
  76. * Returns whether the blacklist is used as a whitelist.
  77. *
  78. * @return whether the blacklist is be used as a whitelist
  79. */
  80. public boolean isWhitelist() {
  81. return useAsWhitelist;
  82. }
  83. /**
  84. * Get the log.
  85. *
  86. * @return The logger used in this blacklist
  87. */
  88. public BlacklistLoggerHandler getLogger() {
  89. return blacklistLogger;
  90. }
  91. /**
  92. * Method to handle the event.
  93. *
  94. * @param event The event to check
  95. * @param forceRepeat Whether to force quickly repeating notifications
  96. * @param silent Whether to force-deny notifications
  97. * @return Whether the event is allowed
  98. */
  99. public boolean check(BlacklistEvent event, boolean forceRepeat, boolean silent) {
  100. List<BlacklistEntry> entries = index.getEntries(event.getTarget());
  101. if (entries == null) {
  102. return true;
  103. }
  104. boolean ret = true;
  105. for (BlacklistEntry entry : entries) {
  106. if (!entry.check(useAsWhitelist, event, forceRepeat, silent)) {
  107. ret = false;
  108. }
  109. }
  110. return ret;
  111. }
  112. /**
  113. * Load the blacklist.
  114. *
  115. * @param file The file to load from
  116. * @throws IOException if an error occurred reading from the file
  117. */
  118. public void load(File file) throws IOException {
  119. FileReader input = null;
  120. MatcherIndex.Builder builder = new MatcherIndex.Builder();
  121. TargetMatcherParser targetMatcherParser = new TargetMatcherParser();
  122. try {
  123. input = new FileReader(file);
  124. BufferedReader buff = new BufferedReader(input);
  125. String line;
  126. List<BlacklistEntry> currentEntries = null;
  127. while ((line = buff.readLine()) != null) {
  128. line = line.trim();
  129. // Blank line
  130. if (line.isEmpty()) {
  131. continue;
  132. } else if (line.charAt(0) == ';' || line.charAt(0) == '#') {
  133. continue;
  134. }
  135. if (line.matches("^\\[.*\\]$")) {
  136. String[] items = line.substring(1, line.length() - 1).split(",");
  137. currentEntries = new ArrayList<BlacklistEntry>();
  138. for (String item : items) {
  139. try {
  140. TargetMatcher matcher = targetMatcherParser.fromInput(item.trim());
  141. BlacklistEntry entry = new BlacklistEntry(this);
  142. builder.add(matcher, entry);
  143. currentEntries.add(entry);
  144. } catch (TargetMatcherParseException e) {
  145. log.log(Level.WARNING, "Could not parse a block/item heading: " + e.getMessage());
  146. }
  147. }
  148. } else if (currentEntries != null) {
  149. String[] parts = line.split("=");
  150. if (parts.length == 1) {
  151. log.log(Level.WARNING, "Found option with no value " + file.getName() + " for '" + line + "'");
  152. continue;
  153. }
  154. boolean unknownOption = false;
  155. for (BlacklistEntry entry : currentEntries) {
  156. if (parts[0].equalsIgnoreCase("ignore-groups")) {
  157. entry.setIgnoreGroups(parts[1].split(","));
  158. } else if (parts[0].equalsIgnoreCase("ignore-perms")) {
  159. entry.setIgnorePermissions(parts[1].split(","));
  160. } else if (parts[0].equalsIgnoreCase("message")) {
  161. entry.setMessage(CommandUtils.replaceColorMacros(parts[1].trim()));
  162. } else if (parts[0].equalsIgnoreCase("comment")) {
  163. entry.setComment(CommandUtils.replaceColorMacros(parts[1].trim()));
  164. } else {
  165. boolean found = false;
  166. for (EventType type : EventType.values()) {
  167. if (type.getRuleName().equalsIgnoreCase(parts[0])) {
  168. entry.getActions(type.getEventClass()).addAll(parseActions(entry, parts[1]));
  169. found = true;
  170. break;
  171. }
  172. }
  173. if (!found) {
  174. unknownOption = true;
  175. }
  176. }
  177. }
  178. if (unknownOption) {
  179. log.log(Level.WARNING, "Unknown option '" + parts[0] + "' in " + file.getName() + " for '" + line + "'");
  180. }
  181. } else {
  182. log.log(Level.WARNING, "Found option with no heading "
  183. + file.getName() + " for '" + line + "'");
  184. }
  185. }
  186. this.index = builder.build();
  187. } finally {
  188. try {
  189. if (input != null) {
  190. input.close();
  191. }
  192. } catch (IOException ignore) {
  193. }
  194. }
  195. }
  196. private List<Action> parseActions(BlacklistEntry entry, String raw) {
  197. String[] split = raw.split(",");
  198. List<Action> actions = new ArrayList<Action>();
  199. for (String name : split) {
  200. name = name.trim();
  201. boolean found = false;
  202. for (ActionType type : ActionType.values()) {
  203. if (type.getActionName().equalsIgnoreCase(name)) {
  204. actions.add(type.parseInput(this, entry));
  205. found = true;
  206. break;
  207. }
  208. }
  209. if (!found) {
  210. log.log(Level.WARNING, "Unknown blacklist action: " + name);
  211. }
  212. }
  213. return actions;
  214. }
  215. /**
  216. * Get the last event.
  217. *
  218. * @return The last event
  219. */
  220. public BlacklistEvent getLastEvent() {
  221. return lastEvent;
  222. }
  223. /**
  224. * Notify administrators.
  225. *
  226. * @param event The event to notify about
  227. * @param comment The comment to notify with
  228. */
  229. public void notify(BlacklistEvent event, String comment) {
  230. lastEvent = event;
  231. broadcastNotification(ChatColor.GRAY + "WG: "
  232. + ChatColor.LIGHT_PURPLE + event.getCauseName()
  233. + ChatColor.GOLD + " (" + event.getDescription() + ") "
  234. + ChatColor.WHITE
  235. + event.getTarget().getFriendlyName()
  236. + (comment != null ? " (" + comment + ")" : "") + ".");
  237. }
  238. /**
  239. * Sends a notification to all subscribing users.
  240. *
  241. * @param msg The message to broadcast
  242. */
  243. public abstract void broadcastNotification(String msg);
  244. public LoadingCache<String, TrackedEvent> getRepeatingEventCache() {
  245. return repeatingEventCache;
  246. }
  247. }