Skip to content

Commit

Permalink
v3.0.5 - Filtering Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Majekdor committed Sep 4, 2022
1 parent f181ac4 commit b70ce6c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 30 deletions.
18 changes: 0 additions & 18 deletions .github/workflows/qodana-scan.yml

This file was deleted.

8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Plugin Changelog

# 3.0.5 - Filtering Feature

- `/nickcolor` will now allow you to include colors and your username. See [#85](https://github.com/MajekDev/HexNicks/issues/85).
- You can now block nicknames in the config with regex support.
- Removed qodana scan workflow.

The filtering feature config option is the main new thing here. The `/nickcolor` thing was more of a bug if we're being honest. It should have worked that way from the start.

# 3.0.4 - Logging Is A Feature

- Added a lot of logging when in debug mode.
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.majek</groupId>
<artifactId>hexnicks</artifactId>
<version>3.0.4</version>
<version>3.0.5</version>
<packaging>jar</packaging>

<name>HexNicks</name>
Expand Down Expand Up @@ -186,7 +186,7 @@
<dependency>
<groupId>net.essentialsx</groupId>
<artifactId>EssentialsX</artifactId>
<version>2.19.6</version>
<version>2.19.7</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand All @@ -203,4 +203,4 @@
<scope>test</scope>
</dependency>
</dependencies>
</project>
</project>
17 changes: 11 additions & 6 deletions src/main/java/dev/majek/hexnicks/command/CommandNick.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
.removeColors(MiscUtils.blockedColors(player))
.build().mmParse(nickInput);

// Make sure the nickname is alphanumeric if that's enabled
String plainTextNick = PlainTextComponentSerializer.plainText().serialize(nickname);
if (HexNicks.config().REQUIRE_ALPHANUMERIC) {
if (!plainTextNick.matches("[a-zA-Z0-9]+")) {
Messages.NON_ALPHANUMERIC.send(player);
return true;
}

// First make sure the nickname is allowed
if (MiscUtils.isBlocked(plainTextNick)) {
Messages.NOT_ALLOWED.send(player);
return true;
}

// Make sure the nickname is alphanumeric if that's enabled
if (HexNicks.config().REQUIRE_ALPHANUMERIC && !plainTextNick.matches("[a-zA-Z0-9]+")) {
Messages.NON_ALPHANUMERIC.send(player);
return true;
}

// Set the nickname to the default color if there's no color specified
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
// If there are no colors the length should be 0
String plainTextInput = PlainTextComponentSerializer.plainText()
.serialize(MiniMessageWrapper.legacy().mmParse(nickInput));
if (plainTextInput.length() > 0) {
if (plainTextInput.length() > 0 && !plainTextInput.equals(player.getName())) {
Messages.ONLY_COLOR_CODES.send(player);
return true;
}
Expand All @@ -87,7 +87,9 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}
}

Component nickname = wrapper.mmParse(wrapper.mmString(nickInput) + plainTextNick);
Component nickname = wrapper.mmParse(
wrapper.mmString(nickInput) + (plainTextInput.length() == 0 ? plainTextNick : "")
);

// Call event
NickColorEvent colorEvent = new NickColorEvent(player, nickname,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/majek/hexnicks/config/ConfigValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpRequest;
import java.util.List;
import dev.majek.hexnicks.util.MiscUtils;
import net.kyori.adventure.text.format.TextColor;
import org.jetbrains.annotations.NotNull;
Expand All @@ -55,6 +56,7 @@ public class ConfigValues {
public Boolean NICK_OTHER_OVERRIDE;
public Boolean PREVENT_DUPLICATE_NICKS;
public Boolean PREVENT_DUPLICATE_NICKS_STRICT;
public List<String> BLOCKED_NICKNAMES;
public Boolean DEBUG;

public ConfigValues() {
Expand All @@ -79,6 +81,7 @@ public void reload() {
NICK_OTHER_OVERRIDE = HexNicks.core().getConfig().getBoolean("nickother-override", false);
PREVENT_DUPLICATE_NICKS = HexNicks.core().getConfig().getBoolean("prevent-duplicate-nicks", true);
PREVENT_DUPLICATE_NICKS_STRICT = HexNicks.core().getConfig().getBoolean("prevent-duplicate-nicks-strict", false);
BLOCKED_NICKNAMES = HexNicks.core().getConfig().getStringList("blocked-nicknames");
DEBUG = HexNicks.core().getConfig().getBoolean("debug", false);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/dev/majek/hexnicks/config/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public interface Messages {

Args0 WORKING = () -> MiscUtils.configString("messages.working", "<gray>Working...");

Args0 NOT_ALLOWED = () -> MiscUtils.configString("messages.notAllowed", "<red>That nickname is not allowed!");

/**
* A message that has no arguments that need to be replaced.
*/
Expand Down
29 changes: 28 additions & 1 deletion src/main/java/dev/majek/hexnicks/util/MiscUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import dev.majek.hexnicks.HexNicks;
import dev.majek.hexnicks.config.Messages;

import java.io.IOException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
Expand All @@ -34,6 +33,8 @@
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
Expand Down Expand Up @@ -103,6 +104,32 @@ public static boolean preventDuplicates(@NotNull Component nickname, @NotNull Pl
return false;
}

/**
* Check whether the given nickname is blocked by config settings.
*
* @param plainTextNick the provided nickname in plain text
* @return whether the nickname is blocked.
*/
public static boolean isBlocked(@NotNull String plainTextNick) {
for (String blockedNick : HexNicks.config().BLOCKED_NICKNAMES) {
if (isValidRegex(blockedNick) && plainTextNick.matches(blockedNick)) {
return true;
} else if (blockedNick.equalsIgnoreCase(plainTextNick)) {
return true;
}
}
return false;
}

private static boolean isValidRegex(@NotNull String string) {
try {
Pattern.compile(string);
return true;
} catch (PatternSyntaxException ignored) {
return false;
}
}

/**
* Get the legacy color code from a named text color.
*
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ nickother-override: false
prevent-duplicate-nicks: true
prevent-duplicate-nicks-strict: false

# A list of nicknames that will not be allowed, ignoring case.
# This list can also accept regex.
# Using /nickother will not check if the nickname is blocked.
#
# Ex.
# blocked-nicknames:
# - "idiot"
# - "(i|1)d(i|1)(0|o)t" # this is regex
blocked-nicknames:
- "replace these lines"
- "what to block"

# Database settings
# Only enable this if you know what you're doing and have an existing database
database-enabled: false
Expand Down Expand Up @@ -87,3 +99,4 @@ messages:
invalidLink: "<red>The link provided is not valid! See console for further details."
latestLog: "<green>View the latest log <click:open_url:'%link%'><aqua><u>here</u></aqua></click>."
working: "<gray>Working..."
notAllowed: "<red>That nickname is not allowed!"

0 comments on commit b70ce6c

Please sign in to comment.