From d4761d438600a848fe3f1aa31eac084e4a572396 Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Sun, 30 Jan 2022 14:56:08 -0600 Subject: [PATCH] Many, many bug fixes - Fix permission checks - Fix unban command - getOfflinePlexPlayer no longer uses cache - Register worlds before trying to load gamerules - Customizable ban messages --- src/main/java/dev/plex/Plex.java | 6 ++-- .../java/dev/plex/command/PlexCommand.java | 18 ++++++++--- .../java/dev/plex/command/impl/BanCMD.java | 6 ++-- .../java/dev/plex/command/impl/PlexCMD.java | 16 ++++++---- .../java/dev/plex/command/impl/UnbanCMD.java | 9 ++++-- .../dev/plex/handlers/CommandHandler.java | 2 +- .../dev/plex/listener/impl/ChatListener.java | 13 ++------ .../dev/plex/listener/impl/LoginListener.java | 12 ++++--- .../dev/plex/services/ServiceManager.java | 31 +++++++++++++------ src/main/java/dev/plex/util/PlexUtils.java | 2 +- src/main/resources/config.yml | 7 ++++- src/main/resources/messages.yml | 2 +- 12 files changed, 74 insertions(+), 50 deletions(-) diff --git a/src/main/java/dev/plex/Plex.java b/src/main/java/dev/plex/Plex.java index 5617b32..e900564 100644 --- a/src/main/java/dev/plex/Plex.java +++ b/src/main/java/dev/plex/Plex.java @@ -105,7 +105,7 @@ public class Plex extends JavaPlugin sqlPlayerData = new SQLPlayerData(); } - new ListenerHandler(); // this doesn't need a variable. + new ListenerHandler(); new CommandHandler(); @@ -119,14 +119,14 @@ public class Plex extends JavaPlugin banManager = new BanManager(); PlexLog.log("Punishment System initialized"); + generateWorlds(); + serviceManager = new ServiceManager(); PlexLog.log("Service Manager initialized"); serviceManager.startServices(); PlexLog.log("Started " + serviceManager.serviceCount() + " services."); - generateWorlds(); - reloadPlayers(); } diff --git a/src/main/java/dev/plex/command/PlexCommand.java b/src/main/java/dev/plex/command/PlexCommand.java index 8715bd9..32b14f1 100644 --- a/src/main/java/dev/plex/command/PlexCommand.java +++ b/src/main/java/dev/plex/command/PlexCommand.java @@ -167,6 +167,16 @@ public abstract class PlexCommand extends Command audience.sendMessage(component); } + protected boolean checkRank(CommandSender sender, Rank rank, String permission) + { + if (!isConsole(sender)) + { + checkRank((Player)sender, rank, permission); + return true; + } + return true; + } + protected boolean checkRank(Player player, Rank rank, String permission) { PlexPlayer plexPlayer = getPlexPlayer(player); @@ -174,16 +184,14 @@ public abstract class PlexCommand extends Command { if (!plexPlayer.getRank().equals(rank.toString())) { - send(player, tl("noPermissionRank", rank.toString())); - return true; + throw new CommandFailException(PlexUtils.tl("noPermissionRank", ChatColor.stripColor(rank.getLoginMSG()))); } } else if (plugin.getRanksOrPermissions().equalsIgnoreCase("permissions")) { if (!player.hasPermission(permission)) { - send(player, tl("noPermissionNode", permission)); - return true; + throw new CommandFailException(PlexUtils.tl("noPermissionNode", permission)); } } return true; @@ -268,7 +276,7 @@ public abstract class PlexCommand extends Command protected PlexPlayer getOfflinePlexPlayer(UUID uuid) { - PlexPlayer plexPlayer = PlayerCache.getPlexPlayer(uuid); + PlexPlayer plexPlayer = DataUtils.getPlayer(uuid); if (plexPlayer == null) { throw new PlayerNotFoundException(); diff --git a/src/main/java/dev/plex/command/impl/BanCMD.java b/src/main/java/dev/plex/command/impl/BanCMD.java index 77fefdf..a4874cc 100644 --- a/src/main/java/dev/plex/command/impl/BanCMD.java +++ b/src/main/java/dev/plex/command/impl/BanCMD.java @@ -64,11 +64,10 @@ public class BanCMD extends PlexCommand punishment.setType(PunishmentType.BAN); punishment.setReason(""); punishment.setPunishedUsername(plexPlayer.getName()); - //TODO: Debug End date - punishment.setEndDate(new Date(Instant.now().plusSeconds(10/*PlexUtils.secondsToHours(24)*/).getEpochSecond())); + punishment.setEndDate(new Date(Instant.now().plusSeconds(PlexUtils.hoursToSeconds(24)).getEpochSecond())); punishment.setCustomTime(false); plugin.getPunishmentManager().doPunishment(punishedPlayer, punishment); - PlexUtils.broadcast(tl("unbanningPlayer", sender.getName(), plexPlayer.getName())); + PlexUtils.broadcast(tl("banningPlayer", sender.getName(), plexPlayer.getName())); if (Bukkit.getPlayer(targetUUID) != null) { Bukkit.getPlayer(targetUUID).kick(componentFromString("&cYou've been banned.")); @@ -82,5 +81,4 @@ public class BanCMD extends PlexCommand { return args.length == 1 && isAdmin(sender) ? PlexUtils.getPlayerNameList() : ImmutableList.of(); } - } diff --git a/src/main/java/dev/plex/command/impl/PlexCMD.java b/src/main/java/dev/plex/command/impl/PlexCMD.java index b409d7e..f815513 100644 --- a/src/main/java/dev/plex/command/impl/PlexCMD.java +++ b/src/main/java/dev/plex/command/impl/PlexCMD.java @@ -17,17 +17,20 @@ import org.jetbrains.annotations.NotNull; @CommandPermissions(level = Rank.OP, permission = "plex.plex", source = RequiredCommandSource.ANY) @CommandParameters(name = "plex", usage = "/ [reload]", aliases = "plexhelp", description = "Show information about Plex or reload it") -public class PlexCMD extends PlexCommand { +public class PlexCMD extends PlexCommand +{ @Override - public Component execute(CommandSender sender, String[] args) { - if (args.length == 0) { + public Component execute(CommandSender sender, String[] args) + { + if (args.length == 0) + { send(sender, ChatColor.LIGHT_PURPLE + "Plex. The long awaited TotalFreedomMod rewrite starts here..."); return componentFromString(ChatColor.LIGHT_PURPLE + "Plugin version: " + ChatColor.GOLD + "1.0"); } if (args[0].equals("reload")) { - checkRank((Player)sender, Rank.SENIOR_ADMIN, "plex.reload"); + checkRank(sender, Rank.SENIOR_ADMIN, "plex.reload"); Plex.get().config.load(); send(sender, "Reloaded config file"); Plex.get().messages.load(); @@ -35,7 +38,9 @@ public class PlexCMD extends PlexCommand { Plex.get().getRankManager().importDefaultRanks(); send(sender, "Imported ranks"); send(sender, "Plex successfully reloaded."); - } else { + } + else + { throw new CommandArgumentException(); } return null; @@ -46,5 +51,4 @@ public class PlexCMD extends PlexCommand { { return ImmutableList.of("reload"); } - } \ No newline at end of file diff --git a/src/main/java/dev/plex/command/impl/UnbanCMD.java b/src/main/java/dev/plex/command/impl/UnbanCMD.java index f2ba51c..c520839 100644 --- a/src/main/java/dev/plex/command/impl/UnbanCMD.java +++ b/src/main/java/dev/plex/command/impl/UnbanCMD.java @@ -8,11 +8,14 @@ import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.exception.PlayerNotBannedException; import dev.plex.command.exception.PlayerNotFoundException; import dev.plex.command.source.RequiredCommandSource; +import dev.plex.player.PlexPlayer; import dev.plex.rank.enums.Rank; import dev.plex.util.PlexUtils; import java.util.List; import java.util.UUID; import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; @@ -33,9 +36,9 @@ public class UnbanCMD extends PlexCommand if (args.length == 1) { UUID targetUUID = PlexUtils.getFromName(args[0]); - Player player = getNonNullPlayer(args[0]); + PlexPlayer plexPlayer = getOfflinePlexPlayer(targetUUID); - if (targetUUID == null || !DataUtils.hasPlayedBefore(targetUUID)) + if (!DataUtils.hasPlayedBefore(targetUUID)) { throw new PlayerNotFoundException(); } @@ -46,7 +49,7 @@ public class UnbanCMD extends PlexCommand } plugin.getBanManager().unban(targetUUID); - PlexUtils.broadcast(tl("unbanningPlayer", sender.getName(), player.getName())); + PlexUtils.broadcast(tl("unbanningPlayer", sender.getName(), plexPlayer.getName())); } return null; } diff --git a/src/main/java/dev/plex/handlers/CommandHandler.java b/src/main/java/dev/plex/handlers/CommandHandler.java index 1195dfa..5ae4fde 100644 --- a/src/main/java/dev/plex/handlers/CommandHandler.java +++ b/src/main/java/dev/plex/handlers/CommandHandler.java @@ -19,6 +19,7 @@ public class CommandHandler extends PlexBase commands.add(new DeopCMD()); commands.add(new OpAllCMD()); commands.add(new OpCMD()); + commands.add(new RankCMD()); } commands.add(new AdminworldCMD()); commands.add(new AdventureCMD()); @@ -31,7 +32,6 @@ public class CommandHandler extends PlexBase commands.add(new NameHistoryCMD()); commands.add(new PlexCMD()); commands.add(new PunishmentsCMD()); - commands.add(new RankCMD()); commands.add(new SpectatorCMD()); commands.add(new SurvivalCMD()); commands.add(new UnbanCMD()); diff --git a/src/main/java/dev/plex/listener/impl/ChatListener.java b/src/main/java/dev/plex/listener/impl/ChatListener.java index 96bb7f2..ba38361 100644 --- a/src/main/java/dev/plex/listener/impl/ChatListener.java +++ b/src/main/java/dev/plex/listener/impl/ChatListener.java @@ -28,22 +28,13 @@ public class ChatListener extends PlexListener { renderer.hasPrefix = true; renderer.prefix = plexPlayer.getPrefix(); - } else if (Plex.get().getRankManager().isAdmin(plexPlayer)) + } + else if (Plex.get().getRankManager().isAdmin(plexPlayer)) { renderer.hasPrefix = true; renderer.prefix = plexPlayer.getRankFromString().getPrefix(); } event.renderer(renderer); - /*if (!plexPlayer.getPrefix().isEmpty()) - { - event.setFormat(String.format("%s %s §7» %s", plexPlayer.getPrefix(), ChatColor.RESET + plexPlayer.displayName(), event.getMessage())); - } else if (Plex.get().getRankManager().isAdmin(plexPlayer)) - { - event.setFormat(String.format("%s %s §7» %s", plexPlayer.getRankFromString().getPrefix(), ChatColor.RESET + plexPlayer.displayName(), event.getMessage())); - } else - { - event.setFormat(String.format("%s §7» %s", ChatColor.RESET + plexPlayer.displayName(), event.getMessage())); - }*/ } public static class PlexChatRenderer implements ChatRenderer diff --git a/src/main/java/dev/plex/listener/impl/LoginListener.java b/src/main/java/dev/plex/listener/impl/LoginListener.java index 63d9f73..5d3d9d6 100644 --- a/src/main/java/dev/plex/listener/impl/LoginListener.java +++ b/src/main/java/dev/plex/listener/impl/LoginListener.java @@ -1,21 +1,25 @@ package dev.plex.listener.impl; import dev.plex.listener.PlexListener; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.player.AsyncPlayerPreLoginEvent; +import org.jetbrains.annotations.NotNull; public class LoginListener extends PlexListener { - //TODO: Customizable MSGS + private final String banMessage = plugin.config.getString("banning.message"); @EventHandler public void onPreLogin(AsyncPlayerPreLoginEvent event) { - // PlexLog.log(String.valueOf(plugin.getBanManager().isBanned(event.getUniqueId()))); if (plugin.getBanManager().isBanned(event.getUniqueId())) { - event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, "§cYou're currently banned from this server.\n§cPlease appeal at §6https://forum.telesphoreo.me"); + event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_BANNED, + LegacyComponentSerializer.legacyAmpersand().deserialize(banMessage)); } } - } diff --git a/src/main/java/dev/plex/services/ServiceManager.java b/src/main/java/dev/plex/services/ServiceManager.java index 0ee7458..b918f4d 100644 --- a/src/main/java/dev/plex/services/ServiceManager.java +++ b/src/main/java/dev/plex/services/ServiceManager.java @@ -7,31 +7,42 @@ import dev.plex.services.impl.GameRuleService; import java.util.List; import org.bukkit.Bukkit; -public class ServiceManager { +public class ServiceManager +{ private final List services = Lists.newArrayList(); - public ServiceManager() { - registerService(new BanService()); + public ServiceManager() + { + //registerService(new BanService()); registerService(new GameRuleService()); } - public void startServices() { - for (AbstractService service : services) { - if (!service.isRepeating()) { + public void startServices() + { + for (AbstractService service : services) + { + if (!service.isRepeating()) + { Bukkit.getScheduler().runTask(Plex.get(), service::run); - } else if (service.isRepeating() && service.isAsynchronous()) { + } + else if (service.isRepeating() && service.isAsynchronous()) + { Bukkit.getScheduler().runTaskTimerAsynchronously(Plex.get(), service::run, 0, 20 * service.repeatInSeconds()); - } else if (service.isRepeating() && !service.isAsynchronous()) { + } + else if (service.isRepeating() && !service.isAsynchronous()) + { Bukkit.getScheduler().runTaskTimer(Plex.get(), service::run, 0, 20 * service.repeatInSeconds()); } } } - private void registerService(AbstractService service) { + private void registerService(AbstractService service) + { services.add(service); } - public int serviceCount() { + public int serviceCount() + { return services.size(); } } diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 90eb5be..c24c6be 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -251,7 +251,7 @@ public class PlexUtils extends PlexBase public static long hoursToSeconds(long hours) { - return hours * 60 * 60; + return hours * 3600; } public static long minutesToSeconds(long minutes) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e6fa7d0..3efcc27 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,10 +1,15 @@ # Plex Configuration File server: - name: "ExampleFreedom" + name: "Plexus" motd: "%servername% - Minecraft %mcversion%" colorize_motd: true +# Ban message is customized here. Additional messages can be configured in messages.yml +banning: + message: "&cYou are banned! Please appeal on the forums at https://forum.plex.us.org/" + + # Settings for commands relating to Plex commands: # Should Plex use a "true op" system with ranks or only permission nodes diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index bd2d3e6..b0654ac 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -42,7 +42,7 @@ frozePlayer: " - Froze " unfrozePlayer: " - Unfroze " noPermission: "You cannot use this command!" # 1: the login message (uncolored) of the rank required to use the command -noPermissionRank: "You must be at least rank to use this command!" +noPermissionRank: "You must be at least to use this command!" noPermissionNode: "You must have the permission: to use this command!" noPermissionInGame: "You must be in console to use this command!" noPermissionConsole: "You must be in-game to use this command!"