Merge pull request #305 from stefvanschie/master

Changing item drag events
This commit is contained in:
TheMode 2021-06-09 18:12:08 +02:00 committed by GitHub
commit 82a351492b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 98 additions and 48 deletions

View File

@ -9,8 +9,14 @@ public enum ClickType {
START_SHIFT_CLICK,
SHIFT_CLICK,
START_DRAGGING,
DRAGGING,
START_LEFT_DRAGGING,
START_RIGHT_DRAGGING,
LEFT_DRAGGING,
RIGHT_DRAGGING,
END_LEFT_DRAGGING,
END_RIGHT_DRAGGING,
START_DOUBLE_CLICK,
DOUBLE_CLICK,

View File

@ -232,7 +232,7 @@ public class InventoryClickProcessor {
@NotNull ItemStack clicked, @NotNull ItemStack cursor,
@NotNull Int2ObjectFunction<ItemStack> itemGetter,
@NotNull BiConsumer<Integer, ItemStack> itemSetter) {
InventoryClickResult clickResult = startCondition(inventory, player, slot, ClickType.START_DRAGGING, clicked, cursor);
InventoryClickResult clickResult = null;
final StackingRule stackingRule = cursor.getStackingRule();
@ -240,9 +240,15 @@ public class InventoryClickProcessor {
// Start or end left/right drag
if (button == 0) {
// Start left
clickResult = startCondition(inventory, player, slot, ClickType.START_LEFT_DRAGGING, clicked, cursor);
if (!clickResult.isCancel())
this.leftDraggingMap.put(player, new IntOpenHashSet());
} else if (button == 4) {
// Start right
clickResult = startCondition(inventory, player, slot, ClickType.START_RIGHT_DRAGGING, clicked, cursor);
if (!clickResult.isCancel())
this.rightDraggingMap.put(player, new IntOpenHashSet());
} else if (button == 2) {
// End left
@ -253,16 +259,30 @@ public class InventoryClickProcessor {
final int cursorAmount = stackingRule.getAmount(cursor);
if (slotCount > cursorAmount)
return null;
// Should be size of each defined slot (if not full)
final int slotSize = (int) ((float) cursorAmount / (float) slotCount);
int finalCursorAmount = cursorAmount;
boolean cancel = false;
for (int s : slots) {
ItemStack slotItem = itemGetter.apply(s);
clickResult = startCondition(inventory, player, s, ClickType.DRAGGING, slotItem, cursor);
if (clickResult.isCancel())
clickResult = startCondition(inventory, player, s, ClickType.LEFT_DRAGGING, slotItem, cursor);
if (clickResult.isCancel()) {
cancel = true;
break;
}
}
cancel |= startCondition(inventory, player, slot, ClickType.END_LEFT_DRAGGING, clicked, cursor).isCancel();
// Should be size of each defined slot (if not full)
final int slotSize = (int) ((float) cursorAmount / (float) slotCount);
int finalCursorAmount = cursorAmount;
if (!cancel) {
for (int s : slots) {
ItemStack slotItem = itemGetter.apply(s);
StackingRule slotItemRule = slotItem.getStackingRule();
final int maxSize = stackingRule.getMaxSize(cursor);
@ -282,10 +302,15 @@ public class InventoryClickProcessor {
}
itemSetter.accept(s, slotItem);
callClickEvent(player, inventory, s, ClickType.DRAGGING, slotItem, cursor);
callClickEvent(player, inventory, s, ClickType.LEFT_DRAGGING, slotItem, cursor);
}
// If no slots were dragged over, no need to apply any kind of stacking rules
if (clickResult != null) {
cursor = stackingRule.apply(cursor, finalCursorAmount);
clickResult.setCursor(cursor);
}
}
leftDraggingMap.remove(player);
} else if (button == 6) {
@ -297,14 +322,28 @@ public class InventoryClickProcessor {
int cursorAmount = stackingRule.getAmount(cursor);
if (size > cursorAmount)
return null;
boolean cancel = false;
for (int s : slots) {
ItemStack slotItem = itemGetter.apply(s);
clickResult = startCondition(inventory, player, s, ClickType.RIGHT_DRAGGING, slotItem, cursor);
if (clickResult.isCancel()) {
cancel = true;
break;
}
}
cancel |= startCondition(inventory, player, slot, ClickType.END_RIGHT_DRAGGING, clicked, cursor).isCancel();
if (!cancel) {
for (int s : slots) {
ItemStack draggedItem = cursor;
ItemStack slotItem = itemGetter.apply(s);
clickResult = startCondition(inventory, player, s, ClickType.DRAGGING, slotItem, cursor);
if (clickResult.isCancel())
break;
StackingRule slotItemRule = slotItem.getStackingRule();
if (stackingRule.canBeStacked(draggedItem, slotItem)) {
final int amount = slotItemRule.getAmount(slotItem) + 1;
@ -319,10 +358,15 @@ public class InventoryClickProcessor {
cursorAmount -= 1;
}
callClickEvent(player, inventory, s, ClickType.DRAGGING, draggedItem, cursor);
callClickEvent(player, inventory, s, ClickType.RIGHT_DRAGGING, draggedItem, cursor);
}
// If no slots were dragged over, no need to apply any kind of stacking rules
if (clickResult != null) {
cursor = stackingRule.apply(cursor, cursorAmount);
clickResult.setCursor(cursor);
}
}
rightDraggingMap.remove(player);