From e54f88fa5e23766d96b07ca46823a69c0fa0b5c6 Mon Sep 17 00:00:00 2001 From: Shiewk Date: Tue, 18 Jun 2024 18:01:13 +0200 Subject: [PATCH] Add Invsee command and permissions --- .../de/shiewk/smoderation/SModeration.java | 8 +++ .../smoderation/command/InvseeCommand.java | 64 +++++++++++++++++++ .../smoderation/event/InvSeeEvents.java | 29 +++++++++ .../shiewk/smoderation/util/PlayerUtil.java | 10 +++ src/main/resources/plugin.yml | 21 +++++- 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/shiewk/smoderation/command/InvseeCommand.java create mode 100644 src/main/java/de/shiewk/smoderation/event/InvSeeEvents.java diff --git a/src/main/java/de/shiewk/smoderation/SModeration.java b/src/main/java/de/shiewk/smoderation/SModeration.java index d8075cb..bb386ae 100644 --- a/src/main/java/de/shiewk/smoderation/SModeration.java +++ b/src/main/java/de/shiewk/smoderation/SModeration.java @@ -2,6 +2,7 @@ package de.shiewk.smoderation; import de.shiewk.smoderation.command.*; import de.shiewk.smoderation.event.CustomInventoryEvents; +import de.shiewk.smoderation.event.InvSeeEvents; import de.shiewk.smoderation.listener.PunishmentListener; import de.shiewk.smoderation.storage.PunishmentContainer; import net.kyori.adventure.text.Component; @@ -26,6 +27,7 @@ public final class SModeration extends JavaPlugin { public static final TextColor PRIMARY_COLOR = TextColor.color(212, 0, 255); public static final TextColor SECONDARY_COLOR = TextColor.color(52, 143, 255); public static final TextColor INACTIVE_COLOR = NamedTextColor.GRAY; + public static final TextColor FAIL_COLOR = NamedTextColor.RED; public static final TextComponent CHAT_PREFIX = Component.text("SM \u00BB ").color(PRIMARY_COLOR); @Override @@ -39,6 +41,7 @@ public final class SModeration extends JavaPlugin { public void onEnable() { getPluginManager().registerEvents(new PunishmentListener(), this); getPluginManager().registerEvents(new CustomInventoryEvents(), this); + getPluginManager().registerEvents(new InvSeeEvents(), this); final PluginCommand mute = getCommand("mute"); assert mute != null; @@ -75,6 +78,11 @@ public final class SModeration extends JavaPlugin { unban.setExecutor(new UnbanCommand()); unban.setTabCompleter(new UnbanCommand()); + final PluginCommand invsee = getCommand("invsee"); + assert invsee != null; + invsee.setExecutor(new InvseeCommand()); + invsee.setTabCompleter(new InvseeCommand()); + container.load(SAVE_FILE); } diff --git a/src/main/java/de/shiewk/smoderation/command/InvseeCommand.java b/src/main/java/de/shiewk/smoderation/command/InvseeCommand.java new file mode 100644 index 0000000..97250c1 --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/command/InvseeCommand.java @@ -0,0 +1,64 @@ +package de.shiewk.smoderation.command; + +import de.shiewk.smoderation.util.PlayerUtil; +import net.kyori.adventure.text.Component; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; +import org.bukkit.entity.HumanEntity; +import org.bukkit.entity.Player; +import org.bukkit.util.StringUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; + +import static de.shiewk.smoderation.SModeration.*; + +public class InvseeCommand implements CommandExecutor, TabCompleter { + + + @Override + public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (args.length < 1) { + return false; + } + if (sender instanceof HumanEntity human){ + final Player player = PlayerUtil.findOnlinePlayer(args[0]); + if (player != null) { + if (human.getUniqueId().equals(player.getUniqueId())){ + human.sendMessage(Component.text("You can't open your own inventory.").color(FAIL_COLOR)); + } else { + human.sendMessage(CHAT_PREFIX.append( + Component.text("Opening inventory of ").color(PRIMARY_COLOR) + .append(Component.text(player.getName()).color(SECONDARY_COLOR)) + .append(Component.text(".")) + )); + human.openInventory(player.getInventory()); + } + } else { + human.sendMessage(Component.text("This player is not online.").color(FAIL_COLOR)); + } + } else { + sender.sendMessage(Component.text("Only an entity that can open inventories can execute this command!").color(FAIL_COLOR)); + } + return true; + } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { + if (args.length > 1){ + return List.of(); + } + List available = new ArrayList<>(); + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { + available.add(onlinePlayer.getName()); + } + List completions = new ArrayList<>(); + StringUtil.copyPartialMatches(args.length > 0 ? args[0] : "", available, completions); + return completions; + } +} diff --git a/src/main/java/de/shiewk/smoderation/event/InvSeeEvents.java b/src/main/java/de/shiewk/smoderation/event/InvSeeEvents.java new file mode 100644 index 0000000..0c96c82 --- /dev/null +++ b/src/main/java/de/shiewk/smoderation/event/InvSeeEvents.java @@ -0,0 +1,29 @@ +package de.shiewk.smoderation.event; + +import org.bukkit.entity.HumanEntity; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.PlayerInventory; + +public class InvSeeEvents implements Listener { + + @EventHandler + public void onInventoryClick(InventoryClickEvent event){ + final Inventory clicked = event.getView().getTopInventory(); + if (clicked instanceof PlayerInventory inventory){ + final HumanEntity holder = inventory.getHolder(); + if (!event.getWhoClicked().hasPermission("smod.invsee.modify")){ + event.setCancelled(true); + return; + } + if (holder != null) { + if (holder.hasPermission("smod.invsee.preventmodify")){ + event.setCancelled(true); + } + } + } + } + +} diff --git a/src/main/java/de/shiewk/smoderation/util/PlayerUtil.java b/src/main/java/de/shiewk/smoderation/util/PlayerUtil.java index 590f489..8fdf034 100644 --- a/src/main/java/de/shiewk/smoderation/util/PlayerUtil.java +++ b/src/main/java/de/shiewk/smoderation/util/PlayerUtil.java @@ -5,6 +5,7 @@ import de.shiewk.smoderation.punishments.Punishment; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -45,4 +46,13 @@ public abstract class PlayerUtil { } } + public static @Nullable Player findOnlinePlayer(String name){ + for (Player onlinePlayer : Bukkit.getOnlinePlayers()) { + if (onlinePlayer.getName().equalsIgnoreCase(name)){ + return onlinePlayer; + } + } + return null; + } + } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 20e0b93..4c85e98 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -54,6 +54,14 @@ commands: - spardon permission: smod.unban description: Unbans a banned player. + invsee: + usage: "§cUsage: /invsee " + aliases: + - sinvsee + - smodinvsee + - invs + permission: smod.invsee + description: Views the inventory of another player. permissions: smod.mute: default: op @@ -80,4 +88,15 @@ permissions: description: Allows the player to unban other players. smod.logs: default: op - description: Allows the player to view mod logs. \ No newline at end of file + description: Allows the player to view mod logs. + smod.invsee: + default: op + description: Allows the player to view other players inventories. + smod.invsee.modify: + default: op + description: Allows the player to view and modify other players inventories. + children: + - smod.invsee + smod.invsee.preventmodify: + default: op + description: When giving this permission to a player, prevents their inventory from being modified. \ No newline at end of file