admin cmd done

This commit is contained in:
spacerocket62 2020-11-05 13:17:14 -08:00
parent 8428e2a6d3
commit ce76aa9ce1
22 changed files with 461 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package me.totalfreedom.plex;
import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.plex.admin.AdminList;
import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.SQLPlayerData;
import me.totalfreedom.plex.config.Config;
@ -37,6 +38,8 @@ public class Plex extends JavaPlugin
private RankManager rankManager;
private AdminList adminList;
public static Plex get()
{
return plugin;
@ -96,6 +99,8 @@ public class Plex extends JavaPlugin
rankManager.importDefaultRanks();
PlexLog.log("Rank Manager initialized");
adminList = new AdminList();
generateWorlds();
}

View File

@ -0,0 +1,31 @@
package me.totalfreedom.plex.admin;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import me.totalfreedom.plex.rank.enums.Rank;
import java.util.UUID;
@Getter
@Setter
public class Admin
{
@Setter(AccessLevel.NONE)
private UUID uuid;
private Rank rank;
private boolean commandSpy = true;
private boolean staffChat = false;
public Admin(UUID uuid)
{
this.uuid = uuid;
this.rank = Rank.ADMIN;
}
}

View File

@ -0,0 +1,75 @@
package me.totalfreedom.plex.admin;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import dev.morphia.Datastore;
import dev.morphia.query.Query;
import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.internal.MorphiaCursor;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank;
import me.totalfreedom.plex.storage.StorageType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class AdminList
{
private final Map<UUID, Admin> admins = Maps.newHashMap();
public void addToCache(Admin admin)
{
admins.put(admin.getUuid(), admin);
}
public void removeFromCache(UUID uuid)
{
admins.remove(uuid);
}
public List<String> getAllAdmins()
{
List<String> admins = Lists.newArrayList();
if (Plex.get().getStorageType() == StorageType.MONGO)
{
Datastore store = Plex.get().getMongoConnection().getDatastore();
Query<PlexPlayer> query = store.find(PlexPlayer.class);
MorphiaCursor<PlexPlayer> cursor = query.iterator();
while (cursor.hasNext())
{
PlexPlayer player = cursor.next();
if (player.getRankFromString().isAtLeast(Rank.ADMIN))
{
admins.add(player.getName());
}
}
} else {
try (Connection con = Plex.get().getSqlConnection().getCon())
{
PreparedStatement statement = con.prepareStatement("SELECT * FROM `players` WHERE rank IN(?, ?, ?)");
statement.setString(1, Rank.ADMIN.name().toLowerCase());
statement.setString(2, Rank.SENIOR_ADMIN.name().toLowerCase());
statement.setString(3, Rank.EXECUTIVE.name().toLowerCase());
ResultSet set = statement.executeQuery();
while (set.next())
{
admins.add(set.getString("name"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
return admins;
}
}

View File

@ -0,0 +1,47 @@
package me.totalfreedom.plex.cache;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.storage.StorageType;
import java.util.UUID;
public class DataUtils
{
public static boolean hasPlayedBefore(UUID uuid)
{
if (Plex.get().getStorageType() == StorageType.MONGO)
{
return Plex.get().getMongoPlayerData().exists(uuid);
} else {
return Plex.get().getSqlPlayerData().exists(uuid);
}
}
public static PlexPlayer getPlayer(UUID uuid)
{
if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
{
return PlayerCache.getPlexPlayerMap().get(uuid);
}
if (Plex.get().getStorageType() == StorageType.MONGO)
{
return Plex.get().getMongoPlayerData().getByUUID(uuid);
} else {
return Plex.get().getSqlPlayerData().getByUUID(uuid);
}
}
public static void update(PlexPlayer plexPlayer)
{
if (Plex.get().getStorageType() == StorageType.MONGO)
{
Plex.get().getMongoPlayerData().update(plexPlayer);
} else {
Plex.get().getSqlPlayerData().update(plexPlayer);
}
}
}

View File

@ -55,6 +55,7 @@ public class MongoPlayerData
updateOps.execute();
}
public void save(PlexPlayer plexPlayer)
{
datastore.save(plexPlayer);

View File

@ -1,11 +1,12 @@
package me.totalfreedom.plex.cache;
import com.google.common.collect.Maps;
import java.util.Map;
import java.util.UUID;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.player.PunishedPlayer;
import java.util.Map;
import java.util.UUID;
public class PlayerCache
{
private static final Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();

View File

@ -73,21 +73,21 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
PlexLog.log(this.sender.getSender().getName());
if (commandSource == RequiredCommandSource.CONSOLE && sender instanceof Player)
{
send(tl("noPermissionInGame"));
sender.sendMessage(tl("noPermissionInGame"));
return true;
}
if (commandSource == RequiredCommandSource.IN_GAME)
{
if (sender instanceof ConsoleCommandSender)
{
send(tl("noPermissionConsole"));
sender.sendMessage(tl("noPermissionConsole"));
return true;
}
Player player = (Player) sender;
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
send(tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG())));
sender.sendMessage(tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG())));
return true;
}
}
@ -161,6 +161,11 @@ public abstract class PlexCommand extends Command implements TabExecutor, IPlexC
player.sendMessage(s);
}
protected String usage(String s)
{
return ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + s;
}
protected void send(String s)
{
if (sender == null)

View File

@ -0,0 +1,158 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.cache.DataUtils;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.event.AdminAddEvent;
import me.totalfreedom.plex.event.AdminRemoveEvent;
import me.totalfreedom.plex.event.AdminSetRankEvent;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank;
import me.totalfreedom.plex.util.PlexUtils;
import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
@CommandPermissions(level = Rank.SENIOR_ADMIN, source = RequiredCommandSource.CONSOLE)
@CommandParameters(usage = "/<command> <add | remove | setrank | list> [player] [rank]", aliases = "adminconfig,adminmanage", description = "Manages all admins")
public class AdminCMD extends PlexCommand
{
public AdminCMD() {
super("admin");
}
@Override
public void execute(CommandSource sender, String[] args)
{
if (args.length == 0)
{
sender.send(usage(getUsage()));
return;
}
if (args[0].equalsIgnoreCase("add"))
{
if (args.length != 2)
{
sender.send(usage("/admin add <player>"));
return;
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
sender.send(PlexUtils.tl("playerNotFound"));
return;
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
plexPlayer.setRank(Rank.ADMIN.name());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminAddEvent(plexPlayer));
return;
}
if (args[0].equalsIgnoreCase("remove"))
{
if (args.length != 2)
{
sender.send(usage("/admin remove <player>"));
return;
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
sender.send(PlexUtils.tl("playerNotFound"));
return;
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
plexPlayer.setRank("");
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminRemoveEvent(plexPlayer));
return;
}
if (args[0].equalsIgnoreCase("setrank"))
{
if (args.length != 3)
{
sender.send(usage("/admin setrank <player> <rank>"));
return;
}
UUID targetUUID = PlexUtils.getFromName(args[1]);
if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID))
{
sender.send(PlexUtils.tl("playerNotFound"));
return;
}
if (!rankExists(args[2]))
{
sender.send("Rank not found");
return;
}
Rank rank = Rank.valueOf(args[2].toUpperCase());
if (!rank.isAtLeast(Rank.ADMIN))
{
sender.send("Must be admin+");
return;
}
PlexPlayer plexPlayer = DataUtils.getPlayer(targetUUID);
plexPlayer.setRank(rank.name().toLowerCase());
DataUtils.update(plexPlayer);
Bukkit.getServer().getPluginManager().callEvent(new AdminSetRankEvent(plexPlayer, rank));
return;
}
if (args[0].equalsIgnoreCase("list"))
{
if (args.length != 1)
{
sender.send(usage("/admin list"));
return;
}
sender.send("Admins: " + StringUtils.join(plugin.getAdminList().getAllAdmins(), ", "));
return;
}
}
@Override
public List<String> onTabComplete(CommandSource sender, String[] args) {
if (args.length == 1)
{
return Arrays.asList("add", "remove", "setrank", "list");
} else if (args.length == 2 && !args[0].equalsIgnoreCase("list"))
{
return PlexUtils.getPlayerNameList();
}
return ImmutableList.of();
}
private boolean rankExists(String rank)
{
for (Rank ranks : Rank.values())
{
if (ranks.name().equalsIgnoreCase(rank))
{
return true;
}
}
return false;
}
}

View File

@ -1,5 +1,6 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotation.CommandParameters;
@ -119,7 +120,7 @@ public class FionnCMD extends PlexCommand
@Override
public List<String> onTabComplete(CommandSource sender, String[] args)
{
return null;
return ImmutableList.of();
}
public static void lateFakeChat(String name, String message, ChatColor color, int delay)

View File

@ -1,5 +1,6 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotation.CommandParameters;
@ -39,6 +40,6 @@ public class FreezeCMD extends PlexCommand
@Override
public List<String> onTabComplete(CommandSource sender, String[] args)
{
return args.length == 1 ? PlexUtils.getPlayerNameList() : null;
return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of();
}
}

View File

@ -1,5 +1,6 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
@ -7,9 +8,6 @@ import me.totalfreedom.plex.command.exception.CommandArgumentException;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.rank.enums.Rank;
import me.totalfreedom.plex.util.PlexUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;
@ -73,6 +71,6 @@ public class NameHistoryCMD extends PlexCommand
@Override
public List<String> onTabComplete(CommandSource sender, String[] args)
{
return args.length == 1 ? PlexUtils.getPlayerNameList() : null;
return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of();
}
}

View File

@ -1,5 +1,6 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
@ -32,6 +33,6 @@ public class OpAllCMD extends PlexCommand
@Override
public List<String> onTabComplete(CommandSource sender, String[] args) {
return null;
return ImmutableList.of();
}
}

View File

@ -1,5 +1,6 @@
package me.totalfreedom.plex.command.impl;
import com.google.common.collect.ImmutableList;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
@ -35,6 +36,6 @@ public class OpCMD extends PlexCommand
@Override
public List<String> onTabComplete(CommandSource sender, String[] args)
{
return args.length == 1 ? PlexUtils.getPlayerNameList() : null;
return args.length == 1 ? PlexUtils.getPlayerNameList() : ImmutableList.of();
}
}

View File

@ -1,18 +1,17 @@
package me.totalfreedom.plex.command.impl;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.annotation.CommandParameters;
import me.totalfreedom.plex.command.annotation.CommandPermissions;
import me.totalfreedom.plex.command.PlexCommand;
import me.totalfreedom.plex.command.source.CommandSource;
import me.totalfreedom.plex.command.source.RequiredCommandSource;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.command.CommandSender;
import java.util.Arrays;
import java.util.List;
@CommandPermissions(level = Rank.OP, source = RequiredCommandSource.ANY)
@CommandParameters(usage = "/<command>", aliases = "plexhelp", description = "Help with plex")
@CommandParameters(aliases = "plexhelp", description = "Help with plex")
public class PlexCMD extends PlexCommand
{
public PlexCMD() {

View File

@ -0,0 +1,27 @@
package me.totalfreedom.plex.event;
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 PlexPlayer plexPlayer;
public AdminAddEvent(PlexPlayer plexPlayer)
{
this.plexPlayer = plexPlayer;
}
@Override
public HandlerList getHandlers()
{
return handlers;
}
public PlexPlayer getPlexPlayer() {
return plexPlayer;
}
}

View File

@ -0,0 +1,27 @@
package me.totalfreedom.plex.event;
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 PlexPlayer plexPlayer;
public AdminRemoveEvent(PlexPlayer plexPlayer)
{
this.plexPlayer = plexPlayer;
}
@Override
public HandlerList getHandlers()
{
return handlers;
}
public PlexPlayer getPlexPlayer() {
return plexPlayer;
}
}

View File

@ -0,0 +1,34 @@
package me.totalfreedom.plex.event;
import me.totalfreedom.plex.player.PlexPlayer;
import me.totalfreedom.plex.rank.enums.Rank;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
public class AdminSetRankEvent extends Event
{
private final HandlerList handlers = new HandlerList();
private PlexPlayer plexPlayer;
private Rank rank;
public AdminSetRankEvent(PlexPlayer plexPlayer, Rank rank)
{
this.plexPlayer = plexPlayer;
this.rank = rank;
}
@Override
public HandlerList getHandlers()
{
return handlers;
}
public PlexPlayer getPlexPlayer() {
return plexPlayer;
}
public Rank getRank() {
return rank;
}
}

View File

@ -22,6 +22,7 @@ public class CommandHandler
commands.add(new OpCMD());
commands.add(new FreezeCMD());
commands.add(new NameHistoryCMD());
commands.add(new AdminCMD());
PlexLog.log(String.format("Registered %s commands!", commands.size()));
}

View File

@ -1,7 +1,10 @@
package me.totalfreedom.plex.listener.impl;
import java.util.Arrays;
import java.util.UUID;
import me.totalfreedom.plex.Plex;
import me.totalfreedom.plex.admin.Admin;
import me.totalfreedom.plex.cache.MongoPlayerData;
import me.totalfreedom.plex.cache.PlayerCache;
import me.totalfreedom.plex.cache.SQLPlayerData;
@ -73,6 +76,11 @@ public class PlayerListener extends PlexListener
if (plugin.getRankManager().isAdmin(plexPlayer))
{
Admin admin = new Admin(UUID.fromString(plexPlayer.getUuid()));
admin.setRank(plexPlayer.getRankFromString());
plugin.getAdminList().addToCache(admin);
if (!plexPlayer.getLoginMSG().isEmpty())
{
event.setJoinMessage(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getLoginMSG());
@ -89,6 +97,11 @@ public class PlayerListener extends PlexListener
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId()); //get the player because it's literally impossible for them to not have an object
if (plugin.getRankManager().isAdmin(plexPlayer))
{
plugin.getAdminList().removeFromCache(UUID.fromString(plexPlayer.getUuid()));
}
if (mongoPlayerData != null) //back to mongo checking
{
mongoPlayerData.update(plexPlayer); //update the player's document
@ -103,6 +116,7 @@ public class PlayerListener extends PlexListener
PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache
PlayerCache.getPunishedPlayerMap().remove(event.getPlayer().getUniqueId());
}
// unrelated player quitting

View File

@ -21,7 +21,7 @@ public class WorldListener extends PlexListener
Player player = e.getPlayer();
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
World world = player.getWorld();
switch (world.getName())
switch (world.getName().toLowerCase())
{
case "adminworld":
{

View File

@ -8,6 +8,7 @@ import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
import me.totalfreedom.plex.Plex;
@ -18,6 +19,7 @@ import org.bukkit.command.Command;
import org.bukkit.command.PluginCommandYamlParser;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
@ -157,4 +159,17 @@ public class PlexUtils
connection.disconnect();
return new JSONParser().parse(content.toString());
}
public static UUID getFromName(String name)
{
JSONObject profile;
try {
profile = (JSONObject) PlexUtils.simpleGET("https://api.ashcon.app/mojang/v2/user/" + name);
} catch (IOException | ParseException e) {
e.printStackTrace();
return null;
}
String uuidString = (String) profile.get("uuid");
return UUID.fromString(uuidString);
}
}

View File

@ -1,7 +1,9 @@
# Plex Configuration File
server:
name: "TotalFreedom"
test: "test string"
motd: "&~%servername% &8- %mcversion%"
data:
central: