BundleMouseActions .java

net.minecraft.client.gui.BundleMouseActions (class, 75 lines): members: unselectedBundleItem, scrollWheelHandler, onStopHovering, onSlotClicked, matches.

net.minecraft.client.guiclass75 linesclient2.7 KB

Implements

API Surface (5)

boolean matches(final Slot slot)
boolean onMouseScrolled(final double scrollX, final double scrollY, final int slotIndex, final ItemStack itemStack)
void onStopHovering(final Slot hoveredSlot)
void onSlotClicked(final Slot slot, final ContainerInput containerInput)
void unselectedBundleItem(final ItemStack bundleItem, final int slotIndex)

Fields & Constants (2)

minecraft
scrollWheelHandler

Full Source

BundleMouseActions.java75 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.gui;
2
3
import net.fabricmc.api.EnvType;
4
import net.fabricmc.api.Environment;
5
import net.minecraft.client.Minecraft;
6
import net.minecraft.client.ScrollWheelHandler;
7
import net.minecraft.client.multiplayer.ClientPacketListener;
8
import net.minecraft.network.protocol.game.ServerboundSelectBundleItemPacket;
9
import net.minecraft.tags.ItemTags;
10
import net.minecraft.world.inventory.ContainerInput;
11
import net.minecraft.world.inventory.Slot;
12
import net.minecraft.world.item.BundleItem;
13
import net.minecraft.world.item.ItemStack;
14
import org.joml.Vector2i;
15
16
@Environment(EnvType.CLIENT)
17
public class BundleMouseActions implements ItemSlotMouseAction {
18
	private final Minecraft minecraft;
19
	private final ScrollWheelHandler scrollWheelHandler;
20
21
	public BundleMouseActions(final Minecraft minecraft) {
22
		this.minecraft = minecraft;
23
		this.scrollWheelHandler = new ScrollWheelHandler();
24
	}
25
26
	@Override
27
	public boolean matches(final Slot slot) {
28
		return slot.getItem().is(ItemTags.BUNDLES);
29
	}
30
31
	@Override
32
	public boolean onMouseScrolled(final double scrollX, final double scrollY, final int slotIndex, final ItemStack itemStack) {
33
		int amountOfShownItems = BundleItem.getNumberOfItemsToShow(itemStack);
34
		if (amountOfShownItems == 0) {
35
			return false;
36
		}
37
38
		Vector2i wheelXY = this.scrollWheelHandler.onMouseScroll(scrollX, scrollY);
39
		int wheel = wheelXY.y == 0 ? -wheelXY.x : wheelXY.y;
40
		if (wheel != 0) {
41
			int selectedItem = BundleItem.getSelectedItemIndex(itemStack);
42
			int updatedSelectedItem = ScrollWheelHandler.getNextScrollWheelSelection(wheel, selectedItem, amountOfShownItems);
43
			if (selectedItem != updatedSelectedItem) {
44
				this.toggleSelectedBundleItem(itemStack, slotIndex, updatedSelectedItem);
45
			}
46
		}
47
48
		return true;
49
	}
50
51
	@Override
52
	public void onStopHovering(final Slot hoveredSlot) {
53
		this.unselectedBundleItem(hoveredSlot.getItem(), hoveredSlot.index);
54
	}
55
56
	@Override
57
	public void onSlotClicked(final Slot slot, final ContainerInput containerInput) {
58
		if (containerInput == ContainerInput.QUICK_MOVE || containerInput == ContainerInput.SWAP) {
59
			this.unselectedBundleItem(slot.getItem(), slot.index);
60
		}
61
	}
62
63
	private void toggleSelectedBundleItem(final ItemStack bundleItem, final int slotIndex, final int selectedItem) {
64
		if (this.minecraft.getConnection() != null && selectedItem < BundleItem.getNumberOfItemsToShow(bundleItem)) {
65
			ClientPacketListener connection = this.minecraft.getConnection();
66
			BundleItem.toggleSelectedItem(bundleItem, selectedItem);
67
			connection.send(new ServerboundSelectBundleItemPacket(slotIndex, selectedItem));
68
		}
69
	}
70
71
	public void unselectedBundleItem(final ItemStack bundleItem, final int slotIndex) {
72
		this.toggleSelectedBundleItem(bundleItem, slotIndex, -1);
73
	}
74
}
75

Decompiled Minecraft 26.3 source of BundleMouseActions (net/minecraft/client/gui/BundleMouseActions.java) with Mojang official mappings, stored verbatim on the site.