SteamWar/BungeeCore
Archiviert
13
2

Commits vergleichen

...
Dieses Repository wurde am 2024-08-05 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.

1 Commits

Autor SHA1 Nachricht Datum
82f753e9fd Implemented FilterTree
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful
2024-05-15 18:37:17 +02:00
2 geänderte Dateien mit 88 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,4 @@
package de.steamwar.bungeecore.chat;
public class ChatFilter {
}

Datei anzeigen

@ -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;
}
}