Skip to content

Commit ca4b818

Browse files
committed
feat: custom menu items now support permissions
- Add permissions support to custom menu items - If the player tries to use the item without the permission they will receive the no permission message and the item's actions won't run
1 parent 988b003 commit ca4b818

File tree

3 files changed

+27
-7
lines changed

3 files changed

+27
-7
lines changed

src/main/java/me/zetastormy/akropolis/inventory/InventoryItem.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
package me.zetastormy.akropolis.inventory;
2121

2222
import org.bukkit.inventory.ItemStack;
23+
import org.jetbrains.annotations.NotNull;
24+
import org.jetbrains.annotations.Nullable;
2325

2426
import java.util.ArrayList;
2527
import java.util.List;
2628

2729
public class InventoryItem {
2830
public final ItemStack itemStack;
2931
public final List<ClickAction> clickActions;
32+
public final @Nullable String permission;
3033

31-
public InventoryItem(ItemStack itemStack) {
34+
public InventoryItem(@NotNull ItemStack itemStack, @Nullable String permission) {
3235
this.clickActions = new ArrayList<>();
3336
this.itemStack = itemStack;
37+
this.permission = permission;
3438
}
3539

3640
public InventoryItem addClickAction(ClickAction clickAction) {
@@ -45,4 +49,8 @@ public List<ClickAction> getClickActions() {
4549
public ItemStack getItemStack() {
4650
return this.itemStack;
4751
}
52+
53+
public @Nullable String getPermission() {
54+
return this.permission;
55+
}
4856
}

src/main/java/me/zetastormy/akropolis/inventory/InventoryListener.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package me.zetastormy.akropolis.inventory;
2121

22+
import me.zetastormy.akropolis.config.Message;
2223
import org.bukkit.Material;
2324
import org.bukkit.entity.Player;
2425
import org.bukkit.event.EventHandler;
@@ -46,6 +47,11 @@ public void onInventoryClick(InventoryClickEvent event) {
4647
if (item == null)
4748
return;
4849

50+
if (item.getPermission() != null && !player.hasPermission(item.getPermission())) {
51+
Message.NO_PERMISSION.send(player);
52+
return;
53+
}
54+
4955
for (final ClickAction clickAction : item.getClickActions()) {
5056
clickAction.execute(player);
5157
}

src/main/java/me/zetastormy/akropolis/inventory/inventories/CustomGUI.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
import org.bukkit.configuration.ConfigurationSection;
2828
import org.bukkit.configuration.file.FileConfiguration;
2929
import org.bukkit.inventory.Inventory;
30+
import org.jetbrains.annotations.NotNull;
31+
32+
import java.util.Objects;
3033

3134
public class CustomGUI extends AbstractInventory {
3235
private final FileConfiguration config;
@@ -66,16 +69,19 @@ public void onEnable() {
6669
inventory = inventoryBuilder;
6770
}
6871

69-
private InventoryItem build(String item) {
72+
private InventoryItem build(final @NotNull String itemKey) {
73+
// if the key exists then the section should exist too
74+
final ConfigurationSection itemConfig = Objects.requireNonNull(itemsSection.getConfigurationSection(itemKey));
7075
ItemStackBuilder itemStackBuilder = ItemStackBuilder
71-
.getItemStack(itemsSection.getConfigurationSection(item));
76+
.getItemStack(itemConfig);
7277
InventoryItem inventoryItem;
7378

74-
if (!itemsSection.contains(item + ".actions")) {
75-
inventoryItem = new InventoryItem(itemStackBuilder.build());
79+
if (!itemsSection.contains(itemKey + ".actions")) {
80+
inventoryItem = new InventoryItem(itemStackBuilder.build(), itemConfig.getString("permission"));
7681
} else {
77-
inventoryItem = new InventoryItem(itemStackBuilder.build()).addClickAction(p -> getPlugin()
78-
.getActionManager().executeActions(p, itemsSection.getStringList(item + ".actions")));
82+
inventoryItem = new InventoryItem(itemStackBuilder.build(), itemConfig.getString("permission"))
83+
.addClickAction(p -> getPlugin().getActionManager()
84+
.executeActions(p, itemsSection.getStringList(itemKey + ".actions")));
7985
}
8086

8187
return inventoryItem;

0 commit comments

Comments
 (0)