add mysql support
This commit is contained in:
2
pom.xml
2
pom.xml
@ -5,7 +5,7 @@
|
||||
|
||||
<groupId>com.sophiaatkinson</groupId>
|
||||
<artifactId>PronounsPlaceholder</artifactId>
|
||||
<version>1.0.2</version>
|
||||
<version>1.0.3</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>PronounsPlaceholder</name>
|
||||
|
@ -10,6 +10,7 @@ import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@ -18,6 +19,7 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class PronounsPlugin extends JavaPlugin {
|
||||
private Connection connection;
|
||||
private boolean usingMySQL;
|
||||
private List<String> blockedPronouns;
|
||||
private List<String> pronounSuggestions;
|
||||
private final List<String> errorLog = new ArrayList<>();
|
||||
@ -53,15 +55,27 @@ public class PronounsPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
private void initDatabase() {
|
||||
try {
|
||||
File dbFile = new File(getDataFolder(), "pronouns.db");
|
||||
if (!getDataFolder().exists()) getDataFolder().mkdirs();
|
||||
usingMySQL = getConfig().getBoolean("mysql.enabled", false);
|
||||
|
||||
String url = "jdbc:sqlite:" + dbFile.getAbsolutePath();
|
||||
connection = DriverManager.getConnection(url);
|
||||
try {
|
||||
if (usingMySQL) {
|
||||
String host = getConfig().getString("mysql.host");
|
||||
int port = getConfig().getInt("mysql.port");
|
||||
String database = getConfig().getString("mysql.database");
|
||||
String user = getConfig().getString("mysql.user");
|
||||
String password = getConfig().getString("mysql.password");
|
||||
|
||||
String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?useSSL=false&autoReconnect=true";
|
||||
connection = DriverManager.getConnection(url, user, password);
|
||||
} else {
|
||||
File dbFile = new File(getDataFolder(), "pronouns.db");
|
||||
if (!getDataFolder().exists()) getDataFolder().mkdirs();
|
||||
String url = "jdbc:sqlite:" + dbFile.getAbsolutePath();
|
||||
connection = DriverManager.getConnection(url);
|
||||
}
|
||||
|
||||
Statement stmt = connection.createStatement();
|
||||
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS pronouns (uuid TEXT PRIMARY KEY, pronouns TEXT);");
|
||||
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS pronouns (uuid VARCHAR(36) PRIMARY KEY, pronouns TEXT);");
|
||||
} catch (SQLException e) {
|
||||
errorLog.add("Database initialization error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
@ -81,9 +95,7 @@ public class PronounsPlugin extends JavaPlugin {
|
||||
}
|
||||
|
||||
for (String blocked : blockedPronouns) {
|
||||
if (pronouns.contains(blocked)) {
|
||||
return;
|
||||
}
|
||||
if (pronouns.contains(blocked)) return;
|
||||
}
|
||||
|
||||
try (PreparedStatement ps = connection.prepareStatement("REPLACE INTO pronouns (uuid, pronouns) VALUES (?, ?)")) {
|
||||
@ -91,6 +103,18 @@ public class PronounsPlugin extends JavaPlugin {
|
||||
ps.setString(2, pronouns);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
// Fallback for MySQL since REPLACE INTO isn't ideal for all cases
|
||||
if (usingMySQL) {
|
||||
try (PreparedStatement ps = connection.prepareStatement("INSERT INTO pronouns (uuid, pronouns) VALUES (?, ?) ON DUPLICATE KEY UPDATE pronouns = VALUES(pronouns)")) {
|
||||
ps.setString(1, uuid.toString());
|
||||
ps.setString(2, pronouns);
|
||||
ps.executeUpdate();
|
||||
return;
|
||||
} catch (SQLException ex) {
|
||||
errorLog.add("MySQL upsert error: " + ex.getMessage());
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
errorLog.add("Database update error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -109,18 +133,18 @@ public class PronounsPlugin extends JavaPlugin {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public Map<UUID, String> getAllPronouns() {
|
||||
Map<UUID, String> result = new java.util.HashMap<>();
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
ResultSet rs = stmt.executeQuery("SELECT uuid, pronouns FROM pronouns");
|
||||
while (rs.next()) {
|
||||
result.put(UUID.fromString(rs.getString("uuid")), rs.getString("pronouns"));
|
||||
public Map<UUID, String> getAllPronouns() {
|
||||
Map<UUID, String> result = new HashMap<>();
|
||||
try (Statement stmt = connection.createStatement()) {
|
||||
ResultSet rs = stmt.executeQuery("SELECT uuid, pronouns FROM pronouns");
|
||||
while (rs.next()) {
|
||||
result.put(UUID.fromString(rs.getString("uuid")), rs.getString("pronouns"));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
errorLog.add("Database fetch all error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
errorLog.add("Database fetch all error: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
return result;
|
||||
return result;
|
||||
}
|
||||
|
||||
public void generateDebugFile() {
|
||||
@ -133,6 +157,7 @@ public Map<UUID, String> getAllPronouns() {
|
||||
writer.write("=== PronounsPlugin Debug Report ===\n");
|
||||
writer.write("Plugin Enabled: " + isEnabled() + "\n");
|
||||
writer.write("Database Connected: " + (connection != null) + "\n");
|
||||
writer.write("Using MySQL: " + usingMySQL + "\n");
|
||||
writer.write("Blocked Pronouns: " + blockedPronouns + "\n");
|
||||
writer.write("Pronoun Suggestions: " + pronounSuggestions + "\n");
|
||||
writer.write("Error Log:\n");
|
||||
|
@ -27,12 +27,18 @@ debug: true
|
||||
|
||||
generate-debug-file: true
|
||||
|
||||
database:
|
||||
use-sqlite: true
|
||||
mysql:
|
||||
enabled: false # Setting to false uses SQLite
|
||||
host: localhost
|
||||
port: 3306
|
||||
database: example
|
||||
user: example
|
||||
password: example
|
||||
|
||||
admin-permissions:
|
||||
- "pronouns.admin"
|
||||
- "pronouns.override"
|
||||
- "pronouns.reload"
|
||||
|
||||
player-permissions:
|
||||
- "pronouns.set"
|
||||
|
@ -1,7 +1,9 @@
|
||||
name: PronounsPlaceholder
|
||||
version: 1.0.2
|
||||
version: 1.0.3
|
||||
main: com.sophiaatkinson.pronouns.PronounsPlugin
|
||||
api-version: 1.16
|
||||
authors: [Sophia Atkinson]
|
||||
website: https://sophia.wtf
|
||||
commands:
|
||||
pronouns:
|
||||
description: Manage pronouns for players
|
||||
@ -19,6 +21,9 @@ commands:
|
||||
pronouns.admin:
|
||||
description: Allows admin to list, set, remove pronouns
|
||||
default: op
|
||||
pronouns.reload:
|
||||
description: Reloads plugin configuration
|
||||
default: op
|
||||
aliases:
|
||||
- mypronouns
|
||||
- setpronouns
|
||||
|
Reference in New Issue
Block a user