-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMenu.java
More file actions
155 lines (134 loc) · 4.87 KB
/
Menu.java
File metadata and controls
155 lines (134 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package me.marvin.menu;
import com.google.common.base.Preconditions;
import me.marvin.menu.button.Button;
import me.marvin.menu.button.ButtonEvent;
import me.marvin.menu.listener.ButtonInteractionData;
import me.marvin.menu.listener.ButtonInteractionListener;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.InventoryView;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
/**
* An abstract menu class which holds an inventory created using the
* {@link Bukkit#createInventory(InventoryHolder, int)} method.
* Using this method we can check if the clicked inventory's holder
* is a {@link Menu}.
*
* @author marvin
*
* @see ButtonInteractionListener
* @see InventoryHolder
* @see Button
*
* @since 1.0
*/
public abstract class Menu implements InventoryHolder {
protected static final char ALTERNATE_COLOR_CHAR = '&';
/**
* Returns the menu what the player is currently interacting with.
* @param player the player
* @param <M> the type of the menu
* @return the menu, or null
*
* @since 1.2
*/
@Nullable
@SuppressWarnings("unchecked")
public static <M extends Menu> M getCurrent(Player player) {
InventoryView view = player.getOpenInventory();
if (!(view.getTopInventory().getHolder() instanceof Menu)) {
return null;
}
return (M) view.getTopInventory();
}
/**
* The inventory held by this object.
*/
protected Inventory inventory;
/**
* Returns the buttons used by this {@linkplain Menu}.
*
* @param player the player
* @return the buttons used by this menu
*/
@NotNull public abstract Map<Integer, Button> getButtons(@NotNull Player player);
/**
* Returns the title of this {@linkplain Menu}.
*
* @param player the player
* @return the title of this menu
*/
@NotNull public abstract String getMenuTitle(@NotNull Player player);
/**
* Returns the desired inventory size.
*
* @param player the player
* @return the desired inventory size
* @throws IllegalArgumentException if the size is not a multiple of 9
*/
public abstract int getSize(@NotNull Player player);
/**
* This method opens this {@linkplain Menu} for the given player.
*
* @param player the player
* @throws IllegalArgumentException if the menu's size is not acceptable
* @throws IndexOutOfBoundsException if one of the button's position is out of range (should be between 0 and 53)
*/
public void openMenu(@NotNull Player player) {
String title = getMenuTitle(player);
title = title.substring(0, Math.min(title.length(), 32));
title = ChatColor.translateAlternateColorCodes(ALTERNATE_COLOR_CHAR, title);
int size = getSize(player);
Preconditions.checkArgument(size % 9 == 0, "menu size must be a multiple of 9");
Preconditions.checkArgument(size <= 54, "menu size must be smaller than 54");
Preconditions.checkArgument(size >= 9, "menu size must be larger than 9");
inventory = Bukkit.createInventory(this, size, title);
refreshMenu(player);
player.openInventory(inventory);
}
/**
* This method (re)initiates the the buttons of this {@linkplain Menu}.
*
* @param player the player
* @throws NullPointerException if the inventory was not opened before
*/
public void refreshMenu(@NotNull Player player) {
Preconditions.checkNotNull(inventory, "refresh");
inventory.clear();
for (Map.Entry<Integer, Button> entry : getButtons(player).entrySet()) {
Preconditions.checkElementIndex(entry.getKey(), inventory.getSize(), "button position");
inventory.setItem(entry.getKey(), entry.getValue().getItem());
}
}
/**
* Handles the interaction made with this menu.
*
* @param event the event
* @since 2.0
*/
public void handleInteraction(@NotNull InventoryClickEvent event) {
Player player = (Player) event.getWhoClicked();
Button button = getButtons(player).get(event.getSlot());
if (button == null) return;
ButtonEvent buttonEvent = Preconditions.checkNotNull(button.getEvent(), "event was null");
if (!button.isStrict() || button.getItem().isSimilar(inventory.getItem(event.getSlot()))) {
buttonEvent.accept(new ButtonInteractionData(event, button, this));
}
}
/**
* Returns the inventory held by this {@linkplain Menu}.
*
* @return the inventory held by this menu
*/
@Override
@NotNull
public Inventory getInventory() {
return inventory;
}
}