-
Notifications
You must be signed in to change notification settings - Fork 53
feat: add DisallowedWordsCommand #354
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
John-Paul-R
wants to merge
8
commits into
joinpoint-separate-mod
Choose a base branch
from
disallowed-words-homoglyph
base: joinpoint-separate-mod
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c7b1db
feat: add DisallowedWordsCommand
John-Paul-R a9c034a
add the rest of the commands
John-Paul-R 426654d
add straggler playerprofile file to FileUtil
John-Paul-R b652b5f
handle multiple charsets, and save on allow/disallow
John-Paul-R f819861
split out DisallowWordsSystem into its own file
John-Paul-R d9c48f5
wire up to NicknameSetCommand and minor refactor in that area
John-Paul-R f04bed0
rm probably unnecessary maven clauses
John-Paul-R 5547f10
add nickname set disallowed words error text
John-Paul-R File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/main/java/com/fibermc/essentialcommands/DisallowWordsSystem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package com.fibermc.essentialcommands; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.Collection; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
|
|
||
| import com.fibermc.essentialcommands.util.FileUtil; | ||
| import net.codebox.homoglyph.Homoglyph; | ||
| import net.codebox.homoglyph.HomoglyphBuilder; | ||
|
|
||
| import net.minecraft.server.MinecraftServer; | ||
| import net.minecraft.util.Util; | ||
|
|
||
| public final class DisallowWordsSystem { | ||
| private final Homoglyph homoglyph; | ||
| private final ArrayList<String> disallowedWords; | ||
| private static Path disallowedWordsFile; | ||
| private static final Comparator<String> ORDER = String.CASE_INSENSITIVE_ORDER; | ||
|
|
||
| private static MinecraftServer currentServer; | ||
| private static DisallowWordsSystem instance; | ||
|
|
||
| public static DisallowWordsSystem current(MinecraftServer server) { | ||
| if (currentServer != server) { | ||
| instance = create(server); | ||
| } | ||
| return instance; | ||
| } | ||
|
|
||
| public static DisallowWordsSystem create(MinecraftServer server) { | ||
| try { | ||
| currentServer = server; | ||
| disallowedWordsFile = FileUtil.FilePaths.current(server).disallowedWordsFile(); | ||
| if (FileUtil.createFileWithDirs(disallowedWordsFile)) { | ||
| EssentialCommands.LOGGER.info("Created disallowed words file at path: {}", disallowedWordsFile); | ||
| } | ||
| return instance = new DisallowWordsSystem( | ||
| Files.readAllLines(disallowedWordsFile, FileUtil.detectCharset(disallowedWordsFile)) | ||
| ); | ||
| } catch (IOException ex) { | ||
| throw new RuntimeException(ex); | ||
| } | ||
| } | ||
|
|
||
| public DisallowWordsSystem(Collection<String> disallowedWords) throws IOException { | ||
| homoglyph = HomoglyphBuilder.build(); | ||
| this.disallowedWords = new ArrayList<>(disallowedWords); | ||
| this.disallowedWords.sort(ORDER); | ||
| } | ||
|
|
||
| private void save() { | ||
| Util.getIoWorkerExecutor().execute(() -> { | ||
| try { | ||
| Files.write(disallowedWordsFile, disallowedWords, StandardCharsets.UTF_8); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| public Collection<String> words() { | ||
| return this.disallowedWords; | ||
| } | ||
|
|
||
| public boolean isAllowed(String text) { | ||
| var result = homoglyph.search(text, disallowedWords); | ||
| return result.isEmpty(); | ||
| } | ||
|
|
||
| public boolean disallow(String text) { | ||
| // the index of the search key, if it is contained in the list; | ||
| // otherwise, (-(insertion point) - 1). The insertion point is | ||
| // defined as the point at which the key would be inserted into the | ||
| // list: the index of the first element greater than the key, or | ||
| // list.size() if all elements in the list are less than the | ||
| // specified key. Note that this guarantees that the return value | ||
| // will be >= 0 if and only if the key is found. | ||
| int index = Collections.binarySearch(disallowedWords, text, ORDER); | ||
| if (index >= 0) { | ||
| // already exists | ||
| return false; | ||
| } | ||
|
|
||
| disallowedWords.add(-(index + 1), text); | ||
|
|
||
| save(); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| public boolean allow(String text) { | ||
| int index = Collections.binarySearch(disallowedWords, text, ORDER); | ||
| if (index < 0) { | ||
| // is not disallowed | ||
| return false; | ||
| } | ||
|
|
||
| disallowedWords.remove(index); | ||
|
|
||
| save(); | ||
|
|
||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.