From 273dd8a5efedfe8734e80d64948cf1c93599156a Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Fri, 25 Mar 2022 00:45:55 -0500 Subject: [PATCH] Add blockedit command --- .../java/dev/plex/command/PlexCommand.java | 18 ++++ .../dev/plex/command/impl/BlockEditCMD.java | 98 +++++++++++++++++++ .../dev/plex/listener/impl/BlockListener.java | 40 ++++++++ src/main/resources/messages.yml | 24 ++++- 4 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 src/main/java/dev/plex/command/impl/BlockEditCMD.java create mode 100644 src/main/java/dev/plex/listener/impl/BlockListener.java diff --git a/src/main/java/dev/plex/command/PlexCommand.java b/src/main/java/dev/plex/command/PlexCommand.java index f8372df..4f67ad0 100644 --- a/src/main/java/dev/plex/command/PlexCommand.java +++ b/src/main/java/dev/plex/command/PlexCommand.java @@ -274,6 +274,24 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC return true; } + protected boolean silentCheckRank(Player player, Rank rank, String permission) + { + if (player instanceof ConsoleCommandSender) + { + return true; + } + PlexPlayer plexPlayer = getPlexPlayer(player); + if (plugin.getSystem().equalsIgnoreCase("ranks")) + { + return plexPlayer.getRankFromString().isAtLeast(rank); + } + else if (plugin.getSystem().equalsIgnoreCase("permissions")) + { + return player.hasPermission(permission); + } + return false; + } + /** * Checks whether a sender has enough permissions or is high enough a rank * diff --git a/src/main/java/dev/plex/command/impl/BlockEditCMD.java b/src/main/java/dev/plex/command/impl/BlockEditCMD.java new file mode 100644 index 0000000..6fdbcc2 --- /dev/null +++ b/src/main/java/dev/plex/command/impl/BlockEditCMD.java @@ -0,0 +1,98 @@ +package dev.plex.command.impl; + +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import dev.plex.listener.impl.BlockListener; +import dev.plex.rank.enums.Rank; +import dev.plex.util.PlexUtils; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +@CommandPermissions(level = Rank.ADMIN, permission = "plex.blockedit") +@CommandParameters(name = "blockedit", usage = "/ [list | purge | all | ]", aliases = "bedit", description = "Prevent players from modifying blocks") +public class BlockEditCMD extends PlexCommand +{ + private final BlockListener bl = new BlockListener(); + + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, @NotNull String[] args) + { + if (args.length == 0) + { + return usage(); + } + + if (args[0].equalsIgnoreCase("list")) + { + send(sender, "The following have block modification abilities restricted:"); + int count = 0; + for (String player : bl.blockedPlayers.stream().toList()) + { + send(sender, "- " + player); + ++count; + } + if (count == 0) + { + send(sender, "- none"); + } + return null; + } + else if (args[0].equalsIgnoreCase("purge")) + { + PlexUtils.broadcast(componentFromString(sender.getName() + " - Unblocking block modification abilities for all players").color(NamedTextColor.AQUA)); + int count = 0; + for (String player : bl.blockedPlayers.stream().toList()) + { + if (bl.blockedPlayers.contains(player)) + { + bl.blockedPlayers.remove(player); + ++count; + } + } + return messageComponent("unblockedEditsSize", count); + } + else if (args[0].equalsIgnoreCase("all")) + { + PlexUtils.broadcast(componentFromString(sender.getName() + " - Blocking block modification abilities for all non-admins").color(NamedTextColor.RED)); + int count = 0; + for (final Player player : Bukkit.getOnlinePlayers()) + { + if (!silentCheckRank(player, Rank.ADMIN, "plex.blockedit")) + { + bl.blockedPlayers.add(player.getName()); + ++count; + } + } + + return messageComponent("blockedEditsSize", count); + } + + final Player player = getNonNullPlayer(args[0]); + if (!bl.blockedPlayers.contains(player.getName())) + { + if (silentCheckRank(player, Rank.ADMIN, "plex.blockedit")) + { + send(sender, messageComponent("higherRankThanYou")); + return null; + } + PlexUtils.broadcast(messageComponent("blockingEditFor", sender.getName(), player.getName())); + bl.blockedPlayers.add(player.getName()); + send(player, messageComponent("yourEditsHaveBeenBlocked")); + send(sender, messageComponent("editsBlocked", player.getName())); + } + else + { + PlexUtils.broadcast(messageComponent("unblockingEditFor", sender.getName(), player.getName())); + bl.blockedPlayers.remove(player.getName()); + send(player, messageComponent("yourEditsHaveBeenUnblocked")); + send(sender, messageComponent("editsUnblocked", player.getName())); + } + return null; + } +} diff --git a/src/main/java/dev/plex/listener/impl/BlockListener.java b/src/main/java/dev/plex/listener/impl/BlockListener.java new file mode 100644 index 0000000..9bec3ea --- /dev/null +++ b/src/main/java/dev/plex/listener/impl/BlockListener.java @@ -0,0 +1,40 @@ +package dev.plex.listener.impl; + +import dev.plex.listener.PlexListener; +import java.util.ArrayList; +import java.util.List; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; + +public class BlockListener extends PlexListener +{ + public List blockedPlayers = new ArrayList<>(); + + @EventHandler(priority = EventPriority.LOW) + public void onBlockPlace(BlockPlaceEvent event) + { + if (blockedPlayers.size() == 0) + { + return; + } + if (blockedPlayers.contains(event.getPlayer().getName())) + { + event.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.LOW) + public void onBlockBreak(BlockBreakEvent event) + { + if (blockedPlayers.size() == 0) + { + return; + } + if (blockedPlayers.contains(event.getPlayer().getName())) + { + event.setCancelled(true); + } + } +} diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index 9a074ef..07df062 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -85,7 +85,7 @@ adminRemoved: " - Removing from the admin list" adminSetRank: " - Setting 's rank to " # The world name teleportedToWorld: "You have been teleported to the ." -higherRankThanYou: "This player is an admin and a higher rank than you." +higherRankThanYou: "This player is an admin or a higher rank than you." playerNotAdmin: "That player is not an admin." playerIsAdmin: "That player is already an admin." rankNotFound: "The rank you entered was not found." @@ -110,6 +110,8 @@ 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!" +# 1. The command sender +# 2. The player kickedPlayer: " - Kicking " teleportedToWorldSpawn: "Teleporting to the local spawn" toggleCommandSpy: "CommandSpy has been" @@ -118,5 +120,23 @@ disabled: "disabled." adminChatFormat: '[AdminChat] ยป ' maximumPrefixLength: "The maximum length for a tag may only be ." prefixCleared: "Your prefix has been cleared." +# 1. The player name otherPrefixCleared: "You have cleared 's prefix." -prefixSetTo: "Your prefix has been set to " \ No newline at end of file +# 1. The new prefix +prefixSetTo: "Your prefix has been set to " +# 1. The amount of players whose edits are blocked +blockedEditsSize: "Blocked block modification abilities for players." +# 1. The amount of players whose edits are unblocked +unblockedEditsSize: "Unblocked all block modification abilities for players." +# 1. The command sender +# 2. The player name +blockingEditFor: " - Blocking block modification abilities for " +# 1. The command sender +# 2. The player name +unblockingEditFor: " - Unblocking block modification abilities for " +yourEditsHaveBeenBlocked: "Your block modification abilities have been blocked." +yourEditsHaveBeenUnblocked: "Your block modification abilities have been restored." +# 1. The player name +editsBlocked: "Blocked block modification abilities for " +# 1. The player name +editsUnblocked: "Unblocked block modification abilities for " \ No newline at end of file