Skip to content

Commit

Permalink
v3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Majekdor committed Jan 31, 2023
1 parent 68f068a commit 06bc25d
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 199 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Plugin Changelog

# 3.1.0 - Hello Hikari

- Started using [HikariCP](https://github.com/brettwooldridge/HikariCP) for managing MySQL.
- Fixed bug with server lagging on shutdown.
- Fixed bug with failing to remove Essentials prefix.
- Bumped annotations and Maven shade dependencies.
- Bug fixes related to color/chat formatting.

There have been a number of SQL issues reported recently. This update should fix all of them and make the SQL integration faster overall.

# 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).
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/developers.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ HexNicks is hosted in Majekdor's Maven Repository [here](https://repo.majek.dev)
<dependency>
<groupId>dev.majek</groupId>
<artifactId>hexnicks</artifactId>
<version>3.0.5</version>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand All @@ -38,7 +38,7 @@ repositories {
}
dependencies {
compileOnly "dev.majek:hexnicks:3.0.5"
compileOnly "dev.majek:hexnicks:3.1.0"
}
```

Expand Down
13 changes: 10 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.6</version>
<version>3.1.0</version>
<packaging>jar</packaging>

<name>HexNicks</name>
Expand Down Expand Up @@ -80,7 +80,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down Expand Up @@ -151,7 +151,7 @@
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>23.0.0</version>
<version>24.0.0</version>
<scope>compile</scope>
</dependency>
<!-- PlaceholderAPI -->
Expand Down Expand Up @@ -202,5 +202,12 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- Hikari -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.1</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
64 changes: 26 additions & 38 deletions src/main/java/dev/majek/hexnicks/HexNicks.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,15 @@
import dev.majek.hexnicks.api.HexNicksApi;
import dev.majek.hexnicks.command.*;
import dev.majek.hexnicks.config.ConfigValues;
import dev.majek.hexnicks.storage.SqlManager;
import dev.majek.hexnicks.storage.*;
import dev.majek.hexnicks.event.PaperTabCompleteEvent;
import dev.majek.hexnicks.event.PlayerChat;
import dev.majek.hexnicks.event.PlayerJoin;
import dev.majek.hexnicks.hook.HookManager;
import dev.majek.hexnicks.storage.JsonStorage;
import dev.majek.hexnicks.storage.SqlStorage;
import dev.majek.hexnicks.storage.StorageMethod;
import dev.majek.hexnicks.util.LoggingManager;
import dev.majek.hexnicks.util.UpdateChecker;
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -50,6 +48,7 @@
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -66,7 +65,6 @@ public final class HexNicks extends JavaPlugin {
private static HexNicksApi api;
private static LoggingManager logging;
private static ConfigValues config;
private static SqlManager sql;
private static HookManager hooks;
private static StorageMethod storage;
private final File jsonFile;
Expand All @@ -76,15 +74,13 @@ public final class HexNicks extends JavaPlugin {

/**
* Initialize plugin.
*
* Logging must be instantiated before other managers.
*/
public HexNicks() {
core = this;
api = new HexNicksApi();
logging = new LoggingManager(this, new File(this.getDataFolder(), "logs"));
config = new ConfigValues();
sql = new SqlManager();
hooks = new HookManager();
this.jsonFile = new File(this.getDataFolder(), "nicknames.json");
this.nickMap = new HashMap<>();
Expand Down Expand Up @@ -137,28 +133,18 @@ public void onEnable() {
// Load nicknames from storage
if (this.getConfig().getBoolean("database-enabled")) {
try {
sql.connect();
HikariManager.createTable();
storage = new SqlStorage();
storage.updateNicks();
logging.log("Successfully connected to MySQL database.");
Bukkit.getScheduler().scheduleSyncRepeatingTask(HexNicks.core(), () -> HexNicks.storage().updateNicks(),
200L, this.getConfig().getInt("update-interval", 300) * 20L);
} catch (final SQLException ex) {
logging.error("Failed to connect to MySQL database", ex);
this.loadNicknamesFromJson();
}
}
if (sql.isConnected()) {
logging.log("Successfully connected to MySQL database.");
storage = new SqlStorage();
sql.createTable();
storage.updateNicks();
} else {
try {
storage = new JsonStorage();
JsonObject json = (JsonObject) JsonParser.parseReader(new FileReader(HexNicks.core().jsonFile()));
for (final String key : json.keySet()) {
this.nickMap.put(UUID.fromString(key), GsonComponentSerializer.gson()
.deserializeFromTree(json.get(key)));
}
} catch (final IOException ex) {
logging.error("Error loading nickname data from nicknames.json file", ex);
}
logging.log("Successfully loaded nicknames from Json storage.");
this.loadNicknamesFromJson();
}

// Set debug status
Expand Down Expand Up @@ -187,15 +173,26 @@ public void onEnable() {
}
}

private void loadNicknamesFromJson() {
try {
storage = new JsonStorage();
JsonObject json = (JsonObject) JsonParser.parseReader(new FileReader(HexNicks.core().jsonFile()));
for (final String key : json.keySet()) {
this.nickMap.put(UUID.fromString(key), GsonComponentSerializer.gson()
.deserializeFromTree(json.get(key)));
}
} catch (final IOException ex) {
logging.error("Error loading nickname data from nicknames.json file", ex);
}
logging.log("Successfully loaded nicknames from Json storage.");
}

/**
* Plugin shutdown logic.
*/
@Override
public void onDisable() {
// Disconnect from Sql if necessary
if (sql.isConnected()) {
sql.disconnect();
}

}

/**
Expand Down Expand Up @@ -253,15 +250,6 @@ public static ConfigValues config() {
return config;
}

/**
* Access the plugin's SQL connection if connected.
*
* @return sql manager
*/
public static SqlManager sql() {
return sql;
}

/**
* Check what plugins this plugin has hooked into.
*
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/dev/majek/hexnicks/command/CommandNick.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

// Remove nickname prefix if essentials is hooked
if (HexNicks.hooks().isEssentialsHooked()) {
String nickPrefix = HexNicks.hooks().getEssNickPrefix();
if (nickPrefix != null && plainTextNick.startsWith(nickPrefix)) {
plainTextNick = plainTextNick.substring(nickPrefix.length());
}
}

// Set the nickname to the default color if there's no color specified
nickname = nickname.colorIfAbsent(HexNicks.config().DEFAULT_NICK_COLOR);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}
}

// Remove nickname prefix if essentials is hooked
if (HexNicks.hooks().isEssentialsHooked()) {
String nickPrefix = HexNicks.hooks().getEssNickPrefix();
if (nickPrefix != null && plainTextNick.startsWith(nickPrefix)) {
plainTextNick = plainTextNick.substring(nickPrefix.length());
}
}

// Make sure the nickname isn't too short
if (plainTextNick.length() < minLength) {
Messages.TOO_SHORT.send(sender, minLength);
Expand Down
45 changes: 21 additions & 24 deletions src/main/java/dev/majek/hexnicks/event/PlayerChat.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ public class PlayerChat implements Listener {
public void onChat(final AsyncChatEvent event) {
if (HexNicks.config().CHAT_FORMATTER) {
HexNicks.logging().debug("Original message: " + PlainTextComponentSerializer.plainText().serialize(event.message()));
event.message(formatChat(event.getPlayer(), PlainTextComponentSerializer.plainText().serialize(event.message()), true));
event.renderer((source, sourceDisplayName, message, viewer) ->
formatChat(event.getPlayer(), PlainTextComponentSerializer.plainText().serialize(event.message()))
);
}
}

Expand All @@ -38,7 +40,7 @@ public void onChat(final AsyncChatEvent event) {
* @param message the message
* @return formatted chat
*/
private @NotNull Component formatChat(final @NotNull Player source, final @NotNull String message, boolean justMessage) {
private @NotNull Component formatChat(final @NotNull Player source, final @NotNull String message) {
final MiniMessageWrapper miniMessageWrapper = MiniMessageWrapper.builder()
.advancedTransformations(source.hasPermission("hexnicks.chat.advanced"))
.gradients(source.hasPermission("hexnicks.color.gradient"))
Expand All @@ -48,28 +50,23 @@ public void onChat(final AsyncChatEvent event) {
.removeColors(MiscUtils.blockedColors(source))
.build();

Component ret;
if (justMessage) {
ret = miniMessageWrapper.mmParse(message);
} else {
ret = miniMessageWrapper.mmParse(HexNicks.hooks().applyPlaceHolders(source, HexNicks.config().CHAT_FORMAT))
// Replace display name placeholder with HexNicks nick
.replaceText(TextReplacementConfig.builder().matchLiteral("{displayname}")
.replacement(HexNicks.core().getDisplayName(source)).build()
)
// Replace prefix placeholder with Vault prefix
.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}")
.replacement(HexNicks.hooks().vaultPrefix(source)).build()
)
// Replace suffix placeholder with Vault Suffix
.replaceText(TextReplacementConfig.builder().matchLiteral("{suffix}")
.replacement(HexNicks.hooks().vaultSuffix(source)).build()
)
// Replace message placeholder with the formatted message from the event
.replaceText(TextReplacementConfig.builder().matchLiteral("{message}")
.replacement(miniMessageWrapper.mmParse(message)).build()
);
}
Component ret = miniMessageWrapper.mmParse(HexNicks.hooks().applyPlaceHolders(source, HexNicks.config().CHAT_FORMAT))
// Replace display name placeholder with HexNicks nick
.replaceText(TextReplacementConfig.builder().matchLiteral("{displayname}")
.replacement(HexNicks.core().getDisplayName(source)).build()
)
// Replace prefix placeholder with Vault prefix
.replaceText(TextReplacementConfig.builder().matchLiteral("{prefix}")
.replacement(HexNicks.hooks().vaultPrefix(source)).build()
)
// Replace suffix placeholder with Vault Suffix
.replaceText(TextReplacementConfig.builder().matchLiteral("{suffix}")
.replacement(HexNicks.hooks().vaultSuffix(source)).build()
)
// Replace message placeholder with the formatted message from the event
.replaceText(TextReplacementConfig.builder().matchLiteral("{message}")
.replacement(miniMessageWrapper.mmParse(message)).build()
);
HexNicks.logging().debug("Formatted message: " + PlainTextComponentSerializer.plainText().serialize(ret));
return ret;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,17 @@ final class MiniMessageWrapperImpl implements MiniMessageWrapper {
matcher.appendTail(sb);
mmString = sb.toString();
} else {
mmString = mmString.replaceAll("&#([0-9a-fA-F]{6})", "");
mmString = mmString.replaceAll("&x(&[0-9a-fA-F]){6}", "");
mmString = mmString.replaceAll("[&§]#([0-9a-fA-F]{6})", "");
mmString = mmString.replaceAll("[&§]x(&[0-9a-fA-F]){6}", "");
}

final Pattern legacyCharPattern = Pattern.compile("(?<!\\\\)(&([a-f0-9k-or]))");
final Pattern legacyCharPattern = Pattern.compile("(?<!\\\\)([&§]([a-f0-9k-or]))");
mmString = legacyCharPattern.matcher(mmString).replaceAll((result) -> CHAR_COLORS.get(result.group(2).charAt(0)));

final Pattern escapedLegacyCharPattern = Pattern.compile("(\\\\&([a-f0-9k-or]))");
final Pattern escapedLegacyCharPattern = Pattern.compile("(\\\\[&§]([a-f0-9k-or]))");
mmString = escapedLegacyCharPattern.matcher(mmString).replaceAll((result) -> "&" + result.group(2));
} else {
mmString = mmString.replaceAll("(&[0-9a-fA-Fk-oK-OxXrR])+", "");
mmString = mmString.replaceAll("([&§][0-9a-fA-Fk-oK-OxXrR])+", "");
}

if (!this.gradients) {
Expand Down
Loading

0 comments on commit 06bc25d

Please sign in to comment.