From a4507d6a3b3e0552bed958ecc3b65ae2693ad076 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crist=C3=B3bal=20Veas?= Date: Fri, 20 Dec 2024 23:49:57 -0300 Subject: [PATCH] feat: allow to toggle hotbar items easily with a command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Cristóbal Veas --- .../team/devblook/akropolis/Permissions.java | 2 +- .../command/commands/AkropolisCommand.java | 32 +++++++++++++++++++ .../devblook/akropolis/config/Message.java | 2 ++ .../module/modules/hotbar/HotbarManager.java | 31 ++++++++++++++---- src/main/resources/messages.yml | 4 +++ 5 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/main/java/team/devblook/akropolis/Permissions.java b/src/main/java/team/devblook/akropolis/Permissions.java index 63df56b..b71851a 100644 --- a/src/main/java/team/devblook/akropolis/Permissions.java +++ b/src/main/java/team/devblook/akropolis/Permissions.java @@ -23,7 +23,7 @@ public enum Permissions { // Command permissions COMMAND_AKROPOLIS_HELP("command.help"), COMMAND_AKROPOLIS_RELOAD("command.reload"), COMMAND_SCOREBOARD_TOGGLE("command.scoreboard"), COMMAND_OPEN_MENUS("command.openmenu"), - COMMAND_HOLOGRAMS("command.holograms"), + COMMAND_HOLOGRAMS("command.holograms"), COMMAND_HOTBAR_TOGGLE("command.hotbar"), // Misc permissions COMMAND_GAMEMODE("command.gamemode"), COMMAND_GAMEMODE_OTHERS("command.gamemode.others"), diff --git a/src/main/java/team/devblook/akropolis/command/commands/AkropolisCommand.java b/src/main/java/team/devblook/akropolis/command/commands/AkropolisCommand.java index b132f80..ca57f45 100644 --- a/src/main/java/team/devblook/akropolis/command/commands/AkropolisCommand.java +++ b/src/main/java/team/devblook/akropolis/command/commands/AkropolisCommand.java @@ -115,6 +115,38 @@ else if (args[0].equalsIgnoreCase("scoreboard")) { } } + /* + * Command: hotbar Description: toggles the hotbar on/off + */ + else if (args[0].equalsIgnoreCase("hotbar")) { + + if (!(sender instanceof Player player)) { + Message.CONSOLE_NOT_ALLOWED.sendFrom(sender); + return; + } + + if (!sender.hasPermission(Permissions.COMMAND_HOTBAR_TOGGLE.getPermission())) { + Message.NO_PERMISSION.sendFrom(sender); + return; + } + + if (!plugin.getModuleManager().isEnabled(ModuleType.HOTBAR_ITEMS)) { + sender.sendMessage(TextUtil.parse("The hotbar module is not enabled in the configuration.")); + return; + } + + HotbarManager hotbarManager = ((HotbarManager) plugin.getModuleManager() + .getModule(ModuleType.HOTBAR_ITEMS)); + + if (hotbarManager.hasHotbar(player.getUniqueId())) { + hotbarManager.removeItemsFromPlayer(player); + Message.HOTBAR_DISABLE.sendFrom(player); + } else { + hotbarManager.giveItemsToPlayer(player); + Message.HOTBAR_ENABLE.sendFrom(player); + } + } + /* * Command: info Description: displays useful information about the * configuration diff --git a/src/main/java/team/devblook/akropolis/config/Message.java b/src/main/java/team/devblook/akropolis/config/Message.java index fc9157d..744a19e 100644 --- a/src/main/java/team/devblook/akropolis/config/Message.java +++ b/src/main/java/team/devblook/akropolis/config/Message.java @@ -54,6 +54,8 @@ public enum Message { SCOREBOARD_ENABLE("SCOREBOARD.ENABLE"), SCOREBOARD_DISABLE("SCOREBOARD.DISABLE"), + HOTBAR_ENABLE("HOTBAR.ENABLE"), HOTBAR_DISABLE("HOTBAR.DISABLE"), + DOUBLE_JUMP_COOLDOWN("DOUBLE_JUMP.COOLDOWN_ACTIVE"), EVENT_ITEM_DROP("WORLD_EVENT_MODIFICATIONS.ITEM_DROP"), EVENT_ITEM_PICKUP("WORLD_EVENT_MODIFICATIONS.ITEM_PICKUP"), diff --git a/src/main/java/team/devblook/akropolis/module/modules/hotbar/HotbarManager.java b/src/main/java/team/devblook/akropolis/module/modules/hotbar/HotbarManager.java index 7a991bb..a062a36 100644 --- a/src/main/java/team/devblook/akropolis/module/modules/hotbar/HotbarManager.java +++ b/src/main/java/team/devblook/akropolis/module/modules/hotbar/HotbarManager.java @@ -22,6 +22,7 @@ import org.bukkit.Bukkit; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import team.devblook.akropolis.AkropolisPlugin; import team.devblook.akropolis.config.ConfigType; @@ -31,11 +32,11 @@ import team.devblook.akropolis.module.modules.hotbar.items.PlayerHider; import team.devblook.akropolis.util.ItemStackBuilder; -import java.util.ArrayList; -import java.util.List; +import java.util.*; public class HotbarManager extends Module { private List hotbarItems; + private Set players; public HotbarManager(AkropolisPlugin plugin) { super(plugin, ModuleType.HOTBAR_ITEMS); @@ -44,6 +45,8 @@ public HotbarManager(AkropolisPlugin plugin) { @Override public void onEnable() { hotbarItems = new ArrayList<>(); + players = new HashSet<>(); + FileConfiguration config = getConfig(ConfigType.SETTINGS); ConfigurationSection customItemsSections = config.getConfigurationSection("custom_join_items"); @@ -115,13 +118,29 @@ public void registerHotbarItem(HotbarItem hotbarItem) { } private void giveItems() { - Bukkit.getOnlinePlayers().stream().filter(player -> !inDisabledWorld(player.getLocation())) - .forEach(player -> hotbarItems.forEach(hotbarItem -> hotbarItem.giveItem(player))); + Bukkit.getOnlinePlayers().forEach(this::giveItemsToPlayer); } private void removeItems() { - Bukkit.getOnlinePlayers().stream().filter(player -> !inDisabledWorld(player.getLocation())) - .forEach(player -> hotbarItems.forEach(hotbarItem -> hotbarItem.removeItem(player))); + Bukkit.getOnlinePlayers().forEach(this::removeItemsFromPlayer); + } + + public void giveItemsToPlayer(Player player) { + if (inDisabledWorld(player.getLocation())) return; + + hotbarItems.forEach(hotbarItem -> hotbarItem.giveItem(player)); + players.add(player.getUniqueId()); + } + + public void removeItemsFromPlayer(Player player) { + if (inDisabledWorld(player.getLocation())) return; + + hotbarItems.forEach(hotbarItem -> hotbarItem.removeItem(player)); + players.remove(player.getUniqueId()); + } + + public boolean hasHotbar(UUID playerUuid) { + return players.contains(playerUuid); } public List getHotbarItems() { diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index d56cf13..5b88cd4 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -100,6 +100,10 @@ Messages: ENABLE: " You have enabled the scoreboard." DISABLE: " You have disabled the scoreboard." + HOTBAR: + ENABLE: " You have enabled the hotbar items." + DISABLE: " You have disabled the hotbar items." + DOUBLE_JUMP: COOLDOWN_ACTIVE: " You must wait