refactor packages and rename + add sql permission system + mongodb

This commit is contained in:
Taah 2022-04-11 10:56:26 -07:00
parent 583f502cee
commit 336f17b1cf
21 changed files with 212 additions and 72 deletions

View File

@ -47,7 +47,9 @@ dependencies {
library "org.eclipse.jetty:jetty-proxy:11.0.9"
library "com.google.code.gson:gson:2.9.0"
compileOnly "io.papermc.paper:paper-api:1.18.2-R0.1-SNAPSHOT"
compileOnly "com.github.MilkBowl:VaultAPI:1.7"
compileOnly ("com.github.MilkBowl:VaultAPI:1.7") {
exclude group: "org.bukkit", module: "bukkit"
}
implementation "org.bstats:bstats-base:3.0.0"
implementation "org.bstats:bstats-bukkit:3.0.0"
}

View File

@ -3,11 +3,12 @@ package dev.plex;
import dev.plex.admin.Admin;
import dev.plex.admin.AdminList;
import dev.plex.cache.DataUtils;
import dev.plex.cache.player.MongoPlayerData;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.player.SQLPlayerData;
import dev.plex.cache.sql.SQLNotes;
import dev.plex.cache.sql.SQLPunishment;
import dev.plex.storage.permission.SQLPermissions;
import dev.plex.storage.player.MongoPlayerData;
import dev.plex.cache.PlayerCache;
import dev.plex.storage.player.SQLPlayerData;
import dev.plex.storage.punishment.SQLNotes;
import dev.plex.storage.punishment.SQLPunishment;
import dev.plex.config.Config;
import dev.plex.handlers.CommandHandler;
import dev.plex.handlers.ListenerHandler;
@ -60,6 +61,7 @@ public class Plex extends JavaPlugin
private SQLPunishment sqlPunishment;
private SQLNotes sqlNotes;
private SQLPermissions sqlPermissions;
private ModuleManager moduleManager;
private RankManager rankManager;
@ -155,6 +157,7 @@ public class Plex extends JavaPlugin
sqlPlayerData = new SQLPlayerData();
sqlPunishment = new SQLPunishment();
sqlNotes = new SQLNotes();
sqlPermissions = new SQLPermissions();
}
new ListenerHandler();

View File

@ -1,7 +1,6 @@
package dev.plex.cache;
import dev.plex.Plex;
import dev.plex.cache.player.PlayerCache;
import dev.plex.player.PlexPlayer;
import dev.plex.storage.StorageType;
import java.util.UUID;

View File

@ -1,4 +1,4 @@
package dev.plex.cache.player;
package dev.plex.cache;
import com.google.common.collect.Maps;
import dev.plex.player.PlexPlayer;

View File

@ -1,40 +0,0 @@
package dev.plex.cache.notes;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
public class PlayerNotes
{
private final String SELECT = "SELECT * FROM `notes` WHERE uuid=?";
//private final String UPDATE = "UPDATE `notes` SET name=?, written_by=?, note=? WHERE uuid=?";
private final String INSERT = "INSERT INTO `notes` (`uuid`, `name`, `written_by`, `note`) VALUES (?, ?, ?, ?);";
public PlexPlayer getByUUID(UUID uuid)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(SELECT);
statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery();
PlexPlayer plexPlayer = new PlexPlayer(uuid);
while (set.next())
{
String name = set.getString("name");
String writtenBy = set.getString("written_by");
String note = set.getString("note");
}
return plexPlayer;
}
catch (SQLException throwables)
{
throwables.printStackTrace();
}
return null;
}
}

View File

@ -2,7 +2,7 @@ package dev.plex.command;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.PlayerCache;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.exception.*;

View File

@ -1,6 +1,6 @@
package dev.plex.command.impl;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.PlayerCache;
import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions;

View File

@ -1,6 +1,6 @@
package dev.plex.listener.impl;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener;
import dev.plex.listener.annotation.Toggleable;
import dev.plex.player.PlexPlayer;
@ -10,9 +10,6 @@ import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.commons.lang.StringUtils;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.jetbrains.annotations.NotNull;

View File

@ -1,16 +1,13 @@
package dev.plex.listener.impl;
import dev.plex.cache.DataUtils;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.PlayerCache;
import dev.plex.command.blocking.BlockedCommand;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.services.impl.CommandBlockerService;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;

View File

@ -1,13 +1,12 @@
package dev.plex.listener.impl;
import dev.plex.cache.DataUtils;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.storage.StorageType;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -20,7 +19,7 @@ import org.bukkit.event.player.PlayerQuitEvent;
import java.util.Arrays;
import java.util.List;
public class PlayerListener extends PlexListener
public class PlayerListener<T> extends PlexListener
{
// setting up a player's data
@EventHandler(priority = EventPriority.HIGHEST)
@ -73,19 +72,17 @@ public class PlayerListener extends PlexListener
player.openInventory(player.getInventory());
}
assert plexPlayer != null;
String loginMessage = plugin.getRankManager().getLoginMessage(plexPlayer);
if (!loginMessage.isEmpty())
{
PlexUtils.broadcast(PlexUtils.mmDeserialize("<aqua>" + player.getName() + " is " + loginMessage));
}
PlexUtils.setupPermissions(player);
if (plugin.getStorageType() != StorageType.MONGODB)
{
plexPlayer.loadNotes().whenComplete((notes, throwable) ->
{
//TODO: Send note messages to admins
});
plexPlayer.loadNotes();
}
}

View File

@ -2,7 +2,7 @@ package dev.plex.listener.impl;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;

View File

@ -2,7 +2,7 @@ package dev.plex.menu;
import com.google.common.collect.Lists;
import dev.plex.cache.DataUtils;
import dev.plex.cache.player.PlayerCache;
import dev.plex.cache.PlayerCache;
import dev.plex.player.PlexPlayer;
import dev.plex.punishment.Punishment;
import dev.plex.util.PlexUtils;

View File

@ -0,0 +1,15 @@
package dev.plex.permission;
import dev.morphia.annotations.Entity;
import lombok.Data;
import java.util.UUID;
@Data
@Entity
public class Permission
{
private final UUID uuid;
private final String permission;
private boolean allowed = true;
}

View File

@ -7,6 +7,7 @@ import dev.morphia.annotations.Id;
import dev.morphia.annotations.IndexOptions;
import dev.morphia.annotations.Indexed;
import dev.plex.Plex;
import dev.plex.permission.Permission;
import dev.plex.punishment.Punishment;
import dev.plex.punishment.extra.Note;
import dev.plex.rank.enums.Rank;
@ -26,6 +27,7 @@ import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
@Getter
@Setter
@ -64,6 +66,9 @@ public class PlexPlayer
private List<String> ips = Lists.newArrayList();
private List<Punishment> punishments = Lists.newArrayList();
private List<Note> notes = Lists.newArrayList();
private List<Permission> permissions = Lists.newArrayList();
private transient PermissionAttachment permissionAttachment;
public PlexPlayer()
{
@ -88,6 +93,10 @@ public class PlexPlayer
this.rank = "";
this.loadPunishments();
if (Plex.get().getStorageType() != StorageType.MONGODB)
{
this.permissions.addAll(Plex.get().getSqlPermissions().getPermissions(this.uuid));
}
}
public String displayName()

View File

@ -92,6 +92,11 @@ public class SQLConnection extends PlexBase
"`note` VARCHAR(2000), " +
"`timestamp` BIGINT" +
");").execute();
con.prepareStatement("CREATE TABLE IF NOT EXISTS `permissions` (" +
"`uuid` VARCHAR(46) NOT NULL," +
"`permission` VARCHAR(1000) NOT NULL," +
"`allowed` BOOLEAN" +
");");
}
catch (SQLException throwables)
{

View File

@ -0,0 +1,92 @@
package dev.plex.storage.permission;
import com.google.common.collect.Lists;
import dev.plex.Plex;
import dev.plex.permission.Permission;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
public class SQLPermissions
{
private static final String SELECT = "SELECT * FROM `permissions` WHERE uuid=?";
private static final String INSERT = "INSERT INTO `permissions` (`uuid`, `permission`, `allowed`) VALUES(?, ?, ?)";
private static final String REMOVE_PERMISSION = "DELETE FROM `permissions` WHERE uuid=? AND permission=?";
private static final String UPDATE_PERMISSION = "UPDATE `permissions` SET allowed=? WHERE uuid=? AND permission=?";
public List<Permission> getPermissions(UUID uuid)
{
List<Permission> permissions = Lists.newArrayList();
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(SELECT);
statement.setString(1, uuid.toString());
ResultSet set = statement.executeQuery();
while (set.next())
{
Permission permission = new Permission(UUID.fromString(set.getString("uuid")), set.getString("permission"));
permission.setAllowed(set.getBoolean("allowed"));
permissions.add(permission);
}
} catch (SQLException e)
{
e.printStackTrace();
}
return permissions;
}
public void addPermission(Permission permission)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(INSERT);
statement.setString(1, permission.getUuid().toString());
statement.setString(2, permission.getPermission().toLowerCase(Locale.ROOT));
statement.setBoolean(3, permission.isAllowed());
statement.execute();
} catch (SQLException e)
{
e.printStackTrace();
}
}
public void updatePermission(Permission permission, boolean newValue)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(UPDATE_PERMISSION);
statement.setBoolean(1, newValue);
statement.setString(2, permission.getUuid().toString());
statement.setString(3, permission.getPermission().toLowerCase(Locale.ROOT));
statement.executeUpdate();
} catch (SQLException e)
{
e.printStackTrace();
}
}
public void removePermission(Permission permission)
{
this.removePermission(permission.getUuid(), permission.getPermission());
}
public void removePermission(UUID uuid, String permission)
{
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement(REMOVE_PERMISSION);
statement.setString(1, uuid.toString());
statement.setString(2, permission.toLowerCase(Locale.ROOT));
statement.execute();
} catch (SQLException e)
{
e.printStackTrace();
}
}
}

View File

@ -1,4 +1,4 @@
package dev.plex.cache.player;
package dev.plex.storage.player;
import dev.morphia.Datastore;
import dev.morphia.query.Query;
@ -6,6 +6,7 @@ import dev.morphia.query.Update;
import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.experimental.updates.UpdateOperators;
import dev.plex.Plex;
import dev.plex.cache.PlayerCache;
import dev.plex.player.PlexPlayer;
import java.util.Collections;
import java.util.List;

View File

@ -1,8 +1,9 @@
package dev.plex.cache.player;
package dev.plex.storage.player;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import dev.plex.Plex;
import dev.plex.cache.PlayerCache;
import dev.plex.player.PlexPlayer;
import java.sql.Connection;
import java.sql.PreparedStatement;

View File

@ -1,4 +1,4 @@
package dev.plex.cache.sql;
package dev.plex.storage.punishment;
import com.google.common.collect.Lists;
import dev.plex.Plex;

View File

@ -1,4 +1,4 @@
package dev.plex.cache.sql;
package dev.plex.storage.punishment;
import com.google.common.collect.Lists;
import dev.plex.Plex;

View File

@ -4,7 +4,10 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.reflect.ClassPath;
import dev.plex.Plex;
import dev.plex.PlexBase;
import dev.plex.cache.DataUtils;
import dev.plex.config.Config;
import dev.plex.permission.Permission;
import dev.plex.player.PlexPlayer;
import dev.plex.storage.StorageType;
import java.time.format.DateTimeFormatter;
import net.kyori.adventure.text.Component;
@ -17,7 +20,9 @@ import org.bukkit.*;
import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachment;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@ -73,6 +78,63 @@ public class PlexUtils extends PlexBase
}
}
public static void setupPermissions(@NotNull Player player)
{
PlexPlayer plexPlayer = DataUtils.getPlayer(player.getUniqueId());
PermissionAttachment attachment = player.addAttachment(Plex.get());
plexPlayer.getPermissions().forEach(permission -> attachment.setPermission(permission.getPermission(), permission.isAllowed()));
plexPlayer.setPermissionAttachment(attachment);
}
public static void addPermission(PlexPlayer player, Permission permission)
{
Plex.get().getSqlPermissions().addPermission(addToArrayList(player.getPermissions(), permission));
Player p = Bukkit.getPlayer(player.getUuid());
if (p == null)
{
return;
}
player.getPermissionAttachment().setPermission(permission.getPermission(), permission.isAllowed());
}
public static void addPermission(PlexPlayer player, String permission)
{
addPermission(player, new Permission(player.getUuid(), permission));
}
public static void removePermission(PlexPlayer player, String permission)
{
Plex.get().getSqlPermissions().removePermission(player.getUuid(), permission);
player.getPermissions().removeIf(permission1 -> permission1.getPermission().equalsIgnoreCase(permission));
Player p = Bukkit.getPlayer(player.getUuid());
if (p == null)
{
return;
}
player.getPermissionAttachment().unsetPermission(permission);
}
public static void updatePermission(PlexPlayer player, String permission, boolean newValue)
{
player.getPermissions().stream().filter(permission1 -> permission.equalsIgnoreCase(permission)).findFirst().ifPresent(permission1 -> {
Plex.get().getSqlPermissions().updatePermission(permission1, newValue);
});
player.getPermissions().removeIf(permission1 -> permission1.getPermission().equalsIgnoreCase(permission));
Player p = Bukkit.getPlayer(player.getUuid());
if (p == null)
{
return;
}
player.getPermissionAttachment().unsetPermission(permission);
}
public static <T> T addToArrayList(List<T> list, T object)
{
list.add(object);
return object;
}
public static void disabledEffect(Player player, Location location)
{
Particle.CLOUD.builder().location(location).receivers(player).extra(0).offset(0.5, 0.5, 0.5).count(5).spawn();