Commits vergleichen
1 Commits
master
...
add-chat-f
Autor | SHA1 | Datum | |
---|---|---|---|
82f753e9fd |
4
src/de/steamwar/bungeecore/chat/ChatFilter.java
Normale Datei
4
src/de/steamwar/bungeecore/chat/ChatFilter.java
Normale Datei
@ -0,0 +1,4 @@
|
||||
package de.steamwar.bungeecore.chat;
|
||||
|
||||
public class ChatFilter {
|
||||
}
|
84
src/de/steamwar/bungeecore/chat/FilterTree.java
Normale Datei
84
src/de/steamwar/bungeecore/chat/FilterTree.java
Normale Datei
@ -0,0 +1,84 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2024 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bungeecore.chat;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FilterTree {
|
||||
/**
|
||||
* Root of this prefix tree
|
||||
*/
|
||||
final private TreeNode root = new TreeNode();
|
||||
|
||||
/**
|
||||
* Embeds the given word into the tree
|
||||
*
|
||||
* @param word the word to embed
|
||||
* @param value the value given to this word
|
||||
*/
|
||||
public void addWord(@NotNull String word, int value) {
|
||||
char[] chars = word.toCharArray();
|
||||
|
||||
TreeNode current = root;
|
||||
for (char c : chars) {
|
||||
if (current.children.containsKey(c)) {
|
||||
current = current.children.get(c);
|
||||
} else {
|
||||
TreeNode next = new TreeNode();
|
||||
current.children.put(c, next);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
current.value = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the tree to find the value that should be assigned to
|
||||
* the given word
|
||||
*
|
||||
* @param word the word to evaluate
|
||||
* @return the value assigned to the given word
|
||||
*/
|
||||
public int evaluateWord(@NotNull String word) {
|
||||
char[] chars = word.toCharArray();
|
||||
|
||||
int value = 0;
|
||||
TreeNode current = root;
|
||||
for (char c : chars) {
|
||||
if (current.children.containsKey(c)) {
|
||||
current = current.children.get(c);
|
||||
value += current.value;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static class TreeNode {
|
||||
final Map<Character, TreeNode> children = new HashMap<>();
|
||||
int value;
|
||||
}
|
||||
}
|
In neuem Issue referenzieren
Einen Benutzer sperren