Skip to content

Commit

Permalink
v3.0.4 - Logging Is A Feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Majekdor committed Aug 17, 2022
1 parent 5503812 commit f181ac4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Plugin Changelog

# 3.0.4 - Logging Is A Feature

- Added a lot of logging when in debug mode.

This logging was meant to help find an issue... then the issue fixed itself. The logging could still be helpful in finding future issues, though.

# 3.0.3 - Small Patches

- Fixed bug with messages on `/nickother`.
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.3</version>
<version>3.0.4</version>
<packaging>jar</packaging>

<name>HexNicks</name>
Expand Down Expand Up @@ -50,7 +50,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.0</version>
<version>3.4.1</version>
<executions>
<execution>
<id>attach-javadocs</id>
Expand Down Expand Up @@ -186,7 +186,7 @@
<dependency>
<groupId>net.essentialsx</groupId>
<artifactId>EssentialsX</artifactId>
<version>2.19.4</version>
<version>2.19.6</version>
<scope>provided</scope>
<exclusions>
<exclusion>
Expand Down
1 change: 1 addition & 0 deletions src/main/java/dev/majek/hexnicks/HexNicks.java
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ public void setNick(@NotNull Player player, @NotNull Component nick) {
* @param player the player whose nickname to remove
*/
public void removeNick(@NotNull Player player) {
logging.debug("Removing " + player.getName() + "'s nickname.");
this.nickMap.remove(player.getUniqueId());
player.displayName(Component.text(player.getName()));
if (config.TAB_NICKS) {
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/dev/majek/hexnicks/event/PlayerJoin.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ public void onPlayerJoin(PlayerJoinEvent event) {
// Set the joining player's nickname to their stored nickname if they have one
HexNicks.storage().hasNick(player.getUniqueId()).whenCompleteAsync((aBoolean, throwable) -> {
if (aBoolean) {
HexNicks.logging().debug("Player " + player.getName() + " joined and has nickname, setting...");
HexNicks.storage().getNick(player.getUniqueId()).whenCompleteAsync((component, throwable1) ->
HexNicks.core().setNick(player, component)
);
} else {
HexNicks.logging().debug("Player " + player.getName() + " joined and has no nickname.");
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/majek/hexnicks/storage/SqlManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void connect() throws SQLException {
if (!isConnected()) {
connection = DriverManager.getConnection("jdbc:mysql://" +
host + ":" + port + "/" + database + "?useSSL=" + useSSL + "&autoReconnect=" + autoReconnect, username, password);
HexNicks.logging().debug("Connecting to sql database '" + database + "'...");
}
Bukkit.getScheduler().scheduleSyncRepeatingTask(HexNicks.core(), () -> HexNicks.storage().updateNicks(), 200L, updateInterval * 20L);
}
Expand All @@ -74,6 +75,7 @@ public void connect() throws SQLException {
public void disconnect() {
if (isConnected()) {
try {
HexNicks.logging().debug("Disconnecting from sql database...");
connection.close();
} catch (SQLException e) {
e.printStackTrace();
Expand All @@ -94,12 +96,14 @@ public Connection getConnection() {
* Create the MySQL table if it doesn't already exist.
*/
public void createTable() {
HexNicks.logging().debug("Creating sql database table...");
PreparedStatement ps;
try {
ps = HexNicks.sql().getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS " +
"nicknameTable (uniqueId VARCHAR(100),nickname VARCHAR(10000),PRIMARY KEY (uniqueId))");
ps.executeUpdate();
} catch (SQLException ex) {
HexNicks.logging().error("Error creating table in database", ex);
ex.printStackTrace();
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/dev/majek/hexnicks/storage/SqlStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
Expand All @@ -46,6 +45,7 @@ public class SqlStorage implements StorageMethod {

@Override
public CompletableFuture<Boolean> hasNick(@NotNull UUID uuid) {
HexNicks.logging().debug("Firing SqlStorage#hasNick for uuid: " + uuid);
return CompletableFuture.supplyAsync(() -> {
try {
PreparedStatement ps = HexNicks.sql().getConnection()
Expand All @@ -57,9 +57,11 @@ public CompletableFuture<Boolean> hasNick(@NotNull UUID uuid) {
nickname = resultSet.getString("nickname");
return nickname != null;
} else {
HexNicks.logging().debug("No nickname found in database for uuid: " + uuid);
return false;
}
} catch (SQLException ex) {
HexNicks.logging().error("Error with SqlStorage#hasNick", ex);
ex.printStackTrace();
return false;
}
Expand All @@ -69,6 +71,7 @@ public CompletableFuture<Boolean> hasNick(@NotNull UUID uuid) {
@Override
@SuppressWarnings("ConstantConditions")
public CompletableFuture<Component> getNick(@NotNull UUID uuid) {
HexNicks.logging().debug("Firing SqlStorage#getNick for uuid: " + uuid);
return CompletableFuture.supplyAsync(() -> {
try {
PreparedStatement ps = HexNicks.sql().getConnection()
Expand All @@ -78,48 +81,59 @@ public CompletableFuture<Component> getNick(@NotNull UUID uuid) {
String nickname;
if (resultSet.next()) {
nickname = resultSet.getString("nickname");
HexNicks.logging().debug("Nickname found for uuid: " + uuid);
return GsonComponentSerializer.gson().deserialize(nickname);
} else {
HexNicks.logging().debug("No nickname found in database for uuid: " + uuid);
}
} catch (SQLException ex) {
HexNicks.logging().error("Error with SqlStorage#getNick", ex);
ex.printStackTrace();
}
HexNicks.logging().debug("Returning player's username as nickname");
return Component.text(Bukkit.getOfflinePlayer(uuid).getName());
});
}

@Override
public void removeNick(@NotNull UUID uuid) {
HexNicks.logging().debug("Firing SqlStorage#removeNick...");
Bukkit.getScheduler().runTaskAsynchronously(HexNicks.core(), () -> {
try {
PreparedStatement ps = HexNicks.sql().getConnection()
.prepareStatement("DELETE FROM nicknameTable WHERE uniqueId=?");
ps.setString(1, uuid.toString());
ps.executeUpdate();
} catch (SQLException ex) {
HexNicks.logging().error("Error with SqlStorage#removeNick", ex);
ex.printStackTrace();
}
});
}

@Override
public void saveNick(@NotNull Player player, @NotNull Component nickname) {
HexNicks.logging().debug("Firing SqlStorage#saveNick...");
Bukkit.getScheduler().runTaskAsynchronously(HexNicks.core(), () ->
hasNick(player.getUniqueId()).whenCompleteAsync((hasNick, throwable) -> {
try {
PreparedStatement update;
if (hasNick) {
HexNicks.logging().debug("Has nick already, updating table...");
update = HexNicks.sql().getConnection()
.prepareStatement("UPDATE nicknameTable SET nickname=? WHERE uniqueId=?");
update.setString(1, GsonComponentSerializer.gson().serialize(nickname));
update.setString(2, player.getUniqueId().toString());
} else {
HexNicks.logging().debug("No nick found, inserting into table...");
update = HexNicks.sql().getConnection()
.prepareStatement("INSERT INTO `nicknameTable` (`uniqueId`, `nickname`) VALUES (?, ?);");
update.setString(1, player.getUniqueId().toString());
update.setString(2, GsonComponentSerializer.gson().serialize(nickname));
}
update.executeUpdate();
} catch (SQLException ex) {
HexNicks.logging().error("Error with SqlStorage#saveNick", ex);
ex.printStackTrace();
}
})
Expand All @@ -128,6 +142,7 @@ public void saveNick(@NotNull Player player, @NotNull Component nickname) {

@Override
public CompletableFuture<Boolean> nicknameExists(@NotNull Component nickname, boolean strict, @NotNull Player player) {
HexNicks.logging().debug("Firing SqlStorage#nicknameExists...");
return CompletableFuture.supplyAsync(() -> {
try {
// Add all player names except the player setting the nickname
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public interface StorageMethod {
* Update the nickname of all online players from storage.
*/
default void updateNicks() {
HexNicks.logging().debug("Firing StorageMethod#updateNicks...");
for (Player player : Bukkit.getOnlinePlayers()) {
hasNick(player.getUniqueId()).whenCompleteAsync((aBoolean, throwable) -> {
if (aBoolean) {
Expand Down

0 comments on commit f181ac4

Please sign in to comment.