Skip to content

Commit a4507d6

Browse files
committed
feat: allow to toggle hotbar items easily with a command
Signed-off-by: Cristóbal Veas <[email protected]>
1 parent a798241 commit a4507d6

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

src/main/java/team/devblook/akropolis/Permissions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public enum Permissions {
2323
// Command permissions
2424
COMMAND_AKROPOLIS_HELP("command.help"), COMMAND_AKROPOLIS_RELOAD("command.reload"),
2525
COMMAND_SCOREBOARD_TOGGLE("command.scoreboard"), COMMAND_OPEN_MENUS("command.openmenu"),
26-
COMMAND_HOLOGRAMS("command.holograms"),
26+
COMMAND_HOLOGRAMS("command.holograms"), COMMAND_HOTBAR_TOGGLE("command.hotbar"),
2727

2828
// Misc permissions
2929
COMMAND_GAMEMODE("command.gamemode"), COMMAND_GAMEMODE_OTHERS("command.gamemode.others"),

src/main/java/team/devblook/akropolis/command/commands/AkropolisCommand.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,38 @@ else if (args[0].equalsIgnoreCase("scoreboard")) {
115115
}
116116
}
117117

118+
/*
119+
* Command: hotbar Description: toggles the hotbar on/off
120+
*/
121+
else if (args[0].equalsIgnoreCase("hotbar")) {
122+
123+
if (!(sender instanceof Player player)) {
124+
Message.CONSOLE_NOT_ALLOWED.sendFrom(sender);
125+
return;
126+
}
127+
128+
if (!sender.hasPermission(Permissions.COMMAND_HOTBAR_TOGGLE.getPermission())) {
129+
Message.NO_PERMISSION.sendFrom(sender);
130+
return;
131+
}
132+
133+
if (!plugin.getModuleManager().isEnabled(ModuleType.HOTBAR_ITEMS)) {
134+
sender.sendMessage(TextUtil.parse("<red>The hotbar module is not enabled in the configuration."));
135+
return;
136+
}
137+
138+
HotbarManager hotbarManager = ((HotbarManager) plugin.getModuleManager()
139+
.getModule(ModuleType.HOTBAR_ITEMS));
140+
141+
if (hotbarManager.hasHotbar(player.getUniqueId())) {
142+
hotbarManager.removeItemsFromPlayer(player);
143+
Message.HOTBAR_DISABLE.sendFrom(player);
144+
} else {
145+
hotbarManager.giveItemsToPlayer(player);
146+
Message.HOTBAR_ENABLE.sendFrom(player);
147+
}
148+
}
149+
118150
/*
119151
* Command: info Description: displays useful information about the
120152
* configuration

src/main/java/team/devblook/akropolis/config/Message.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public enum Message {
5454

5555
SCOREBOARD_ENABLE("SCOREBOARD.ENABLE"), SCOREBOARD_DISABLE("SCOREBOARD.DISABLE"),
5656

57+
HOTBAR_ENABLE("HOTBAR.ENABLE"), HOTBAR_DISABLE("HOTBAR.DISABLE"),
58+
5759
DOUBLE_JUMP_COOLDOWN("DOUBLE_JUMP.COOLDOWN_ACTIVE"),
5860

5961
EVENT_ITEM_DROP("WORLD_EVENT_MODIFICATIONS.ITEM_DROP"), EVENT_ITEM_PICKUP("WORLD_EVENT_MODIFICATIONS.ITEM_PICKUP"),

src/main/java/team/devblook/akropolis/module/modules/hotbar/HotbarManager.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.bukkit.Bukkit;
2323
import org.bukkit.configuration.ConfigurationSection;
2424
import org.bukkit.configuration.file.FileConfiguration;
25+
import org.bukkit.entity.Player;
2526
import org.bukkit.inventory.ItemStack;
2627
import team.devblook.akropolis.AkropolisPlugin;
2728
import team.devblook.akropolis.config.ConfigType;
@@ -31,11 +32,11 @@
3132
import team.devblook.akropolis.module.modules.hotbar.items.PlayerHider;
3233
import team.devblook.akropolis.util.ItemStackBuilder;
3334

34-
import java.util.ArrayList;
35-
import java.util.List;
35+
import java.util.*;
3636

3737
public class HotbarManager extends Module {
3838
private List<HotbarItem> hotbarItems;
39+
private Set<UUID> players;
3940

4041
public HotbarManager(AkropolisPlugin plugin) {
4142
super(plugin, ModuleType.HOTBAR_ITEMS);
@@ -44,6 +45,8 @@ public HotbarManager(AkropolisPlugin plugin) {
4445
@Override
4546
public void onEnable() {
4647
hotbarItems = new ArrayList<>();
48+
players = new HashSet<>();
49+
4750
FileConfiguration config = getConfig(ConfigType.SETTINGS);
4851
ConfigurationSection customItemsSections = config.getConfigurationSection("custom_join_items");
4952

@@ -115,13 +118,29 @@ public void registerHotbarItem(HotbarItem hotbarItem) {
115118
}
116119

117120
private void giveItems() {
118-
Bukkit.getOnlinePlayers().stream().filter(player -> !inDisabledWorld(player.getLocation()))
119-
.forEach(player -> hotbarItems.forEach(hotbarItem -> hotbarItem.giveItem(player)));
121+
Bukkit.getOnlinePlayers().forEach(this::giveItemsToPlayer);
120122
}
121123

122124
private void removeItems() {
123-
Bukkit.getOnlinePlayers().stream().filter(player -> !inDisabledWorld(player.getLocation()))
124-
.forEach(player -> hotbarItems.forEach(hotbarItem -> hotbarItem.removeItem(player)));
125+
Bukkit.getOnlinePlayers().forEach(this::removeItemsFromPlayer);
126+
}
127+
128+
public void giveItemsToPlayer(Player player) {
129+
if (inDisabledWorld(player.getLocation())) return;
130+
131+
hotbarItems.forEach(hotbarItem -> hotbarItem.giveItem(player));
132+
players.add(player.getUniqueId());
133+
}
134+
135+
public void removeItemsFromPlayer(Player player) {
136+
if (inDisabledWorld(player.getLocation())) return;
137+
138+
hotbarItems.forEach(hotbarItem -> hotbarItem.removeItem(player));
139+
players.remove(player.getUniqueId());
140+
}
141+
142+
public boolean hasHotbar(UUID playerUuid) {
143+
return players.contains(playerUuid);
125144
}
126145

127146
public List<HotbarItem> getHotbarItems() {

src/main/resources/messages.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ Messages:
100100
ENABLE: "<prefix> <gray>You have <yellow>enabled <gray>the scoreboard."
101101
DISABLE: "<prefix> <gray>You have <yellow>disabled <gray>the scoreboard."
102102

103+
HOTBAR:
104+
ENABLE: "<prefix> <gray>You have <yellow>enabled <gray>the hotbar items."
105+
DISABLE: "<prefix> <gray>You have <yellow>disabled <gray>the hotbar items."
106+
103107
DOUBLE_JUMP:
104108
COOLDOWN_ACTIVE: "<prefix> <red>You must wait <yellow><time>s <red>to double jump again!"
105109

0 commit comments

Comments
 (0)