forked from never2nv/NVus_Prison
v1.0.1 - Re-wrote rank commands and set them up to use MySQL/SQLite!!!!
This commit is contained in:
parent
7745ca2d93
commit
b48bcb7688
2
pom.xml
2
pom.xml
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
<groupId>me.NVus</groupId>
|
<groupId>me.NVus</groupId>
|
||||||
<artifactId>NVus_Prison</artifactId>
|
<artifactId>NVus_Prison</artifactId>
|
||||||
<version>1.0.0</version>
|
<version>1.0.1</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>NVus_PrisonSetup</name>
|
<name>NVus_PrisonSetup</name>
|
||||||
|
|
|
@ -6,12 +6,19 @@ import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import me.nvus.nvus_prison_setup.Gangs.GangInfo;
|
import me.nvus.nvus_prison_setup.Gangs.GangInfo;
|
||||||
|
import me.nvus.nvus_prison_setup.Ranks.Rank;
|
||||||
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import me.nvus.nvus_prison_setup.Configs.ConfigManager;
|
import me.nvus.nvus_prison_setup.Configs.ConfigManager;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
@ -42,8 +49,10 @@ public class DatabaseManager {
|
||||||
} else {
|
} else {
|
||||||
this.url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password;
|
this.url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?user=" + username + "&password=" + password;
|
||||||
}
|
}
|
||||||
|
// Init Gangs Database
|
||||||
initializeDatabase();
|
initializeGangDatabase();
|
||||||
|
// Init Ranks Database
|
||||||
|
initializeRanksDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -51,7 +60,317 @@ public class DatabaseManager {
|
||||||
return DriverManager.getConnection(url);
|
return DriverManager.getConnection(url);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeDatabase() {
|
|
||||||
|
|
||||||
|
|
||||||
|
// Public Accessor to initialize the database
|
||||||
|
public void initGangDatabase() {
|
||||||
|
initializeGangDatabase();
|
||||||
|
}
|
||||||
|
public void initRanksDatabase() {
|
||||||
|
initializeRanksDatabase();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* ================================================================================================================
|
||||||
|
RANKS RELATED METHODS
|
||||||
|
==================================================================================================================*/
|
||||||
|
|
||||||
|
private void initializeRanksDatabase() {
|
||||||
|
String sqlRanks = "CREATE TABLE IF NOT EXISTS nvus_ranks ("
|
||||||
|
+ "rank_name VARCHAR(255) NOT NULL PRIMARY KEY,"
|
||||||
|
+ "cost DOUBLE NOT NULL,"
|
||||||
|
+ "commands TEXT NOT NULL"
|
||||||
|
+ ");";
|
||||||
|
|
||||||
|
String sqlRanksPlayers = "CREATE TABLE IF NOT EXISTS nvus_ranks_players ("
|
||||||
|
+ "uuid VARCHAR(36) NOT NULL,"
|
||||||
|
+ "username VARCHAR(255) NOT NULL,"
|
||||||
|
+ "rank_name VARCHAR(255) NOT NULL,"
|
||||||
|
+ "FOREIGN KEY (rank_name) REFERENCES nvus_ranks(rank_name) ON DELETE CASCADE ON UPDATE CASCADE"
|
||||||
|
+ ");";
|
||||||
|
|
||||||
|
try (Connection conn = connect(); Statement stmt = conn.createStatement()) {
|
||||||
|
stmt.execute(sqlRanks);
|
||||||
|
stmt.execute(sqlRanksPlayers);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
System.out.println("Error initializing Ranks database: " + e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void loadRanksFromConfig(FileConfiguration ranksConfig) {
|
||||||
|
ConfigurationSection ranksSection = ranksConfig.getConfigurationSection("Ranks");
|
||||||
|
if (ranksSection != null) {
|
||||||
|
for (String rankKey : ranksSection.getKeys(false)) {
|
||||||
|
String rankName = rankKey;
|
||||||
|
double cost = ranksSection.getDouble(rankKey + ".Cost");
|
||||||
|
List<String> commandsList = ranksSection.getStringList(rankKey + ".Commands");
|
||||||
|
// Join the commands list into a single string with ";" as the delimiter
|
||||||
|
String commands = String.join(";", commandsList);
|
||||||
|
|
||||||
|
// Insert or update the ranks in the database with the commands as a single string
|
||||||
|
upsertRankInDatabase(rankName, cost, commands);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void syncRanks() {
|
||||||
|
FileConfiguration ranksConfig = configManager.getConfig("ranks.yml");
|
||||||
|
// Assuming you've moved loadRanksFromConfig method logic directly here or keep it separated as below
|
||||||
|
loadRanksFromConfig(ranksConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void upsertRankInDatabase(String rankName, double cost, String commandsAsString) {
|
||||||
|
// This method will check if the rank exists and update or insert accordingly
|
||||||
|
String selectQuery = "SELECT COUNT(*) FROM nvus_ranks WHERE rank_name = ?";
|
||||||
|
String insertQuery = "INSERT INTO nvus_ranks (rank_name, cost, commands) VALUES (?, ?, ?)";
|
||||||
|
String updateQuery = "UPDATE nvus_ranks SET cost = ?, commands = ? WHERE rank_name = ?";
|
||||||
|
|
||||||
|
try (Connection conn = connect();
|
||||||
|
PreparedStatement selectStmt = conn.prepareStatement(selectQuery);
|
||||||
|
PreparedStatement insertStmt = conn.prepareStatement(insertQuery);
|
||||||
|
PreparedStatement updateStmt = conn.prepareStatement(updateQuery)) {
|
||||||
|
|
||||||
|
// Check if rank exists
|
||||||
|
selectStmt.setString(1, rankName);
|
||||||
|
ResultSet rs = selectStmt.executeQuery();
|
||||||
|
if (rs.next() && rs.getInt(1) > 0) {
|
||||||
|
// Rank exists, update it
|
||||||
|
updateStmt.setDouble(1, cost);
|
||||||
|
updateStmt.setString(2, commandsAsString);
|
||||||
|
updateStmt.setString(3, rankName);
|
||||||
|
updateStmt.executeUpdate();
|
||||||
|
} else {
|
||||||
|
// Rank doesn't exist, insert it
|
||||||
|
insertStmt.setString(1, rankName);
|
||||||
|
insertStmt.setDouble(2, cost);
|
||||||
|
insertStmt.setString(3, commandsAsString);
|
||||||
|
insertStmt.executeUpdate();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("Error upserting rank: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initializeAndSyncRanks() {
|
||||||
|
// Initialize the database (tables creation)
|
||||||
|
initializeRanksDatabase();
|
||||||
|
|
||||||
|
// Load ranks from the ranks.yml config and sync them with the database
|
||||||
|
syncRanks();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void reloadRanks() {
|
||||||
|
syncRanks();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now it's a onstructor to re-build string comments from DB with ; as delimeter to a LIST/ARRAY :P
|
||||||
|
public Rank getCurrentRank(Player player) {
|
||||||
|
String playerRankSql = "SELECT rank_name FROM nvus_ranks_players WHERE uuid = ?";
|
||||||
|
String rankDetailsSql = "SELECT * FROM nvus_ranks WHERE rank_name = ?";
|
||||||
|
|
||||||
|
try (Connection conn = connect();
|
||||||
|
PreparedStatement pstmtPlayerRank = conn.prepareStatement(playerRankSql)) {
|
||||||
|
|
||||||
|
pstmtPlayerRank.setString(1, player.getUniqueId().toString());
|
||||||
|
ResultSet rsPlayerRank = pstmtPlayerRank.executeQuery();
|
||||||
|
|
||||||
|
if (rsPlayerRank.next()) {
|
||||||
|
String rankName = rsPlayerRank.getString("rank_name");
|
||||||
|
|
||||||
|
try (PreparedStatement pstmtRankDetails = conn.prepareStatement(rankDetailsSql)) {
|
||||||
|
pstmtRankDetails.setString(1, rankName);
|
||||||
|
ResultSet rsRankDetails = pstmtRankDetails.executeQuery();
|
||||||
|
|
||||||
|
if (rsRankDetails.next()) {
|
||||||
|
double cost = rsRankDetails.getDouble("cost");
|
||||||
|
String commandsAsString = rsRankDetails.getString("commands");
|
||||||
|
List<String> commands = new ArrayList<>();
|
||||||
|
if (commandsAsString != null && !commandsAsString.isEmpty()) {
|
||||||
|
commands = Arrays.asList(commandsAsString.split(";"));
|
||||||
|
}
|
||||||
|
return new Rank(rankName, cost, commands);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Rank getCurrentPlayerRank(Player player) {
|
||||||
|
return getCurrentRank(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private Rank getNextRank(Player player) {
|
||||||
|
Rank currentRank = getCurrentRank(player);
|
||||||
|
if (currentRank == null) {
|
||||||
|
return null; // Player has no current rank, handle as needed
|
||||||
|
}
|
||||||
|
|
||||||
|
final String query = "SELECT * FROM nvus_ranks WHERE cost > ? ORDER BY cost ASC LIMIT 1";
|
||||||
|
|
||||||
|
try (Connection conn = connect();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(query)) {
|
||||||
|
|
||||||
|
pstmt.setDouble(1, currentRank.getCost());
|
||||||
|
ResultSet rs = pstmt.executeQuery();
|
||||||
|
|
||||||
|
if (rs.next()) {
|
||||||
|
String rankName = rs.getString("rank_name");
|
||||||
|
double cost = rs.getDouble("cost");
|
||||||
|
String commandsAsString = rs.getString("commands");
|
||||||
|
List<String> commands = new ArrayList<>();
|
||||||
|
if (commandsAsString != null && !commandsAsString.isEmpty()) {
|
||||||
|
commands = Arrays.asList(commandsAsString.split(";"));
|
||||||
|
}
|
||||||
|
return new Rank(rankName, cost, commands);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null; // Player is at the highest rank or an error occurred
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public Rank getPlayerNextRank(Player player) {
|
||||||
|
return getNextRank(player);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updatePlayerRank(UUID uuid, String username, String newRankName) {
|
||||||
|
// SQL query to update the player's rank
|
||||||
|
String updateSql = "UPDATE nvus_ranks_players SET rank_name = ?, username = ? WHERE uuid = ?;";
|
||||||
|
|
||||||
|
// SQL query to insert a new row if the player does not exist in the table
|
||||||
|
String insertSql = "INSERT INTO nvus_ranks_players (uuid, username, rank_name) SELECT ?, ?, ? WHERE NOT EXISTS (SELECT 1 FROM nvus_ranks_players WHERE uuid = ?);";
|
||||||
|
|
||||||
|
try (Connection conn = connect();
|
||||||
|
PreparedStatement updateStmt = conn.prepareStatement(updateSql);
|
||||||
|
PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
|
||||||
|
|
||||||
|
// First, try to update the player's rank
|
||||||
|
updateStmt.setString(1, newRankName);
|
||||||
|
updateStmt.setString(2, username);
|
||||||
|
updateStmt.setString(3, uuid.toString());
|
||||||
|
int rowsAffected = updateStmt.executeUpdate();
|
||||||
|
|
||||||
|
// If the player was not already in the table, insert a new row
|
||||||
|
if (rowsAffected == 0) {
|
||||||
|
insertStmt.setString(1, uuid.toString());
|
||||||
|
insertStmt.setString(2, username);
|
||||||
|
insertStmt.setString(3, newRankName);
|
||||||
|
insertStmt.setString(4, uuid.toString());
|
||||||
|
insertStmt.executeUpdate();
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("Error updating player rank: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void updatePlayerRankData(Player player, Rank nextRank) {
|
||||||
|
// Extract UUID and username from the Player object
|
||||||
|
UUID playerUUID = player.getUniqueId();
|
||||||
|
String username = player.getName();
|
||||||
|
|
||||||
|
// Extract the new rank name from the Rank object
|
||||||
|
String newRankName = nextRank.getName();
|
||||||
|
|
||||||
|
// Update the player's rank in the database
|
||||||
|
updatePlayerRank(playerUUID, username, newRankName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Rank> getAllRanksSorted() {
|
||||||
|
List<Rank> sortedRanks = new ArrayList<>();
|
||||||
|
final String query = "SELECT * FROM nvus_ranks ORDER BY cost ASC";
|
||||||
|
|
||||||
|
try (Connection conn = connect();
|
||||||
|
PreparedStatement pstmt = conn.prepareStatement(query);
|
||||||
|
ResultSet rs = pstmt.executeQuery()) {
|
||||||
|
|
||||||
|
while (rs.next()) {
|
||||||
|
String rankName = rs.getString("rank_name");
|
||||||
|
double cost = rs.getDouble("cost");
|
||||||
|
String commandsAsString = rs.getString("commands");
|
||||||
|
List<String> commands = new ArrayList<>();
|
||||||
|
if (commandsAsString != null && !commandsAsString.isEmpty()) {
|
||||||
|
commands = Arrays.asList(commandsAsString.split(";"));
|
||||||
|
}
|
||||||
|
Rank rank = new Rank(rankName, cost, commands);
|
||||||
|
sortedRanks.add(rank);
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("Error retrieving all ranks sorted: " + e.getMessage());
|
||||||
|
}
|
||||||
|
return sortedRanks;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private void setPlayerDefaultRank(UUID playerUuid, String playerName) {
|
||||||
|
final String defaultRankQuery = "SELECT * FROM nvus_ranks ORDER BY cost ASC LIMIT 1";
|
||||||
|
final String insertOrUpdatePlayerRankSql = "INSERT INTO nvus_ranks_players (uuid, username, rank_name) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE rank_name = VALUES(rank_name), username = VALUES(username)";
|
||||||
|
|
||||||
|
try (Connection conn = connect();
|
||||||
|
PreparedStatement defaultRankStmt = conn.prepareStatement(defaultRankQuery);
|
||||||
|
ResultSet rsDefaultRank = defaultRankStmt.executeQuery()) {
|
||||||
|
|
||||||
|
if (rsDefaultRank.next()) {
|
||||||
|
String defaultRankName = rsDefaultRank.getString("rank_name");
|
||||||
|
|
||||||
|
try (PreparedStatement insertOrUpdateStmt = conn.prepareStatement(insertOrUpdatePlayerRankSql)) {
|
||||||
|
insertOrUpdateStmt.setString(1, playerUuid.toString());
|
||||||
|
insertOrUpdateStmt.setString(2, playerName);
|
||||||
|
insertOrUpdateStmt.setString(3, defaultRankName);
|
||||||
|
insertOrUpdateStmt.executeUpdate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
System.out.println("Error setting default rank for player: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void assignPlayerToDefaultRank(Player player) {
|
||||||
|
setPlayerDefaultRank(player.getUniqueId(), player.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* ================================================================================================================
|
||||||
|
GANG RELATED METHODS
|
||||||
|
==================================================================================================================*/
|
||||||
|
|
||||||
|
private void initializeGangDatabase() {
|
||||||
String sqlGangs = "CREATE TABLE IF NOT EXISTS nvus_gangs ("
|
String sqlGangs = "CREATE TABLE IF NOT EXISTS nvus_gangs ("
|
||||||
+ "id INTEGER PRIMARY KEY " + (databaseType.equalsIgnoreCase("SQLite") ? "AUTOINCREMENT" : "AUTO_INCREMENT") + ","
|
+ "id INTEGER PRIMARY KEY " + (databaseType.equalsIgnoreCase("SQLite") ? "AUTOINCREMENT" : "AUTO_INCREMENT") + ","
|
||||||
+ "name TEXT NOT NULL,"
|
+ "name TEXT NOT NULL,"
|
||||||
|
@ -76,12 +395,6 @@ public class DatabaseManager {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Public Accessor to initialize the database
|
|
||||||
public void initDatabase() {
|
|
||||||
initializeDatabase();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void createGang(String name, String ownerUuid, String ownerName) {
|
public void createGang(String name, String ownerUuid, String ownerName) {
|
||||||
String insertGangSQL = "INSERT INTO nvus_gangs(name, owner_uuid) VALUES(?,?)";
|
String insertGangSQL = "INSERT INTO nvus_gangs(name, owner_uuid) VALUES(?,?)";
|
||||||
String insertMemberSQL = "INSERT INTO nvus_gangs_members(uuid, username, gang_id, rank) VALUES(?,?,(SELECT id FROM nvus_gangs WHERE owner_uuid = ?),'Owner')";
|
String insertMemberSQL = "INSERT INTO nvus_gangs_members(uuid, username, gang_id, rank) VALUES(?,?,(SELECT id FROM nvus_gangs WHERE owner_uuid = ?),'Owner')";
|
||||||
|
|
|
@ -10,6 +10,11 @@ import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
import net.md_5.bungee.api.chat.ClickEvent;
|
||||||
|
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||||
|
import net.md_5.bungee.api.chat.HoverEvent;
|
||||||
|
import net.md_5.bungee.api.chat.TextComponent;
|
||||||
|
|
||||||
public class CommandListener implements CommandExecutor {
|
public class CommandListener implements CommandExecutor {
|
||||||
private final JavaPlugin plugin;
|
private final JavaPlugin plugin;
|
||||||
private final ConfigManager configManager;
|
private final ConfigManager configManager;
|
||||||
|
@ -60,6 +65,9 @@ public class CommandListener implements CommandExecutor {
|
||||||
case "version":
|
case "version":
|
||||||
handleVersionCommand(sender);
|
handleVersionCommand(sender);
|
||||||
break;
|
break;
|
||||||
|
case "id":
|
||||||
|
handleIdCommand(sender);
|
||||||
|
break;
|
||||||
case "menu":
|
case "menu":
|
||||||
if (!(sender instanceof Player)) {
|
if (!(sender instanceof Player)) {
|
||||||
sender.sendMessage(ChatColor.RED + "This command can only be used by players.");
|
sender.sendMessage(ChatColor.RED + "This command can only be used by players.");
|
||||||
|
@ -145,6 +153,36 @@ public class CommandListener implements CommandExecutor {
|
||||||
sender.sendMessage(ChatColor.GREEN + "Plugin version: " + plugin.getDescription().getVersion());
|
sender.sendMessage(ChatColor.GREEN + "Plugin version: " + plugin.getDescription().getVersion());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void handleIdCommand(CommandSender sender) {
|
||||||
|
// Check if the sender is a player
|
||||||
|
if (sender instanceof Player) {
|
||||||
|
Player player = (Player) sender;
|
||||||
|
|
||||||
|
// Your existing logic here
|
||||||
|
// For example, sending the player their username and UUID in a simple message
|
||||||
|
player.sendMessage("Your username: " + player.getName());
|
||||||
|
player.sendMessage("Your UUID: " + player.getUniqueId().toString());
|
||||||
|
|
||||||
|
// Now send the clickable UUID message
|
||||||
|
sendClickableUUID(player);
|
||||||
|
} else {
|
||||||
|
// If the command sender is not a player (e.g., console), handle appropriately
|
||||||
|
sender.sendMessage("This command can only be used by a player.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sendClickableUUID(Player player) {
|
||||||
|
String uuid = player.getUniqueId().toString();
|
||||||
|
|
||||||
|
TextComponent message = new TextComponent("Click here to copy your UUID");
|
||||||
|
message.setColor(net.md_5.bungee.api.ChatColor.GOLD);
|
||||||
|
message.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, uuid));
|
||||||
|
message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder("Click to copy your UUID").color(net.md_5.bungee.api.ChatColor.YELLOW).create()));
|
||||||
|
|
||||||
|
player.spigot().sendMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private void handleToggleConfigCommand(CommandSender sender, String key, String value) {
|
private void handleToggleConfigCommand(CommandSender sender, String key, String value) {
|
||||||
boolean boolValue = Boolean.parseBoolean(value);
|
boolean boolValue = Boolean.parseBoolean(value);
|
||||||
FileConfiguration config = configManager.getConfig("config.yml");
|
FileConfiguration config = configManager.getConfig("config.yml");
|
||||||
|
@ -153,4 +191,5 @@ public class CommandListener implements CommandExecutor {
|
||||||
sender.sendMessage(ChatColor.GREEN + key + " set to " + boolValue + ".");
|
sender.sendMessage(ChatColor.GREEN + key + " set to " + boolValue + ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,7 +73,8 @@ public final class PrisonSetup extends JavaPlugin {
|
||||||
// Check if SQLite DB Exists, if not init it
|
// Check if SQLite DB Exists, if not init it
|
||||||
File databaseFile = new File(getDataFolder(), "nvus_prison.db");
|
File databaseFile = new File(getDataFolder(), "nvus_prison.db");
|
||||||
if (!databaseFile.exists()) {
|
if (!databaseFile.exists()) {
|
||||||
dbManager.initDatabase(); // Correct use of dbManager after initialization
|
dbManager.initGangDatabase(); // Correct use of dbManager after initialization
|
||||||
|
dbManager.initRanksDatabase(); // Correct use of dbManager after initialization
|
||||||
getLogger().info("Database initialized successfully.");
|
getLogger().info("Database initialized successfully.");
|
||||||
} else {
|
} else {
|
||||||
getLogger().info("SQLite database already exists.");
|
getLogger().info("SQLite database already exists.");
|
||||||
|
@ -140,8 +141,11 @@ public final class PrisonSetup extends JavaPlugin {
|
||||||
// Ranks Manager
|
// Ranks Manager
|
||||||
boolean prisonerRanksEnabled = configManager.getConfig("config.yml").getBoolean("PrisonerRanks", true);
|
boolean prisonerRanksEnabled = configManager.getConfig("config.yml").getBoolean("PrisonerRanks", true);
|
||||||
if (prisonerRanksEnabled) {
|
if (prisonerRanksEnabled) {
|
||||||
|
// Init & Sync Ranks with Database
|
||||||
|
dbManager.initializeAndSyncRanks();
|
||||||
|
|
||||||
// Initialize RankManager and other initializations
|
// Initialize RankManager and other initializations
|
||||||
rankManager = new RankManager(this);
|
rankManager = new RankManager(dbManager,econ); // Use the corrected dbManager
|
||||||
|
|
||||||
// Register RankListener
|
// Register RankListener
|
||||||
getServer().getPluginManager().registerEvents(new RankListener(rankManager), this);
|
getServer().getPluginManager().registerEvents(new RankListener(rankManager), this);
|
||||||
|
@ -149,6 +153,7 @@ public final class PrisonSetup extends JavaPlugin {
|
||||||
// Register commands
|
// Register commands
|
||||||
this.getCommand("rankup").setExecutor(new RankCommands(this));
|
this.getCommand("rankup").setExecutor(new RankCommands(this));
|
||||||
this.getCommand("ranks").setExecutor(new RankCommands(this));
|
this.getCommand("ranks").setExecutor(new RankCommands(this));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Successful Startup/Enable
|
// Successful Startup/Enable
|
||||||
|
|
|
@ -1,317 +1,428 @@
|
||||||
package me.nvus.nvus_prison_setup.Ranks;
|
package me.nvus.nvus_prison_setup.Ranks;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
import me.nvus.nvus_prison_setup.Database.DatabaseManager;
|
||||||
import me.nvus.nvus_prison_setup.PrisonSetup;
|
|
||||||
import net.milkbowl.vault.economy.Economy;
|
import net.milkbowl.vault.economy.Economy;
|
||||||
import net.milkbowl.vault.economy.EconomyResponse;
|
import net.milkbowl.vault.economy.EconomyResponse;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
import java.io.*;
|
import java.util.List;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.*;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
import com.google.gson.GsonBuilder;
|
|
||||||
import com.google.gson.reflect.TypeToken;
|
|
||||||
import java.io.FileReader;
|
|
||||||
import java.io.FileWriter;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class RankManager {
|
public class RankManager {
|
||||||
|
|
||||||
private JavaPlugin plugin;
|
private final DatabaseManager dbManager;
|
||||||
private List<Rank> ranks = new ArrayList<>();
|
private final Economy economy; // Assuming you have a method to get the Economy service
|
||||||
private Map<UUID, String> playerRanks = new HashMap<>();
|
|
||||||
|
|
||||||
|
public RankManager(DatabaseManager dbManager, Economy economy) {
|
||||||
public RankManager(JavaPlugin plugin) {
|
this.dbManager = dbManager;
|
||||||
this.plugin = plugin;
|
this.economy = economy;
|
||||||
ensureRankDataFile();
|
syncRanksWithDatabase();
|
||||||
loadRanksFromConfig();
|
|
||||||
loadPlayerRanks();
|
|
||||||
loadRanksFromRankDataFile();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ensureRankDataFile() {
|
public void syncRanksWithDatabase() {
|
||||||
File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
dbManager.syncRanks();
|
||||||
if (!rankDataFile.exists()) {
|
|
||||||
try {
|
|
||||||
plugin.saveResource("ranks.yml", false);
|
|
||||||
File ranksYmlFile = new File(plugin.getDataFolder(), "ranks.yml");
|
|
||||||
FileConfiguration ranksConfig = YamlConfiguration.loadConfiguration(ranksYmlFile);
|
|
||||||
updateRankDataFromYml(ranksConfig, rankDataFile);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updateRankDataFromYml(FileConfiguration ranksConfig, File rankDataFile) {
|
|
||||||
try {
|
|
||||||
Set<String> rankKeys = ranksConfig.getConfigurationSection("Ranks").getKeys(false);
|
|
||||||
List<Rank> ranks = rankKeys.stream().map(rankKey -> {
|
|
||||||
String path = "Ranks." + rankKey;
|
|
||||||
String name = rankKey;
|
|
||||||
double cost = ranksConfig.getDouble(path + ".Cost");
|
|
||||||
List<String> commands = ranksConfig.getStringList(path + ".Commands");
|
|
||||||
return new Rank(name, cost, commands);
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
|
|
||||||
Map<String, Object> rankData = new HashMap<>();
|
|
||||||
rankData.put("ranks", ranks);
|
|
||||||
// Add a dummy player data for demonstration. Replace with actual player data logic.
|
|
||||||
rankData.put("players", Collections.singletonList(new PlayerRankData(UUID.randomUUID(), "DummyPlayer", "Default")));
|
|
||||||
|
|
||||||
try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(rankDataFile), StandardCharsets.UTF_8))) {
|
|
||||||
new Gson().toJson(rankData, writer);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void loadRanksFromConfig() {
|
|
||||||
File ranksYmlFile = new File(plugin.getDataFolder(), "ranks.yml");
|
|
||||||
if (ranksYmlFile.exists()) {
|
|
||||||
FileConfiguration ranksConfig = YamlConfiguration.loadConfiguration(ranksYmlFile);
|
|
||||||
File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
|
||||||
updateRankDataFromYml(ranksConfig, rankDataFile);
|
|
||||||
// Debug log to check ranks loading
|
|
||||||
System.out.println("Loaded ranks: " + ranks.size());
|
|
||||||
ranks.forEach(rank -> System.out.println(rank.getName() + ": $" + rank.getCost()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void loadRanksFromRankDataFile() {
|
|
||||||
File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
|
||||||
if (rankDataFile.exists()) {
|
|
||||||
try (Reader reader = new FileReader(rankDataFile)) {
|
|
||||||
Gson gson = new Gson();
|
|
||||||
Type type = new TypeToken<Map<String, Object>>() {}.getType();
|
|
||||||
Map<String, Object> rankDataMap = gson.fromJson(reader, type);
|
|
||||||
|
|
||||||
List<Map<String, Object>> ranksList = (List<Map<String, Object>>) rankDataMap.get("ranks");
|
|
||||||
if (ranksList != null) {
|
|
||||||
this.ranks.clear(); // Clear existing ranks before loading new ones
|
|
||||||
for (Map<String, Object> rankMap : ranksList) {
|
|
||||||
String name = (String) rankMap.get("name");
|
|
||||||
double cost = ((Number) rankMap.get("cost")).doubleValue();
|
|
||||||
List<String> commands = (List<String>) rankMap.get("commands");
|
|
||||||
Rank rank = new Rank(name, cost, commands);
|
|
||||||
this.ranks.add(rank);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
System.out.println("Loaded ranks from rank_data.json: " + ranks.size());
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Rank getCurrentRank(Player player) {
|
public Rank getCurrentRank(Player player) {
|
||||||
String currentRankName = playerRanks.get(player.getUniqueId());
|
return dbManager.getCurrentPlayerRank(player);
|
||||||
if (currentRankName == null) {
|
|
||||||
// Reload ranks from config to ensure they are up to date
|
|
||||||
loadRanksFromConfig();
|
|
||||||
loadPlayerRanks();
|
|
||||||
|
|
||||||
// Assign default rank if the player has no current rank
|
|
||||||
assignDefaultRank(player);
|
|
||||||
currentRankName = playerRanks.get(player.getUniqueId());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use a final variable for the lambda expression
|
|
||||||
final String rankNameForLambda = currentRankName;
|
|
||||||
|
|
||||||
return ranks.stream()
|
|
||||||
.filter(rank -> rank.getName().equals(rankNameForLambda))
|
|
||||||
.findFirst()
|
|
||||||
.orElse(null); // Consider handling the default rank if not found.
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public Rank getNextRank(Player player) {
|
public Rank getNextRank(Player player) {
|
||||||
Rank currentRank = getCurrentRank(player);
|
return dbManager.getPlayerNextRank(player);
|
||||||
if (currentRank == null) {
|
|
||||||
return null; // Handle appropriately, maybe return the first rank if implementing a default catch-up mechanism.
|
|
||||||
}
|
|
||||||
int currentIndex = ranks.indexOf(currentRank);
|
|
||||||
if (currentIndex < ranks.size() - 1) {
|
|
||||||
return ranks.get(currentIndex + 1);
|
|
||||||
}
|
|
||||||
return null; // Player is at the highest rank.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean rankUp(Player player) {
|
public boolean rankUp(Player player) {
|
||||||
// Directly retrieve the next rank using the player object
|
|
||||||
Rank nextRank = getNextRank(player);
|
Rank nextRank = getNextRank(player);
|
||||||
|
|
||||||
if (nextRank == null) {
|
if (nextRank == null) {
|
||||||
player.sendMessage(ChatColor.RED + "You are already at the highest rank!");
|
player.sendMessage(ChatColor.RED + "You are already at the highest rank!");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Economy economy = PrisonSetup.getEconomy(); // Assuming this static method access is correct
|
|
||||||
double balance = economy.getBalance(player);
|
double balance = economy.getBalance(player);
|
||||||
|
if (balance >= nextRank.getCost()) {
|
||||||
if (balance < nextRank.getCost()) {
|
|
||||||
player.sendMessage(ChatColor.RED + "You cannot afford to rank up. You have " + balance + ", but need " + nextRank.getCost() + ".");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
EconomyResponse response = economy.withdrawPlayer(player, nextRank.getCost());
|
EconomyResponse response = economy.withdrawPlayer(player, nextRank.getCost());
|
||||||
if (!response.transactionSuccess()) {
|
if (response.transactionSuccess()) {
|
||||||
|
dbManager.updatePlayerRankData(player, nextRank);
|
||||||
|
executeRankCommands(player, nextRank.getCommands());
|
||||||
|
//player.sendMessage(ChatColor.GREEN + "Congratulations! You've been ranked up to " + nextRank.getName() + ".");
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
player.sendMessage(ChatColor.RED + "Transaction failed: " + response.errorMessage);
|
player.sendMessage(ChatColor.RED + "Transaction failed: " + response.errorMessage);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
// Update the player's rank in memory and any persistent storage
|
player.sendMessage(ChatColor.RED + "You cannot afford to rank up. You need $" + nextRank.getCost() + ", but you only have $" + balance + ".");
|
||||||
playerRanks.put(player.getUniqueId(), nextRank.getName());
|
return false;
|
||||||
saveRankData(); // Ensure to implement this method to persist changes to 'rank_data.json'
|
|
||||||
|
|
||||||
// Execute rank-up commands
|
|
||||||
executeRankCommands(player, nextRank.getCommands());
|
|
||||||
|
|
||||||
player.sendMessage(ChatColor.GREEN + "You've been ranked up to " + nextRank.getName() + "!");
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void updatePlayerRank(Player player, Rank nextRank) {
|
|
||||||
// Update the player's current rank in memory (if you're keeping a cache)
|
|
||||||
playerRanks.put(player.getUniqueId(), nextRank.getName());
|
|
||||||
// Save the player's current rank to persistent storage
|
|
||||||
saveRankData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void executeRankCommands(Player player, List<String> commands) {
|
private void executeRankCommands(Player player, List<String> commands) {
|
||||||
if (commands == null || commands.isEmpty()) return;
|
if (commands == null || commands.isEmpty()) return;
|
||||||
String playerName = player.getName();
|
for (String command : commands) {
|
||||||
commands.forEach(command ->
|
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command.replace("%player%", player.getName()));
|
||||||
Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command.replace("%player%", playerName))
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void saveRankData() {
|
|
||||||
File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
|
||||||
Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
|
||||||
|
|
||||||
// Load existing rank data to merge with updates
|
|
||||||
Map<String, Object> existingData = new HashMap<>();
|
|
||||||
try (Reader reader = new FileReader(rankDataFile)) {
|
|
||||||
Type type = new TypeToken<Map<String, Object>>() {}.getType();
|
|
||||||
existingData = gson.fromJson(reader, type);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
// If there's no existing data or it's the first time, initialize structures
|
|
||||||
if (existingData == null) {
|
|
||||||
existingData = new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare rank data to be saved
|
|
||||||
List<Map<String, Object>> serializedRanks = ranks.stream().map(rank -> {
|
|
||||||
Map<String, Object> rankMap = new HashMap<>();
|
|
||||||
rankMap.put("name", rank.getName());
|
|
||||||
rankMap.put("cost", rank.getCost());
|
|
||||||
rankMap.put("commands", rank.getCommands());
|
|
||||||
return rankMap;
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
|
|
||||||
// Prepare player rank data to be saved
|
|
||||||
List<Map<String, Object>> serializedPlayerRanks = playerRanks.entrySet().stream().map(entry -> {
|
|
||||||
Map<String, Object> playerRankMap = new HashMap<>();
|
|
||||||
playerRankMap.put("uuid", entry.getKey().toString());
|
|
||||||
playerRankMap.put("rankName", entry.getValue());
|
|
||||||
return playerRankMap;
|
|
||||||
}).collect(Collectors.toList());
|
|
||||||
|
|
||||||
// Update the existing data map
|
|
||||||
existingData.put("ranks", serializedRanks);
|
|
||||||
existingData.put("players", serializedPlayerRanks);
|
|
||||||
|
|
||||||
// Save the updated data back to the file
|
|
||||||
try (Writer writer = new FileWriter(rankDataFile)) {
|
|
||||||
gson.toJson(existingData, writer);
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getRanksDisplay(Player player) {
|
public String getRanksDisplay(Player player) {
|
||||||
StringBuilder ranksMessage = new StringBuilder(ChatColor.GOLD + "Available Ranks:\n");
|
StringBuilder ranksMessage = new StringBuilder(ChatColor.GOLD + "Available Ranks:\n");
|
||||||
for (Rank rank : ranks) {
|
Rank currentRank = getCurrentRank(player);
|
||||||
|
// Assuming you have a method to get all ranks sorted by cost
|
||||||
|
List<Rank> allRanks = dbManager.getAllRanksSorted();
|
||||||
|
for (Rank rank : allRanks) {
|
||||||
ranksMessage.append(ChatColor.YELLOW).append(rank.getName())
|
ranksMessage.append(ChatColor.YELLOW).append(rank.getName())
|
||||||
.append(ChatColor.WHITE).append(" - $")
|
.append(ChatColor.WHITE).append(" - $")
|
||||||
.append(ChatColor.GREEN).append(rank.getCost()).append("\n");
|
.append(ChatColor.GREEN).append(rank.getCost()).append("\n");
|
||||||
}
|
}
|
||||||
|
ranksMessage.append(ChatColor.GOLD + "\nYour current rank: " + ChatColor.YELLOW + currentRank.getName());
|
||||||
// Fetch the player's current rank from the stored data
|
|
||||||
String currentRankName = playerRanks.getOrDefault(player.getUniqueId(), "Unranked");
|
|
||||||
// Using Vault to get the player's balance
|
|
||||||
Economy economy = PrisonSetup.getEconomy();
|
|
||||||
double balance = economy.getBalance(player);
|
double balance = economy.getBalance(player);
|
||||||
|
|
||||||
// Append the player's current rank and balance to the message
|
|
||||||
ranksMessage.append(ChatColor.GOLD + "\nYour current rank: " + ChatColor.YELLOW + currentRankName);
|
|
||||||
ranksMessage.append(ChatColor.GOLD + "\nYour balance: " + ChatColor.GREEN + "$" + balance);
|
ranksMessage.append(ChatColor.GOLD + "\nYour balance: " + ChatColor.GREEN + "$" + balance);
|
||||||
|
|
||||||
return ranksMessage.toString();
|
return ranksMessage.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void assignDefaultRank(Player player) {
|
public void assignDefaultRank(Player player) {
|
||||||
if (!playerRanks.containsKey(player.getUniqueId())) {
|
dbManager.assignPlayerToDefaultRank(player);
|
||||||
// Ensure this method successfully finds and assigns the default rank
|
// You might want to log this action or send a message to the player
|
||||||
Rank defaultRank = ranks.stream().filter(r -> r.getName().equalsIgnoreCase("Default")).findFirst().orElse(null);
|
player.sendMessage(ChatColor.GREEN + "You have been assigned the default rank.");
|
||||||
if (defaultRank != null) {
|
|
||||||
playerRanks.put(player.getUniqueId(), defaultRank.getName());
|
|
||||||
// Debug log
|
|
||||||
System.out.println("Assigning default rank to " + player.getName());
|
|
||||||
saveRankData();
|
|
||||||
player.sendMessage(ChatColor.GREEN + "You've been assigned the default rank: " + defaultRank.getName());
|
|
||||||
} else {
|
|
||||||
System.out.println("Default rank not found in loaded ranks.");
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void loadPlayerRanks() {
|
|
||||||
File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
|
||||||
if (rankDataFile.exists()) {
|
|
||||||
try (Reader reader = new FileReader(rankDataFile)) {
|
|
||||||
Type type = new TypeToken<Map<String, List<PlayerRankData>>>() {}.getType();
|
|
||||||
Map<String, List<PlayerRankData>> data = new Gson().fromJson(reader, type);
|
|
||||||
List<PlayerRankData> playerRankDataList = data.get("players");
|
|
||||||
if (playerRankDataList != null) {
|
|
||||||
playerRankDataList.forEach(playerRankData -> playerRanks.put(playerRankData.getUuid(), playerRankData.getRankName()));
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//package me.nvus.nvus_prison_setup.Ranks;
|
||||||
|
//
|
||||||
|
//import com.google.gson.Gson;
|
||||||
|
//import me.nvus.nvus_prison_setup.PrisonSetup;
|
||||||
|
//import net.milkbowl.vault.economy.Economy;
|
||||||
|
//import net.milkbowl.vault.economy.EconomyResponse;
|
||||||
|
//import org.bukkit.Bukkit;
|
||||||
|
//import org.bukkit.ChatColor;
|
||||||
|
//import org.bukkit.configuration.file.FileConfiguration;
|
||||||
|
//import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
//import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
//import org.bukkit.entity.Player;
|
||||||
|
//
|
||||||
|
//import java.io.*;
|
||||||
|
//import java.nio.charset.StandardCharsets;
|
||||||
|
//import java.nio.file.Files;
|
||||||
|
//import java.nio.file.Paths;
|
||||||
|
//import java.util.*;
|
||||||
|
//import java.util.stream.Collectors;
|
||||||
|
//import com.google.gson.GsonBuilder;
|
||||||
|
//import com.google.gson.reflect.TypeToken;
|
||||||
|
//import java.io.FileReader;
|
||||||
|
//import java.io.FileWriter;
|
||||||
|
//import java.lang.reflect.Type;
|
||||||
|
//import java.util.HashMap;
|
||||||
|
//import java.util.Map;
|
||||||
|
//import java.util.UUID;
|
||||||
|
//
|
||||||
|
//public class RankManager {
|
||||||
|
//
|
||||||
|
// private JavaPlugin plugin;
|
||||||
|
// private List<Rank> ranks = new ArrayList<>();
|
||||||
|
// private Map<UUID, String> playerRanks = new HashMap<>();
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// public RankManager(JavaPlugin plugin) {
|
||||||
|
// this.plugin = plugin;
|
||||||
|
// ensureRankDataFile();
|
||||||
|
// loadRanksFromConfig();
|
||||||
|
// loadPlayerRanks();
|
||||||
|
// loadRanksFromRankDataFile();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private void ensureRankDataFile() {
|
||||||
|
// File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
||||||
|
// if (!rankDataFile.exists()) {
|
||||||
|
// try {
|
||||||
|
// plugin.saveResource("ranks.yml", false);
|
||||||
|
// File ranksYmlFile = new File(plugin.getDataFolder(), "ranks.yml");
|
||||||
|
// FileConfiguration ranksConfig = YamlConfiguration.loadConfiguration(ranksYmlFile);
|
||||||
|
// updateRankDataFromYml(ranksConfig, rankDataFile);
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private void updateRankDataFromYml(FileConfiguration ranksConfig, File rankDataFile) {
|
||||||
|
// try {
|
||||||
|
// Set<String> rankKeys = ranksConfig.getConfigurationSection("Ranks").getKeys(false);
|
||||||
|
// List<Rank> ranks = rankKeys.stream().map(rankKey -> {
|
||||||
|
// String path = "Ranks." + rankKey;
|
||||||
|
// String name = rankKey;
|
||||||
|
// double cost = ranksConfig.getDouble(path + ".Cost");
|
||||||
|
// List<String> commands = ranksConfig.getStringList(path + ".Commands");
|
||||||
|
// return new Rank(name, cost, commands);
|
||||||
|
// }).collect(Collectors.toList());
|
||||||
|
//
|
||||||
|
// Map<String, Object> rankData = new HashMap<>();
|
||||||
|
// rankData.put("ranks", ranks);
|
||||||
|
// // Add a dummy player data for demonstration. Replace with actual player data logic.
|
||||||
|
// rankData.put("players", Collections.singletonList(new PlayerRankData(UUID.randomUUID(), "DummyPlayer", "Default")));
|
||||||
|
//
|
||||||
|
// try (Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(rankDataFile), StandardCharsets.UTF_8))) {
|
||||||
|
// new Gson().toJson(rankData, writer);
|
||||||
|
// }
|
||||||
|
// } catch (IOException e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void loadRanksFromConfig() {
|
||||||
|
// File ranksYmlFile = new File(plugin.getDataFolder(), "ranks.yml");
|
||||||
|
// if (ranksYmlFile.exists()) {
|
||||||
|
// FileConfiguration ranksConfig = YamlConfiguration.loadConfiguration(ranksYmlFile);
|
||||||
|
// File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
||||||
|
// updateRankDataFromYml(ranksConfig, rankDataFile);
|
||||||
|
// // Debug log to check ranks loading
|
||||||
|
// System.out.println("Loaded ranks: " + ranks.size());
|
||||||
|
// ranks.forEach(rank -> System.out.println(rank.getName() + ": $" + rank.getCost()));
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// private void loadRanksFromRankDataFile() {
|
||||||
|
// File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
||||||
|
// if (rankDataFile.exists()) {
|
||||||
|
// try (Reader reader = new FileReader(rankDataFile)) {
|
||||||
|
// Gson gson = new Gson();
|
||||||
|
// Type type = new TypeToken<Map<String, Object>>() {}.getType();
|
||||||
|
// Map<String, Object> rankDataMap = gson.fromJson(reader, type);
|
||||||
|
//
|
||||||
|
// List<Map<String, Object>> ranksList = (List<Map<String, Object>>) rankDataMap.get("ranks");
|
||||||
|
// if (ranksList != null) {
|
||||||
|
// this.ranks.clear(); // Clear existing ranks before loading new ones
|
||||||
|
// for (Map<String, Object> rankMap : ranksList) {
|
||||||
|
// String name = (String) rankMap.get("name");
|
||||||
|
// double cost = ((Number) rankMap.get("cost")).doubleValue();
|
||||||
|
// List<String> commands = (List<String>) rankMap.get("commands");
|
||||||
|
// Rank rank = new Rank(name, cost, commands);
|
||||||
|
// this.ranks.add(rank);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// System.out.println("Loaded ranks from rank_data.json: " + ranks.size());
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// public Rank getCurrentRank(Player player) {
|
||||||
|
// String currentRankName = playerRanks.get(player.getUniqueId());
|
||||||
|
// if (currentRankName == null) {
|
||||||
|
// // Reload ranks from config to ensure they are up to date
|
||||||
|
// loadRanksFromConfig();
|
||||||
|
// loadPlayerRanks();
|
||||||
|
//
|
||||||
|
// // Assign default rank if the player has no current rank
|
||||||
|
// assignDefaultRank(player);
|
||||||
|
// currentRankName = playerRanks.get(player.getUniqueId());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Use a final variable for the lambda expression
|
||||||
|
// final String rankNameForLambda = currentRankName;
|
||||||
|
//
|
||||||
|
// return ranks.stream()
|
||||||
|
// .filter(rank -> rank.getName().equals(rankNameForLambda))
|
||||||
|
// .findFirst()
|
||||||
|
// .orElse(null); // Consider handling the default rank if not found.
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// public Rank getNextRank(Player player) {
|
||||||
|
// Rank currentRank = getCurrentRank(player);
|
||||||
|
// if (currentRank == null) {
|
||||||
|
// return null; // Handle appropriately, maybe return the first rank if implementing a default catch-up mechanism.
|
||||||
|
// }
|
||||||
|
// int currentIndex = ranks.indexOf(currentRank);
|
||||||
|
// if (currentIndex < ranks.size() - 1) {
|
||||||
|
// return ranks.get(currentIndex + 1);
|
||||||
|
// }
|
||||||
|
// return null; // Player is at the highest rank.
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public boolean rankUp(Player player) {
|
||||||
|
// // Directly retrieve the next rank using the player object
|
||||||
|
// Rank nextRank = getNextRank(player);
|
||||||
|
//
|
||||||
|
// if (nextRank == null) {
|
||||||
|
// player.sendMessage(ChatColor.RED + "You are already at the highest rank!");
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// Economy economy = PrisonSetup.getEconomy(); // Assuming this static method access is correct
|
||||||
|
// double balance = economy.getBalance(player);
|
||||||
|
//
|
||||||
|
// if (balance < nextRank.getCost()) {
|
||||||
|
// player.sendMessage(ChatColor.RED + "You cannot afford to rank up. You have " + balance + ", but need " + nextRank.getCost() + ".");
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// EconomyResponse response = economy.withdrawPlayer(player, nextRank.getCost());
|
||||||
|
// if (!response.transactionSuccess()) {
|
||||||
|
// player.sendMessage(ChatColor.RED + "Transaction failed: " + response.errorMessage);
|
||||||
|
// return false;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Update the player's rank in memory and any persistent storage
|
||||||
|
// playerRanks.put(player.getUniqueId(), nextRank.getName());
|
||||||
|
// saveRankData(); // Ensure to implement this method to persist changes to 'rank_data.json'
|
||||||
|
//
|
||||||
|
// // Execute rank-up commands
|
||||||
|
// executeRankCommands(player, nextRank.getCommands());
|
||||||
|
//
|
||||||
|
// player.sendMessage(ChatColor.GREEN + "You've been ranked up to " + nextRank.getName() + "!");
|
||||||
|
// return true;
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// private void updatePlayerRank(Player player, Rank nextRank) {
|
||||||
|
// // Update the player's current rank in memory (if you're keeping a cache)
|
||||||
|
// playerRanks.put(player.getUniqueId(), nextRank.getName());
|
||||||
|
// // Save the player's current rank to persistent storage
|
||||||
|
// saveRankData();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// private void executeRankCommands(Player player, List<String> commands) {
|
||||||
|
// if (commands == null || commands.isEmpty()) return;
|
||||||
|
// String playerName = player.getName();
|
||||||
|
// commands.forEach(command ->
|
||||||
|
// Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), command.replace("%player%", playerName))
|
||||||
|
// );
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// private void saveRankData() {
|
||||||
|
// File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
||||||
|
// Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
|
//
|
||||||
|
// // Load existing rank data to merge with updates
|
||||||
|
// Map<String, Object> existingData = new HashMap<>();
|
||||||
|
// try (Reader reader = new FileReader(rankDataFile)) {
|
||||||
|
// Type type = new TypeToken<Map<String, Object>>() {}.getType();
|
||||||
|
// existingData = gson.fromJson(reader, type);
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // If there's no existing data or it's the first time, initialize structures
|
||||||
|
// if (existingData == null) {
|
||||||
|
// existingData = new HashMap<>();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Prepare rank data to be saved
|
||||||
|
// List<Map<String, Object>> serializedRanks = ranks.stream().map(rank -> {
|
||||||
|
// Map<String, Object> rankMap = new HashMap<>();
|
||||||
|
// rankMap.put("name", rank.getName());
|
||||||
|
// rankMap.put("cost", rank.getCost());
|
||||||
|
// rankMap.put("commands", rank.getCommands());
|
||||||
|
// return rankMap;
|
||||||
|
// }).collect(Collectors.toList());
|
||||||
|
//
|
||||||
|
// // Prepare player rank data to be saved
|
||||||
|
// List<Map<String, Object>> serializedPlayerRanks = playerRanks.entrySet().stream().map(entry -> {
|
||||||
|
// Map<String, Object> playerRankMap = new HashMap<>();
|
||||||
|
// playerRankMap.put("uuid", entry.getKey().toString());
|
||||||
|
// playerRankMap.put("rankName", entry.getValue());
|
||||||
|
// return playerRankMap;
|
||||||
|
// }).collect(Collectors.toList());
|
||||||
|
//
|
||||||
|
// // Update the existing data map
|
||||||
|
// existingData.put("ranks", serializedRanks);
|
||||||
|
// existingData.put("players", serializedPlayerRanks);
|
||||||
|
//
|
||||||
|
// // Save the updated data back to the file
|
||||||
|
// try (Writer writer = new FileWriter(rankDataFile)) {
|
||||||
|
// gson.toJson(existingData, writer);
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public String getRanksDisplay(Player player) {
|
||||||
|
// StringBuilder ranksMessage = new StringBuilder(ChatColor.GOLD + "Available Ranks:\n");
|
||||||
|
// for (Rank rank : ranks) {
|
||||||
|
// ranksMessage.append(ChatColor.YELLOW).append(rank.getName())
|
||||||
|
// .append(ChatColor.WHITE).append(" - $")
|
||||||
|
// .append(ChatColor.GREEN).append(rank.getCost()).append("\n");
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Fetch the player's current rank from the stored data
|
||||||
|
// String currentRankName = playerRanks.getOrDefault(player.getUniqueId(), "Unranked");
|
||||||
|
// // Using Vault to get the player's balance
|
||||||
|
// Economy economy = PrisonSetup.getEconomy();
|
||||||
|
// double balance = economy.getBalance(player);
|
||||||
|
//
|
||||||
|
// // Append the player's current rank and balance to the message
|
||||||
|
// ranksMessage.append(ChatColor.GOLD + "\nYour current rank: " + ChatColor.YELLOW + currentRankName);
|
||||||
|
// ranksMessage.append(ChatColor.GOLD + "\nYour balance: " + ChatColor.GREEN + "$" + balance);
|
||||||
|
//
|
||||||
|
// return ranksMessage.toString();
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// public void assignDefaultRank(Player player) {
|
||||||
|
// if (!playerRanks.containsKey(player.getUniqueId())) {
|
||||||
|
// // Ensure this method successfully finds and assigns the default rank
|
||||||
|
// Rank defaultRank = ranks.stream().filter(r -> r.getName().equalsIgnoreCase("Default")).findFirst().orElse(null);
|
||||||
|
// if (defaultRank != null) {
|
||||||
|
// playerRanks.put(player.getUniqueId(), defaultRank.getName());
|
||||||
|
// // Debug log
|
||||||
|
// System.out.println("Assigning default rank to " + player.getName());
|
||||||
|
// saveRankData();
|
||||||
|
// player.sendMessage(ChatColor.GREEN + "You've been assigned the default rank: " + defaultRank.getName());
|
||||||
|
// } else {
|
||||||
|
// System.out.println("Default rank not found in loaded ranks.");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// private void loadPlayerRanks() {
|
||||||
|
// File rankDataFile = new File(plugin.getDataFolder(), "rank_data.json");
|
||||||
|
// if (rankDataFile.exists()) {
|
||||||
|
// try (Reader reader = new FileReader(rankDataFile)) {
|
||||||
|
// Type type = new TypeToken<Map<String, List<PlayerRankData>>>() {}.getType();
|
||||||
|
// Map<String, List<PlayerRankData>> data = new Gson().fromJson(reader, type);
|
||||||
|
// List<PlayerRankData> playerRankDataList = data.get("players");
|
||||||
|
// if (playerRankDataList != null) {
|
||||||
|
// playerRankDataList.forEach(playerRankData -> playerRanks.put(playerRankData.getUuid(), playerRankData.getRankName()));
|
||||||
|
// }
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// e.printStackTrace();
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//}
|
||||||
|
|
|
@ -21,14 +21,18 @@ commands:
|
||||||
usage: |
|
usage: |
|
||||||
/nvus reload - Reloads all configuration files.
|
/nvus reload - Reloads all configuration files.
|
||||||
/nvus version - Shows the plugin version.
|
/nvus version - Shows the plugin version.
|
||||||
|
/nvus id - Display your username, UUID and send a clickable UUID.
|
||||||
/nvus menu - Opens the settings menu.
|
/nvus menu - Opens the settings menu.
|
||||||
/nvus autopickup <true|false> - Toggles the auto item pickup feature.
|
/nvus autopickup <true|false> - Toggles the auto item pickup feature.
|
||||||
/nvus autoswitch <true|false> - Toggles the auto tool switch feature.
|
/nvus autoswitch <true|false> - Toggles the auto tool switch feature.
|
||||||
/nvus prisonerarmor <true|false> - Toggles giving prisoners armor on join.
|
/nvus prisonerarmor <true|false> - Toggles giving prisoners armor on join.
|
||||||
/nvus restrictarmor <true|false> - Toggles the restriction on changing prisoner armor.
|
/nvus restrictarmor <true|false> - Toggles the restriction on changing prisoner armor.
|
||||||
/nvus tooldamage <true|false> - Toggle if prisoner tools receive damage. FALSE = No Damage.
|
/nvus tooldamage <true|false> - Toggle if prisoner tools receive damage. FALSE = No Damage.
|
||||||
|
/nvus treefarm <true|false> - Toggle the TreeFarm feature for prisoners.
|
||||||
|
/nvus prisonerranks <true|false> - Toggle if prisoners can use the /rankup and /ranks commands.
|
||||||
/nvus autosell <true|false> - Toggle if prisoners can auto toggle auto selling of items as they are mined.
|
/nvus autosell <true|false> - Toggle if prisoners can auto toggle auto selling of items as they are mined.
|
||||||
/nvus sellall <true|false> - Toggle if prisoners can use /sellall to manually sell items form inventory.
|
/nvus sellall <true|false> - Toggle if prisoners can use /sellall to manually sell items form inventory.
|
||||||
|
|
||||||
aliases: [prison]
|
aliases: [prison]
|
||||||
|
|
||||||
gang:
|
gang:
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
#======================================================================================#
|
#======================================================================================#
|
||||||
Ranks:
|
Ranks:
|
||||||
Default: # DO NOT CHANGE THE NAME 'Default' or this will break the plugin! This is the first/default rank!
|
Default: # DO NOT CHANGE THE NAME 'Default' or this will break the plugin! This is the first/default rank!
|
||||||
Cost: 100
|
Cost: 0 # This should always be free, as it's the default rank!
|
||||||
Commands:
|
Commands:
|
||||||
- broadcast %player% has been processed as a new inmate!
|
- broadcast %player% has been processed as a new inmate!
|
||||||
Tier2:
|
Tier2:
|
||||||
|
|
Loading…
Reference in New Issue