Add documentation to dev.plex.admin, dev.plex.banning, dev.plex.cache, and dev.plex.command (annotations and PlexCommand.java only)

Convert Date in punishments to LocalDateTime
This commit is contained in:
spacerocket62 2022-02-05 15:14:23 -08:00
parent 5e64e5e1b5
commit 211308e813
17 changed files with 426 additions and 40 deletions

View File

@ -74,4 +74,8 @@ tasks {
build { build {
dependsOn(shadowJar) dependsOn(shadowJar)
} }
javadoc {
options.memberLevel = JavadocMemberLevel.PRIVATE
}
} }

View File

@ -6,18 +6,46 @@ import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
/**
* Admin object to handle cached admins
*/
@Getter @Getter
@Setter @Setter
public class Admin public class Admin
{ {
/**
* Gets the unique ID of an admin (immutable)
*/
@Setter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
private UUID uuid; private UUID uuid;
/**
* Gets the rank of the admin
* <br>
* Contains a #setRank and #getRank by lombok
*/
private Rank rank; private Rank rank;
/**
* Returns if the admin has command spy or not
* <br>
* Contains a #isCommandSpy and #setCommandSpy by lombok
*/
private boolean commandSpy = true; private boolean commandSpy = true;
/**
* Returns if the admin has staff chat toggled or not
* <br>
* Contains a #isStaffChat and #setStaffChat by lombok
*/
private boolean staffChat = false; private boolean staffChat = false;
/**
* Creates an admin with the startig ADMIN rank
* @param uuid
* @see UUID
* @see Rank
*/
public Admin(UUID uuid) public Admin(UUID uuid)
{ {
this.uuid = uuid; this.uuid = uuid;

View File

@ -15,21 +15,43 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
/**
* Cached storage for Admin objects
* @see Admin
*/
public class AdminList public class AdminList
{ {
/**
* Key/Value storage, where the key is the unique ID of the admin
*/
private final Map<UUID, Admin> admins = Maps.newHashMap(); private final Map<UUID, Admin> admins = Maps.newHashMap();
/**
* Adds the admin to cache
* @param admin The admin object
*/
public void addToCache(Admin admin) public void addToCache(Admin admin)
{ {
admins.put(admin.getUuid(), admin); admins.put(admin.getUuid(), admin);
} }
/**
* Removes an admin from the cache
* @param uuid The unique ID of the admin
* @see UUID
*/
public void removeFromCache(UUID uuid) public void removeFromCache(UUID uuid)
{ {
admins.remove(uuid); admins.remove(uuid);
} }
/**
* Gathers every admin's username (cached and databsed)
* @return An array list of the names of every admin
*/
public List<String> getAllAdmins() public List<String> getAllAdmins()
{ {
List<String> admins = Lists.newArrayList(); List<String> admins = Lists.newArrayList();
@ -37,13 +59,7 @@ public class AdminList
{ {
Datastore store = Plex.get().getMongoConnection().getDatastore(); Datastore store = Plex.get().getMongoConnection().getDatastore();
Query<PlexPlayer> query = store.find(PlexPlayer.class); Query<PlexPlayer> query = store.find(PlexPlayer.class);
for (PlexPlayer player : query) admins.addAll(query.stream().filter(plexPlayer -> plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN)).map(PlexPlayer::getName).collect(Collectors.toList()));
{
if (player.getRankFromString().isAtLeast(Rank.ADMIN))
{
admins.add(player.getName());
}
}
} }
else else
{ {

View File

@ -4,6 +4,8 @@ import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Id; import dev.morphia.annotations.Id;
import dev.morphia.annotations.IndexOptions; import dev.morphia.annotations.IndexOptions;
import dev.morphia.annotations.Indexed; import dev.morphia.annotations.Indexed;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.UUID; import java.util.UUID;
import lombok.AccessLevel; import lombok.AccessLevel;
@ -11,43 +13,90 @@ import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.RandomStringUtils;
/**
* The ban object
* @see BanManager
*/
@Getter @Getter
@Setter @Setter
@Entity(value = "bans", useDiscriminator = false) @Entity(value = "bans", useDiscriminator = false)
public class Ban public class Ban
{ {
public Ban() /**
* A constructor for Morphia, can't be used
*/
private Ban()
{ {
} }
/**
* Gets the id of the ban (first 8 characters of a UUID + random 6 letters)
*/
@Setter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
@Id @Id
private String id; private String id;
/**
* The unique ID of the player who was banned
*/
@Setter(AccessLevel.NONE) @Setter(AccessLevel.NONE)
@Indexed(options = @IndexOptions(unique = true)) @Indexed(options = @IndexOptions(unique = true))
private UUID uuid; private UUID uuid;
/**
* The unique ID of the person who banned the player (can be null)
*/
@Indexed // have the banner be indexed in the future to get bans issued by a person @Indexed // have the banner be indexed in the future to get bans issued by a person
private UUID banner; private UUID banner;
/**
* The IP of the banned player
*/
private String ip; private String ip;
/**
* The reason for the ban
*/
private String reason; private String reason;
private Date endDate;
/**
* The end date for the ban
*/
private LocalDateTime endDate;
/**
* Whether the ban is active or not
*/
private boolean active; private boolean active;
public Ban(UUID uuid, UUID banner, String ip, String reason, Date endDate) /**
* Creates a ban object
* @param uuid The unique ID of the player being banned
* @param banner The unique ID of the sender banning the player
* @param ip The IP of the player being banned
* @param reason The reason for the ban
* @param endDate When the ban will expire
*/
public Ban(UUID uuid, UUID banner, String ip, String reason, LocalDateTime endDate)
{ {
this.uuid = uuid; this(uuid.toString().substring(0, 8) + "-" + RandomStringUtils.randomAlphabetic(6),
this.id = uuid.toString().substring(0, 8) + "-" + RandomStringUtils.randomAlphabetic(6); uuid,
this.banner = banner; banner,
this.ip = ip; ip,
this.reason = reason; reason,
this.endDate = endDate; endDate);
this.active = true;
} }
public Ban(String id, UUID uuid, UUID banner, String ip, String reason, Date endDate) /**
* Creates a ban object
* @param id The custom ID of the ban
* @param uuid The unique ID of the player being banned
* @param banner The unique ID of the sender banning the player
* @param ip The IP of the player being banned
* @param reason The reason for the ban
* @param endDate When the ban will expire
*/
public Ban(String id, UUID uuid, UUID banner, String ip, String reason, LocalDateTime endDate)
{ {
this.uuid = uuid; this.uuid = uuid;
this.id = id; this.id = id;

View File

@ -6,11 +6,15 @@ import dev.morphia.query.experimental.filters.Filters;
import dev.morphia.query.experimental.updates.UpdateOperators; import dev.morphia.query.experimental.updates.UpdateOperators;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.storage.StorageType; import dev.plex.storage.StorageType;
import java.sql.Connection; import java.sql.Connection;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Date; import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -19,6 +23,10 @@ public class BanManager
private final String SELECT = "SELECT * FROM `bans` WHERE uuid=?"; private final String SELECT = "SELECT * FROM `bans` WHERE uuid=?";
private final String INSERT = "INSERT INTO `bans` (`banID`, `uuid`, `banner`, `ip`, `reason`, `enddate`, `active`) VALUES (?, ?, ?, ?, ?, ?, ?);"; private final String INSERT = "INSERT INTO `bans` (`banID`, `uuid`, `banner`, `ip`, `reason`, `enddate`, `active`) VALUES (?, ?, ?, ?, ?, ?, ?);";
/**
* Adds the ban to the database
* @param ban The ban object
*/
public void executeBan(Ban ban) public void executeBan(Ban ban)
{ {
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)
@ -35,7 +43,7 @@ public class BanManager
statement.setString(3, ban.getBanner() == null ? "" : ban.getBanner().toString()); statement.setString(3, ban.getBanner() == null ? "" : ban.getBanner().toString());
statement.setString(4, ban.getIp()); statement.setString(4, ban.getIp());
statement.setString(5, ban.getReason()); statement.setString(5, ban.getReason());
statement.setLong(6, ban.getEndDate().toInstant().toEpochMilli()); statement.setLong(6, ban.getEndDate().toInstant(ZoneOffset.of(ZoneId.systemDefault().getId())).toEpochMilli());
statement.setBoolean(7, ban.isActive()); statement.setBoolean(7, ban.isActive());
statement.execute(); statement.execute();
@ -47,6 +55,11 @@ public class BanManager
} }
} }
/**
* Checks if the unique ID has an active ban in the database
* @param uuid The unique ID of the player
* @return true if the unique ID is banned
*/
public boolean isBanned(UUID uuid) public boolean isBanned(UUID uuid)
{ {
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)
@ -81,6 +94,10 @@ public class BanManager
return false; return false;
} }
/**
* Unbans a player if they have an active ban on record
* @param uuid The unique ID of the player
*/
public void unban(UUID uuid) public void unban(UUID uuid)
{ {
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)
@ -95,9 +112,10 @@ public class BanManager
{ {
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
{ {
PreparedStatement statement = con.prepareStatement("UPDATE `bans` SET active=? WHERE uuid=?"); PreparedStatement statement = con.prepareStatement("UPDATE `bans` SET active=? WHERE uuid=? AND active=?");
statement.setBoolean(1, false); statement.setBoolean(1, false);
statement.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
statement.setBoolean(3, true);
statement.executeUpdate(); statement.executeUpdate();
} }
catch (SQLException throwables) catch (SQLException throwables)
@ -107,6 +125,10 @@ public class BanManager
} }
} }
/**
* Unbans a player if they have an active ban on record
* @param id Custom ID of the ban
*/
public void unban(String id) public void unban(String id)
{ {
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)
@ -133,6 +155,10 @@ public class BanManager
} }
} }
/**
* Gets a list of all the current bans active
* @return An arraylist of bans
*/
public List<Ban> getActiveBans() public List<Ban> getActiveBans()
{ {
List<Ban> bans = Lists.newArrayList(); List<Ban> bans = Lists.newArrayList();
@ -158,7 +184,7 @@ public class BanManager
UUID banner = set.getString("banner").isEmpty() ? null : UUID.fromString(set.getString("banner")); UUID banner = set.getString("banner").isEmpty() ? null : UUID.fromString(set.getString("banner"));
String ip = set.getString("ip"); String ip = set.getString("ip");
String reason = set.getString("reason"); String reason = set.getString("reason");
Date endDate = set.getLong("enddate") != 0 ? new Date(set.getLong("enddate")) : null; LocalDateTime endDate = set.getLong("enddate") != 0 ? LocalDateTime.ofInstant(Instant.ofEpochMilli(set.getLong("enddate")), ZoneId.systemDefault()) : null;
Ban ban = new Ban(id, uuid, banner, ip, reason, endDate); Ban ban = new Ban(id, uuid, banner, ip, reason, endDate);
bans.add(ban); bans.add(ban);
} }

View File

@ -6,10 +6,16 @@ import dev.plex.storage.StorageType;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
/**
* Parent cache class
*/
public class DataUtils public class DataUtils
{ {
/* PLEX PLAYER METHODS */ /**
* Checks if the player has been on the server before
* @param uuid The unique ID of the player
* @return true if the player is registered in the database
*/
public static boolean hasPlayedBefore(UUID uuid) public static boolean hasPlayedBefore(UUID uuid)
{ {
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)
@ -22,6 +28,12 @@ public class DataUtils
} }
} }
/**
* Gets a player from cache or from the database
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public static PlexPlayer getPlayer(UUID uuid) public static PlexPlayer getPlayer(UUID uuid)
{ {
if (PlayerCache.getPlexPlayerMap().containsKey(uuid)) if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
@ -39,11 +51,22 @@ public class DataUtils
} }
} }
/**
* Gets a player from cache or from the database
* @param name Username of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public static PlexPlayer getPlayer(String name) public static PlexPlayer getPlayer(String name)
{ {
return getPlayer(Bukkit.getPlayer(name).getUniqueId()); return getPlayer(Bukkit.getPlayer(name).getUniqueId());
} }
/**
* Updates a player's information in the database
* @param plexPlayer The PlexPlayer to update
* @see PlexPlayer
*/
public static void update(PlexPlayer plexPlayer) public static void update(PlexPlayer plexPlayer)
{ {
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)
@ -56,6 +79,11 @@ public class DataUtils
} }
} }
/**
* Inserts a player's information in the database
* @param plexPlayer The PlexPlayer to insert
* @see PlexPlayer
*/
public static void insert(PlexPlayer plexPlayer) public static void insert(PlexPlayer plexPlayer)
{ {
if (Plex.get().getStorageType() == StorageType.MONGODB) if (Plex.get().getStorageType() == StorageType.MONGODB)

View File

@ -9,15 +9,29 @@ import dev.plex.Plex;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
import java.util.UUID; import java.util.UUID;
/**
* Mongo fetching utilities for players
*/
public class MongoPlayerData public class MongoPlayerData
{ {
/**
* The datastore object / database
*/
private final Datastore datastore; private final Datastore datastore;
/**
* Creates an instance of the player data
*/
public MongoPlayerData() public MongoPlayerData()
{ {
this.datastore = Plex.get().getMongoConnection().getDatastore(); this.datastore = Plex.get().getMongoConnection().getDatastore();
} }
/**
* Checks whether the player exists in mongo's database
* @param uuid The unique ID of the player
* @return true if the player was found
*/
public boolean exists(UUID uuid) public boolean exists(UUID uuid)
{ {
Query<PlexPlayer> query = datastore.find(PlexPlayer.class) Query<PlexPlayer> query = datastore.find(PlexPlayer.class)
@ -26,6 +40,12 @@ public class MongoPlayerData
return query.first() != null; return query.first() != null;
} }
/**
* Gets the player from cache or from mongo's database
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByUUID(UUID uuid) public PlexPlayer getByUUID(UUID uuid)
{ {
if (PlayerCache.getPlexPlayerMap().containsKey(uuid)) if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
@ -37,6 +57,11 @@ public class MongoPlayerData
return query2.first(); return query2.first();
} }
/**
* Updates a player's information in the mongo database
* @param player The PlexPlayer object
* @see PlexPlayer
*/
public void update(PlexPlayer player) public void update(PlexPlayer player)
{ {
Query<PlexPlayer> filter = datastore.find(PlexPlayer.class) Query<PlexPlayer> filter = datastore.find(PlexPlayer.class)
@ -56,6 +81,11 @@ public class MongoPlayerData
} }
/**
* Saves the player's information in the database
* @param plexPlayer The PlexPlayer object
* @see PlexPlayer
*/
public void save(PlexPlayer plexPlayer) public void save(PlexPlayer plexPlayer)
{ {
datastore.save(plexPlayer); datastore.save(plexPlayer);

View File

@ -6,9 +6,20 @@ import dev.plex.player.PunishedPlayer;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
/**
* Cache storage
*/
public class PlayerCache public class PlayerCache
{ {
/**
* A key/value pair where the key is the unique ID of the Plex Player
*/
private static final Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap(); private static final Map<UUID, PlexPlayer> plexPlayerMap = Maps.newHashMap();
/**
* A key/value pair where the key is the unique ID of the Punished Player
*/
private static final Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap(); private static final Map<UUID, PunishedPlayer> punishedPlayerMap = Maps.newHashMap();
public static Map<UUID, PunishedPlayer> getPunishedPlayerMap() public static Map<UUID, PunishedPlayer> getPunishedPlayerMap()

View File

@ -11,12 +11,20 @@ import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
/**
* SQL fetching utilities for players
*/
public class SQLPlayerData public class SQLPlayerData
{ {
private final String SELECT = "SELECT * FROM `players` WHERE uuid=?"; private final String SELECT = "SELECT * FROM `players` WHERE uuid=?";
private final String UPDATE = "UPDATE `players` SET name=?, login_msg=?, prefix=?, rank=?, ips=?, coins=?, vanished=? WHERE uuid=?"; 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 (?, ?, ?, ?, ?, ?, ?, ?);"; private final String INSERT = "INSERT INTO `players` (`uuid`, `name`, `login_msg`, `prefix`, `rank`, `ips`, `coins`, `vanished`) VALUES (?, ?, ?, ?, ?, ?, ?, ?);";
/**
* Checks if a player exists in the SQL database
* @param uuid The unique ID of the player
* @return true if the player was found in the database
*/
public boolean exists(UUID uuid) public boolean exists(UUID uuid)
{ {
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
@ -33,6 +41,12 @@ public class SQLPlayerData
return false; return false;
} }
/**
* Gets the player from cache or from the SQL database
* @param uuid The unique ID of the player
* @return a PlexPlayer object
* @see PlexPlayer
*/
public PlexPlayer getByUUID(UUID uuid) public PlexPlayer getByUUID(UUID uuid)
{ {
if (PlayerCache.getPlexPlayerMap().containsKey(uuid)) if (PlayerCache.getPlexPlayerMap().containsKey(uuid))
@ -74,6 +88,11 @@ public class SQLPlayerData
return null; return null;
} }
/**
* Updates a player's information in the SQL database
* @param player The PlexPlayer object
* @see PlexPlayer
*/
public void update(PlexPlayer player) public void update(PlexPlayer player)
{ {
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())
@ -95,6 +114,11 @@ public class SQLPlayerData
} }
} }
/**
* Inserts the player's information in the database
* @param player The PlexPlayer object
* @see PlexPlayer
*/
public void insert(PlexPlayer player) public void insert(PlexPlayer player)
{ {
try (Connection con = Plex.get().getSqlConnection().getCon()) try (Connection con = Plex.get().getSqlConnection().getCon())

View File

@ -26,16 +26,39 @@ import org.jetbrains.annotations.Nullable;
import java.util.Arrays; import java.util.Arrays;
import java.util.UUID; import java.util.UUID;
/**
* Superclass for all commands
*/
public abstract class PlexCommand extends Command implements PluginIdentifiableCommand public abstract class PlexCommand extends Command implements PluginIdentifiableCommand
{ {
/**
* Returns the instance of the plugin
*/
protected static Plex plugin = Plex.get(); protected static Plex plugin = Plex.get();
/**
* The parameters for the command
*/
private final CommandParameters params; private final CommandParameters params;
/**
* The permissions for the command
*/
private final CommandPermissions perms; private final CommandPermissions perms;
/**
* Minimum required rank fetched from the permissions
*/
private final Rank level; private final Rank level;
/**
* Required command source fetched from the permissions
*/
private final RequiredCommandSource commandSource; private final RequiredCommandSource commandSource;
/**
* Creates an instance of the command
*/
public PlexCommand() public PlexCommand()
{ {
super(""); super("");
@ -56,9 +79,19 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
getMap().register("plex", this); getMap().register("plex", this);
} }
/**
* Executes the command
* @param sender The sender of the command
* @param playerSender The player who executed the command (null if command source is console or if command source is any but console executed)
* @param args A Kyori Component to send to the sender (can be null)
* @return
*/
protected abstract Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args); protected abstract Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args);
/**
* @hidden
*/
@Override @Override
public boolean execute(@NotNull CommandSender sender, @NotNull String label, String[] args) public boolean execute(@NotNull CommandSender sender, @NotNull String label, String[] args)
{ {
@ -158,6 +191,11 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
/**
* Checks if the string given is a command string
* @param label The string to check
* @return true if the string is a command name or alias
*/
private boolean matches(String label) private boolean matches(String label)
{ {
if (params.aliases().split(",").length > 0) if (params.aliases().split(",").length > 0)
@ -176,31 +214,63 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return false; return false;
} }
/**
* Gets a PlexPlayer from Player object
* @param player The player object
* @return PlexPlayer Object
* @see PlexPlayer
*/
protected PlexPlayer getPlexPlayer(@NotNull Player player) protected PlexPlayer getPlexPlayer(@NotNull Player player)
{ {
return DataUtils.getPlayer(player.getUniqueId()); return DataUtils.getPlayer(player.getUniqueId());
} }
/**
* Sends a message to an audience
* @param audience The audience to send the message to
* @param s The message to send
*/
protected void send(Audience audience, String s) protected void send(Audience audience, String s)
{ {
audience.sendMessage(componentFromString(s)); audience.sendMessage(componentFromString(s));
} }
/**
* Sends a message to an audience
* @param audience The audience to send the message to
* @param component The component to send
*/
protected void send(Audience audience, Component component) protected void send(Audience audience, Component component)
{ {
audience.sendMessage(component); audience.sendMessage(component);
} }
/**
* Checks whether a sender has enough permissions or is high enough a rank
* @param sender A command sender
* @param rank The rank to check (if the server is using ranks)
* @param permission The permission to check (if the server is using permissions)
* @return true if the sender has enough permissions
* @see Rank
*/
protected boolean checkRank(CommandSender sender, Rank rank, String permission) protected boolean checkRank(CommandSender sender, Rank rank, String permission)
{ {
if (!isConsole(sender)) if (!isConsole(sender))
{ {
checkRank((Player) sender, rank, permission); return checkRank((Player) sender, rank, permission);
return true; // return true;
} }
return true; return true;
} }
/**
* Checks whether a player has enough permissions or is high enough a rank
* @param player The player object
* @param rank The rank to check (if the server is using ranks)
* @param permission The permission to check (if the server is using permissions)
* @return true if the sender has enough permissions
* @see Rank
*/
protected boolean checkRank(Player player, Rank rank, String permission) protected boolean checkRank(Player player, Rank rank, String permission)
{ {
PlexPlayer plexPlayer = getPlexPlayer(player); PlexPlayer plexPlayer = getPlexPlayer(player);
@ -220,11 +290,22 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return true; return true;
} }
/**
* Checks if a player is an admin
* @param plexPlayer The PlexPlayer object
* @return true if the player is an admin
* @see PlexPlayer
*/
protected boolean isAdmin(PlexPlayer plexPlayer) protected boolean isAdmin(PlexPlayer plexPlayer)
{ {
return Plex.get().getRankManager().isAdmin(plexPlayer); return Plex.get().getRankManager().isAdmin(plexPlayer);
} }
/**
* Checks if a sender is an admin
* @param sender A command sender
* @return true if the sender is an admin or if console
*/
protected boolean isAdmin(CommandSender sender) protected boolean isAdmin(CommandSender sender)
{ {
if (!(sender instanceof Player player)) if (!(sender instanceof Player player))
@ -235,12 +316,22 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return plugin.getRankManager().isAdmin(plexPlayer); return plugin.getRankManager().isAdmin(plexPlayer);
} }
/**
* Checks if a username is an admin
* @param name The username
* @return true if the username is an admin
*/
protected boolean isAdmin(String name) protected boolean isAdmin(String name)
{ {
PlexPlayer plexPlayer = DataUtils.getPlayer(name); PlexPlayer plexPlayer = DataUtils.getPlayer(name);
return plugin.getRankManager().isAdmin(plexPlayer); return plugin.getRankManager().isAdmin(plexPlayer);
} }
/**
* Checks if a sender is a senior admin
* @param sender A command sender
* @return true if the sender is a senior admin or if console
*/
protected boolean isSeniorAdmin(CommandSender sender) protected boolean isSeniorAdmin(CommandSender sender)
{ {
if (!(sender instanceof Player player)) if (!(sender instanceof Player player))
@ -251,6 +342,12 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return plugin.getRankManager().isSeniorAdmin(plexPlayer); return plugin.getRankManager().isSeniorAdmin(plexPlayer);
} }
/**
* Gets the UUID of the sender
* @param sender A command sender
* @return A unique ID or null if the sender is console
* @see UUID
*/
protected UUID getUUID(CommandSender sender) protected UUID getUUID(CommandSender sender)
{ {
if (!(sender instanceof Player player)) if (!(sender instanceof Player player))
@ -260,23 +357,43 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return player.getUniqueId(); return player.getUniqueId();
} }
/**
* The plugin
* @return The instance of the plugin
* @see Plex
*/
@Override @Override
public @NotNull Plugin getPlugin() public @NotNull Plex getPlugin()
{ {
return plugin; return plugin;
} }
/**
* Checks whether a sender is console
* @param sender A command sender
* @return true if the sender is console
*/
protected boolean isConsole(CommandSender sender) protected boolean isConsole(CommandSender sender)
{ {
return !(sender instanceof Player); return !(sender instanceof Player);
} }
/**
* Converts a message entry from the "messages.yml" to a component
* @param s The message entry
* @param objects Any objects to replace in order
* @return A kyori component
*/
protected Component tl(String s, Object... objects) protected Component tl(String s, Object... objects)
{ {
return componentFromString(PlexUtils.tl(s, objects)); return componentFromString(PlexUtils.tl(s, objects));
} }
/**
* Converts usage to a component
* @param s The usage to convert
* @return A kyori component stating the usage
*/
protected Component usage(String s) protected Component usage(String s)
{ {
return componentFromString(ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + s); return componentFromString(ChatColor.YELLOW + "Correct Usage: " + ChatColor.GRAY + s);
@ -323,6 +440,11 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
return world; return world;
} }
/**
* Converts a string to a legacy kyori component
* @param s The string to convert
* @return A kyori component
*/
protected Component componentFromString(String s) protected Component componentFromString(String s)
{ {
return LegacyComponentSerializer.legacyAmpersand().deserialize(s); return LegacyComponentSerializer.legacyAmpersand().deserialize(s);

View File

@ -3,14 +3,33 @@ package dev.plex.command.annotation;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
/**
* Storage for a command's parameters
*/
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface CommandParameters public @interface CommandParameters
{ {
/**
* The name
* @return Name of the command
*/
String name(); String name();
/**
* The description
* @return Description of the command
*/
String description() default ""; String description() default "";
/**
* The usage (optional)
* @return The usage of the command
*/
String usage() default "/<command>"; String usage() default "/<command>";
/**
* The aliases (optional)
* @return The aliases of the command
*/
String aliases() default ""; String aliases() default "";
} }

View File

@ -5,12 +5,29 @@ import dev.plex.rank.enums.Rank;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
/**
* Storage for the command's permissions
*/
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
public @interface CommandPermissions public @interface CommandPermissions
{ {
/**
* Minimum rank required
* @return Minimum rank required for the command
* @see Rank
*/
Rank level() default Rank.IMPOSTOR; Rank level() default Rank.IMPOSTOR;
/**
* Required command source
* @return The required command source of the command
* @see RequiredCommandSource
*/
RequiredCommandSource source() default RequiredCommandSource.ANY; RequiredCommandSource source() default RequiredCommandSource.ANY;
/**
* The permission
* @return Permission of the command
*/
String permission() default ""; // No idea what to put here String permission() default ""; // No idea what to put here
} }

View File

@ -15,6 +15,8 @@ import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -75,8 +77,8 @@ public class BanCMD extends PlexCommand
punishment.setReason("No reason provided."); punishment.setReason("No reason provided.");
} }
punishment.setPunishedUsername(plexPlayer.getName()); punishment.setPunishedUsername(plexPlayer.getName());
Date date = new Date(); LocalDateTime date = LocalDateTime.now();
punishment.setEndDate(DateUtils.addDays(date, 1)); punishment.setEndDate(date.plusDays(1));
punishment.setCustomTime(false); punishment.setCustomTime(false);
plugin.getPunishmentManager().doPunishment(punishedPlayer, punishment); plugin.getPunishmentManager().doPunishment(punishedPlayer, punishment);
PlexUtils.broadcast(tl("banningPlayer", sender.getName(), plexPlayer.getName())); PlexUtils.broadcast(tl("banningPlayer", sender.getName(), plexPlayer.getName()));

View File

@ -11,6 +11,8 @@ import dev.plex.punishment.Punishment;
import dev.plex.punishment.PunishmentType; import dev.plex.punishment.PunishmentType;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexUtils; import dev.plex.util.PlexUtils;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -36,8 +38,8 @@ public class FreezeCMD extends PlexCommand
PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId()); PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId());
Punishment punishment = new Punishment(UUID.fromString(punishedPlayer.getUuid()), getUUID(sender)); Punishment punishment = new Punishment(UUID.fromString(punishedPlayer.getUuid()), getUUID(sender));
punishment.setCustomTime(false); punishment.setCustomTime(false);
Date date = new Date(); LocalDateTime date = LocalDateTime.now();
punishment.setEndDate(DateUtils.addMinutes(date, 5)); punishment.setEndDate(date.plusMinutes(5));
punishment.setType(PunishmentType.FREEZE); punishment.setType(PunishmentType.FREEZE);
punishment.setPunishedUsername(player.getName()); punishment.setPunishedUsername(player.getName());
punishment.setReason(""); punishment.setReason("");

View File

@ -2,6 +2,8 @@ package dev.plex.punishment;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.gson.Gson; import com.google.gson.Gson;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -22,7 +24,7 @@ public class Punishment
private PunishmentType type; private PunishmentType type;
private String reason; private String reason;
private boolean customTime; private boolean customTime;
private Date endDate; private LocalDateTime endDate;
public Punishment(UUID punished, UUID punisher) public Punishment(UUID punished, UUID punisher)
{ {

View File

@ -13,6 +13,10 @@ import java.io.FileInputStream;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -101,9 +105,9 @@ public class PunishmentManager extends PlexBase
else if (punishment.getType() == PunishmentType.FREEZE) else if (punishment.getType() == PunishmentType.FREEZE)
{ {
player.setFrozen(true); player.setFrozen(true);
Date now = new Date(); LocalDateTime now = LocalDateTime.now();
Date then = punishment.getEndDate(); LocalDateTime then = punishment.getEndDate();
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime()); long seconds = ChronoUnit.SECONDS.between(now, then);
new BukkitRunnable() new BukkitRunnable()
{ {
@Override @Override
@ -123,9 +127,9 @@ public class PunishmentManager extends PlexBase
else if (punishment.getType() == PunishmentType.MUTE) else if (punishment.getType() == PunishmentType.MUTE)
{ {
player.setMuted(true); player.setMuted(true);
Date now = new Date(); LocalDateTime now = LocalDateTime.now();
Date then = punishment.getEndDate(); LocalDateTime then = punishment.getEndDate();
long seconds = TimeUnit.MILLISECONDS.toSeconds(then.getTime() - now.getTime()); long seconds = ChronoUnit.SECONDS.between(now, then);
new BukkitRunnable() new BukkitRunnable()
{ {
@Override @Override

View File

@ -3,6 +3,8 @@ package dev.plex.services.impl;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.banning.Ban; import dev.plex.banning.Ban;
import dev.plex.services.AbstractService; import dev.plex.services.AbstractService;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -18,7 +20,7 @@ public class BanService extends AbstractService
{ {
for (Ban ban : Plex.get().getBanManager().getActiveBans()) for (Ban ban : Plex.get().getBanManager().getActiveBans())
{ {
if (new Date().after(ban.getEndDate())) if (LocalDateTime.now().isAfter(ban.getEndDate()))
{ {
Plex.get().getBanManager().unban(ban.getId()); Plex.get().getBanManager().unban(ban.getId());
Bukkit.broadcastMessage("Plex - Unbanned " + Bukkit.getOfflinePlayer(ban.getUuid()).getName()); Bukkit.broadcastMessage("Plex - Unbanned " + Bukkit.getOfflinePlayer(ban.getUuid()).getName());