Merge branch 'master' into event-api

This commit is contained in:
TheMode 2021-06-10 14:42:07 +02:00
commit 78cc392007
3 changed files with 104 additions and 48 deletions

View File

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

View File

@ -233,7 +233,7 @@ public class InventoryClickProcessor {
@NotNull ItemStack clicked, @NotNull ItemStack cursor, @NotNull ItemStack clicked, @NotNull ItemStack cursor,
@NotNull Int2ObjectFunction<ItemStack> itemGetter, @NotNull Int2ObjectFunction<ItemStack> itemGetter,
@NotNull BiConsumer<Integer, ItemStack> itemSetter) { @NotNull BiConsumer<Integer, ItemStack> itemSetter) {
InventoryClickResult clickResult = startCondition(inventory, player, slot, ClickType.START_DRAGGING, clicked, cursor); InventoryClickResult clickResult = null;
final StackingRule stackingRule = cursor.getStackingRule(); final StackingRule stackingRule = cursor.getStackingRule();
@ -241,9 +241,15 @@ public class InventoryClickProcessor {
// Start or end left/right drag // Start or end left/right drag
if (button == 0) { if (button == 0) {
// Start left // Start left
clickResult = startCondition(inventory, player, slot, ClickType.START_LEFT_DRAGGING, clicked, cursor);
if (!clickResult.isCancel())
this.leftDraggingMap.put(player, new IntOpenHashSet()); this.leftDraggingMap.put(player, new IntOpenHashSet());
} else if (button == 4) { } else if (button == 4) {
// Start right // Start right
clickResult = startCondition(inventory, player, slot, ClickType.START_RIGHT_DRAGGING, clicked, cursor);
if (!clickResult.isCancel())
this.rightDraggingMap.put(player, new IntOpenHashSet()); this.rightDraggingMap.put(player, new IntOpenHashSet());
} else if (button == 2) { } else if (button == 2) {
// End left // End left
@ -254,16 +260,30 @@ public class InventoryClickProcessor {
final int cursorAmount = stackingRule.getAmount(cursor); final int cursorAmount = stackingRule.getAmount(cursor);
if (slotCount > cursorAmount) if (slotCount > cursorAmount)
return null; return null;
// Should be size of each defined slot (if not full)
final int slotSize = (int) ((float) cursorAmount / (float) slotCount); boolean cancel = false;
int finalCursorAmount = cursorAmount;
for (int s : slots) { for (int s : slots) {
ItemStack slotItem = itemGetter.apply(s); ItemStack slotItem = itemGetter.apply(s);
clickResult = startCondition(inventory, player, s, ClickType.DRAGGING, slotItem, cursor); clickResult = startCondition(inventory, player, s, ClickType.LEFT_DRAGGING, slotItem, cursor);
if (clickResult.isCancel())
if (clickResult.isCancel()) {
cancel = true;
break; 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(); StackingRule slotItemRule = slotItem.getStackingRule();
final int maxSize = stackingRule.getMaxSize(cursor); final int maxSize = stackingRule.getMaxSize(cursor);
@ -283,10 +303,15 @@ public class InventoryClickProcessor {
} }
itemSetter.accept(s, slotItem); 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); cursor = stackingRule.apply(cursor, finalCursorAmount);
clickResult.setCursor(cursor); clickResult.setCursor(cursor);
}
}
leftDraggingMap.remove(player); leftDraggingMap.remove(player);
} else if (button == 6) { } else if (button == 6) {
@ -298,14 +323,28 @@ public class InventoryClickProcessor {
int cursorAmount = stackingRule.getAmount(cursor); int cursorAmount = stackingRule.getAmount(cursor);
if (size > cursorAmount) if (size > cursorAmount)
return null; 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) { for (int s : slots) {
ItemStack draggedItem = cursor; ItemStack draggedItem = cursor;
ItemStack slotItem = itemGetter.apply(s); ItemStack slotItem = itemGetter.apply(s);
clickResult = startCondition(inventory, player, s, ClickType.DRAGGING, slotItem, cursor);
if (clickResult.isCancel())
break;
StackingRule slotItemRule = slotItem.getStackingRule(); StackingRule slotItemRule = slotItem.getStackingRule();
if (stackingRule.canBeStacked(draggedItem, slotItem)) { if (stackingRule.canBeStacked(draggedItem, slotItem)) {
final int amount = slotItemRule.getAmount(slotItem) + 1; final int amount = slotItemRule.getAmount(slotItem) + 1;
@ -320,10 +359,15 @@ public class InventoryClickProcessor {
cursorAmount -= 1; 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); cursor = stackingRule.apply(cursor, cursorAmount);
clickResult.setCursor(cursor); clickResult.setCursor(cursor);
}
}
rightDraggingMap.remove(player); rightDraggingMap.remove(player);

View File

@ -21,6 +21,7 @@ import net.minestom.server.network.PacketProcessor;
import net.minestom.server.network.netty.channel.ClientChannel; import net.minestom.server.network.netty.channel.ClientChannel;
import net.minestom.server.network.netty.codec.*; import net.minestom.server.network.netty.codec.*;
import net.minestom.server.utils.validate.Check; import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -28,6 +29,7 @@ import org.slf4j.LoggerFactory;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
@ApiStatus.Internal
public final class NettyServer { public final class NettyServer {
public static final Logger LOGGER = LoggerFactory.getLogger(NettyServer.class); public static final Logger LOGGER = LoggerFactory.getLogger(NettyServer.class);
@ -172,6 +174,10 @@ public final class NettyServer {
} }
} }
public ServerSocketChannel getServerChannel() {
return serverChannel;
}
/** /**
* Gets the address of the server. * Gets the address of the server.
* *