forked from never2nv/NVus_Prison
Finishing Up Gangs and Database Crapola
This commit is contained in:
parent
52cd391c18
commit
5db131ccab
|
@ -20,6 +20,10 @@ public class ConfigManager {
|
|||
this.configFiles = new HashMap<>();
|
||||
}
|
||||
|
||||
public File getDataFolder() {
|
||||
return plugin.getDataFolder();
|
||||
}
|
||||
|
||||
public void reloadConfig(String configName) {
|
||||
File configFile = getConfigFile(configName);
|
||||
FileConfiguration config = YamlConfiguration.loadConfiguration(configFile);
|
||||
|
|
|
@ -8,81 +8,69 @@ import java.sql.SQLException;
|
|||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
import java.io.File;
|
||||
|
||||
import me.nvus.nvus_prison_setup.Configs.ConfigManager;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import me.nvus.nvus_prison_setup.Configs.ConfigManager;
|
||||
|
||||
public class DatabaseManager {
|
||||
|
||||
private ConfigManager configManager;
|
||||
private String url;
|
||||
private String databaseType; // To store the database type (MySQL or SQLite)
|
||||
|
||||
public DatabaseManager(ConfigManager configManager) {
|
||||
this.configManager = configManager;
|
||||
loadDatabaseConfig();
|
||||
initializeDatabase();
|
||||
setupDatabase();
|
||||
}
|
||||
|
||||
// public DatabaseManager(File dataFolder) {
|
||||
// // Construct the file path for the SQLite database
|
||||
// File databaseFile = new File(dataFolder, "nvus_prison.db");
|
||||
// this.url = "jdbc:sqlite:" + databaseFile.getAbsolutePath();
|
||||
// initializeDatabase();
|
||||
// }
|
||||
|
||||
public DatabaseManager() {
|
||||
initializeDatabase();
|
||||
}
|
||||
|
||||
private void loadDatabaseConfig() {
|
||||
private void setupDatabase() {
|
||||
FileConfiguration config = configManager.getConfig("config.yml");
|
||||
String host = config.getString("host", "localhost");
|
||||
int port = config.getInt("port", 3306);
|
||||
String database = config.getString("database", "nvus_prison");
|
||||
String username = config.getString("username", "root");
|
||||
String password = config.getString("password", "");
|
||||
this.databaseType = config.getString("Database.Type", "MySQL");
|
||||
|
||||
// Adjust this URL format according to your DBMS (MySQL in this case)
|
||||
this.url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password;
|
||||
if ("SQLite".equalsIgnoreCase(databaseType)) {
|
||||
File dbFile = new File(configManager.getDataFolder(), "nvus_prison.db");
|
||||
this.url = "jdbc:sqlite:" + dbFile.getAbsolutePath();
|
||||
} else {
|
||||
// Default to MySQL
|
||||
String host = config.getString("host", "localhost");
|
||||
int port = config.getInt("port", 3306);
|
||||
String database = config.getString("database", "nvus_prison");
|
||||
String username = config.getString("username", "root");
|
||||
String password = config.getString("password", "");
|
||||
this.url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password;
|
||||
}
|
||||
|
||||
// Attempt to initialize the database structure
|
||||
initializeDatabase();
|
||||
}
|
||||
|
||||
private Connection connect() {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DriverManager.getConnection(url);
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
return conn;
|
||||
private Connection connect() throws SQLException {
|
||||
return DriverManager.getConnection(url);
|
||||
}
|
||||
|
||||
private void initializeDatabase() {
|
||||
// SQL statement for creating gangs table
|
||||
String sqlGangs = "CREATE TABLE IF NOT EXISTS gangs ("
|
||||
+ " id INTEGER PRIMARY KEY,"
|
||||
+ " name TEXT NOT NULL,"
|
||||
+ " owner_uuid TEXT NOT NULL"
|
||||
+ "id INTEGER PRIMARY KEY " + (databaseType.equalsIgnoreCase("SQLite") ? "AUTOINCREMENT" : "AUTO_INCREMENT") + ","
|
||||
+ "name TEXT NOT NULL,"
|
||||
+ "owner_uuid TEXT NOT NULL"
|
||||
+ ");";
|
||||
|
||||
// SQL statement for creating members table
|
||||
String sqlMembers = "CREATE TABLE IF NOT EXISTS members ("
|
||||
+ " uuid TEXT PRIMARY KEY,"
|
||||
+ " username TEXT NOT NULL,"
|
||||
+ " gang_id INTEGER NOT NULL,"
|
||||
+ " rank TEXT NOT NULL,"
|
||||
+ " FOREIGN KEY (gang_id) REFERENCES gangs(id)"
|
||||
+ "uuid TEXT PRIMARY KEY,"
|
||||
+ "username TEXT NOT NULL,"
|
||||
+ "gang_id INTEGER NOT NULL,"
|
||||
+ "rank TEXT NOT NULL,"
|
||||
+ "FOREIGN KEY (gang_id) REFERENCES gangs(id)"
|
||||
+ ");";
|
||||
|
||||
try (Connection conn = connect();
|
||||
Statement stmt = conn.createStatement()) {
|
||||
// Create the gangs and members tables
|
||||
try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
|
||||
stmt.execute(sqlGangs);
|
||||
stmt.execute(sqlMembers);
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
System.out.println("Error initializing database: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Public Accessor to initialize the database
|
||||
public void initDatabase() {
|
||||
initializeDatabase();
|
||||
|
@ -101,6 +89,19 @@ public class DatabaseManager {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean removeGang(String gangName) {
|
||||
String sql = "DELETE FROM gangs WHERE name = ?";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
pstmt.setString(1, gangName);
|
||||
int affectedRows = pstmt.executeUpdate();
|
||||
return affectedRows > 0;
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean addMember(String uuid, String username, int gangId, String rank) {
|
||||
String sql = "INSERT INTO members(uuid, username, gang_id, rank) VALUES(?,?,?,?)";
|
||||
try (Connection conn = this.connect();
|
||||
|
@ -255,5 +256,19 @@ public class DatabaseManager {
|
|||
}
|
||||
}
|
||||
|
||||
public boolean removeMembersByGangId(int gangId) {
|
||||
String sql = "DELETE FROM members WHERE gang_id = ?";
|
||||
|
||||
try (Connection conn = this.connect(); PreparedStatement pstmt = conn.prepareStatement(sql)) {
|
||||
pstmt.setInt(1, gangId);
|
||||
int affectedRows = pstmt.executeUpdate();
|
||||
return affectedRows > 0;
|
||||
} catch (SQLException e) {
|
||||
System.out.println(e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -44,8 +44,12 @@ public class GangCommands implements CommandExecutor {
|
|||
return handleGangLeave(player, player.getUniqueId());
|
||||
case "promote":
|
||||
return handleGangPromote(sender, args);
|
||||
case "demote":
|
||||
return handleGangDemote(sender, args);
|
||||
case "kick":
|
||||
return handleGangKick(sender, args);
|
||||
case "disband":
|
||||
return handleGangDisband(sender, args);
|
||||
default:
|
||||
player.sendMessage(ChatColor.RED + "Invalid gang command. Use /gang help for a list of commands.");
|
||||
return true;
|
||||
|
@ -85,6 +89,54 @@ public class GangCommands implements CommandExecutor {
|
|||
return true;
|
||||
}
|
||||
|
||||
private boolean handleGangDemote(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "This command can only be executed by a player.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
|
||||
if (args.length != 3) {
|
||||
sender.sendMessage(ChatColor.RED + "Usage: /gang demote <playerName>");
|
||||
return true;
|
||||
}
|
||||
|
||||
String targetPlayerName = args[2];
|
||||
Player targetPlayer = player.getServer().getPlayer(targetPlayerName);
|
||||
|
||||
if (targetPlayer == null) {
|
||||
sender.sendMessage(ChatColor.RED + "Player " + targetPlayerName + " not found.");
|
||||
return true;
|
||||
}
|
||||
|
||||
UUID playerUuid = player.getUniqueId();
|
||||
UUID targetPlayerUuid = targetPlayer.getUniqueId();
|
||||
String gangName = gangManager.getCurrentGangName(playerUuid);
|
||||
|
||||
if (gangName == null) {
|
||||
player.sendMessage(ChatColor.RED + "You are not part of a gang.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Check if the player has the authority to demote members in the gang
|
||||
if (!gangManager.canKickOrPromote(playerUuid, gangName)) {
|
||||
player.sendMessage(ChatColor.RED + "You do not have the permission to demote members in the gang.");
|
||||
return true;
|
||||
}
|
||||
|
||||
// Attempt to demote the member
|
||||
boolean success = gangManager.demoteMember(playerUuid, targetPlayerUuid, gangName);
|
||||
if (success) {
|
||||
player.sendMessage(ChatColor.GREEN + "Successfully demoted " + targetPlayer.getName() + " in the gang.");
|
||||
targetPlayer.sendMessage(ChatColor.YELLOW + "You have been demoted in the gang " + gangName + ".");
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "Failed to demote " + targetPlayer.getName() + ".");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean handleGangKick(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage("Only players can execute this command.");
|
||||
|
@ -118,6 +170,30 @@ public class GangCommands implements CommandExecutor {
|
|||
return true;
|
||||
}
|
||||
|
||||
private boolean handleGangDisband(CommandSender sender, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
sender.sendMessage(ChatColor.RED + "Only players can use this command.");
|
||||
return true;
|
||||
}
|
||||
|
||||
Player player = (Player) sender;
|
||||
String gangName = gangManager.getCurrentGangName(player.getUniqueId());
|
||||
|
||||
if (gangName == null) {
|
||||
player.sendMessage(ChatColor.RED + "You are not in a gang.");
|
||||
return true;
|
||||
}
|
||||
|
||||
if (gangManager.disbandGang(gangName, player.getUniqueId())) {
|
||||
player.sendMessage(ChatColor.GREEN + "Your gang has been successfully disbanded.");
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "Failed to disband your gang. You must be the gang owner.");
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private void sendGangCommandHelp(Player player) {
|
||||
StringBuilder message = new StringBuilder();
|
||||
message.append(ChatColor.GREEN).append("\n");
|
||||
|
|
|
@ -32,6 +32,19 @@ public class GangManager {
|
|||
return currentRank;
|
||||
}
|
||||
|
||||
public String getPreviousRank(String currentRank) {
|
||||
try {
|
||||
GangRank current = GangRank.valueOf(currentRank.toUpperCase());
|
||||
int prevOrdinal = current.ordinal() - 1;
|
||||
if (prevOrdinal >= 0) {
|
||||
return GangRank.values()[prevOrdinal].name();
|
||||
}
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Handle invalid rank input
|
||||
}
|
||||
return currentRank; // Return current rank if it's already the lowest or not found
|
||||
}
|
||||
|
||||
public boolean createGang(String gangName, Player owner) {
|
||||
if (dbManager.getGangIdByName(gangName) != null) return false;
|
||||
dbManager.createGang(gangName, owner.getUniqueId().toString());
|
||||
|
@ -85,6 +98,28 @@ public class GangManager {
|
|||
return dbManager.updateMemberRank(targetUuid, gangName, newRank);
|
||||
}
|
||||
|
||||
public boolean demoteMember(UUID playerUuid, UUID targetUuid, String gangName) {
|
||||
if (!canKickOrPromote(playerUuid, gangName)) return false;
|
||||
String currentRank = dbManager.getMemberRank(targetUuid, gangName);
|
||||
String newRank = getPreviousRank(currentRank);
|
||||
if (newRank.equals(currentRank)) return false; // Prevent demotion if already at lowest rank
|
||||
return dbManager.updateMemberRank(targetUuid, gangName, newRank);
|
||||
}
|
||||
|
||||
public boolean disbandGang(String gangName, UUID ownerUuid) {
|
||||
// Ensure the requester is the gang owner
|
||||
Integer gangId = dbManager.getGangIdByName(gangName);
|
||||
String ownerUuidString = ownerUuid.toString();
|
||||
if (gangId != null && dbManager.getGangIdByOwnerUuid(ownerUuidString).equals(gangId)) {
|
||||
// First, remove all members from the gang to maintain referential integrity
|
||||
dbManager.removeMembersByGangId(gangId);
|
||||
// Then, remove the gang itself
|
||||
return dbManager.removeGang(gangName);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public boolean leaveGang(UUID playerUuid, String gangName) {
|
||||
return dbManager.removeMember(playerUuid, gangName);
|
||||
}
|
||||
|
|
|
@ -39,26 +39,21 @@ public final class PrisonSetup extends JavaPlugin {
|
|||
// Initialize the ConfigManager
|
||||
configManager = new ConfigManager(this);
|
||||
|
||||
// Get the plugin's data folder
|
||||
File dataFolder = getDataFolder();
|
||||
|
||||
// Initialize the DatabaseManager with the plugin's data folder
|
||||
DatabaseManager databaseManager = new DatabaseManager(configManager);
|
||||
// Initialize the DatabaseManager with ConfigManager
|
||||
dbManager = new DatabaseManager(configManager); // Correctly assign to the class field
|
||||
|
||||
// Initialize the GangManager with the DatabaseManager
|
||||
gangManager = new GangManager(dbManager);
|
||||
gangManager = new GangManager(dbManager); // Use the corrected dbManager
|
||||
|
||||
// Check if SQLite DB Exists, if not init it
|
||||
File databaseFile = new File(dataFolder, "nvus_prison.db");
|
||||
File databaseFile = new File(getDataFolder(), "nvus_prison.db");
|
||||
if (!databaseFile.exists()) {
|
||||
// If the database file doesn't exist, initialize the database
|
||||
dbManager.initDatabase();
|
||||
dbManager.initDatabase(); // Correct use of dbManager after initialization
|
||||
getLogger().info("SQLite database initialized successfully.");
|
||||
} else {
|
||||
getLogger().info("SQLite database already exists.");
|
||||
}
|
||||
|
||||
|
||||
// Save the default configs, if they don't exist
|
||||
configManager.saveDefaultConfig("config.yml");
|
||||
configManager.saveDefaultConfig("banned_items.yml");
|
||||
|
@ -72,8 +67,8 @@ public final class PrisonSetup extends JavaPlugin {
|
|||
getServer().getPluginManager().registerEvents(new ToolSwitchListener(configManager), this);
|
||||
this.getCommand("nvus").setExecutor(new CommandListener(this, configManager));
|
||||
|
||||
// Gang Related...... GANG, GANG #LOLOLOLOL
|
||||
this.getCommand("gang").setExecutor(new GangCommands(dbManager));
|
||||
// Gang Related... GANG, GANG #LOLOLOLOL
|
||||
this.getCommand("gang").setExecutor(new GangCommands(dbManager)); // Now correctly using initialized dbManager
|
||||
|
||||
// Settings Menu
|
||||
getServer().getPluginManager().registerEvents(new SettingsMenu(this, configManager), this);
|
||||
|
@ -89,7 +84,6 @@ public final class PrisonSetup extends JavaPlugin {
|
|||
new UpdateChecker(this, 12345).getVersion(version -> {
|
||||
if (!this.getDescription().getVersion().equals(version)) {
|
||||
getLogger().info("There is a new update available for NVus Prison Setup! Grab it from SpigotMC here: https://www.spigotmc.org/resources/nvus-prison-setup.115441/");
|
||||
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(this, () -> {
|
||||
for (Player player : Bukkit.getOnlinePlayers()) {
|
||||
if (player.isOp() || player.hasPermission("nvus.admin")) {
|
||||
|
@ -101,10 +95,10 @@ public final class PrisonSetup extends JavaPlugin {
|
|||
}, 20L * 60);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
// Save the config when disabling the plugin
|
||||
|
|
|
@ -31,6 +31,8 @@ PrisonerTools:
|
|||
ToolDamage: false
|
||||
|
||||
# Database Settings
|
||||
# MySQL or SQLite (local)
|
||||
Database.Type: MySQL
|
||||
host: 0.0.0.0
|
||||
port: 3306
|
||||
database: nvus_prison
|
||||
|
|
|
@ -21,7 +21,7 @@ commands:
|
|||
/nvus prisonerarmor <true|false> - Toggles giving prisoners armor on join.
|
||||
/nvus restrictarmor <true|false> - Toggles the restriction on changing prisoner armor.
|
||||
/nvus tooldamage <true|false> - Toggle if prisoner tools receive damage. FALSE = No Damage.
|
||||
aliases: [nvusmenu]
|
||||
aliases: [prison]
|
||||
|
||||
gang:
|
||||
description: Base command for gang-related actions.
|
||||
|
@ -31,7 +31,6 @@ commands:
|
|||
/gang accept - Accept a Gang Invite.
|
||||
/gang deny - Decline a Gang Invite.
|
||||
/gang leave - Leave your current Gang.
|
||||
-----COMING SOON-----
|
||||
/gang disband - Disband your gang.
|
||||
/gang kick <player> - Kick a player from your gang.
|
||||
/gang promote <player> - Promote a player to a higher rank.
|
||||
|
|
Loading…
Reference in New Issue