Modified File Name Structure, Fixed issues with not removing player permission (needed the player to rejoin),
TODO: - Add in game method to manage ranks, permissions and players
This commit is contained in:
2
gradlew.bat
vendored
2
gradlew.bat
vendored
@@ -10,7 +10,7 @@
|
|||||||
@rem Unless required by applicable law or agreed to in writing, software
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
@rem See the License for the specific language governing userInfo and
|
@rem See the License for the specific language governing User and
|
||||||
@rem limitations under the License.
|
@rem limitations under the License.
|
||||||
@rem
|
@rem
|
||||||
|
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
package me.monster.ranks;
|
package me.monster.ranks;
|
||||||
|
|
||||||
import me.monster.ranks.database.dbManager;
|
import me.monster.ranks.database.DbManager;
|
||||||
import me.monster.ranks.config.configManager;
|
import me.monster.ranks.config.ConfigManager;
|
||||||
import me.monster.ranks.utils.defaultGroups;
|
import me.monster.ranks.utils.DefaultGroups;
|
||||||
import me.monster.ranks.utils.loaders.loadCommands;
|
import me.monster.ranks.utils.loaders.LoadCommands;
|
||||||
import me.monster.ranks.utils.loaders.loadListeners;
|
import me.monster.ranks.utils.loaders.LoadListener;
|
||||||
import org.bukkit.plugin.Plugin;
|
import org.bukkit.plugin.Plugin;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
@@ -14,19 +14,17 @@ public final class Main extends JavaPlugin {
|
|||||||
|
|
||||||
public static Main plugin;
|
public static Main plugin;
|
||||||
public static Plugin getPlugin() { return plugin; }
|
public static Plugin getPlugin() { return plugin; }
|
||||||
private static dbManager database;
|
private static DbManager database;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
plugin = this;
|
plugin = this;
|
||||||
// Plugin startup logic
|
|
||||||
|
|
||||||
// Config file creation and loading
|
// Config file creation and loading
|
||||||
new configManager().createConfigFiles();
|
new ConfigManager().createConfigFiles();
|
||||||
|
|
||||||
|
|
||||||
// Database connection etc
|
// Database connection etc
|
||||||
database = new dbManager();
|
database = new DbManager();
|
||||||
try {
|
try {
|
||||||
database.initializeDatabase();
|
database.initializeDatabase();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
@@ -34,29 +32,20 @@ public final class Main extends JavaPlugin {
|
|||||||
Main.getLogger("Could not initialize database. (" + e.getMessage() + ")");
|
Main.getLogger("Could not initialize database. (" + e.getMessage() + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
new defaultGroups().createDefaultGroups();
|
new DefaultGroups().createDefaultGroups();
|
||||||
|
|
||||||
// Load event listeners and commands
|
// Load event listeners and commands
|
||||||
|
|
||||||
loadListeners.load();
|
LoadListener.load();
|
||||||
loadCommands.load();
|
LoadCommands.load();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() { Main.getLogger("Shutting down plugin"); }
|
||||||
// Plugin shutdown logic
|
|
||||||
Main.getLogger("Shutting down plugin");
|
|
||||||
|
|
||||||
}
|
public static void getLogger(String string) { plugin.getLogger().info(string); }
|
||||||
|
|
||||||
public static void getLogger(String string) {
|
public static DbManager getDatabase() { return database; }
|
||||||
plugin.getLogger().info(string);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static dbManager getDatabase() {
|
|
||||||
return database;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package me.monster.ranks.commands;
|
package me.monster.ranks.commands;
|
||||||
|
|
||||||
import me.monster.ranks.utils.updatePlayer;
|
import me.monster.ranks.utils.PlayerUpdater;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandExecutor;
|
import org.bukkit.command.CommandExecutor;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
@@ -9,13 +9,16 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class test implements CommandExecutor {
|
import static me.monster.ranks.permission.PermissionManager.clearPlayerPermissions;
|
||||||
|
|
||||||
|
public class TestCommand implements CommandExecutor {
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
|
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String @NotNull [] args) {
|
||||||
if (sender.hasPermission("ranks.test")) {
|
if (sender.hasPermission("ranks.test")) {
|
||||||
if (sender instanceof Player p) {
|
if (sender instanceof Player player) {
|
||||||
try {
|
try {
|
||||||
new updatePlayer(p);
|
clearPlayerPermissions(player);
|
||||||
|
new PlayerUpdater(player);
|
||||||
return true;
|
return true;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
@@ -7,7 +7,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
public class configManager {
|
public class ConfigManager {
|
||||||
static Plugin plugin = Main.getPlugin();
|
static Plugin plugin = Main.getPlugin();
|
||||||
|
|
||||||
|
|
||||||
@@ -26,16 +26,4 @@ public class configManager {
|
|||||||
static File configFile = new File(plugin.getDataFolder(), "config.yml");
|
static File configFile = new File(plugin.getDataFolder(), "config.yml");
|
||||||
public static @NotNull YamlConfiguration configFile() { return YamlConfiguration.loadConfiguration(configFile); }
|
public static @NotNull YamlConfiguration configFile() { return YamlConfiguration.loadConfiguration(configFile); }
|
||||||
|
|
||||||
public static void saveConfigFiles() {
|
|
||||||
Main.getLogger("Saving config");
|
|
||||||
plugin.saveConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void reloadConfigFiles() {
|
|
||||||
Main.getLogger("Reloading config");
|
|
||||||
plugin.reloadConfig();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
116
src/main/java/me/monster/ranks/database/DbManager.java
Normal file
116
src/main/java/me/monster/ranks/database/DbManager.java
Normal file
@@ -0,0 +1,116 @@
|
|||||||
|
package me.monster.ranks.database;
|
||||||
|
|
||||||
|
import me.monster.ranks.Main;
|
||||||
|
import me.monster.ranks.database.managers.GroupManager;
|
||||||
|
import me.monster.ranks.database.managers.PermissionsManager;
|
||||||
|
import me.monster.ranks.database.managers.PlayerManager;
|
||||||
|
import me.monster.ranks.database.models.GroupsPermissions;
|
||||||
|
import me.monster.ranks.config.ConfigManager;
|
||||||
|
import me.monster.ranks.database.models.User;
|
||||||
|
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class DbManager {
|
||||||
|
|
||||||
|
private static Connection connection;
|
||||||
|
|
||||||
|
public static Connection getConnection() throws SQLException {
|
||||||
|
|
||||||
|
if(connection != null){
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
String port = ConfigManager.configFile().getString("mysql.port");
|
||||||
|
String host = ConfigManager.configFile().getString("mysql.host");
|
||||||
|
String user = ConfigManager.configFile().getString("mysql.user");
|
||||||
|
String password = ConfigManager.configFile().getString("mysql.password");
|
||||||
|
String database = ConfigManager.configFile().getString("mysql.database");
|
||||||
|
String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
|
||||||
|
|
||||||
|
Connection connection = DriverManager.getConnection(url, user, password);
|
||||||
|
|
||||||
|
DbManager.connection = connection;
|
||||||
|
|
||||||
|
Main.getLogger("Connected to database.");
|
||||||
|
|
||||||
|
return connection;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void initializeDatabase() throws SQLException {
|
||||||
|
|
||||||
|
Statement statement = getConnection().createStatement();
|
||||||
|
|
||||||
|
String player_infoSQL = "CREATE TABLE IF NOT EXISTS player_info (uuid varchar(36), playerRank TEXT, temp INT, date DATETIME)";
|
||||||
|
String groupsSQL = "CREATE TABLE IF NOT EXISTS groups (groupName TEXT, groupPrefix TEXT, groupSuffix TEXT, groupWeight INT, groupParent TEXT)";
|
||||||
|
String groupsPermissionsSQL = "CREATE TABLE IF NOT EXISTS group_permissions (groupName TEXT, permission TEXT, value INT, context TEXT)";
|
||||||
|
|
||||||
|
|
||||||
|
statement.execute(player_infoSQL);
|
||||||
|
statement.execute(groupsSQL);
|
||||||
|
statement.execute(groupsPermissionsSQL);
|
||||||
|
|
||||||
|
statement.close();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//################################################
|
||||||
|
//########### Player Info Section ################
|
||||||
|
//################################################
|
||||||
|
|
||||||
|
public static User findPlayerGroupByUUID(String string) throws SQLException {
|
||||||
|
return PlayerManager.findPlayerGroupByUUID(string);
|
||||||
|
}
|
||||||
|
public static List<User> findPlayerGroups(String uuid) throws SQLException {
|
||||||
|
return PlayerManager.findPlayerGroups(uuid);
|
||||||
|
}
|
||||||
|
public static void deletePlayerGroups(User databaseModel) throws SQLException {
|
||||||
|
PlayerManager.deletePlayerGroups(databaseModel);
|
||||||
|
}
|
||||||
|
public static void updatePlayerGroups(User databaseModel) throws SQLException {
|
||||||
|
PlayerManager.updatePlayerGroups(databaseModel);
|
||||||
|
}
|
||||||
|
public static void createPlayer(User databaseModel) throws SQLException {
|
||||||
|
PlayerManager.createPlayer(databaseModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
//################################################
|
||||||
|
//############# Groups Section ##################
|
||||||
|
//################################################
|
||||||
|
|
||||||
|
public static void createGroup(me.monster.ranks.database.models.Groups databaseModel) throws SQLException {
|
||||||
|
GroupManager.createGroup(databaseModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void updateGroup(me.monster.ranks.database.models.Groups databaseModel) throws SQLException {
|
||||||
|
GroupManager.updateGroup(databaseModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deleteGroup(me.monster.ranks.database.models.Groups databaseModel) throws SQLException {
|
||||||
|
GroupManager.deleteGroup(databaseModel);
|
||||||
|
}
|
||||||
|
public static me.monster.ranks.database.models.Groups findGroupByName(String groupName) throws SQLException {
|
||||||
|
return GroupManager.findGroupByName(groupName);
|
||||||
|
}
|
||||||
|
|
||||||
|
//################################################
|
||||||
|
//########### Permission Section ################
|
||||||
|
//################################################
|
||||||
|
|
||||||
|
public static List<GroupsPermissions> fetchGroupPermissions(String groupName) throws SQLException {
|
||||||
|
return PermissionsManager.fetchGroupPermissions(groupName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void updateGroupPermission(GroupsPermissions permission) throws SQLException {
|
||||||
|
PermissionsManager.updateGroupPermission(permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addGroupPermission(GroupsPermissions permission) throws SQLException {
|
||||||
|
PermissionsManager.addGroupPermission(permission);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deleteGroupPermission(String groupName, String permission, String context) throws SQLException {
|
||||||
|
PermissionsManager.deleteGroupPermission(groupName, permission, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,117 +0,0 @@
|
|||||||
package me.monster.ranks.database;
|
|
||||||
|
|
||||||
import me.monster.ranks.Main;
|
|
||||||
import me.monster.ranks.database.managers.groups;
|
|
||||||
import me.monster.ranks.database.managers.permissions;
|
|
||||||
import me.monster.ranks.database.managers.players;
|
|
||||||
import me.monster.ranks.database.models.groupPermissions;
|
|
||||||
import me.monster.ranks.config.configManager;
|
|
||||||
import me.monster.ranks.database.models.ranks;
|
|
||||||
import me.monster.ranks.database.models.userInfo;
|
|
||||||
|
|
||||||
import java.sql.*;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class dbManager {
|
|
||||||
|
|
||||||
private static Connection connection;
|
|
||||||
|
|
||||||
public static Connection getConnection() throws SQLException {
|
|
||||||
|
|
||||||
if(connection != null){
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
String port = configManager.configFile().getString("mysql.port");
|
|
||||||
String host = configManager.configFile().getString("mysql.host");
|
|
||||||
String user = configManager.configFile().getString("mysql.user");
|
|
||||||
String password = configManager.configFile().getString("mysql.password");
|
|
||||||
String database = configManager.configFile().getString("mysql.database");
|
|
||||||
String url = "jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true";
|
|
||||||
|
|
||||||
Connection connection = DriverManager.getConnection(url, user, password);
|
|
||||||
|
|
||||||
dbManager.connection = connection;
|
|
||||||
|
|
||||||
Main.getLogger("Connected to database.");
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void initializeDatabase() throws SQLException {
|
|
||||||
|
|
||||||
Statement statement = getConnection().createStatement();
|
|
||||||
|
|
||||||
String player_infoSQL = "CREATE TABLE IF NOT EXISTS player_info (uuid varchar(36), playerRank TEXT, temp INT, date DATETIME)";
|
|
||||||
String groupsSQL = "CREATE TABLE IF NOT EXISTS groups (groupName TEXT, groupPrefix TEXT, groupSuffix TEXT, groupWeight INT, groupParent TEXT)";
|
|
||||||
String groupsPermissionsSQL = "CREATE TABLE IF NOT EXISTS group_permissions (groupName TEXT, permission TEXT, value INT, context TEXT)";
|
|
||||||
|
|
||||||
|
|
||||||
statement.execute(player_infoSQL);
|
|
||||||
statement.execute(groupsSQL);
|
|
||||||
statement.execute(groupsPermissionsSQL);
|
|
||||||
|
|
||||||
statement.close();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//################################################
|
|
||||||
//########### Player Info Section ################
|
|
||||||
//################################################
|
|
||||||
|
|
||||||
public static userInfo findPlayerGroupByUUID(String string) throws SQLException {
|
|
||||||
return players.findPlayerGroupByUUID(string);
|
|
||||||
}
|
|
||||||
public static List<userInfo> findPlayerGroups(String uuid) throws SQLException {
|
|
||||||
return players.findPlayerGroups(uuid);
|
|
||||||
}
|
|
||||||
public static void deletePlayerGroups(userInfo databaseModel) throws SQLException {
|
|
||||||
players.deletePlayerGroups(databaseModel);
|
|
||||||
}
|
|
||||||
public static void updatePlayerGroups(userInfo databaseModel) throws SQLException {
|
|
||||||
players.updatePlayerGroups(databaseModel);
|
|
||||||
}
|
|
||||||
public static void createPlayer(userInfo databaseModel) throws SQLException {
|
|
||||||
players.createPlayer(databaseModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
//################################################
|
|
||||||
//############# Groups Section ##################
|
|
||||||
//################################################
|
|
||||||
|
|
||||||
public static void createGroup(ranks databaseModel) throws SQLException {
|
|
||||||
groups.createGroup(databaseModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void updateGroup(ranks databaseModel) throws SQLException {
|
|
||||||
groups.updateGroup(databaseModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void deleteGroup(ranks databaseModel) throws SQLException {
|
|
||||||
groups.deleteGroup(databaseModel);
|
|
||||||
}
|
|
||||||
public static ranks findGroupByName(String groupName) throws SQLException {
|
|
||||||
return groups.findGroupByName(groupName);
|
|
||||||
}
|
|
||||||
|
|
||||||
//################################################
|
|
||||||
//########### Permission Section ################
|
|
||||||
//################################################
|
|
||||||
|
|
||||||
public static List<groupPermissions> fetchGroupPermissions(String groupName) throws SQLException {
|
|
||||||
return permissions.fetchGroupPermissions(groupName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void updateGroupPermission(groupPermissions permission) throws SQLException {
|
|
||||||
permissions.updateGroupPermission(permission);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void addGroupPermission(groupPermissions permission) throws SQLException {
|
|
||||||
permissions.addGroupPermission(permission);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void deleteGroupPermission(String groupName, String permission, String context) throws SQLException {
|
|
||||||
permissions.deleteGroupPermission(groupName, permission, context);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,16 +1,15 @@
|
|||||||
package me.monster.ranks.database.managers;
|
package me.monster.ranks.database.managers;
|
||||||
|
|
||||||
import me.monster.ranks.Main;
|
import me.monster.ranks.Main;
|
||||||
import me.monster.ranks.database.dbManager;
|
import me.monster.ranks.database.DbManager;
|
||||||
import me.monster.ranks.database.models.ranks;
|
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class groups extends dbManager {
|
public class GroupManager extends DbManager {
|
||||||
|
|
||||||
public static void createGroup(ranks databaseModel) throws SQLException {
|
public static void createGroup(me.monster.ranks.database.models.Groups databaseModel) throws SQLException {
|
||||||
|
|
||||||
PreparedStatement statement = getConnection()
|
PreparedStatement statement = getConnection()
|
||||||
.prepareStatement("INSERT INTO groups (groupName, groupPrefix, groupSuffix, groupWeight, groupParent) VALUES (?, ?, ?, ?, ?)");
|
.prepareStatement("INSERT INTO groups (groupName, groupPrefix, groupSuffix, groupWeight, groupParent) VALUES (?, ?, ?, ?, ?)");
|
||||||
@@ -25,7 +24,7 @@ public class groups extends dbManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateGroup(ranks databaseModel) throws SQLException {
|
public static void updateGroup(me.monster.ranks.database.models.Groups databaseModel) throws SQLException {
|
||||||
|
|
||||||
PreparedStatement statement = getConnection()
|
PreparedStatement statement = getConnection()
|
||||||
.prepareStatement("UPDATE groups SET groupName = ?, groupPrefix = ?, groupSuffix = ?, groupWeight = ?, groupParent = ? WHERE groupName = ?");
|
.prepareStatement("UPDATE groups SET groupName = ?, groupPrefix = ?, groupSuffix = ?, groupWeight = ?, groupParent = ? WHERE groupName = ?");
|
||||||
@@ -40,7 +39,7 @@ public class groups extends dbManager {
|
|||||||
statement.close();
|
statement.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void deleteGroup(ranks databaseModel) throws SQLException {
|
public static void deleteGroup(me.monster.ranks.database.models.Groups databaseModel) throws SQLException {
|
||||||
|
|
||||||
PreparedStatement statement = getConnection().prepareStatement("DELETE FROM groups WHERE groupName = ?");
|
PreparedStatement statement = getConnection().prepareStatement("DELETE FROM groups WHERE groupName = ?");
|
||||||
statement.setString(1, databaseModel.getGroupName());
|
statement.setString(1, databaseModel.getGroupName());
|
||||||
@@ -52,13 +51,13 @@ public class groups extends dbManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static ranks findGroupByName(String groupName) throws SQLException {
|
public static me.monster.ranks.database.models.Groups findGroupByName(String groupName) throws SQLException {
|
||||||
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM groups WHERE groupName = ?");
|
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM groups WHERE groupName = ?");
|
||||||
statement.setString(1, groupName);
|
statement.setString(1, groupName);
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
ranks databaseModel;
|
me.monster.ranks.database.models.Groups databaseModel;
|
||||||
if(resultSet.next()){
|
if(resultSet.next()){
|
||||||
databaseModel = new ranks(resultSet.getString("groupName"), resultSet.getString("groupPrefix"), resultSet.getString("groupSuffix"), resultSet.getInt("groupWeight"), resultSet.getString("groupParent"));
|
databaseModel = new me.monster.ranks.database.models.Groups(resultSet.getString("groupName"), resultSet.getString("groupPrefix"), resultSet.getString("groupSuffix"), resultSet.getInt("groupWeight"), resultSet.getString("groupParent"));
|
||||||
statement.close();
|
statement.close();
|
||||||
return databaseModel;
|
return databaseModel;
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package me.monster.ranks.database.managers;
|
package me.monster.ranks.database.managers;
|
||||||
|
|
||||||
import me.monster.ranks.database.dbManager;
|
import me.monster.ranks.database.DbManager;
|
||||||
import me.monster.ranks.database.models.groupPermissions;
|
import me.monster.ranks.database.models.GroupsPermissions;
|
||||||
|
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
@@ -9,19 +9,19 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class permissions extends dbManager {
|
public class PermissionsManager extends DbManager {
|
||||||
|
|
||||||
public static List<groupPermissions> fetchGroupPermissions(String groupName) throws SQLException {
|
public static List<GroupsPermissions> fetchGroupPermissions(String groupName) throws SQLException {
|
||||||
String query = "SELECT * FROM group_permissions WHERE groupName = ?";
|
String query = "SELECT * FROM group_permissions WHERE groupName = ?";
|
||||||
PreparedStatement statement = getConnection().prepareStatement(query);
|
PreparedStatement statement = getConnection().prepareStatement(query);
|
||||||
statement.setString(1, groupName);
|
statement.setString(1, groupName);
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
List<groupPermissions> permissions = new ArrayList<>();
|
List<GroupsPermissions> permissions = new ArrayList<>();
|
||||||
|
|
||||||
|
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
groupPermissions permission = new groupPermissions(
|
GroupsPermissions permission = new GroupsPermissions(
|
||||||
resultSet.getString("groupName"),
|
resultSet.getString("groupName"),
|
||||||
resultSet.getString("permission"),
|
resultSet.getString("permission"),
|
||||||
resultSet.getInt("value"),
|
resultSet.getInt("value"),
|
||||||
@@ -34,7 +34,7 @@ public class permissions extends dbManager {
|
|||||||
return permissions;
|
return permissions;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updateGroupPermission(groupPermissions permission) throws SQLException {
|
public static void updateGroupPermission(GroupsPermissions permission) throws SQLException {
|
||||||
String query = "UPDATE group_permissions SET permission = ?, value = ?, context = ? WHERE groupName = ? AND permission = ? AND context = ?";
|
String query = "UPDATE group_permissions SET permission = ?, value = ?, context = ? WHERE groupName = ? AND permission = ? AND context = ?";
|
||||||
PreparedStatement statement = getConnection().prepareStatement(query);
|
PreparedStatement statement = getConnection().prepareStatement(query);
|
||||||
|
|
||||||
@@ -49,7 +49,7 @@ public class permissions extends dbManager {
|
|||||||
statement.close();
|
statement.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void addGroupPermission(groupPermissions permission) throws SQLException {
|
public static void addGroupPermission(GroupsPermissions permission) throws SQLException {
|
||||||
String query = "INSERT INTO group_permissions (groupName, permission, value, context) VALUES (?, ?, ?, ?)";
|
String query = "INSERT INTO group_permissions (groupName, permission, value, context) VALUES (?, ?, ?, ?)";
|
||||||
PreparedStatement statement = getConnection().prepareStatement(query);
|
PreparedStatement statement = getConnection().prepareStatement(query);
|
||||||
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
package me.monster.ranks.database.managers;
|
package me.monster.ranks.database.managers;
|
||||||
|
|
||||||
import me.monster.ranks.database.dbManager;
|
import me.monster.ranks.database.DbManager;
|
||||||
import me.monster.ranks.database.models.userInfo;
|
import me.monster.ranks.database.models.User;
|
||||||
|
|
||||||
import java.sql.Date;
|
import java.sql.Date;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
@@ -10,20 +10,20 @@ import java.sql.SQLException;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class players extends dbManager {
|
public class PlayerManager extends DbManager {
|
||||||
|
|
||||||
public static userInfo findPlayerGroupByUUID(String uuid) throws SQLException {
|
public static User findPlayerGroupByUUID(String uuid) throws SQLException {
|
||||||
|
|
||||||
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM player_info WHERE uuid = ?");
|
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM player_info WHERE uuid = ?");
|
||||||
statement.setString(1, uuid);
|
statement.setString(1, uuid);
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
|
||||||
userInfo databaseModel;
|
User databaseModel;
|
||||||
|
|
||||||
if(resultSet.next()){
|
if(resultSet.next()){
|
||||||
|
|
||||||
databaseModel = new userInfo(resultSet.getString("uuid"), resultSet.getString("playerRank"), resultSet.getInt("temp"), resultSet.getDate("date"));
|
databaseModel = new User(resultSet.getString("uuid"), resultSet.getString("playerRank"), resultSet.getInt("temp"), resultSet.getDate("date"));
|
||||||
statement.close();
|
statement.close();
|
||||||
return databaseModel;
|
return databaseModel;
|
||||||
}
|
}
|
||||||
@@ -33,17 +33,17 @@ public class players extends dbManager {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static List<userInfo> findPlayerGroups(String uuid) throws SQLException {
|
public static List<User> findPlayerGroups(String uuid) throws SQLException {
|
||||||
|
|
||||||
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM player_info WHERE uuid = ?");
|
PreparedStatement statement = getConnection().prepareStatement("SELECT * FROM player_info WHERE uuid = ?");
|
||||||
statement.setString(1, uuid);
|
statement.setString(1, uuid);
|
||||||
|
|
||||||
ResultSet resultSet = statement.executeQuery();
|
ResultSet resultSet = statement.executeQuery();
|
||||||
|
|
||||||
List<userInfo> databaseModel = new ArrayList<>(); // Use ArrayList to hold all results
|
List<User> databaseModel = new ArrayList<>(); // Use ArrayList to hold all results
|
||||||
|
|
||||||
while (resultSet.next()) {
|
while (resultSet.next()) {
|
||||||
userInfo user = new userInfo(resultSet.getString("uuid"), resultSet.getString("playerRank"), resultSet.getInt("temp"), resultSet.getTimestamp("date"));
|
User user = new User(resultSet.getString("uuid"), resultSet.getString("playerRank"), resultSet.getInt("temp"), resultSet.getTimestamp("date"));
|
||||||
databaseModel.add(user);
|
databaseModel.add(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ public class players extends dbManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void createPlayer(userInfo databaseModel) throws SQLException {
|
public static void createPlayer(User databaseModel) throws SQLException {
|
||||||
PreparedStatement statement = getConnection()
|
PreparedStatement statement = getConnection()
|
||||||
.prepareStatement("INSERT INTO player_info (uuid, playerRank, temp, date) VALUES (?, ?, ?, ?)");
|
.prepareStatement("INSERT INTO player_info (uuid, playerRank, temp, date) VALUES (?, ?, ?, ?)");
|
||||||
statement.setString(1, databaseModel.getPlayerUUID());
|
statement.setString(1, databaseModel.getPlayerUUID());
|
||||||
@@ -64,7 +64,7 @@ public class players extends dbManager {
|
|||||||
statement.close();
|
statement.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void updatePlayerGroups(userInfo databaseModel) throws SQLException {
|
public static void updatePlayerGroups(User databaseModel) throws SQLException {
|
||||||
PreparedStatement statement = getConnection().prepareStatement("UPDATE player_info SET playerRank = ?, temp = ?, date = ? WHERE uuid = ?");
|
PreparedStatement statement = getConnection().prepareStatement("UPDATE player_info SET playerRank = ?, temp = ?, date = ? WHERE uuid = ?");
|
||||||
statement.setString(1, databaseModel.getPlayerRank());
|
statement.setString(1, databaseModel.getPlayerRank());
|
||||||
statement.setInt(2, databaseModel.getPlayerRankTemp());
|
statement.setInt(2, databaseModel.getPlayerRankTemp());
|
||||||
@@ -74,7 +74,7 @@ public class players extends dbManager {
|
|||||||
statement.close();
|
statement.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void deletePlayerGroups(userInfo databaseModel) throws SQLException {
|
public static void deletePlayerGroups(User databaseModel) throws SQLException {
|
||||||
|
|
||||||
PreparedStatement statement = getConnection().prepareStatement("DELETE FROM player_info WHERE uuid = ? AND playerRank = ?");
|
PreparedStatement statement = getConnection().prepareStatement("DELETE FROM player_info WHERE uuid = ? AND playerRank = ?");
|
||||||
statement.setString(1, databaseModel.getPlayerUUID());
|
statement.setString(1, databaseModel.getPlayerUUID());
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package me.monster.ranks.database.models;
|
package me.monster.ranks.database.models;
|
||||||
|
|
||||||
public class ranks {
|
public class Groups {
|
||||||
private String groupName;
|
private String groupName;
|
||||||
private String groupPrefix;
|
private String groupPrefix;
|
||||||
private String groupSuffix;
|
private String groupSuffix;
|
||||||
private Integer groupWeight;
|
private Integer groupWeight;
|
||||||
private String groupParent;
|
private String groupParent;
|
||||||
|
|
||||||
public ranks(String groupName, String groupPrefix, String groupSuffix, Integer groupWeight, String groupParent) {
|
public Groups(String groupName, String groupPrefix, String groupSuffix, Integer groupWeight, String groupParent) {
|
||||||
this.groupName = groupName;
|
this.groupName = groupName;
|
||||||
this.groupPrefix = groupPrefix;
|
this.groupPrefix = groupPrefix;
|
||||||
this.groupSuffix = groupSuffix;
|
this.groupSuffix = groupSuffix;
|
||||||
@@ -1,13 +1,13 @@
|
|||||||
package me.monster.ranks.database.models;
|
package me.monster.ranks.database.models;
|
||||||
|
|
||||||
public class groupPermissions {
|
public class GroupsPermissions {
|
||||||
|
|
||||||
private String groupName;
|
private String groupName;
|
||||||
private String permission;
|
private String permission;
|
||||||
private Integer value;
|
private Integer value;
|
||||||
private String context;
|
private String context;
|
||||||
|
|
||||||
public groupPermissions(String groupName, String permission, Integer value, String context) {
|
public GroupsPermissions(String groupName, String permission, Integer value, String context) {
|
||||||
this.groupName = groupName;
|
this.groupName = groupName;
|
||||||
this.permission = permission;
|
this.permission = permission;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
@@ -2,13 +2,13 @@ package me.monster.ranks.database.models;
|
|||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class userInfo {
|
public class User {
|
||||||
private String playerUUID;
|
private String playerUUID;
|
||||||
private String playerRank;
|
private String playerRank;
|
||||||
private Integer temp;
|
private Integer temp;
|
||||||
private Date date;
|
private Date date;
|
||||||
|
|
||||||
public userInfo(String playerUUID, String playerRank, Integer temp, Date date) {
|
public User(String playerUUID, String playerRank, Integer temp, Date date) {
|
||||||
this.playerUUID = playerUUID;
|
this.playerUUID = playerUUID;
|
||||||
this.playerRank = playerRank;
|
this.playerRank = playerRank;
|
||||||
this.temp = temp;
|
this.temp = temp;
|
||||||
@@ -1,18 +1,18 @@
|
|||||||
package me.monster.ranks.listeners;
|
package me.monster.ranks.listeners;
|
||||||
|
|
||||||
import me.monster.ranks.utils.groupsPriority;
|
import me.monster.ranks.utils.GroupsPriority;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.EventPriority;
|
import org.bukkit.event.EventPriority;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
|
||||||
public class chatListener implements Listener {
|
public class ChatListener implements Listener {
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.HIGH)
|
@EventHandler(priority = EventPriority.HIGH)
|
||||||
public void onChat(org.bukkit.event.player.AsyncPlayerChatEvent event) {
|
public void onChat(org.bukkit.event.player.AsyncPlayerChatEvent event) {
|
||||||
Player p = event.getPlayer();
|
Player p = event.getPlayer();
|
||||||
|
|
||||||
event.setFormat(groupsPriority.getHighestPrefix(p) + " " + event.getPlayer().getDisplayName() + groupsPriority.getHighestSuffix(p) + ": " + event.getMessage());
|
event.setFormat(GroupsPriority.getHighestPrefix(p) + " " + event.getPlayer().getDisplayName() + GroupsPriority.getHighestSuffix(p) + ": " + event.getMessage());
|
||||||
event.setCancelled(false);
|
event.setCancelled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
package me.monster.ranks.listeners;
|
package me.monster.ranks.listeners;
|
||||||
|
|
||||||
import me.monster.ranks.Main;
|
import me.monster.ranks.Main;
|
||||||
import me.monster.ranks.database.dbManager;
|
import me.monster.ranks.database.DbManager;
|
||||||
import me.monster.ranks.database.models.userInfo;
|
import me.monster.ranks.database.models.User;
|
||||||
import me.monster.ranks.utils.updatePlayer;
|
import me.monster.ranks.utils.PlayerUpdater;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
import org.bukkit.event.EventHandler;
|
import org.bukkit.event.EventHandler;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
@@ -11,14 +11,13 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
|||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class joinListener implements Listener {
|
public class JoinListener implements Listener {
|
||||||
|
|
||||||
public void createPlayer(Player player) throws SQLException {
|
public void createPlayer(Player player) throws SQLException {
|
||||||
userInfo userInfo2 = dbManager.findPlayerGroupByUUID(player.getUniqueId().toString());
|
User user2 = DbManager.findPlayerGroupByUUID(player.getUniqueId().toString());
|
||||||
|
|
||||||
if(userInfo2 == null) {
|
|
||||||
dbManager.createPlayer(new userInfo(player.getUniqueId().toString(), "default", 0, null));
|
|
||||||
|
|
||||||
|
if(user2 == null) {
|
||||||
|
DbManager.createPlayer(new User(player.getUniqueId().toString(), "default", 0, null));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -31,7 +30,7 @@ public class joinListener implements Listener {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Main.getLogger("Could not update player stats after join.");
|
Main.getLogger("Could not update player stats after join.");
|
||||||
}
|
}
|
||||||
new updatePlayer(p);
|
new PlayerUpdater(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
149
src/main/java/me/monster/ranks/permission/PermissionManager.java
Normal file
149
src/main/java/me/monster/ranks/permission/PermissionManager.java
Normal file
@@ -0,0 +1,149 @@
|
|||||||
|
package me.monster.ranks.permission;
|
||||||
|
|
||||||
|
import me.monster.ranks.Main;
|
||||||
|
import me.monster.ranks.database.DbManager;
|
||||||
|
import me.monster.ranks.database.models.GroupsPermissions;
|
||||||
|
import me.monster.ranks.database.models.Groups;
|
||||||
|
import me.monster.ranks.utils.GroupsPriority;
|
||||||
|
import org.bukkit.Bukkit;
|
||||||
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.permissions.Permission;
|
||||||
|
import org.bukkit.permissions.PermissionAttachment;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
import static me.monster.ranks.config.ConfigManager.configFile;
|
||||||
|
|
||||||
|
public class PermissionManager {
|
||||||
|
|
||||||
|
static PermissionAttachment attachment;
|
||||||
|
|
||||||
|
static HashMap<UUID, PermissionAttachment> perms = new HashMap<>();
|
||||||
|
|
||||||
|
private static final YamlConfiguration config = configFile();
|
||||||
|
|
||||||
|
public static void loadAndApplyPermissions(Player player) {
|
||||||
|
try {
|
||||||
|
// denyRemovedPermissions(player, getHierarchyPermissions(player));
|
||||||
|
applyPermissionsToPlayer(player, getHierarchyPermissions(player));
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace(); // Log or handle the exception as needed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<Groups> getGroupHierarchy(Player player) throws SQLException {
|
||||||
|
|
||||||
|
List<Groups> groupHierarchy = new ArrayList<>();
|
||||||
|
|
||||||
|
Groups highestGroup = GroupsPriority.getHighestGroup(player);
|
||||||
|
|
||||||
|
groupHierarchy.add(highestGroup);
|
||||||
|
|
||||||
|
groupHierarchy.add(DbManager.findGroupByName(highestGroup.getGroupParent()));
|
||||||
|
while (!groupHierarchy.getFirst().getGroupParent().equals("default")) {
|
||||||
|
highestGroup = DbManager.findGroupByName(groupHierarchy.getFirst().getGroupParent());
|
||||||
|
groupHierarchy.addFirst(highestGroup);
|
||||||
|
}
|
||||||
|
groupHierarchy.addFirst(DbManager.findGroupByName("default"));
|
||||||
|
for (Groups rank : groupHierarchy) {
|
||||||
|
Main.getLogger("Group: " + rank.getGroupName());
|
||||||
|
}
|
||||||
|
return groupHierarchy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<GroupsPermissions> getHierarchyPermissions(Player p) throws SQLException {
|
||||||
|
|
||||||
|
List<Groups> groupHierarchy = getGroupHierarchy(p);
|
||||||
|
List<GroupsPermissions> permissions = new ArrayList<>();
|
||||||
|
|
||||||
|
for (Groups rank : groupHierarchy) {
|
||||||
|
permissions.addAll(DbManager.fetchGroupPermissions(rank.getGroupName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
return permissions;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String getServerName() {
|
||||||
|
return config.getString("server.name");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void applyPermissionsToPlayer(Player player, List<GroupsPermissions> permissions) {
|
||||||
|
attachment = player.addAttachment(Main.getPlugin());
|
||||||
|
|
||||||
|
// Clear existing permissions (optional: only if you need to refresh)
|
||||||
|
attachment.getPermissions().clear();
|
||||||
|
|
||||||
|
// Loop through all the permissions for the player's group
|
||||||
|
for (GroupsPermissions permission : permissions) {
|
||||||
|
String perm = permission.getPermission();
|
||||||
|
boolean allow = permission.getValue() == 1; // 1 = allow, 0 = deny
|
||||||
|
if (permission.getContext().equals("server=" + getServerName()) || permission.getContext().isEmpty()) {
|
||||||
|
perms.put(player.getUniqueId(), attachment);
|
||||||
|
PermissionAttachment pperms = perms.get(player.getUniqueId());
|
||||||
|
pperms.setPermission(perm, allow);
|
||||||
|
}
|
||||||
|
if (permission.getPermission().equals("*")) {
|
||||||
|
setAllPermissions(player, attachment, allow);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void setAllPermissions(Player player, PermissionAttachment attachment, boolean allow) {
|
||||||
|
Set<Permission> permissions = Bukkit.getPluginManager().getPermissions();
|
||||||
|
for (Permission permission : permissions) {
|
||||||
|
perms.put(player.getUniqueId(), attachment);
|
||||||
|
PermissionAttachment pperms = perms.get(player.getUniqueId());
|
||||||
|
pperms.setPermission(permission, allow);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// public static void denyRemovedPermissions(Player player, List<GroupsPermissions> currentPermissions) {
|
||||||
|
// // Retrieve the player's current permission attachment
|
||||||
|
// PermissionAttachment attachment = perms.get(player.getUniqueId());
|
||||||
|
//
|
||||||
|
// if (attachment == null) {
|
||||||
|
// return; // If no permissions are set, nothing to deny
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Get the existing permissions the player has
|
||||||
|
// Set<String> currentPerms = new HashSet<>(attachment.getPermissions().keySet());
|
||||||
|
//
|
||||||
|
// // Get the permissions the player should have after group modification
|
||||||
|
// Set<String> newPerms = new HashSet<>();
|
||||||
|
// for (GroupsPermissions perm : currentPermissions) {
|
||||||
|
// newPerms.add(perm.getPermission());
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Loop through current permissions, and deny those that are no longer present
|
||||||
|
// for (String perm : currentPerms) {
|
||||||
|
// if (!newPerms.contains(perm)) {
|
||||||
|
// // Deny this permission for the player (remove it from the attachment)
|
||||||
|
// attachment.setPermission(perm, false);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
public static void clearPlayerPermissions(Player player) {
|
||||||
|
// Ensure the player has a permission attachment
|
||||||
|
if (perms.containsKey(player.getUniqueId())) {
|
||||||
|
// Get the permission attachment
|
||||||
|
PermissionAttachment attachment = perms.get(player.getUniqueId());
|
||||||
|
|
||||||
|
// Remove the attachment from the player, which clears their permissions
|
||||||
|
player.removeAttachment(attachment);
|
||||||
|
|
||||||
|
// Optionally, clear the entry from the perms map
|
||||||
|
perms.remove(player.getUniqueId());
|
||||||
|
|
||||||
|
// Log or notify that the permissions were cleared
|
||||||
|
Main.getLogger("Cleared permissions for player: " + player.getName());
|
||||||
|
} else {
|
||||||
|
Main.getLogger("No permissions found for player: " + player.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,88 +0,0 @@
|
|||||||
package me.monster.ranks.permission;
|
|
||||||
|
|
||||||
import me.monster.ranks.Main;
|
|
||||||
import me.monster.ranks.database.dbManager;
|
|
||||||
import me.monster.ranks.database.models.groupPermissions;
|
|
||||||
import me.monster.ranks.database.models.ranks;
|
|
||||||
import me.monster.ranks.utils.groupsPriority;
|
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
import org.bukkit.permissions.Permission;
|
|
||||||
import org.bukkit.permissions.PermissionAttachment;
|
|
||||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
import static me.monster.ranks.config.configManager.configFile;
|
|
||||||
|
|
||||||
public class permissionManager {
|
|
||||||
|
|
||||||
static HashMap<UUID, PermissionAttachment> perms = new HashMap<>();
|
|
||||||
|
|
||||||
private static final YamlConfiguration config = configFile();
|
|
||||||
|
|
||||||
|
|
||||||
public static void loadAndApplyPermissions(Player player) {
|
|
||||||
try {
|
|
||||||
// Step 1: Get the player's group and server context
|
|
||||||
ranks groupName = groupsPriority.getHighestGroup(player);
|
|
||||||
|
|
||||||
// Step 2: Fetch the permissions for the group and context
|
|
||||||
List<groupPermissions> permissions = dbManager.fetchGroupPermissions(groupName.getGroupName());
|
|
||||||
|
|
||||||
// Step 3: Apply the permissions to the player
|
|
||||||
applyPermissionsToPlayer(player, permissions);
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace(); // Log or handle the exception as needed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private static String getServerName() {
|
|
||||||
return config.getString("server.name");
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void applyPermissionsToPlayer(Player player, List<groupPermissions> permissions) {
|
|
||||||
PermissionAttachment attachment = player.addAttachment(Main.getPlugin());
|
|
||||||
removePermissions(player, attachment);
|
|
||||||
|
|
||||||
// Clear existing permissions (optional: only if you need to refresh)
|
|
||||||
attachment.getPermissions().clear();
|
|
||||||
|
|
||||||
// Loop through all the permissions for the player's group
|
|
||||||
for (groupPermissions permission : permissions) {
|
|
||||||
String perm = permission.getPermission();
|
|
||||||
boolean allow = permission.getValue() == 1; // 1 = allow, 0 = deny
|
|
||||||
if (permission.getContext().equals("server=" + getServerName()) || permission.getContext().isEmpty()) {
|
|
||||||
perms.put(player.getUniqueId(), attachment);
|
|
||||||
PermissionAttachment pperms = perms.get(player.getUniqueId());
|
|
||||||
pperms.setPermission(perm, allow);
|
|
||||||
}
|
|
||||||
if (permission.getPermission().equals("*")) {
|
|
||||||
getAllPermissions(player, attachment, allow);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void getAllPermissions(Player player, PermissionAttachment attachment, boolean allow) {
|
|
||||||
Set<Permission> permissions = Main.getPlugin().getServer().getPluginManager().getPermissions();
|
|
||||||
for (Permission permission : permissions) {
|
|
||||||
perms.put(player.getUniqueId(), attachment);
|
|
||||||
PermissionAttachment pperms = perms.get(player.getUniqueId());
|
|
||||||
pperms.setPermission(permission, allow);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void removePermissions(Player player, PermissionAttachment attachment) {
|
|
||||||
for (PermissionAttachmentInfo perm : player.getEffectivePermissions()) {
|
|
||||||
perms.put(player.getUniqueId(), attachment);
|
|
||||||
PermissionAttachment pperms = perms.get(player.getUniqueId());
|
|
||||||
pperms.unsetPermission(String.valueOf(perm));
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
21
src/main/java/me/monster/ranks/utils/DefaultGroups.java
Normal file
21
src/main/java/me/monster/ranks/utils/DefaultGroups.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package me.monster.ranks.utils;
|
||||||
|
|
||||||
|
import me.monster.ranks.database.DbManager;
|
||||||
|
import me.monster.ranks.database.models.Groups;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class DefaultGroups {
|
||||||
|
public void createDefaultGroups() {
|
||||||
|
try {
|
||||||
|
if (DbManager.findGroupByName("default") == null) {
|
||||||
|
DbManager.createGroup(new Groups("default", "Player", "", 1, ""));
|
||||||
|
}
|
||||||
|
if (DbManager.findGroupByName("owner") == null ){
|
||||||
|
DbManager.createGroup(new Groups("owner", "Owner ", "", 90, "default"));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
48
src/main/java/me/monster/ranks/utils/GroupsPriority.java
Normal file
48
src/main/java/me/monster/ranks/utils/GroupsPriority.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
package me.monster.ranks.utils;
|
||||||
|
|
||||||
|
import me.monster.ranks.database.DbManager;
|
||||||
|
import me.monster.ranks.database.models.Groups;
|
||||||
|
import me.monster.ranks.database.models.User;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class GroupsPriority {
|
||||||
|
|
||||||
|
public static Groups getHighestGroup(Player p) throws SQLException {
|
||||||
|
List<User> test = DbManager.findPlayerGroups(p.getUniqueId().toString());
|
||||||
|
|
||||||
|
String highestGroupName = null;
|
||||||
|
int highestWeight = Integer.MIN_VALUE; // Start with the lowest possible weight
|
||||||
|
|
||||||
|
for (User User : test) {
|
||||||
|
String groupName = User.getPlayerRank(); // Replace with the correct method or field
|
||||||
|
Groups group = DbManager.findGroupByName(groupName); // Retrieve the group from the database
|
||||||
|
if (group != null) {
|
||||||
|
int weight = group.getGroupWeight();
|
||||||
|
if (weight > highestWeight) {
|
||||||
|
highestWeight = weight;
|
||||||
|
highestGroupName = group.getGroupName(); // Return the group name with the highest weight
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return DbManager.findGroupByName(highestGroupName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getHighestPrefix(Player p) {
|
||||||
|
try {
|
||||||
|
return getHighestGroup(p).getGroupPrefix();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static String getHighestSuffix(Player p) {
|
||||||
|
try {
|
||||||
|
return getHighestGroup(p).getGroupSuffix();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
18
src/main/java/me/monster/ranks/utils/PlayerUpdater.java
Normal file
18
src/main/java/me/monster/ranks/utils/PlayerUpdater.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package me.monster.ranks.utils;
|
||||||
|
|
||||||
|
import me.monster.ranks.database.models.Groups;
|
||||||
|
import me.monster.ranks.permission.PermissionManager;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
|
||||||
|
public class PlayerUpdater {
|
||||||
|
public PlayerUpdater(Player player) throws SQLException {
|
||||||
|
Groups highestGroup = GroupsPriority.getHighestGroup(player);
|
||||||
|
player.setPlayerListName(highestGroup.getGroupPrefix() + " " + player.getName() + highestGroup.getGroupSuffix());
|
||||||
|
PermissionManager.loadAndApplyPermissions(player);
|
||||||
|
player.recalculatePermissions();
|
||||||
|
player.updateCommands();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +0,0 @@
|
|||||||
package me.monster.ranks.utils;
|
|
||||||
|
|
||||||
import me.monster.ranks.database.dbManager;
|
|
||||||
import me.monster.ranks.database.models.ranks;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class defaultGroups {
|
|
||||||
public void createDefaultGroups() {
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (dbManager.findGroupByName("default") == null) {
|
|
||||||
dbManager.createGroup(new ranks("default", "Player", "", 1, "default"));
|
|
||||||
}
|
|
||||||
if (dbManager.findGroupByName("owner") == null ){
|
|
||||||
dbManager.createGroup(new ranks("owner", "Owner ", "", 90, "default"));
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
package me.monster.ranks.utils;
|
|
||||||
|
|
||||||
import me.monster.ranks.database.dbManager;
|
|
||||||
import me.monster.ranks.database.models.ranks;
|
|
||||||
import me.monster.ranks.database.models.userInfo;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class groupsPriority {
|
|
||||||
|
|
||||||
public static ranks getHighestGroup(Player p) throws SQLException {
|
|
||||||
// Get the list of groups associated with the player (userInfo objects)
|
|
||||||
List<userInfo> test = dbManager.findPlayerGroups(p.getUniqueId().toString());
|
|
||||||
|
|
||||||
String highestGroupName = null;
|
|
||||||
int highestWeight = Integer.MIN_VALUE; // Start with the lowest possible weight
|
|
||||||
|
|
||||||
// Loop through the list of groups associated with the player
|
|
||||||
for (userInfo userInfo : test) {
|
|
||||||
// Assuming test.get(i) contains some identifier that can be used to fetch the group
|
|
||||||
String groupName = userInfo.getPlayerRank(); // Replace with the correct method or field
|
|
||||||
ranks group = dbManager.findGroupByName(groupName); // Retrieve the group from the database
|
|
||||||
|
|
||||||
if (group != null) {
|
|
||||||
// Get the weight of the group
|
|
||||||
int weight = group.getGroupWeight();
|
|
||||||
|
|
||||||
// If the current group weight is higher than the previous highest, update the highest group
|
|
||||||
if (weight > highestWeight) {
|
|
||||||
highestWeight = weight;
|
|
||||||
highestGroupName = group.getGroupName(); // Return the group name with the highest weight
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return the name of the group with the highest weight
|
|
||||||
return dbManager.findGroupByName(highestGroupName);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String getHighestPrefix(Player p) {
|
|
||||||
try {
|
|
||||||
return getHighestGroup(p).getGroupPrefix();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public static String getHighestSuffix(Player p) {
|
|
||||||
try {
|
|
||||||
return getHighestGroup(p).getGroupSuffix();
|
|
||||||
} catch (SQLException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -14,7 +14,7 @@ import java.util.Objects;
|
|||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
public class loadCommands {
|
public class LoadCommands {
|
||||||
|
|
||||||
public static void load() {
|
public static void load() {
|
||||||
Plugin plugin = Main.getPlugin();
|
Plugin plugin = Main.getPlugin();
|
||||||
@@ -38,7 +38,7 @@ public class loadCommands {
|
|||||||
instance = clazz.getDeclaredConstructor().newInstance();
|
instance = clazz.getDeclaredConstructor().newInstance();
|
||||||
}
|
}
|
||||||
|
|
||||||
String commandName = clazz.getSimpleName().toLowerCase(); // assuming command name is class name
|
String commandName = clazz.getSimpleName().toLowerCase().replace("command", ""); // assuming command name is class name
|
||||||
PluginCommand command = javaPlugin.getCommand(commandName);
|
PluginCommand command = javaPlugin.getCommand(commandName);
|
||||||
if (command != null) {
|
if (command != null) {
|
||||||
command.setExecutor((CommandExecutor) instance);
|
command.setExecutor((CommandExecutor) instance);
|
||||||
@@ -12,7 +12,7 @@ import java.util.Objects;
|
|||||||
import java.util.jar.JarEntry;
|
import java.util.jar.JarEntry;
|
||||||
import java.util.jar.JarFile;
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
public class loadListeners {
|
public class LoadListener {
|
||||||
|
|
||||||
public static void load() {
|
public static void load() {
|
||||||
Plugin plugin = Main.getPlugin();
|
Plugin plugin = Main.getPlugin();
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package me.monster.ranks.utils;
|
|
||||||
|
|
||||||
import me.monster.ranks.database.models.ranks;
|
|
||||||
import me.monster.ranks.permission.permissionManager;
|
|
||||||
import org.bukkit.entity.Player;
|
|
||||||
|
|
||||||
import java.sql.SQLException;
|
|
||||||
|
|
||||||
public class updatePlayer {
|
|
||||||
public updatePlayer(Player player) throws SQLException {
|
|
||||||
|
|
||||||
ranks highestGroup = groupsPriority.getHighestGroup(player);
|
|
||||||
|
|
||||||
player.setPlayerListName(highestGroup.getGroupPrefix() + " " + player.getName());
|
|
||||||
permissionManager.loadAndApplyPermissions(player);
|
|
||||||
player.recalculatePermissions();
|
|
||||||
player.updateCommands();
|
|
||||||
player.sendMessage("Your group has been updated to " + highestGroup.getGroupName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user