add to central data class

add vanished variable to plexplayers
finish admin cmd except msgs aren't done
fix command source and plexcommand
add sender source to admin events
add admin listener
register admin listener
simplify playerlistener
fix sql syntax on sqlconnection
This commit is contained in:
spacerocket62 2020-11-06 10:19:38 -08:00
parent 542bba062c
commit 87abb0a964
15 changed files with 205 additions and 53 deletions

View File

@ -54,4 +54,14 @@ public class DataUtils
}
}
public static void insert(PlexPlayer plexPlayer)
{
if (Plex.get().getStorageType() == StorageType.MONGO)
{
Plex.get().getMongoPlayerData().save(plexPlayer);
} else {
Plex.get().getSqlPlayerData().insert(plexPlayer);
}
}
}

View File

@ -49,7 +49,8 @@ public class MongoPlayerData
UpdateOperators.set("prefix", player.getPrefix()),
UpdateOperators.set("rank", player.getRank().toLowerCase()),
UpdateOperators.set("ips", player.getIps()),
UpdateOperators.set("coins", player.getCoins()));
UpdateOperators.set("coins", player.getCoins()),
UpdateOperators.set("vanished", player.isVanished()));
updateOps.execute();
}

View File

@ -14,8 +14,8 @@ import me.totalfreedom.plex.player.PlexPlayer;
public class SQLPlayerData
{
private final String SELECT = "SELECT * FROM `players` WHERE uuid=?";
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=?, coins=? WHERE uuid=?";
private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`, `coins`) VALUES (?, ?, ?, ?, ?, ?, ?);";
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=?, coins=?, vanished=? WHERE uuid=?";
private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`, `coins`, `vanished`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
public boolean exists(UUID uuid)
{
@ -53,15 +53,15 @@ public class SQLPlayerData
String prefix = set.getString("prefix");
String rankName = set.getString("rank").toUpperCase();
long coins = set.getLong("coins");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>()
{
}.getType());
boolean vanished = set.getBoolean("vanished");
List<String> ips = new Gson().fromJson(set.getString("ips"), new TypeToken<List<String>>(){}.getType());
plexPlayer.setName(name);
plexPlayer.setLoginMSG(loginMSG);
plexPlayer.setPrefix(prefix);
plexPlayer.setRank(rankName);
plexPlayer.setIps(ips);
plexPlayer.setCoins(coins);
plexPlayer.setVanished(vanished);
}
return plexPlayer;
}
@ -83,7 +83,8 @@ public class SQLPlayerData
statement.setString(4, player.getRank().toLowerCase());
statement.setString(5, new Gson().toJson(player.getIps()));
statement.setLong(6, player.getCoins());
statement.setString(7, player.getUuid());
statement.setBoolean(7, player.isVanished());
statement.setString(8, player.getUuid());
statement.executeUpdate();
}
catch (SQLException throwables)
@ -103,6 +104,8 @@ public class SQLPlayerData
statement.setString(4, player.getPrefix());
statement.setString(5, player.getRank().toLowerCase());
statement.setString(6, new Gson().toJson(player.getIps()));
statement.setLong(7, player.getCoins());
statement.setBoolean(8, player.isVanished());
statement.execute();
}
catch (SQLException throwables)

View File

@ -2,6 +2,7 @@ package me.totalfreedom.plex.command;
import java.util.List;
import me.totalfreedom.plex.command.source.CommandSource;
import org.bukkit.command.CommandSender;
public interface IPlexCommand
{

View File

@ -74,11 +74,7 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
{
return false;
}
if (this.sender == null)
{
this.sender = new CommandSource(sender);
}
PlexLog.log(this.sender.getSender().getName());
if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player)
{
sender.sendMessage(tl("noPermissionInGame"));
@ -92,6 +88,8 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
return true;
}
Player player = (Player)sender;
this.sender = new CommandSource(player);
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
@ -101,6 +99,7 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
}
try
{
this.sender = new CommandSource(sender);
execute(this.sender, args);
}
catch (CommandArgumentException ex)
@ -121,13 +120,12 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
{
return ImmutableList.of();
}
if (this.sender == null)
{
this.sender = new CommandSource(sender);
}
if (sender instanceof Player)
{
Player player = (Player)sender;
this.sender = new CommandSource(player);
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
@ -140,6 +138,7 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
}
else
{
this.sender = new CommandSource(sender);
return onTabComplete(this.sender, args);
}
}
@ -192,7 +191,7 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
protected boolean isConsole()
{
return sender instanceof ConsoleCommandSender;
return !(sender instanceof Player);
}
protected String tl(String s, Object... objects)

View File

@ -20,10 +20,12 @@ import me.totalfreedom.plex.util.PlexUtils;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = RequiredCommandSource.CONSOLE)
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = RequiredCommandSource.ANY)
@CommandParameters(usage = "/<command> <add | remove | setrank | list> [player] [rank]", aliases = "saconfig,slconfig,adminconfig,adminmanage", description = "Manage all admins")
public class AdminCMD extends PlexCommand
{
//TODO: Better return messages
public AdminCMD()
{
super("admin");
@ -46,6 +48,12 @@ public class AdminCMD extends PlexCommand
return;
}
if (!sender.isConsoleSender())
{
sender.send("Console only");
return;
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
@ -53,9 +61,16 @@ public class AdminCMD extends PlexCommand
throw new PlayerNotFoundException();
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (isAdmin(plexPlayer))
{
sender.send("Player is an admin");
return;
}
plexPlayer.setRank(Rank.ADMIN.name());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(plexPlayer));
Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(sender, plexPlayer));
return;
}
if (args[0].equalsIgnoreCase("remove"))
@ -66,6 +81,12 @@ public class AdminCMD extends PlexCommand
return;
}
if (!sender.isConsoleSender())
{
sender.send("Console only");
return;
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
@ -73,9 +94,16 @@ public class AdminCMD extends PlexCommand
throw new PlayerNotFoundException();
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
sender.send("Player is not an admin");
return;
}
plexPlayer.setRank("");
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(plexPlayer));
Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(sender, plexPlayer));
return;
}
@ -87,6 +115,12 @@ public class AdminCMD extends PlexCommand
return;
}
if (!sender.isConsoleSender())
{
sender.send("Console only");
return;
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
@ -109,10 +143,17 @@ public class AdminCMD extends PlexCommand
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
if (!isAdmin(plexPlayer))
{
sender.send("Player is not an admin");
return;
}
plexPlayer.setRank(rank.name().toLowerCase());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(plexPlayer, rank));
Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(sender, plexPlayer, rank));
return;
}

View File

@ -13,10 +13,12 @@ public class CommandSource
private final CommandSender sender;
private final Player player;
private final PlexPlayer plexPlayer;
private final boolean isConsoleSender;
public CommandSource(CommandSender sender)
{
this.sender = sender;
this.isConsoleSender = !(sender instanceof Player);
this.player = sender instanceof Player ? Bukkit.getPlayer(sender.getName()) : null;
this.plexPlayer = sender instanceof Player ? PlayerCache.getPlexPlayerMap().get(((Player)sender).getUniqueId()) : null;
}

View File

@ -1,17 +1,20 @@
package me.totalfreedom.plex.event;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.player.PlexPlayer;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class AdminAddEvent extends Event
{
private final HandlerList handlers = new HandlerList();
private static final HandlerList handlers = new HandlerList();
private CommandSource sender;
private PlexPlayer plexPlayer;
public AdminAddEvent(PlexPlayer plexPlayer)
public AdminAddEvent(CommandSource sender, PlexPlayer plexPlayer)
{
this.sender = sender;
this.plexPlayer = plexPlayer;
}
@ -21,8 +24,17 @@ public class AdminAddEvent extends Event
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public PlexPlayer getPlexPlayer()
{
return plexPlayer;
}
public CommandSource getSender() {
return sender;
}
}

View File

@ -1,17 +1,20 @@
package me.totalfreedom.plex.event;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.player.PlexPlayer;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class AdminRemoveEvent extends Event
{
private final HandlerList handlers = new HandlerList();
private static final HandlerList handlers = new HandlerList();
private PlexPlayer plexPlayer;
private CommandSource sender;
public AdminRemoveEvent(PlexPlayer plexPlayer)
public AdminRemoveEvent(CommandSource sender, PlexPlayer plexPlayer)
{
this.sender = sender;
this.plexPlayer = plexPlayer;
}
@ -21,8 +24,17 @@ public class AdminRemoveEvent extends Event
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public PlexPlayer getPlexPlayer()
{
return plexPlayer;
}
public CommandSource getSender() {
return sender;
}
}

View File

@ -1,5 +1,6 @@
package me.totalfreedom.plex.event;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.event.Event;
@ -7,13 +8,15 @@ import org.bukkit.event.HandlerList;
public class AdminSetRankEvent extends Event
{
private final HandlerList handlers = new HandlerList();
private static final HandlerList handlers = new HandlerList();
private CommandSource sender;
private PlexPlayer plexPlayer;
private Rank rank;
public AdminSetRankEvent(PlexPlayer plexPlayer, Rank rank)
public AdminSetRankEvent(CommandSource sender, PlexPlayer plexPlayer, Rank rank)
{
this.sender = sender;
this.plexPlayer = plexPlayer;
this.rank = rank;
}
@ -24,6 +27,11 @@ public class AdminSetRankEvent extends Event
return handlers;
}
public static HandlerList getHandlerList()
{
return handlers;
}
public PlexPlayer getPlexPlayer()
{
return plexPlayer;
@ -33,4 +41,8 @@ public class AdminSetRankEvent extends Event
{
return rank;
}
public CommandSource getSender() {
return sender;
}
}

View File

@ -3,11 +3,7 @@ package me.totalfreedom.plex.handlers;
import com.google.common.collect.Lists;
import java.util.List;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.listener.impl.ChatListener;
import me.totalfreedom.plex.listener.impl.FreezeListener;
import me.totalfreedom.plex.listener.impl.PlayerListener;
import me.totalfreedom.plex.listener.impl.ServerListener;
import me.totalfreedom.plex.listener.impl.WorldListener;
import me.totalfreedom.plex.listener.impl.*;
import me.totalfreedom.plex.util.PlexLog;
public class ListenerHandler
@ -21,6 +17,7 @@ public class ListenerHandler
listeners.add(new PlayerListener());
listeners.add(new WorldListener());
listeners.add(new FreezeListener());
listeners.add(new AdminListener());
PlexLog.log(String.format("Registered %s listeners!", listeners.size()));
}
}

View File

@ -0,0 +1,44 @@
package me.totalfreedom.plex.listener.impl;
import me.totalfreedom.plex.event.AdminAddEvent;
import me.totalfreedom.plex.event.AdminRemoveEvent;
import me.totalfreedom.plex.event.AdminSetRankEvent;
import me.totalfreedom.plex.listener.PlexListener;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
public class AdminListener extends PlexListener
{
@EventHandler
public void onAdminAdd(AdminAddEvent event)
{
String userSender = event.getSender().getName();
PlexPlayer target = event.getPlexPlayer();
Bukkit.broadcastMessage(String.format(ChatColor.RED + "%s - Adding %s to the admin list!", userSender, target.getName()));
}
@EventHandler
public void onAdminRemove(AdminRemoveEvent event)
{
String userSender = event.getSender().getName();
PlexPlayer target = event.getPlexPlayer();
Bukkit.broadcastMessage(String.format(ChatColor.RED + "%s - Removing %s from the admin list!", userSender, target.getName()));
}
@EventHandler
public void onAdminSetrank(AdminSetRankEvent event)
{
String userSender = event.getSender().getName();
PlexPlayer target = event.getPlexPlayer();
Rank newRank = event.getRank();
Bukkit.broadcastMessage(String.format(ChatColor.RED + "%s - Setting %s's rank to %s!", userSender, target.getName(), newRank.name().toUpperCase()));
}
}

View File

@ -3,6 +3,7 @@ package me.totalfreedom.plex.listener.impl;
import java.util.Arrays;
import java.util.UUID;
import me.totalfreedom.plex.admin.Admin;
import me.totalfreedom.plex.cache.DataUtils;
import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.cache.SQLPlayerData;
@ -30,9 +31,21 @@ public class PlayerListener extends PlexListener
{
Player player = event.getPlayer();
PlexPlayer plexPlayer = null;
PlexPlayer plexPlayer;
if (mongoPlayerData != null) // Alright, check if we're saving with Mongo first
if (!DataUtils.hasPlayedBefore(player.getUniqueId()))
{
PlexLog.log("AYO THIS MAN DONT EXIST"); // funi msg
plexPlayer = new PlexPlayer(player.getUniqueId()); //it doesn't! okay so now create the object
plexPlayer.setName(player.getName()); //set the name of the player
plexPlayer.setIps(Arrays.asList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips
DataUtils.insert(plexPlayer); // insert data in some wack db
} else {
plexPlayer = DataUtils.getPlayer(player.getUniqueId());
}
/*if (mongoPlayerData != null) // Alright, check if we're saving with Mongo first
{
if (!mongoPlayerData.exists(player.getUniqueId())) //okay, we're saving with mongo! now check if the player's document exists
{
@ -64,7 +77,7 @@ public class PlayerListener extends PlexListener
plexPlayer = sqlPlayerData.getByUUID(player.getUniqueId()); //oh they do exist!
plexPlayer.setName(plexPlayer.getName()); //set the name!
}
}
}*/
PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache
PlayerCache.getPunishedPlayerMap().put(player.getUniqueId(), new PunishedPlayer(player.getUniqueId()));

View File

@ -33,6 +33,8 @@ public class PlexPlayer
private String loginMSG;
private String prefix;
private boolean vanished;
private long coins;
private String rank;
@ -54,6 +56,8 @@ public class PlexPlayer
this.loginMSG = "";
this.prefix = "";
this.vanished = false;
this.coins = 0;
this.ips = new ArrayList<>();

View File

@ -42,14 +42,15 @@ public class SQLConnection extends PlexBase
if (connection != null)
{
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `players` (\n" +
"\t`uuid` VARCHAR(46),\n" +
"\t`uuid` VARCHAR(46) NOT NULL,\n" +
"\t`name` VARCHAR(18),\n" +
"\t`login_msg` VARCHAR,\n" +
"\t`prefix` VARCHAR,\n" +
"\t`rank` VARCHAR,\n" +
"\t`ips` VARCHAR,\n" +
"\t`coins` INT\n" +
//"\tPRIMARY KEY (`uuid`)\n" +
"\t`coins` BIGINT,\n" +
"\t`vanished` BOOLEAN,\n" +
"\tPRIMARY KEY (`uuid`)\n" +
");").execute();
}
}