Skip to content

Commit fa6102b

Browse files
committed
🚧 Add cooldown for click, 350ms by default
1 parent 352c501 commit fa6102b

5 files changed

Lines changed: 27 additions & 109 deletions

File tree

changelog.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
# Unreleased
3535

3636
- Added replacement of , by . for placeholders requirements. If your placeholder returns a number with a comma instead of a period, the plugin can handle that.
37+
- Added base64 MaterialLoader. Allows to load ItemStack with all the data it can contain.
38+
- Added cooldown on button click. The default cooldown will be 350ms.
3739
- Fix Folia with VersionChecker [#35](https://github.com/Maxlego08/zMenu/issues/35)
3840
- Fix error with mini message format in 1.16
39-
- Added base64 MaterialLoader. Allows to load itemstacks with all the data it can contain.
40-
- Change `/zm save <item name> <base64/yml>`, saves an element in YML or base64 format. The base64 format will save the itemStack with all its data.
4141
- Fix JUMP Button
42+
- Change `/zm save <item name> <base64/yml>`, saves an element in YML or base64 format. The base64 format will save the itemStack with all its data.
4243
- Change `page` from JUMP Button to `toPage`
4344

4445
# 1.0.2.3

src/fr/maxlego08/menu/inventory/VInventory.java

Lines changed: 1 addition & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -43,88 +43,39 @@ public int getId() {
4343
return id;
4444
}
4545

46-
/**
47-
* Inventory Id
48-
*
49-
* @param id
50-
* @return
51-
*/
5246
public VInventory setId(int id) {
5347
this.id = id;
5448
return this;
5549
}
5650

57-
/**
58-
* Allows you to create the spigot inventory object
59-
*
60-
* @param name
61-
*/
6251
protected void createInventory(String name) {
6352
createInventory(name, 54);
6453
}
6554

66-
/**
67-
* Allows you to create the spigot inventory object
68-
*
69-
* @param name - Inventory name
70-
* @param size - Inventory Size
71-
*/
7255
protected void createInventory(String name, int size) {
7356
this.guiName = name;
7457
this.inventory = Bukkit.createInventory(this, size, name);
7558
}
7659

77-
/**
78-
* Allows you to create the spigot inventory object
79-
*
80-
* @param name - Inventory name
81-
* @param size - Inventory Size
82-
*/
8360
protected void createMetaInventory(String name, int size) {
8461
this.guiName = name;
8562
this.inventory = Meta.meta.createInventory(name, size, this);
8663
}
8764

88-
/**
89-
* Create default inventory with default size and name
90-
*/
9165
private void createDefaultInventory() {
9266
if (this.inventory == null) {
9367
this.inventory = Bukkit.createInventory(this, 54, "§cDefault Inventory");
9468
}
9569
}
9670

97-
/**
98-
* Adding an item to the inventory
99-
*
100-
* @param slot - Inventory slot
101-
* @param material - ItemStack material
102-
* @param name - ItemStack name
103-
* @return ItemButton
104-
*/
10571
public ItemButton addItem(int slot, Material material, String name) {
10672
return addItem(slot, new ItemBuilder(material, name).build());
10773
}
10874

109-
/**
110-
* Adding an item to the inventory
111-
*
112-
* @param slot - Inventory slot
113-
* @param item - ItemBuild
114-
* @return ItemButton
115-
*/
11675
public ItemButton addItem(int slot, ItemBuilder item) {
11776
return addItem(slot, item.build());
11877
}
11978

120-
/**
121-
* Adding an itemStack to the inventory
122-
* Creates the default inventory if it does not exist
123-
*
124-
* @param slot - Inventory slot
125-
* @param itemStack - ItemStack
126-
* @return ItemButton
127-
*/
12879
public ItemButton addItem(int slot, ItemStack itemStack) {
12980

13081
createDefaultInventory();
@@ -145,85 +96,42 @@ public ItemButton addItem(int slot, ItemStack itemStack) {
14596
return button;
14697
}
14798

148-
/**
149-
* Allows you to remove an item from the list of items
150-
*
151-
* @param slot
152-
*/
15399
public void removeItem(int slot) {
154100
this.items.remove(slot);
155101
}
156102

157-
/**
158-
* Allows you to delete all items
159-
*/
160103
public void clearItem() {
161104
this.items.clear();
162105
}
163106

164-
/**
165-
* Allows you to retrieve all items
166-
*
167-
* @return
168-
*/
169107
public Map<Integer, ItemButton> getItems() {
170108
return items;
171109
}
172110

173-
/**
174-
* If the click in the inventory is disabled (which is the default)
175-
* then it will return true
176-
*
177-
* @return vrai ou faux
178-
*/
179111
public boolean isDisableClick() {
180112
return disableClick;
181113
}
182114

183-
/**
184-
* Change the ability to click in the inventory
185-
*
186-
* @param disableClick
187-
*/
188115
public void setDisableClick(boolean disableClick) {
189116
this.disableClick = disableClick;
190117
}
191118

192-
/**
193-
* Allows to recover the player
194-
*
195-
* @return player
196-
*/
197119
public Player getPlayer() {
198120
return player;
199121
}
200122

201-
/**
202-
* Allows you to retrieve the page
203-
*
204-
* @return the page
205-
*/
206123
public int getPage() {
207124
return page;
208125
}
209126

210-
/**
211-
* @return the args
212-
*/
213127
public Object[] getArgs() {
214128
return args;
215129
}
216130

217-
/**
218-
* @return the inventory
219-
*/
220131
public Inventory getSpigotInventory() {
221132
return inventory;
222133
}
223134

224-
/**
225-
* @return the guiName
226-
*/
227135
public String getGuiName() {
228136
return guiName;
229137
}
@@ -245,19 +153,10 @@ protected void onPreClose(InventoryCloseEvent event, MenuPlugin plugin, Player p
245153
this.onClose(event, plugin, player);
246154
}
247155

248-
/**
249-
* @param event
250-
* @param plugin
251-
* @param player
252-
*/
156+
253157
protected void onClose(InventoryCloseEvent event, MenuPlugin plugin, Player player) {
254158
}
255159

256-
/**
257-
* @param event
258-
* @param plugin
259-
* @param player
260-
*/
261160
protected void onDrag(InventoryDragEvent event, MenuPlugin plugin, Player player) {
262161
}
263162

src/fr/maxlego08/menu/inventory/VInventoryManager.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import fr.maxlego08.menu.exceptions.InventoryAlreadyExistException;
55
import fr.maxlego08.menu.exceptions.InventoryOpenException;
66
import fr.maxlego08.menu.listener.ListenerAdapter;
7+
import fr.maxlego08.menu.save.Config;
78
import fr.maxlego08.menu.zcore.enums.EnumInventory;
89
import fr.maxlego08.menu.zcore.enums.Message;
910
import fr.maxlego08.menu.zcore.utils.inventory.InventoryResult;
@@ -15,30 +16,31 @@
1516
import org.bukkit.event.inventory.InventoryDragEvent;
1617
import org.bukkit.event.inventory.InventoryType;
1718
import org.bukkit.event.player.PlayerJoinEvent;
19+
import org.bukkit.event.player.PlayerQuitEvent;
1820
import org.bukkit.inventory.Inventory;
1921
import org.bukkit.inventory.InventoryHolder;
2022

2123
import java.util.HashMap;
2224
import java.util.Map;
2325
import java.util.Optional;
26+
import java.util.UUID;
2427
import java.util.function.Predicate;
2528

2629
public class VInventoryManager extends ListenerAdapter {
2730

2831
private final Map<Integer, VInventory> inventories = new HashMap<>();
2932
private final MenuPlugin plugin;
33+
private Map<UUID, Long> cooldownClick = new HashMap<>();
34+
3035

31-
/**
32-
* @param plugin
33-
*/
3436
public VInventoryManager(MenuPlugin plugin) {
3537
super();
3638
this.plugin = plugin;
3739
}
3840

3941
/**
4042
* Allows you to record an inventory If the inventory ID already exists then
41-
* an exception will be throw
43+
* an exception will be thrown
4244
*
4345
* @param enumInventory
4446
* @param inventory
@@ -130,6 +132,14 @@ protected void onInventoryClick(InventoryClickEvent event, Player player) {
130132
return;
131133
}
132134

135+
if (Config.enableCooldownClick && this.cooldownClick.getOrDefault(player.getUniqueId(), 0L) > System.currentTimeMillis()) {
136+
message(player, Message.CLICK_COOLDOWN);
137+
return;
138+
}
139+
140+
this.cooldownClick.put(player.getUniqueId(),
141+
System.currentTimeMillis() + Config.cooldownClickMilliseconds);
142+
133143
ItemButton button = inventory.getItems().getOrDefault(event.getSlot(), null);
134144
if (button != null) {
135145
button.onClick(event);
@@ -181,7 +191,12 @@ public void close(Predicate<VInventory> predicate) {
181191
protected void onConnect(PlayerJoinEvent event, Player player) {
182192
// Send information to me, because I like to know
183193
if (player.getName().equals("Maxlego08")) {
184-
message(player, "§aLe serveur utilise §2zMenu v" + this.plugin.getDescription().getVersion());
194+
plugin.getScheduler().runTaskLater(player.getLocation(), 20, () -> message(player, "§aLe serveur utilise §2zMenu v" + this.plugin.getDescription().getVersion()));
185195
}
186196
}
197+
198+
@Override
199+
protected void onQuit(PlayerQuitEvent event, Player player) {
200+
this.cooldownClick.remove(player.getUniqueId());
201+
}
187202
}

src/fr/maxlego08/menu/save/Config.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ public class Config implements Savable {
7171
public static List<ClickType> allClicksType = Arrays.asList(ClickType.MIDDLE, ClickType.RIGHT, ClickType.LEFT, ClickType.SHIFT_RIGHT, ClickType.SHIFT_LEFT);
7272
public static boolean enableCacheItemStack = true;
7373

74+
public static boolean enableCooldownClick = true;
75+
public static long cooldownClickMilliseconds = 350;
7476

7577
/**
7678
* static Singleton instance.

src/fr/maxlego08/menu/zcore/enums/Message.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public enum Message {
127127
SAVE_ERROR_NAME("§cThe name already exists for this item, please select another one."),
128128
SAVE_ERROR_TYPE("§cCannot find save type."),
129129
SAVE_SUCCESS("§aYou just saved the item §f%name%§a."),
130+
CLICK_COOLDOWN(MessageType.ACTION, "§cPlease wait a little between two clicks."),
130131

131132
;
132133

0 commit comments

Comments
 (0)