rename AssociationList to NameAPI + rework config

This commit is contained in:
Husky
2025-10-10 16:53:02 -04:00
parent 3735707c60
commit b6a5259a53
4 changed files with 39 additions and 35 deletions

View File

@@ -8,10 +8,12 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
public class AssociationList {
public class NameAPI {
private DataSource db;
private Logger logger;
@@ -22,9 +24,15 @@ public class AssociationList {
private static final String changePlayerName = "update Name_player set player=? where uuid=?";
private static final String getAllPlayerInfo = "select * from Name_player";
public AssociationList(Logger logger, DataSource db) {
this.db = db;
public NameAPI(Logger logger, HikariConfig dbConfig) {
this.logger = logger;
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
this.db = new HikariDataSource(dbConfig);
}
public void migrate() {
@@ -100,7 +108,7 @@ public class AssociationList {
*/
public @Nullable UUID getUUID(String playername) {
try (Connection connection = db.getConnection();
PreparedStatement getUUIDfromPlayer = connection.prepareStatement(AssociationList.getUUIDfromPlayer);) {
PreparedStatement getUUIDfromPlayer = connection.prepareStatement(NameAPI.getUUIDfromPlayer);) {
getUUIDfromPlayer.setString(1, playername);
try (ResultSet set = getUUIDfromPlayer.executeQuery();) {
if (!set.next() || set.wasNull()) return null;
@@ -123,7 +131,7 @@ public class AssociationList {
*/
public @Nullable String getCurrentName(UUID uuid) {
try (Connection connection = db.getConnection();
PreparedStatement getPlayerfromUUID = connection.prepareStatement(AssociationList.getPlayerfromUUID);) {
PreparedStatement getPlayerfromUUID = connection.prepareStatement(NameAPI.getPlayerfromUUID);) {
getPlayerfromUUID.setString(1, uuid.toString());
try (ResultSet set = getPlayerfromUUID.executeQuery();) {
if (!set.next()) return null;
@@ -146,7 +154,7 @@ public class AssociationList {
*/
public void addPlayer(String playername, UUID uuid) {
try (Connection connection = db.getConnection();
PreparedStatement addPlayer = connection.prepareStatement(AssociationList.addPlayer);) {
PreparedStatement addPlayer = connection.prepareStatement(NameAPI.addPlayer);) {
addPlayer.setString(1, playername);
addPlayer.setString(2, uuid.toString());
addPlayer.execute();
@@ -165,7 +173,7 @@ public class AssociationList {
*/
public void changePlayer(String newName, UUID uuid) {
try (Connection connection = db.getConnection();
PreparedStatement changePlayerName = connection.prepareStatement(AssociationList.changePlayerName);) {
PreparedStatement changePlayerName = connection.prepareStatement(NameAPI.changePlayerName);) {
changePlayerName.setString(1, newName);
changePlayerName.setString(2, uuid.toString());
changePlayerName.execute();
@@ -189,7 +197,7 @@ public class AssociationList {
Map<String, UUID> nameMapping = new HashMap<String, UUID>();
Map<UUID, String> uuidMapping = new HashMap<UUID, String>();
try (Connection connection = db.getConnection();
PreparedStatement getAllPlayerInfo = connection.prepareStatement(AssociationList.getAllPlayerInfo);
PreparedStatement getAllPlayerInfo = connection.prepareStatement(NameAPI.getAllPlayerInfo);
ResultSet set = getAllPlayerInfo.executeQuery();) {
while (set.next()) {
UUID uuid = UUID.fromString(set.getString("uuid"));

View File

@@ -13,7 +13,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import javax.sql.DataSource;
import net.civmc.civproxy.renamer.PlayerRenamer;
import org.slf4j.Logger;
import org.spongepowered.configurate.CommentedConfigurationNode;
@@ -27,7 +26,7 @@ public class CivProxyPlugin {
private CommentedConfigurationNode config;
private DataSource source;
private HikariConfig nameAPIConfig;
@Inject
public CivProxyPlugin(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) {
@@ -43,20 +42,15 @@ public class CivProxyPlugin {
@Subscribe
public void onProxyInitialization(ProxyInitializeEvent event) {
loadConnection();
loadNameAPIConfig();
new PlayerCount(this, server).start();
new PlayerRenamer(this, server, source).start();
new PlayerRenamer(this, server, nameAPIConfig).start();
new QueueListener(this, server).start();
}
private void loadConnection() {
private void loadNameAPIConfig() {
CommentedConfigurationNode database = config.node("database");
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:" + database.node("driver").getString("mariadb") + "://" + database.node("host").getString("localhost") + ":" +
database.node("port").getInt(3306) + "/" + database.node("database").getString("minecraft"));
@@ -69,7 +63,7 @@ public class CivProxyPlugin {
if (password != null && !password.isBlank()) {
config.setPassword(password);
}
this.source = new HikariDataSource(config);
this.nameAPIConfig = config;
}
/**

View File

@@ -3,18 +3,18 @@ package net.civmc.civproxy.renamer;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.proxy.ProxyServer;
import net.civmc.nameApi.AssociationList;
import net.civmc.nameApi.NameAPI;
import net.kyori.adventure.text.Component;
import java.util.UUID;
public class ChangePlayerNameCommand implements SimpleCommand {
private final ProxyServer server;
private final AssociationList associationList;
private final NameAPI nameAPI;
public ChangePlayerNameCommand(ProxyServer server, AssociationList associationList) {
public ChangePlayerNameCommand(ProxyServer server, NameAPI nameAPI) {
this.server = server;
this.associationList = associationList;
this.nameAPI = nameAPI;
}
@Override
@@ -26,13 +26,13 @@ public class ChangePlayerNameCommand implements SimpleCommand {
return;
}
UUID uuid = associationList.getUUID(args[0]);
UUID uuid = nameAPI.getUUID(args[0]);
if (uuid == null) {
source.sendPlainMessage("Player not found");
return;
}
String newName = args[1].length() >= 16 ? args[1].substring(0, 16) : args[1];
associationList.changePlayer(newName, uuid);
nameAPI.changePlayer(newName, uuid);
source.sendPlainMessage("Changed name of " + args[0] + " to " + newName);
server.getPlayer(uuid).ifPresent(player ->

View File

@@ -5,40 +5,42 @@ import com.velocitypowered.api.event.player.GameProfileRequestEvent;
import com.velocitypowered.api.proxy.ProxyServer;
import com.velocitypowered.api.util.GameProfile;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariConfig;
import net.civmc.civproxy.CivProxyPlugin;
import net.civmc.nameApi.AssociationList;
import net.civmc.nameApi.NameAPI;
public class PlayerRenamer {
private final CivProxyPlugin plugin;
private final ProxyServer server;
private final AssociationList associations;
private final NameAPI nameAPI;
public PlayerRenamer(CivProxyPlugin plugin, ProxyServer server, DataSource source) {
public PlayerRenamer(CivProxyPlugin plugin, ProxyServer server, HikariConfig nameAPIConfig) {
this.plugin = plugin;
this.server = server;
this.associations = new AssociationList(plugin.getLogger(), source);
this.nameAPI = new NameAPI(plugin.getLogger(), nameAPIConfig);
}
@Subscribe
public void on(GameProfileRequestEvent requestEvent) {
GameProfile profile = requestEvent.getGameProfile();
associations.addPlayer(profile.getName(), profile.getId());
nameAPI.addPlayer(profile.getName(), profile.getId());
String name = associations.getCurrentName(profile.getId());
String name = nameAPI.getCurrentName(profile.getId());
if (name == null) {
associations.addPlayer(profile.getName(), profile.getId());
name = associations.getCurrentName(profile.getId());
// shouldn't the above call have added them?
nameAPI.addPlayer(profile.getName(), profile.getId());
name = nameAPI.getCurrentName(profile.getId());
}
requestEvent.setGameProfile(requestEvent.getGameProfile().withName(name));
}
public void start() {
associations.migrate();
nameAPI.migrate();
server.getEventManager().register(plugin, this);
server.getCommandManager().register(server.getCommandManager().metaBuilder("changeplayername").aliases("nlcpn").plugin(plugin).build(),
new ChangePlayerNameCommand(server, associations));
new ChangePlayerNameCommand(server, nameAPI));
}
}