Skip to content

Commit

Permalink
Version 2.1.12
Browse files Browse the repository at this point in the history
+ Added control-click to move all items of a kind
+ Added control-shift-click to move all items from an inventory (Fixed #25)
* Improved and cleaned some code up
  • Loading branch information
Siphalor committed Aug 10, 2019
1 parent 1f7f63b commit cc58317
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 76 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ dependencies {
include "net.fabricmc.fabric-api:fabric-keybindings-v0:+"
modImplementation "net.fabricmc.fabric-api:fabric-resource-loader-v0:+"
include "net.fabricmc.fabric-api:fabric-resource-loader-v0:+"
modImplementation "net.fabricmc.fabric-api:fabric-events-interaction-v0:0.1.0+ea100f6142"
include "net.fabricmc.fabric-api:fabric-events-interaction-v0:0.1.0+ea100f6142"
modImplementation "net.fabricmc.fabric-api:fabric-events-interaction-v0:+"
include "net.fabricmc.fabric-api:fabric-events-interaction-v0:+"
modImplementation "net.fabricmc.fabric-api:fabric-tag-extensions-v0:+"
include "net.fabricmc.fabric-api:fabric-tag-extensions-v0:+"
modImplementation "net.fabricmc.fabric-api:fabric-mining-levels-v0:+"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ org.gradle.jvmargs=-Xmx1G

# Mod Properties
mod_id = mousewheelie
mod_version = 1.2.11
mod_version = 1.2.12
maven_group = de.siphalor
archives_base_name = mousewheelie

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static void triggerSend() {
sending = false;
}

public static void stopSending() {
public static void clear() {
sending = false;
interactionEventQueue.clear();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import de.siphalor.mousewheelie.client.InteractionManager;
import de.siphalor.mousewheelie.client.util.IContainerScreen;
import de.siphalor.mousewheelie.client.util.ISlot;
import de.siphalor.mousewheelie.client.util.inventory.ContainerScreenHelper;
import de.siphalor.mousewheelie.client.util.inventory.InventorySorter;
import de.siphalor.mousewheelie.client.util.inventory.SortMode;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.container.Container;
import net.minecraft.container.Slot;
import net.minecraft.container.SlotActionType;
Expand All @@ -27,8 +27,7 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.function.BiFunction;

@SuppressWarnings("WeakerAccess")
@Mixin(AbstractContainerScreen.class)
public abstract class MixinAbstractContainerScreen extends Screen implements IContainerScreen {
protected MixinAbstractContainerScreen(Text textComponent_1) {
Expand Down Expand Up @@ -93,6 +92,8 @@ public void onMouseDragged(double x2, double y2, int button, double x1, double y
onMouseClick(hoveredSlot, hoveredSlot.id, 1, SlotActionType.THROW);
} else if(hasShiftDown()) {
onMouseClick(hoveredSlot, hoveredSlot.id, 1, SlotActionType.QUICK_MOVE);
} else if(hasControlDown()) {
new ContainerScreenHelper((AbstractContainerScreen)(Object) this, (slot, data, slotActionType) -> onMouseClick(slot, -1, data, slotActionType)).sendAllOfAKind(hoveredSlot);
}
}
}
Expand All @@ -103,10 +104,25 @@ public void onMouseClick(double x, double y, int button, CallbackInfoReturnable<
if(button == 2) {
if(mouseWheelie_triggerSort())
callbackInfoReturnable.setReturnValue(true);
} else if(button == 0 && hasAltDown()) {
Slot hoveredSlot = getSlotAt(x, y);
if(hoveredSlot != null)
onMouseClick(hoveredSlot, hoveredSlot.id, 1, SlotActionType.THROW);
} else if(button == 0) {
if(hasAltDown()) {
Slot hoveredSlot = getSlotAt(x, y);
if(hoveredSlot != null) {
onMouseClick(hoveredSlot, hoveredSlot.id, 1, SlotActionType.THROW);
callbackInfoReturnable.setReturnValue(true);
}
} else if(hasControlDown()) {
Slot hoveredSlot = getSlotAt(x, y);
if(hoveredSlot != null) {
if (hasShiftDown()) {
new ContainerScreenHelper((AbstractContainerScreen) (Object) this, (slot, data, slotActionType) -> onMouseClick(slot, -1, data, slotActionType)).sendAllFrom(hoveredSlot);
callbackInfoReturnable.setReturnValue(true);
} else {
new ContainerScreenHelper((AbstractContainerScreen) (Object) this, (slot, data, slotActionType) -> onMouseClick(slot, -1, data, slotActionType)).sendAllOfAKind(hoveredSlot);
callbackInfoReturnable.setReturnValue(true);
}
}
}
}
}

Expand All @@ -124,71 +140,11 @@ public boolean mouseWheelie_onMouseScroll(double mouseX, double mouseY, double s
return false;
if(hoveredSlot.getStack().isEmpty())
return false;
ItemStack hoveredStack = hoveredSlot.getStack();
boolean changeInventory;
boolean moveUp = scrollAmount * Config.scrollFactor.value < 0;
BiFunction<Slot, Slot, Boolean> slotsInSameScope;
//noinspection ConstantConditions
if((Screen) this instanceof InventoryScreen) {
changeInventory = ((ISlot) hoveredSlot).mouseWheelie_getInvSlot() < 9 == moveUp;
slotsInSameScope = (slot, slot2) -> (((ISlot) slot).mouseWheelie_getInvSlot() < 9) == (((ISlot) slot2).mouseWheelie_getInvSlot() < 9);
} else {
boolean isPlayerSlot = hoveredSlot.inventory instanceof PlayerInventory;
changeInventory = isPlayerSlot == moveUp;
slotsInSameScope = (slot, slot2) -> slot.inventory == slot2.inventory;
}
if(changeInventory) {
if(!hoveredSlot.canInsert(ItemStack.EMPTY)) {
onMouseClick(hoveredSlot, hoveredSlot.id, 0, SlotActionType.QUICK_MOVE);
}
if(hasControlDown()) {
ItemStack referenceStack = hoveredStack.copy();
for(Slot slot : container.slotList) {
if(slotsInSameScope.apply(slot, hoveredSlot)) {
if(slot.getStack().isItemEqualIgnoreDamage(referenceStack))
onMouseClick(slot, slot.id, 0, SlotActionType.QUICK_MOVE);
}
}
} else if(hasShiftDown()) {
onMouseClick(hoveredSlot, hoveredSlot.id, 0, SlotActionType.QUICK_MOVE);
} else {
mouseWheelie_sendSingleItem(hoveredSlot);
}
} else {
if(hasShiftDown() || hasControlDown()) {
for(Slot slot : container.slotList) {
if(slotsInSameScope.apply(slot, hoveredSlot)) continue;
if(slot.getStack().isItemEqualIgnoreDamage(hoveredStack)) {
onMouseClick(slot, slot.id, 0, SlotActionType.QUICK_MOVE);
if(!hasControlDown())
break;
}
}
} else {
Slot moveSlot = null;
int stackSize = Integer.MAX_VALUE;
for(Slot slot : container.slotList) {
if(slotsInSameScope.apply(slot, hoveredSlot)) continue;
if(slot.getStack().isItemEqualIgnoreDamage(hoveredStack)) {
if(slot.getStack().getCount() < stackSize) {
stackSize = slot.getStack().getCount();
moveSlot = slot;
if(stackSize == 1) break;
}
}
}
if(moveSlot != null)
mouseWheelie_sendSingleItem(moveSlot);
}
}
return true;
}

private void mouseWheelie_sendSingleItem(Slot slot) {
onMouseClick(slot, slot.id, 0, SlotActionType.PICKUP);
onMouseClick(slot, slot.id, 1, SlotActionType.PICKUP);
onMouseClick(slot, slot.id, 0, SlotActionType.QUICK_MOVE);
onMouseClick(slot, slot.id, 0, SlotActionType.PICKUP);
ContainerScreenHelper containerScreenHelper = new ContainerScreenHelper((AbstractContainerScreen)(Object) this, (slot, data, slotActionType) -> onMouseClick(slot, -1, data, slotActionType));
containerScreenHelper.scroll(hoveredSlot, scrollAmount * Config.scrollFactor.value < 0);

return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public MixinClientPlayerEntity(ClientWorld clientWorld_1, GameProfile gameProfil

@Inject(method = "closeScreen", at = @At("HEAD"))
public void onScreenClosed(CallbackInfo callbackInfo) {
InteractionManager.stopSending();
InteractionManager.clear();
}

@Inject(method = "dropSelectedItem", at = @At("HEAD"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package de.siphalor.mousewheelie.client.util.inventory;

import de.siphalor.mousewheelie.client.util.ISlot;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.AbstractContainerScreen;
import net.minecraft.client.gui.screen.ingame.InventoryScreen;
import net.minecraft.container.Slot;
import net.minecraft.container.SlotActionType;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;

@SuppressWarnings("WeakerAccess")
public class ContainerScreenHelper {
private AbstractContainerScreen screen;
private ClickHandler clickHandler;

public ContainerScreenHelper(AbstractContainerScreen screen, ClickHandler clickHandler) {
this.screen = screen;
this.clickHandler = clickHandler;
}

public void scroll(Slot referenceSlot, boolean scrollUp) {
boolean changeInventory;
if(screen instanceof InventoryScreen) {
changeInventory = ((ISlot) referenceSlot).mouseWheelie_getInvSlot() < 9 == scrollUp;
} else {
changeInventory = (referenceSlot.inventory instanceof PlayerInventory) == scrollUp;
}
if(changeInventory) {
if(!referenceSlot.canInsert(ItemStack.EMPTY)) {
clickHandler.handleClick(referenceSlot, 0, SlotActionType.QUICK_MOVE);
}
if(Screen.hasControlDown()) {
sendAllOfAKind(referenceSlot);
} else if(Screen.hasShiftDown()) {
clickHandler.handleClick(referenceSlot, 0, SlotActionType.QUICK_MOVE);
} else {
sendSingleItem(referenceSlot);
}
} else {
ItemStack referenceStack = referenceSlot.getStack().copy();
if(Screen.hasShiftDown() || Screen.hasControlDown()) {
for(Slot slot : screen.getContainer().slotList) {
if(slotsInSameScope(slot, referenceSlot)) continue;
if(slot.getStack().isItemEqualIgnoreDamage(referenceStack)) {
clickHandler.handleClick(slot, 0, SlotActionType.QUICK_MOVE);
if(!Screen.hasControlDown())
break;
}
}
} else {
Slot moveSlot = null;
int stackSize = Integer.MAX_VALUE;
for(Slot slot : screen.getContainer().slotList) {
if(slotsInSameScope(slot, referenceSlot)) continue;
if(slot.getStack().isItemEqualIgnoreDamage(referenceStack)) {
if(slot.getStack().getCount() < stackSize) {
stackSize = slot.getStack().getCount();
moveSlot = slot;
if(stackSize == 1) break;
}
}
}
if(moveSlot != null)
sendSingleItem(moveSlot);
}
}
}

public void sendSingleItem(Slot slot) {
clickHandler.handleClick(slot, 0, SlotActionType.PICKUP);
clickHandler.handleClick(slot, 1, SlotActionType.PICKUP);
clickHandler.handleClick(slot, 0, SlotActionType.QUICK_MOVE);
clickHandler.handleClick(slot, 0, SlotActionType.PICKUP);
}

public void sendAllOfAKind(Slot referenceSlot) {
ItemStack referenceStack = referenceSlot.getStack().copy();
for(Slot slot : screen.getContainer().slotList) {
if(slotsInSameScope(slot, referenceSlot)) {
if(slot.getStack().isItemEqualIgnoreDamage(referenceStack))
clickHandler.handleClick(slot, 0, SlotActionType.QUICK_MOVE);
}
}
}

public void sendAllFrom(Slot referenceSlot) {
for(Slot slot : screen.getContainer().slotList) {
if(slotsInSameScope(slot, referenceSlot)) {
clickHandler.handleClick(slot, 0, SlotActionType.QUICK_MOVE);
}
}
}

public boolean slotsInSameScope(Slot slot1, Slot slot2) {
if(screen instanceof InventoryScreen) {
return (((ISlot) slot1).mouseWheelie_getInvSlot() < 9) == (((ISlot) slot2).mouseWheelie_getInvSlot() < 9);
} else {
return slot1.inventory == slot2.inventory;
}
}

public interface ClickHandler {
void handleClick(Slot slot, int data, SlotActionType slotActionType);
}
}

0 comments on commit cc58317

Please sign in to comment.