Skip to content

Commit ea0e3d6

Browse files
committed
Prevent ClickInventory.Drag and cancel event if slot is not present
1 parent f7a428a commit ea0e3d6

File tree

2 files changed

+47
-40
lines changed

2 files changed

+47
-40
lines changed

build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
allprojects {
22
group = 'com.focamacho'
3-
version = '1.0.3'
3+
version = '1.0.4'
44
}
55

66
subprojects {

sealmenus-sponge/src/main/java/com/focamacho/sealmenus/sponge/ChestMenu.java

+46-39
Original file line numberDiff line numberDiff line change
@@ -198,45 +198,52 @@ public void update() {
198198
// Prevent inventory double clicks from stealing items from the menu
199199
if(ce instanceof ClickInventoryEvent.Double) ce.setCancelled(true);
200200

201-
if(ce.getSlot().isPresent()) {
202-
Integer slot = ce.getSlot().get().getInventoryProperty(SlotIndex.class).get().getValue();
203-
if(slot == null) slot = -1;
204-
if(slot < 9 * this.rows) {
205-
ce.setCancelled(true);
206-
207-
this.getOnClick().accept(ce);
208-
209-
MenuItem item = getItem(slot);
210-
if(item == null) item = dummyItem;
211-
212-
if(ce instanceof ClickInventoryEvent.Double) {
213-
this.getOnDouble().accept((ClickInventoryEvent.Double) ce);
214-
item.getOnDouble().accept((ClickInventoryEvent.Double) ce);
215-
} else if(ce instanceof ClickInventoryEvent.Shift.Primary) {
216-
this.getOnShiftPrimary().accept((ClickInventoryEvent.Shift.Primary) ce);
217-
item.getOnShiftPrimary().accept((ClickInventoryEvent.Shift.Primary) ce);
218-
} else if(ce instanceof ClickInventoryEvent.Shift.Secondary) {
219-
this.getOnShiftSecondary().accept((ClickInventoryEvent.Shift.Secondary) ce);
220-
item.getOnShiftSecondary().accept((ClickInventoryEvent.Shift.Secondary) ce);
221-
} else if(ce instanceof ClickInventoryEvent.Primary) {
222-
this.getOnPrimary().accept((ClickInventoryEvent.Primary) ce);
223-
item.getOnPrimary().accept((ClickInventoryEvent.Primary) ce);
224-
} else if(ce instanceof ClickInventoryEvent.Middle) {
225-
this.getOnMiddle().accept((ClickInventoryEvent.Middle) ce);
226-
item.getOnMiddle().accept((ClickInventoryEvent.Middle) ce);
227-
} else if(ce instanceof ClickInventoryEvent.Secondary) {
228-
this.getOnSecondary().accept((ClickInventoryEvent.Secondary) ce);
229-
item.getOnSecondary().accept((ClickInventoryEvent.Secondary) ce);
230-
} else if(ce instanceof ClickInventoryEvent.Drop.Full) {
231-
this.getOnDropAll().accept((ClickInventoryEvent.Drop.Full) ce);
232-
item.getOnDropAll().accept((ClickInventoryEvent.Drop.Full) ce);
233-
} else if(ce instanceof ClickInventoryEvent.Drop) {
234-
this.getOnDrop().accept((ClickInventoryEvent.Drop.Single) ce);
235-
item.getOnDrop().accept((ClickInventoryEvent.Drop.Single) ce);
236-
} else if(ce instanceof ClickInventoryEvent.NumberPress) {
237-
this.getOnNumber().accept((ClickInventoryEvent.NumberPress) ce);
238-
item.getOnNumber().accept((ClickInventoryEvent.NumberPress) ce);
239-
}
201+
// Prevent dragging items from placing items inside the menu
202+
if(ce instanceof ClickInventoryEvent.Drag) ce.setCancelled(true);
203+
204+
// Prevent weird things from happening when sponge do not send the slot in the event
205+
if(!ce.getSlot().isPresent()) {
206+
ce.setCancelled(true);
207+
return;
208+
}
209+
210+
Integer slot = ce.getSlot().get().getInventoryProperty(SlotIndex.class).get().getValue();
211+
if(slot == null) slot = -1;
212+
if(slot < 9 * this.rows) {
213+
ce.setCancelled(true);
214+
215+
this.getOnClick().accept(ce);
216+
217+
MenuItem item = getItem(slot);
218+
if(item == null) item = dummyItem;
219+
220+
if(ce instanceof ClickInventoryEvent.Double) {
221+
this.getOnDouble().accept((ClickInventoryEvent.Double) ce);
222+
item.getOnDouble().accept((ClickInventoryEvent.Double) ce);
223+
} else if(ce instanceof ClickInventoryEvent.Shift.Primary) {
224+
this.getOnShiftPrimary().accept((ClickInventoryEvent.Shift.Primary) ce);
225+
item.getOnShiftPrimary().accept((ClickInventoryEvent.Shift.Primary) ce);
226+
} else if(ce instanceof ClickInventoryEvent.Shift.Secondary) {
227+
this.getOnShiftSecondary().accept((ClickInventoryEvent.Shift.Secondary) ce);
228+
item.getOnShiftSecondary().accept((ClickInventoryEvent.Shift.Secondary) ce);
229+
} else if(ce instanceof ClickInventoryEvent.Primary) {
230+
this.getOnPrimary().accept((ClickInventoryEvent.Primary) ce);
231+
item.getOnPrimary().accept((ClickInventoryEvent.Primary) ce);
232+
} else if(ce instanceof ClickInventoryEvent.Middle) {
233+
this.getOnMiddle().accept((ClickInventoryEvent.Middle) ce);
234+
item.getOnMiddle().accept((ClickInventoryEvent.Middle) ce);
235+
} else if(ce instanceof ClickInventoryEvent.Secondary) {
236+
this.getOnSecondary().accept((ClickInventoryEvent.Secondary) ce);
237+
item.getOnSecondary().accept((ClickInventoryEvent.Secondary) ce);
238+
} else if(ce instanceof ClickInventoryEvent.Drop.Full) {
239+
this.getOnDropAll().accept((ClickInventoryEvent.Drop.Full) ce);
240+
item.getOnDropAll().accept((ClickInventoryEvent.Drop.Full) ce);
241+
} else if(ce instanceof ClickInventoryEvent.Drop) {
242+
this.getOnDrop().accept((ClickInventoryEvent.Drop.Single) ce);
243+
item.getOnDrop().accept((ClickInventoryEvent.Drop.Single) ce);
244+
} else if(ce instanceof ClickInventoryEvent.NumberPress) {
245+
this.getOnNumber().accept((ClickInventoryEvent.NumberPress) ce);
246+
item.getOnNumber().accept((ClickInventoryEvent.NumberPress) ce);
240247
}
241248
}
242249
})

0 commit comments

Comments
 (0)