Skip to content

Commit

Permalink
Merge branch '1.19.4' into unstable
Browse files Browse the repository at this point in the history
  • Loading branch information
Siphalor committed Aug 5, 2023
2 parents abf86f7 + 6553e3b commit ee41df0
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 96 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ yarn_build=10:v2
loader_version=0.14.21
# Mod Properties
mod_id=mousewheelie
mod_version=1.12.0
mod_version=1.12.1
mod_release=release
mod_mc_version_specifier=1.20.1+
mod_mc_versions=1.20.1
Expand All @@ -18,7 +18,7 @@ archives_base_name=mousewheelie
# Dependencies
lombok_version=1.18.28
# Mod Dependencies
amecs_version=1.5.0+mc1.20-pre1
amecs_version=1.5.1+mc1.20-pre1
coat_version=1.0.0-beta.20+mc1.20-pre1
fabric_api_version=0.86.0+1.20.1
tweed_version=1.3.0+mc1.20-pre1
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import de.siphalor.mousewheelie.client.util.ItemStackUtils;
import de.siphalor.mousewheelie.client.util.ReverseIterator;
import de.siphalor.mousewheelie.client.util.accessors.ISlot;
import it.unimi.dsi.fastutil.ints.IntRBTreeSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.gui.screen.ingame.AbstractInventoryScreen;
Expand All @@ -51,14 +51,15 @@
public class ContainerScreenHelper<T extends HandledScreen<?>> {
protected final T screen;
protected final ClickEventFactory clickEventFactory;
protected final IntSet lockedSlots = new IntRBTreeSet();
protected final ReadWriteLock lockedSlotsLock = new ReentrantReadWriteLock();
protected final ReadWriteLock slotStatesLock = new ReentrantReadWriteLock();
protected final Int2ObjectMap<SlotInteractionState> slotStates;

public static final int INVALID_SCOPE = Integer.MAX_VALUE;

protected ContainerScreenHelper(T screen, ClickEventFactory clickEventFactory) {
this.screen = screen;
this.clickEventFactory = clickEventFactory;
this.slotStates = new Int2ObjectArrayMap<>(10);
}

@SuppressWarnings("unchecked")
Expand All @@ -70,40 +71,53 @@ public static <T extends HandledScreen<?>> ContainerScreenHelper<T> of(T screen,
}

public InteractionManager.InteractionEvent createClickEvent(Slot slot, int action, SlotActionType actionType) {
if (isSlotLocked(slot)) {
if (getSlotState(slot).areInteractionsLocked()) {
return null;
}
return clickEventFactory.create(slot, action, actionType);
}

public boolean isSlotLocked(Slot slot) {
Lock readLock = lockedSlotsLock.readLock();
public SlotInteractionState getSlotState(Slot slot) {
Lock readLock = slotStatesLock.readLock();
readLock.lock();
try {
return lockedSlots.contains(slot.id);
SlotInteractionState state = slotStates.get(slot.id);
if (state == null) {
return SlotInteractionState.NORMAL;
}
return state;
} finally {
readLock.unlock();
}
}

public void lockSlot(Slot slot) {
Lock writeLock = lockedSlotsLock.writeLock();
public void setSloteState(Slot slot, SlotInteractionState state) {
Lock writeLock = slotStatesLock.writeLock();
writeLock.lock();
try {
lockedSlots.add(slot.id);
if (state == SlotInteractionState.NORMAL) {
slotStates.remove(slot.id);
} else {
slotStates.put(slot.id, state);
}
} finally {
writeLock.unlock();
}
}

public void unlockSlot(Slot slot) {
Lock writeLock = lockedSlotsLock.writeLock();
writeLock.lock();
try {
lockedSlots.remove(slot.id);
} finally {
writeLock.unlock();
setSloteState(slot, SlotInteractionState.NORMAL);
}

private InteractionManager.InteractionEvent lockBefore(InteractionManager.InteractionEvent event, Slot slot, SlotInteractionState slotState) {
if (event == null) {
return null;
}

return new InteractionManager.CallbackEvent(() -> {
setSloteState(slot, slotState);
return event.send();
}, event.shouldRunOnMainThread());
}

private InteractionManager.InteractionEvent unlockAfter(InteractionManager.InteractionEvent event, Slot slot) {
Expand Down Expand Up @@ -264,31 +278,16 @@ public int getComplementaryScope(int scope) {
}

public void sendSingleItem(Slot slot) {
if (isSlotLocked(slot)) {
SlotInteractionState slotState = getSlotState(slot);
if (slotState.areInteractionsLocked()) {
return;
}

if (slot.getStack().getCount() == 1) {
if (slotState.isAmountStable() && slot.getStack().getCount() == 1) {
InteractionManager.push(clickEventFactory.create(slot, 0, SlotActionType.QUICK_MOVE));
return;
}
InteractionManager.push(clickEventFactory.create(slot, 0, SlotActionType.PICKUP));
InteractionManager.push(clickEventFactory.create(slot, 1, SlotActionType.PICKUP));
InteractionManager.push(clickEventFactory.create(slot, 0, SlotActionType.QUICK_MOVE));
InteractionManager.push(clickEventFactory.create(slot, 0, SlotActionType.PICKUP));
}

public void sendSingleItemLocked(Slot slot) {
if (isSlotLocked(slot)) {
return;
}

lockSlot(slot);
if (slot.getStack().getCount() == 1) {
InteractionManager.push(unlockAfter(clickEventFactory.create(slot, 0, SlotActionType.QUICK_MOVE), slot));
return;
}
InteractionManager.push(clickEventFactory.create(slot, 0, SlotActionType.PICKUP));
InteractionManager.push(lockBefore(clickEventFactory.create(slot, 0, SlotActionType.PICKUP), slot, SlotInteractionState.UNSTABLE_AMOUNT));
InteractionManager.push(clickEventFactory.create(slot, 1, SlotActionType.PICKUP));
InteractionManager.push(clickEventFactory.create(slot, 0, SlotActionType.QUICK_MOVE));
InteractionManager.push(unlockAfter(clickEventFactory.create(slot, 0, SlotActionType.PICKUP), slot));
Expand All @@ -299,12 +298,11 @@ public void sendStack(Slot slot) {
}

public void sendStackLocked(Slot slot) {
if (isSlotLocked(slot)) {
if (getSlotState(slot).areInteractionsLocked()) {
return;
}

lockSlot(slot);
InteractionManager.push(unlockAfter(clickEventFactory.create(slot, 0, SlotActionType.QUICK_MOVE), slot));
InteractionManager.push(unlockAfter(lockBefore(clickEventFactory.create(slot, 0, SlotActionType.QUICK_MOVE), slot, SlotInteractionState.TEMP_LOCKED), slot));
}

public void sendAllOfAKind(Slot referenceSlot) {
Expand Down Expand Up @@ -425,20 +423,19 @@ public void restockAll(int scope) {
}

public void dropStack(Slot slot) {
if (isSlotLocked(slot)) {
if (getSlotState(slot).areInteractionsLocked()) {
return;
}

InteractionManager.push(createClickEvent(slot, 1, SlotActionType.THROW));
}

public void dropStackLocked(Slot slot) {
if (isSlotLocked(slot)) {
if (getSlotState(slot).areInteractionsLocked()) {
return;
}

lockSlot(slot);
InteractionManager.push(unlockAfter(clickEventFactory.create(slot, 1, SlotActionType.THROW), slot));
InteractionManager.push(unlockAfter(lockBefore(clickEventFactory.create(slot, 1, SlotActionType.THROW), slot, SlotInteractionState.TEMP_LOCKED), slot));
}

public void dropAllOfAKind(Slot referenceSlot) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2020-2022 Siphalor
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied.
* See the License for the specific language governing
* permissions and limitations under the License.
*/

package de.siphalor.mousewheelie.client.inventory;

/**
* Models the internal interaction state of a slot.
*/
public enum SlotInteractionState {
/**
* The slot's default state.
*/
NORMAL,
/**
* Indicates that the slot is still interactable,
* but it is not safe to rely on the amount of the stack, as that is currently in flux.
*/
UNSTABLE_AMOUNT,
/**
* A slot is temporarily locked for interactions.
* Used to stop bulk actions from interfering with each other.
*/
TEMP_LOCKED,
;

/**
* Determines whether new interactions for this slot may be registered.
* @return whether new interactions may be registered.
*/
boolean areInteractionsLocked() {
return this == TEMP_LOCKED;
}

/**
* Determines whether the current stack amount may be relied upon or is in flux.
* @return whether the current stack amount is stable.
*/
boolean isAmountStable() {
return this == NORMAL;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,28 @@ private static void refillFromHotbar(Hand hand, int hotbarSlot) {

private static void refillFromInventory(Hand hand, int inventorySlot) {
if (hand == Hand.OFF_HAND) {
ItemStack mainHandStack = playerInventory.getMainHandStack();
InteractionManager.push(InteractionManager.SWAP_WITH_OFFHAND_EVENT);

pickFromInventory(inventorySlot);

InteractionManager.push(InteractionManager.SWAP_WITH_OFFHAND_EVENT);
// Sometimes the swapping visually duplicates the stack on the client,
// so we're manually fixing the visuals here
InteractionManager.push(() -> {
playerInventory.setStack(playerInventory.selectedSlot, mainHandStack);
return InteractionManager.DUMMY_WAITER;
});
} else {
pickFromInventory(inventorySlot);
}
}

private static void pickFromInventory(int inventorySlot) {
InteractionManager.push(new InteractionManager.PacketEvent(
new PickFromInventoryC2SPacket(inventorySlot),
triggerType -> triggerType == InteractionManager.TriggerType.HELD_ITEM_CHANGE
));
if (hand == Hand.OFF_HAND) {
InteractionManager.push(InteractionManager.SWAP_WITH_OFFHAND_EVENT);
}
}

static {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,10 @@ protected void reorderInventory(int[] sortedIds) {
slotMappings[i * 2] = inventorySlots[sortedIds[i]].id;
slotMappings[i * 2 + 1] = inventorySlots[i].id;
}
MWClientNetworking.send(new ReorderInventoryPacket(containerScreen.getScreenHandler().syncId, slotMappings));
InteractionManager.push(() -> {
MWClientNetworking.send(new ReorderInventoryPacket(containerScreen.getScreenHandler().syncId, slotMappings));
return InteractionManager.TICK_WAITER;
});
}

protected void sortOnClient(int[] sortedIds) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onItemUsed(CallbackInfo callbackInfo) {
refillScheduled = MWClient.scheduleRefillChecked(Hand.MAIN_HAND, player.getInventory(), mainHandStack, player.getMainHandStack());
}
if (!refillScheduled && offHandStack != null) {
MWClient.scheduleRefillChecked(Hand.MAIN_HAND, player.getInventory(), offHandStack, player.getOffHandStack());
MWClient.scheduleRefillChecked(Hand.OFF_HAND, player.getInventory(), offHandStack, player.getOffHandStack());
}
MWClient.performRefill();
mainHandStack = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected MWNetworking() {}

protected static final Identifier REORDER_INVENTORY_C2S_PACKET = new Identifier(MouseWheelie.MOD_ID, "reorder_inventory_c2s");

protected static PacketByteBuf createBuffer() {
public static PacketByteBuf createBuffer() {
return new PacketByteBuf(Unpooled.buffer());
}
}
Loading

0 comments on commit ee41df0

Please sign in to comment.