Skip to content

Commit

Permalink
clarify gui actions and simplify syncing changes
Browse files Browse the repository at this point in the history
compact ids
simplify crate change listener
address todo
only interact when not ghost dragging
  • Loading branch information
ghzdude committed Feb 14, 2025
1 parent 4018512 commit a6dbac5
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntArraySet;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenCustomHashMap;
Expand All @@ -43,7 +44,7 @@ public class CraftingRecipeLogic extends SyncHandler {
// client only
public static final int UPDATE_INGREDIENTS = 1;
public static final int RESET_INGREDIENTS = 2;
public static final int SYNC_STACK = 4;
public static final int SYNC_STACK = 3;

// server only
public static final int UPDATE_MATRIX = 0;
Expand Down Expand Up @@ -71,7 +72,7 @@ public class CraftingRecipeLogic extends SyncHandler {
private final Int2IntMap compactedIndexes = new Int2IntArrayMap(9);
private final Int2IntMap slotMap = new Int2IntArrayMap();

private final Map<Integer, Object2BooleanMap<ItemStack>> replaceAttemptMap = new Int2ObjectArrayMap<>();
private final Int2ObjectMap<Object2BooleanMap<ItemStack>> replaceAttemptMap = new Int2ObjectArrayMap<>();
private final InventoryCrafting craftingMatrix;
private final IInventory craftingResultInventory = new InventoryCraftResult();
private final CachedRecipeData cachedRecipeData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,12 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager)
widgets.get(i).add(new ItemSlot().slot(SyncHandlers.itemSlot(inventory, index)
.slotGroup("item_inv")
.changeListener((newItem, onlyAmountChanged, client, init) -> {
if (!onlyAmountChanged && !client && !init) {
for (var facing : EnumFacing.VALUES) {
var neighbor = getNeighbor(facing);
if (neighbor instanceof IGregTechTileEntity gregTechTileEntity) {
gregTechTileEntity.getMetaTileEntity().onNeighborChanged();
}
if (client || init) return;

for (var facing : EnumFacing.VALUES) {
var neighbor = getNeighbor(facing);
if (neighbor instanceof IGregTechTileEntity gtte) {
gtte.getMetaTileEntity().onNeighborChanged();
}
}
})));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ public IWidget createCraftingGrid() {
public IWidget createCraftingOutput(PosGuiData guiData, PanelSyncManager syncManager) {
var amountCrafted = new IntSyncValue(this::getItemsCrafted, this::setItemsCrafted);
syncManager.syncValue("amount_crafted", amountCrafted);
amountCrafted.updateCacheFromSource(true); // todo remove on mui2 rc3

return Flow.column()
.size(54)
Expand Down Expand Up @@ -418,8 +417,7 @@ public void readHandler(PacketBuffer buf) {
this.toolInventory,
this.connectedInventory));

getCraftingRecipeLogic()
.updateInventory(this.combinedInventory);
getCraftingRecipeLogic().updateInventory(this.combinedInventory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ private CraftingInputSlot(IItemHandlerModifiable handler, int index) {
tooltip.addFromItem(stack);
});

// for hovering with items in hand
listenGuiAction((IGuiAction.MouseDrag) (m, t) -> {
if (isHovering() && dragging && syncHandler.isValid()) {
var player = syncHandler.getSyncManager().getCursorItem();
Expand All @@ -53,6 +54,7 @@ private CraftingInputSlot(IItemHandlerModifiable handler, int index) {
return false;
});

// dragging has stopped
listenGuiAction((IGuiAction.MouseReleased) mouseButton -> {
dragging = false;
return true;
Expand Down Expand Up @@ -162,7 +164,7 @@ public InputSyncHandler(IItemHandlerModifiable handler, int index) {
@Override
public void init(String key, PanelSyncManager syncHandler) {
super.init(key, syncHandler);
this.lastStoredItem = this.handler.getStackInSlot(this.index).copy();
this.lastStoredItem = getStack().copy();
}

@Override
Expand All @@ -185,16 +187,9 @@ public void readOnServer(int id, PacketBuffer buf) {
public void detectAndSendChanges(boolean init) {
ItemStack itemStack = getStack();
if (itemStack.isEmpty() && this.lastStoredItem.isEmpty()) return;
boolean onlyAmountChanged = false;
if (init ||
!ItemHandlerHelper.canItemStacksStack(this.lastStoredItem, itemStack) ||
(onlyAmountChanged = itemStack.getCount() != this.lastStoredItem.getCount())) {
this.listener.onChange(itemStack, onlyAmountChanged, false, init);
if (onlyAmountChanged) {
this.lastStoredItem.setCount(itemStack.getCount());
} else {
this.lastStoredItem = itemStack.isEmpty() ? ItemStack.EMPTY : itemStack.copy();
}
if (init || !ItemHandlerHelper.canItemStacksStack(this.lastStoredItem, itemStack)) {
this.listener.onChange(itemStack, false, false, init);
this.lastStoredItem = itemStack.isEmpty() ? ItemStack.EMPTY : itemStack.copy();
syncToClient(SLOT_CHANGED, buffer -> NetworkUtils.writeItemStack(buffer, itemStack));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/gregtech/mixins/mui2/ModularPanelMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public abstract class ModularPanelMixin extends ParentWidget<ModularPanel> imple
loop:
for (LocatedWidget widget : this.hovering) {
widget.applyMatrix(getContext());
GhostIngredientDrag<?> drag = gregTech$getGhostDrag();
if (widget.getElement() instanceof JeiGhostIngredientSlot<?>ghostSlot &&
Mods.JustEnoughItems.isModLoaded()) {
GhostIngredientDrag<?> drag = gregTech$getGhostDrag();
if (drag != null && gregTech$insertGhostIngredient(drag, ghostSlot)) {
gregTech$stopDrag();
pressed = LocatedWidget.EMPTY;
Expand All @@ -79,7 +79,7 @@ public abstract class ModularPanelMixin extends ParentWidget<ModularPanel> imple
break;
}
}
if (widget.getElement() instanceof Interactable interactable) {
if (drag == null && widget.getElement() instanceof Interactable interactable) {
switch (interactable.onMousePressed(mouseButton)) {
case IGNORE:
break;
Expand Down

0 comments on commit a6dbac5

Please sign in to comment.