From 565cef61db5bd05d9fb0fc0ece86cc98f03595b4 Mon Sep 17 00:00:00 2001 From: Taah Date: Mon, 28 Feb 2022 12:39:04 -0800 Subject: [PATCH] - Add /lockup --- .../java/dev/plex/command/impl/LockupCMD.java | 61 +++++++++++++++++++ .../plex/event/PunishedPlayerFreezeEvent.java | 2 - .../plex/event/PunishedPlayerLockupEvent.java | 43 +++++++++++++ .../plex/event/PunishedPlayerMuteEvent.java | 2 - .../dev/plex/handlers/CommandHandler.java | 1 + .../plex/listener/impl/PlayerListener.java | 27 ++++++++ .../java/dev/plex/player/PunishedPlayer.java | 19 ++++++ src/main/java/dev/plex/util/PlexUtils.java | 27 -------- src/main/resources/messages.yml | 17 ++++-- 9 files changed, 164 insertions(+), 35 deletions(-) create mode 100644 src/main/java/dev/plex/command/impl/LockupCMD.java create mode 100644 src/main/java/dev/plex/event/PunishedPlayerLockupEvent.java diff --git a/src/main/java/dev/plex/command/impl/LockupCMD.java b/src/main/java/dev/plex/command/impl/LockupCMD.java new file mode 100644 index 0000000..717f71d --- /dev/null +++ b/src/main/java/dev/plex/command/impl/LockupCMD.java @@ -0,0 +1,61 @@ +package dev.plex.command.impl; + +import com.google.common.collect.ImmutableList; +import dev.plex.cache.PlayerCache; +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.player.PlexPlayer; +import dev.plex.player.PunishedPlayer; +import dev.plex.punishment.Punishment; +import dev.plex.punishment.PunishmentType; +import dev.plex.rank.enums.Rank; +import dev.plex.util.PlexUtils; +import net.kyori.adventure.text.Component; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.time.LocalDateTime; +import java.util.List; +import java.util.UUID; + +@CommandParameters(name = "lockup", description = "Lockup a player on the server", usage = "/ ") +@CommandPermissions(level = Rank.ADMIN, permission = "plex.lockup") +public class LockupCMD extends PlexCommand +{ + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) + { + if (args.length != 1) + { + return usage(); + } + Player player = getNonNullPlayer(args[0]); + PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId()); + + if (isAdmin(getPlexPlayer(player))) + { + if (!isConsole(sender)) + { + assert playerSender != null; + PlexPlayer plexPlayer1 = getPlexPlayer(playerSender); + if (!plexPlayer1.getRankFromString().isAtLeast(getPlexPlayer(player).getRankFromString())) + { + return messageComponent("higherRankThanYou"); + } + } + } + + punishedPlayer.setLockedUp(!punishedPlayer.isLockedUp()); + PlexUtils.broadcast(messageComponent(punishedPlayer.isLockedUp() ? "lockedUpPlayer" : "unlockedUpPlayer", sender.getName(), player.getName())); + return null; + } + + @Override + public @NotNull List tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException + { + return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.mute") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/event/PunishedPlayerFreezeEvent.java b/src/main/java/dev/plex/event/PunishedPlayerFreezeEvent.java index 2cc27af..cf4e1b9 100644 --- a/src/main/java/dev/plex/event/PunishedPlayerFreezeEvent.java +++ b/src/main/java/dev/plex/event/PunishedPlayerFreezeEvent.java @@ -5,8 +5,6 @@ import lombok.Getter; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; -//TODO: Event doesn't look like it is called - /** * Event that is called when a player is frozen or unfrozen */ diff --git a/src/main/java/dev/plex/event/PunishedPlayerLockupEvent.java b/src/main/java/dev/plex/event/PunishedPlayerLockupEvent.java new file mode 100644 index 0000000..e1aa664 --- /dev/null +++ b/src/main/java/dev/plex/event/PunishedPlayerLockupEvent.java @@ -0,0 +1,43 @@ +package dev.plex.event; + +import dev.plex.player.PunishedPlayer; +import lombok.Getter; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; + +/** + * Event that is called when a player is frozen or unfrozen + */ +@Getter +public class PunishedPlayerLockupEvent extends PunishedPlayerEvent implements Cancellable +{ + private static final HandlerList handlers = new HandlerList(); + + /** + * New lock up state of the player + */ + private final boolean lockedUp; + + /** + * Creates a new event instance + * + * @param punishedPlayer The player who was punished + * @param lockedUp The new muted status + */ + public PunishedPlayerLockupEvent(PunishedPlayer punishedPlayer, boolean lockedUp) + { + super(punishedPlayer); + this.lockedUp = lockedUp; + } + + public static HandlerList getHandlerList() + { + return handlers; + } + + @Override + public HandlerList getHandlers() + { + return handlers; + } +} \ No newline at end of file diff --git a/src/main/java/dev/plex/event/PunishedPlayerMuteEvent.java b/src/main/java/dev/plex/event/PunishedPlayerMuteEvent.java index 4a725ab..46e4593 100644 --- a/src/main/java/dev/plex/event/PunishedPlayerMuteEvent.java +++ b/src/main/java/dev/plex/event/PunishedPlayerMuteEvent.java @@ -5,8 +5,6 @@ import lombok.Getter; import org.bukkit.event.Cancellable; import org.bukkit.event.HandlerList; -//TODO: Event doesn't look like it is called - /** * Event that is called when a player is frozen or unfrozen */ diff --git a/src/main/java/dev/plex/handlers/CommandHandler.java b/src/main/java/dev/plex/handlers/CommandHandler.java index 0550b24..a3e7bb4 100644 --- a/src/main/java/dev/plex/handlers/CommandHandler.java +++ b/src/main/java/dev/plex/handlers/CommandHandler.java @@ -37,6 +37,7 @@ public class CommandHandler extends PlexBase commands.add(new FreezeCMD()); commands.add(new ListCMD()); commands.add(new LocalSpawnCMD()); + commands.add(new LockupCMD()); commands.add(new MasterbuilderworldCMD()); commands.add(new MuteCMD()); commands.add(new NameHistoryCMD()); diff --git a/src/main/java/dev/plex/listener/impl/PlayerListener.java b/src/main/java/dev/plex/listener/impl/PlayerListener.java index fc595a2..92988f1 100644 --- a/src/main/java/dev/plex/listener/impl/PlayerListener.java +++ b/src/main/java/dev/plex/listener/impl/PlayerListener.java @@ -14,10 +14,13 @@ import java.util.UUID; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; @@ -60,6 +63,10 @@ public class PlayerListener extends PlexListener PunishedPlayer punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId()); PlayerCache.getPlexPlayerMap().put(player.getUniqueId(), plexPlayer); //put them into the cache punishedPlayer.convertPunishments(); + if (punishedPlayer.isLockedUp()) + { + player.openInventory(player.getInventory()); + } assert plexPlayer != null; @@ -95,4 +102,24 @@ public class PlayerListener extends PlexListener PlayerCache.getPlexPlayerMap().remove(event.getPlayer().getUniqueId()); //remove them from cache } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onPlayerInventoryClose(InventoryCloseEvent event) + { + PunishedPlayer player = PlayerCache.getPunishedPlayer(event.getPlayer().getUniqueId()); + if (player.isLockedUp()) + { + Bukkit.getScheduler().runTaskLater(plugin, () -> event.getPlayer().openInventory(event.getInventory()), 1L); + } + } + + @EventHandler(priority = EventPriority.HIGHEST) + public void onInventoryClick(InventoryClickEvent event) + { + PunishedPlayer player = PlayerCache.getPunishedPlayer(event.getWhoClicked().getUniqueId()); + if (player.isLockedUp()) + { + event.setCancelled(true); + } + } } diff --git a/src/main/java/dev/plex/player/PunishedPlayer.java b/src/main/java/dev/plex/player/PunishedPlayer.java index c813da8..791bfa3 100644 --- a/src/main/java/dev/plex/player/PunishedPlayer.java +++ b/src/main/java/dev/plex/player/PunishedPlayer.java @@ -5,6 +5,7 @@ import com.google.common.collect.Maps; import dev.plex.Plex; import dev.plex.PlexBase; import dev.plex.event.PunishedPlayerFreezeEvent; +import dev.plex.event.PunishedPlayerLockupEvent; import dev.plex.event.PunishedPlayerMuteEvent; import dev.plex.punishment.Punishment; import dev.plex.util.PlexLog; @@ -23,6 +24,7 @@ import lombok.Setter; import lombok.SneakyThrows; import org.apache.commons.io.FileUtils; import org.bukkit.Bukkit; +import org.bukkit.entity.Player; import org.json.JSONArray; import org.json.JSONObject; import org.json.JSONTokener; @@ -36,12 +38,14 @@ public class PunishedPlayer extends PlexBase private boolean muted; private boolean frozen; + private boolean lockedUp; public PunishedPlayer(UUID playerUUID) { this.uuid = playerUUID.toString(); this.muted = false; this.frozen = false; + this.lockedUp = false; } public void setFrozen(boolean frozen) @@ -64,6 +68,21 @@ public class PunishedPlayer extends PlexBase } } + public void setLockedUp(boolean lockedUp) + { + PunishedPlayerLockupEvent e = new PunishedPlayerLockupEvent(this, this.lockedUp); + Bukkit.getServer().getPluginManager().callEvent(e); + if (!e.isCancelled()) + { + this.lockedUp = lockedUp; + Player self = Bukkit.getPlayer(UUID.fromString(this.uuid)); + if (self != null) + { + self.openInventory(self.getInventory()); + } + } + } + public File getPunishmentsFile() { File folder = new File(Plex.get().getDataFolder() + File.separator + "punishments"); diff --git a/src/main/java/dev/plex/util/PlexUtils.java b/src/main/java/dev/plex/util/PlexUtils.java index 7d8bfee..ec1633c 100644 --- a/src/main/java/dev/plex/util/PlexUtils.java +++ b/src/main/java/dev/plex/util/PlexUtils.java @@ -129,33 +129,6 @@ public class PlexUtils extends PlexBase return ChatColor.translateAlternateColorCodes('&', string); } - /*@Deprecated(forRemoval = true) - // if you think the name of this method is dumb feel free to change it i just thought it'd be cool - public static String messageComponent(String s, Object... objects) - { - if (s.equals("baseColor") || s.equals("errorColor") || s.equals("broadcastColor")) - { - return getChatColorFromConfig(plugin.messages, ChatColor.WHITE, s).toString(); - } - String f = plugin.messages.getString(s); - if (f == null) - { - return ChatColor.RED + "No message"; - } - for (Object object : objects) - { - f = f.replaceFirst("", String.valueOf(object)); - } - ChatColor base = getChatColorFromConfig(plugin.messages, ChatColor.GRAY, "baseColor"); - ChatColor broadcast = getChatColorFromConfig(plugin.messages, ChatColor.AQUA, "broadcastColor"); - ChatColor error = getChatColorFromConfig(plugin.messages, ChatColor.RED, "errorColor"); - f = f.replaceAll("", base.toString()); - f = f.replaceAll("", broadcast.toString()); - f = f.replaceAll("", error.toString()); - f = color(f); - return base + f; - }*/ - public static Component messageComponent(String entry, Object... objects) { return MiniMessage.miniMessage().parse(LegacyComponentSerializer.legacySection().serialize(LegacyComponentSerializer.legacyAmpersand().deserialize(messageString(entry, objects)))); diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index d731d05..141e6c9 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -39,12 +39,19 @@ frozePlayer: " - Froze " # 1. The person who is unfreezing # 2. The person who has been unfrozen unfrozePlayer: " - Unfroze " -# 1. The person who is freezing -# 2. The person who has been frozen +# 1. The person who is muting +# 2. The person who has been muted mutedPlayer: " - Muted " -# 1. The person who is unfreezing -# 2. The person who has been unfrozen +# 1. The person who is unmuting +# 2. The person who has been unmuted unmutedPlayer: " - Unmuted " +# 1. The person who is locking up +# 2. The person who has been locked up +lockedUpPlayer: " - Locking up " +# 1. The person who is unlocking +# 2. The person who has been unlocked +unlockedUpPlayer: " - Unlocking " + noPermission: "You cannot use this command!" # 1. The rank required to use the command noPermissionRank: "You must be at least to use this command!" @@ -106,6 +113,8 @@ playerNotMuted: "That player is not muted!" playerBanned: "That player is already banned!" playerFrozen: "That player is already frozen!" playerMuted: "That player is already muted!" +playerLockedUp: "That player is already locked up!" + muted: "You are currently muted - STFU!" teleportedToWorldSpawn: "Teleporting to the local spawn" toggleCommandSpy: "CommandSpy has been"