Skip to content

Commit

Permalink
feat: allow to toggle hotbar items easily with a command
Browse files Browse the repository at this point in the history
Signed-off-by: Cristóbal Veas <[email protected]>
  • Loading branch information
zetastormy committed Dec 21, 2024
1 parent a798241 commit a4507d6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/main/java/team/devblook/akropolis/Permissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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("<red>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
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/team/devblook/akropolis/config/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HotbarItem> hotbarItems;
private Set<UUID> players;

public HotbarManager(AkropolisPlugin plugin) {
super(plugin, ModuleType.HOTBAR_ITEMS);
Expand All @@ -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");

Expand Down Expand Up @@ -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<HotbarItem> getHotbarItems() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ Messages:
ENABLE: "<prefix> <gray>You have <yellow>enabled <gray>the scoreboard."
DISABLE: "<prefix> <gray>You have <yellow>disabled <gray>the scoreboard."

HOTBAR:
ENABLE: "<prefix> <gray>You have <yellow>enabled <gray>the hotbar items."
DISABLE: "<prefix> <gray>You have <yellow>disabled <gray>the hotbar items."

DOUBLE_JUMP:
COOLDOWN_ACTIVE: "<prefix> <red>You must wait <yellow><time>s <red>to double jump again!"

Expand Down

0 comments on commit a4507d6

Please sign in to comment.