saving work

won't compile! just saving for the night.
This commit is contained in:
Gxorge committed 2023-05-12 22:24:06 +01:00
1 parent 937e846afc
commit 898ad6e545
5 files changed
+176 -3

No files matched your search

@@ -2,6 +2,7 @@ package uk.hotten.staffog.data;
import lombok.Getter;
import org.bukkit.plugin.java.JavaPlugin;
import uk.hotten.staffog.punish.data.PunishType;
import uk.hotten.staffog.utils.Console;
import java.sql.*;
@@ -24,8 +25,8 @@ public class DatabaseManager {
database = plugin.getConfig().getString("sqlDatabase");
port = plugin.getConfig().getInt("sqlPort");
checkPunishmentTable("staffog_bans");
checkPunishmentTable("staffog_mutes");
for (PunishType pt : PunishType.values()) checkPunishmentTable(pt.getTable());
checkNameHistoryTable();
}
public Connection createConnection() throws SQLException, ClassNotFoundException {
@@ -36,7 +37,7 @@ public class DatabaseManager {
}
private void checkPunishmentTable(String table) {
try(Connection connection = createConnection()) {
try (Connection connection = createConnection()) {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, table, new String[] {"TABLE"});
@@ -71,4 +72,33 @@ public class DatabaseManager {
}
}
private void checkNameHistoryTable() {
try (Connection connection = createConnection()) {
DatabaseMetaData meta = connection.getMetaData();
ResultSet resultSet = meta.getTables(null, null, "staffog_history", new String[] {"TABLE"});
if (resultSet.next()) {
return;
}
// Table does not exist
Statement statement = connection.createStatement();
String sql = "CREATE TABLE `staffog_history` (\n" +
" `id` int NOT NULL AUTO_INCREMENT,\n" +
" `date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,\n" +
" `name` varchar(16) NOT NULL,\n" +
" `uuid` varchar(36) NOT NULL,\n" +
" PRIMARY KEY (`id`)\n" +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4";
statement.executeUpdate(sql);
Console.info("Created 'staffog_history' table, was missing.");
} catch (Exception e) {
Console.error("Failed to check staffog_history table.");
e.printStackTrace();
}
}
}
@@ -1,6 +1,15 @@
package uk.hotten.staffog.punish;
import lombok.Getter;
import uk.hotten.staffog.data.DatabaseManager;
import uk.hotten.staffog.punish.data.PunishEntry;
import uk.hotten.staffog.utils.Console;
import uk.hotten.staffog.utils.Message;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.UUID;
public class PunishmentManager {
@@ -10,4 +19,55 @@ public class PunishmentManager {
instance = this;
}
public void checkNameToUuid(String name, UUID uuid) {
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
PreparedStatement statement = connection.prepareStatement("SELECT `id` FROM `staffog_history` WHERE `uuid`=?, `name`=?");
statement.setString(1, uuid.toString());
statement.setString(2, name);
ResultSet rs = statement.executeQuery();
boolean result = rs.next();
if (result)
return;
statement = connection.prepareStatement("INSERT INTO `staffog_history` (`uuid`, `name`) VALUE (?, ?)");
statement.setString(1, uuid.toString());
statement.setString(2, name);
statement.executeUpdate();
} catch (Exception e) {
Console.error("Failed to check username to uuid.");
e.printStackTrace();
}
}
public String getNameFromUUID(UUID uuid) {
}
public void newPunishment(PunishEntry entry) {
try (Connection connection = DatabaseManager.getInstance().createConnection()) {
PreparedStatement statement = connection.prepareStatement(
"INSERT INTO `" + entry.getType().getTable() + "` (`uuid`, `reason`, `by_uuid`, `by_name`, `time`, `until`, `active`) VALUES (?, ?, ?, ?, ?, ?, ?)"
);
statement.setString(1, entry.getUuid().toString());
statement.setString(2, entry.getReason());
statement.setString(3, entry.getByUuid().toString());
statement.setString(4, entry.getByName());
statement.setInt(5, entry.getTime());
statement.setInt(6, entry.getUntil());
statement.setString(7, entry.isActive() ? "1" : "0");
statement.executeUpdate();
Message.broadcast(Message.format());
} catch (Exception e) {
Console.error("Failed to create punishment.");
e.printStackTrace();
}
}
}
@@ -0,0 +1,49 @@
package uk.hotten.staffog.punish.data;
import lombok.Getter;
import java.util.UUID;
public class PunishEntry {
@Getter private PunishType type;
@Getter private UUID uuid;
@Getter private String reason;
@Getter private UUID byUuid;
@Getter private String byName;
@Getter private UUID removedUuid;
@Getter private String removedName;
@Getter private int time;
@Getter private int until;
@Getter private boolean active;
public PunishEntry(PunishType type, UUID uuid, String reason, UUID byUuid, String byName, int time, int until, boolean active) {
this.type = type;
this.uuid = uuid;
this.reason = reason;
this.byUuid = byUuid;
this.byName = byName;
this.time = time;
this.until = until;
this.active = active;
}
public PunishEntry(PunishType type, UUID uuid, String reason, UUID byUuid, String byName, UUID removedUuid, String removedName, int time, int until, boolean active) {
this.type = type;
this.uuid = uuid;
this.reason = reason;
this.byUuid = byUuid;
this.byName = byName;
this.removedUuid = removedUuid;
this.removedName = reason;
this.time = time;
this.until = until;
this.active = active;
}
}
@@ -0,0 +1,16 @@
package uk.hotten.staffog.punish.data;
import lombok.Getter;
public enum PunishType {
BAN("staffog_ban"),
MUTE("staffog_mute");
@Getter private String table;
private PunishType(String table) {
this.table = table;
}
}
@@ -0,0 +1,18 @@
package uk.hotten.staffog.utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
public class Message {
public static String format(String message) {
return "" + ChatColor.GRAY + "[" + ChatColor.YELLOW + "Staff-OG" + ChatColor.GRAY + "] " + ChatColor.RESET + message;
}
public static void broadcast(String message, String permission) {
Bukkit.getServer().getOnlinePlayers().forEach(p -> {
if (p.hasPermission(permission)) p.sendMessage(message);
});
}
}