Skip to content

Commit dcf42a4

Browse files
committed
improve illegal item handling for all modules
1 parent 9789625 commit dcf42a4

File tree

3 files changed

+72
-35
lines changed

3 files changed

+72
-35
lines changed

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/illegals/items/BannedItemNames.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public BannedItemNames() {
6161
optionalListeners.add(new Listener() {
6262
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
6363
private void onPrepareResult(PrepareResultEvent event) {
64+
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getView().getPlayer().getUniqueId())) {
65+
return;
66+
}
67+
6468
if (AnarchyExploitFixes.permissions().permissionValue(event.getView().getPlayer(), bypassPermission.node()).toBoolean()) return;
6569

6670
ItemStack resultItem = event.getResult();

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/illegals/items/IllegalItemModule.java

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ public abstract class IllegalItemModule extends AEFModule implements Listener {
5454

5555
protected final AEFPermission bypassPermission;
5656
protected final IllegalHandling handling;
57-
58-
protected final Set<Listener> optionalListeners;
5957
protected final boolean guiPluginsSupported;
6058

61-
private final Cache<Class<? extends Event>, ExpiringSet<Object>> listenerCooldowns;
62-
private final Function<Class<? extends Event>, @PolyNull ExpiringSet<Object>> createIfAbsent;
59+
protected final Set<Listener> optionalListeners;
60+
protected final Cache<Class<? extends Event>, ExpiringSet<Object>> listenerCooldowns;
61+
protected final Function<Class<? extends Event>, @PolyNull ExpiringSet<Object>> createIfAbsent;
6362

6463
public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
6564
this(configPath, false, bypassPermission, null);
@@ -68,7 +67,7 @@ public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
6867
public IllegalItemModule(String configPath, boolean defEnabled, AEFPermission bypassPermission, String comment) {
6968
super(configPath, defEnabled, comment);
7069
this.bypassPermission = bypassPermission;
71-
this.optionalListeners = new HashSet<>();
70+
this.optionalListeners = new HashSet<>(6);
7271

7372
String configuredHandling = config.getString(configPath + ".handling", IllegalHandling.PREVENT_USE_ONLY.name(),
7473
"Available options:\n" + Arrays.stream(IllegalHandling.values())
@@ -87,7 +86,7 @@ public IllegalItemModule(String configPath, boolean defEnabled, AEFPermission by
8786
Enable this if you have problems with the plugin removing items from chest guis.""");
8887
if (this.handling == IllegalHandling.STRICT) {
8988
optionalListeners.add(new Listener() {
90-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
89+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
9190
private void onInventoryOpen(InventoryOpenEvent event) {
9291
if (AnarchyExploitFixes.permissions().permissionValue(event.getPlayer(), bypassPermission.node()).toBoolean()) return;
9392
// Check if the inventory is connected to a location in the game. If it is not,
@@ -108,7 +107,7 @@ private void onInventoryOpen(InventoryOpenEvent event) {
108107
intense as the event fires in high frequencies as soon as players start using\s
109108
farms or item sorters. Recommended to leave off if not necessary.""")) {
110109
optionalListeners.add(new Listener() {
111-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
110+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
112111
private void onItemGoesThroughHopper(InventoryMoveItemEvent event) {
113112
if (legalityOf(event.getItem()) != ItemLegality.LEGAL) {
114113
event.setCancelled(true);
@@ -127,7 +126,7 @@ private void onItemGoesThroughHopper(InventoryMoveItemEvent event) {
127126
to handle items separately.""");
128127
if (checkOnChunkload) {
129128
optionalListeners.add(new Listener() {
130-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
129+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
131130
private void onChunkLoad(ChunkLoadEvent event) {
132131
if (event.isNewChunk()) return;
133132

@@ -200,8 +199,10 @@ public ItemLegality legalityOf(@Nullable Iterable<ItemStack> itemStacks) {
200199
return ItemLegality.LEGAL;
201200
}
202201

203-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
202+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
204203
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
204+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
205+
205206
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
206207
event.setCancelled(true);
207208
return;
@@ -215,8 +216,10 @@ public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
215216
}
216217
}
217218

218-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
219+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
219220
public void onBlockDispense(BlockDispenseEvent event) {
221+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
222+
220223
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getBlock().getLocation())) {
221224
event.setCancelled(true);
222225
return;
@@ -228,7 +231,7 @@ public void onBlockDispense(BlockDispenseEvent event) {
228231
}
229232
}
230233

231-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
234+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
232235
public void onPlayerArmorChange(PlayerArmorChangeEvent event) {
233236
if (handling == IllegalHandling.PREVENT_USE_ONLY) return; // Cant cancel this event
234237

@@ -238,8 +241,9 @@ public void onPlayerArmorChange(PlayerArmorChangeEvent event) {
238241
}
239242
}
240243

241-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
244+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
242245
public void onInventoryClick(InventoryClickEvent event) {
246+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
243247
if (guiPluginsSupported && event.getInventory().getLocation() == null) return;
244248

245249
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getWhoClicked().getUniqueId())) {
@@ -264,8 +268,9 @@ public void onInventoryClick(InventoryClickEvent event) {
264268
}
265269
}
266270

267-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
271+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
268272
public void onInventoryInteract(InventoryInteractEvent event) {
273+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
269274
if (guiPluginsSupported && event.getInventory().getLocation() == null) return;
270275

271276
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getWhoClicked().getUniqueId())) {
@@ -285,8 +290,10 @@ public void onInventoryInteract(InventoryInteractEvent event) {
285290
}
286291
}
287292

288-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
293+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
289294
public void onPrePlayerAttackEntity(PrePlayerAttackEntityEvent event) {
295+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
296+
290297
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
291298
event.setCancelled(true);
292299
return;
@@ -311,8 +318,10 @@ public void onPrePlayerAttackEntity(PrePlayerAttackEntityEvent event) {
311318
}
312319
}
313320

314-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
321+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
315322
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
323+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
324+
316325
if (event.getDamager().getType() == EntityType.PLAYER) {
317326
final Player player = (Player) event.getDamager();
318327

@@ -359,8 +368,10 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
359368
}
360369
}
361370

362-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
371+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
363372
public void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent event) {
373+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
374+
364375
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
365376
event.setCancelled(true);
366377
return;
@@ -378,8 +389,10 @@ public void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent event) {
378389
}
379390
}
380391

381-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
392+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
382393
public void onPlayerDropItem(PlayerDropItemEvent event) {
394+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
395+
383396
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
384397
event.setCancelled(true);
385398
return;
@@ -398,6 +411,8 @@ public void onPlayerDropItem(PlayerDropItemEvent event) {
398411

399412
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
400413
public void onPlayerInteract(PlayerInteractEvent event) {
414+
if (event.useItemInHand() == Event.Result.DENY && handling == IllegalHandling.PREVENT_USE_ONLY) return;
415+
401416
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
402417
event.setCancelled(true);
403418
return;
@@ -414,8 +429,10 @@ public void onPlayerInteract(PlayerInteractEvent event) {
414429
}
415430
}
416431

417-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
432+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
418433
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
434+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
435+
419436
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
420437
event.setCancelled(true);
421438
return;

AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/illegals/items/IllegalItemModule.java

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,11 @@ public abstract class IllegalItemModule extends AEFModule implements Listener {
5454

5555
protected final AEFPermission bypassPermission;
5656
protected final IllegalHandling handling;
57-
58-
protected final Set<Listener> optionalListeners;
5957
protected final boolean guiPluginsSupported;
6058

61-
private final Cache<Class<? extends Event>, ExpiringSet<Object>> listenerCooldowns;
62-
private final Function<Class<? extends Event>, @PolyNull ExpiringSet<Object>> createIfAbsent;
59+
protected final Set<Listener> optionalListeners;
60+
protected final Cache<Class<? extends Event>, ExpiringSet<Object>> listenerCooldowns;
61+
protected final Function<Class<? extends Event>, @PolyNull ExpiringSet<Object>> createIfAbsent;
6362

6463
public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
6564
this(configPath, false, bypassPermission, null);
@@ -68,7 +67,7 @@ public IllegalItemModule(String configPath, AEFPermission bypassPermission) {
6867
public IllegalItemModule(String configPath, boolean defEnabled, AEFPermission bypassPermission, String comment) {
6968
super(configPath, defEnabled, comment);
7069
this.bypassPermission = bypassPermission;
71-
this.optionalListeners = new HashSet<>();
70+
this.optionalListeners = new HashSet<>(6);
7271

7372
String configuredHandling = config.getString(configPath + ".handling", IllegalHandling.PREVENT_USE_ONLY.name(),
7473
"Available options:\n" + Arrays.stream(IllegalHandling.values())
@@ -87,7 +86,7 @@ public IllegalItemModule(String configPath, boolean defEnabled, AEFPermission by
8786
"Enable this if you have problems with the plugin removing items from chest guis.");
8887
if (this.handling == IllegalHandling.STRICT) {
8988
optionalListeners.add(new Listener() {
90-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
89+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
9190
private void onInventoryOpen(InventoryOpenEvent event) {
9291
if (AnarchyExploitFixes.permissions().permissionValue(event.getPlayer(), bypassPermission.node()).toBoolean()) return;
9392
// Check if the inventory is connected to a location in the game. If it is not,
@@ -108,7 +107,7 @@ private void onInventoryOpen(InventoryOpenEvent event) {
108107
"intense as the event fires in high frequencies as soon as players start using\n" +
109108
"farms or item sorters. Recommended to leave off if not necessary.")) {
110109
optionalListeners.add(new Listener() {
111-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
110+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
112111
private void onItemGoesThroughHopper(InventoryMoveItemEvent event) {
113112
if (legalityOf(event.getItem()) != ItemLegality.LEGAL) {
114113
event.setCancelled(true);
@@ -127,7 +126,7 @@ private void onItemGoesThroughHopper(InventoryMoveItemEvent event) {
127126
"to handle items separately.");
128127
if (checkOnChunkload) {
129128
optionalListeners.add(new Listener() {
130-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
129+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
131130
private void onChunkLoad(ChunkLoadEvent event) {
132131
if (event.isNewChunk()) return;
133132
Chunk chunk = event.getChunk();
@@ -199,8 +198,10 @@ public ItemLegality legalityOf(Iterable<ItemStack> itemStacks) {
199198
return ItemLegality.LEGAL;
200199
}
201200

202-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
201+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
203202
public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
203+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
204+
204205
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
205206
event.setCancelled(true);
206207
return;
@@ -214,8 +215,10 @@ public void onPlayerItemConsume(PlayerItemConsumeEvent event) {
214215
}
215216
}
216217

217-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
218+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
218219
public void onBlockDispense(BlockDispenseEvent event) {
220+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
221+
219222
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getBlock().getLocation())) {
220223
event.setCancelled(true);
221224
return;
@@ -227,7 +230,7 @@ public void onBlockDispense(BlockDispenseEvent event) {
227230
}
228231
}
229232

230-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
233+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
231234
public void onPlayerArmorChange(PlayerArmorChangeEvent event) {
232235
if (handling == IllegalHandling.PREVENT_USE_ONLY) return; // Cant cancel this event
233236

@@ -237,8 +240,10 @@ public void onPlayerArmorChange(PlayerArmorChangeEvent event) {
237240
}
238241
}
239242

240-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
243+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
241244
public void onInventoryClick(InventoryClickEvent event) {
245+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
246+
242247
if (guiPluginsSupported && event.getInventory().getLocation() == null) return;
243248

244249
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getWhoClicked().getUniqueId())) {
@@ -263,8 +268,9 @@ public void onInventoryClick(InventoryClickEvent event) {
263268
}
264269
}
265270

266-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
271+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
267272
public void onInventoryInteract(InventoryInteractEvent event) {
273+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
268274
if (guiPluginsSupported && event.getInventory().getLocation() == null) return;
269275

270276
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getWhoClicked().getUniqueId())) {
@@ -284,8 +290,10 @@ public void onInventoryInteract(InventoryInteractEvent event) {
284290
}
285291
}
286292

287-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
293+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
288294
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
295+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
296+
289297
if (event.getDamager().getType() == EntityType.PLAYER) {
290298
final Player player = (Player) event.getDamager();
291299

@@ -332,8 +340,10 @@ public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
332340
}
333341
}
334342

335-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
343+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
336344
public void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent event) {
345+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
346+
337347
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
338348
event.setCancelled(true);
339349
return;
@@ -351,8 +361,10 @@ public void onPlayerAttemptPickupItem(PlayerAttemptPickupItemEvent event) {
351361
}
352362
}
353363

354-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
364+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
355365
public void onPlayerDropItem(PlayerDropItemEvent event) {
366+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
367+
356368
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
357369
event.setCancelled(true);
358370
return;
@@ -371,6 +383,8 @@ public void onPlayerDropItem(PlayerDropItemEvent event) {
371383

372384
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
373385
public void onPlayerInteract(PlayerInteractEvent event) {
386+
if (event.useItemInHand() == Event.Result.DENY && handling == IllegalHandling.PREVENT_USE_ONLY) return;
387+
374388
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
375389
event.setCancelled(true);
376390
return;
@@ -387,8 +401,10 @@ public void onPlayerInteract(PlayerInteractEvent event) {
387401
}
388402
}
389403

390-
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
404+
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = false)
391405
public void onPlayerInteractEntity(PlayerInteractEntityEvent event) {
406+
if (event.isCancelled() && handling == IllegalHandling.PREVENT_USE_ONLY) return;
407+
392408
if (listenerCooldowns.get(event.getClass(), createIfAbsent).contains(event.getPlayer().getUniqueId())) {
393409
event.setCancelled(true);
394410
return;

0 commit comments

Comments
 (0)