diff --git a/src/main/java/com/github/vfyjxf/nee/client/GuiEventHandler.java b/src/main/java/com/github/vfyjxf/nee/client/GuiEventHandler.java index e7a14f7..4f6c507 100644 --- a/src/main/java/com/github/vfyjxf/nee/client/GuiEventHandler.java +++ b/src/main/java/com/github/vfyjxf/nee/client/GuiEventHandler.java @@ -7,8 +7,6 @@ import static com.github.vfyjxf.nee.utils.GuiUtils.isPatternTerm; import java.awt.Point; import java.awt.Rectangle; -import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -36,6 +34,7 @@ import com.github.vfyjxf.nee.utils.ItemUtils; import com.glodblock.github.client.gui.GuiLevelMaintainer; import appeng.api.events.GuiScrollEvent; +import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; import appeng.client.gui.AEBaseGui; import appeng.client.gui.implementations.GuiInterface; @@ -46,6 +45,7 @@ import appeng.client.gui.slots.VirtualMESlot; import appeng.container.AEBaseContainer; import appeng.container.implementations.ContainerPatternTerm; import appeng.container.slot.SlotFake; +import appeng.util.item.AEItemStack; import codechicken.lib.gui.GuiDraw; import codechicken.nei.NEIClientConfig; import codechicken.nei.NEIServerUtils; @@ -58,6 +58,8 @@ import codechicken.nei.recipe.RecipeInfo; import codechicken.nei.util.NEIMouseUtils; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; public class GuiEventHandler extends INEIGuiAdapter implements IContainerTooltipHandler { @@ -215,8 +217,9 @@ public class GuiEventHandler extends INEIGuiAdapter implements IContainerTooltip } if (sendPacket) { - NEENetworkHandler.getInstance().sendToServer( - new PacketSlotStackChange(copyStack, Collections.singletonList(currentSlot.slotNumber))); + Int2ObjectMap map = new Int2ObjectOpenHashMap(); + map.put(currentSlot.slotNumber, AEItemStack.create(copyStack)); + NEENetworkHandler.getInstance().sendToServer(new PacketSlotStackChange(map)); if (!NEEConfig.keepGhostitems) { draggedStack.stackSize = 0; } @@ -323,37 +326,64 @@ public class GuiEventHandler extends INEIGuiAdapter implements IContainerTooltip private void handleRecipeIngredientChange(GuiPatternTerm gui, VirtualMEPatternSlot currentSlot, int dWheel) { final int currentSlotIndex = currentSlot.getSlotIndex(); - final PositionedStack currentIngredients = NEEPatternTerminalHandler.ingredients - .get(INPUT_KEY + currentSlotIndex); + final PositionedStack baseIngredients = NEEPatternTerminalHandler.ingredients.get(INPUT_KEY + currentSlotIndex); - if (currentIngredients != null && currentIngredients.items.length > 1) { + if (baseIngredients != null && baseIngredients.items.length > 1) { final IAEStack aes = currentSlot.getAEStack(); if (aes == null) return; - final ItemStack slotStack = aes.getItemStackForNEI(); - if (slotStack == null) return; - final List craftingSlots = new ArrayList<>(); - final List items = currentIngredients.getFilteredPermutations(); - final int currentStackIndex = ItemUtils.getPermutationIndex(slotStack, items); - final ItemStack nextStack = items.get((items.size() - dWheel + currentStackIndex) % items.size()); - final ItemStack currentStack = currentIngredients.item; + final ItemStack baseSlotStack = aes.getItemStackForNEI(); + if (baseSlotStack == null) return; + final List items = baseIngredients.getFilteredPermutations(); + final int currentStackIndex = ItemUtils.getPermutationIndex(baseSlotStack, items); + final ItemStack nextStack = items.get((items.size() - dWheel + currentStackIndex) % items.size()).copy(); + final Int2ObjectMap craftingSlotss = new Int2ObjectOpenHashMap<>(); if (NEEConfig.allowSynchronousSwitchIngredient) { + final ItemStack baseStack = baseIngredients.item; for (VirtualMEPatternSlot slot : gui.getCraftingSlots()) { + IAEStack slotAEStack = slot.getAEStack(); + if (!(slotAEStack instanceof IAEItemStack ais)) continue; + final ItemStack slotStack = ais.getItemStackForNEI(); + if (slotStack == null) continue; + final PositionedStack slotIngredients = NEEPatternTerminalHandler.ingredients .get(INPUT_KEY + slot.getSlotIndex()); if (slotIngredients != null && slotIngredients.containsWithNBT(nextStack) - && NEIServerUtils.areStacksSameTypeCraftingWithNBT(slotIngredients.item, currentStack)) { + && NEIServerUtils.areStacksSameTypeCraftingWithNBT(slotStack, baseStack) + && NEIServerUtils.areStacksSameTypeCraftingWithNBT(slotIngredients.item, baseStack)) { + + // If the current slot's stack size is a multiple of the recipe's default, apply that multiplier + // to nextStack. + long nextStackAmount; + if (slotStack.stackSize % baseStack.stackSize == 0) { + nextStackAmount = (long) nextStack.stackSize * (slotStack.stackSize / baseStack.stackSize); + } else { + nextStackAmount = slotAEStack.getStackSize(); + } + + craftingSlotss + .put(slot.getSlotIndex(), AEItemStack.create(nextStack).setStackSize(nextStackAmount)); slotIngredients.setPermutationToRender(nextStack); - craftingSlots.add(slot.getSlotIndex()); } } } else { - currentIngredients.setPermutationToRender(nextStack); - craftingSlots.add(currentSlot.getSlotIndex()); + // If the current slot's stack size is a multiple of the recipe's default, apply that multiplier to + // nextStack. + long nextStackAmount; + if (baseSlotStack.stackSize % baseIngredients.item.stackSize == 0) { + nextStackAmount = (long) nextStack.stackSize + * (baseSlotStack.stackSize / baseIngredients.item.stackSize); + } else { + nextStackAmount = aes.getStackSize(); + } + + craftingSlotss + .put(currentSlot.getSlotIndex(), AEItemStack.create(nextStack).setStackSize(nextStackAmount)); + baseIngredients.setPermutationToRender(nextStack); } - NEENetworkHandler.getInstance().sendToServer(new PacketSlotStackChange(nextStack, craftingSlots)); + NEENetworkHandler.getInstance().sendToServer(new PacketSlotStackChange(craftingSlotss)); } } } diff --git a/src/main/java/com/github/vfyjxf/nee/network/packet/PacketSlotStackChange.java b/src/main/java/com/github/vfyjxf/nee/network/packet/PacketSlotStackChange.java index b2b93be..d867789 100644 --- a/src/main/java/com/github/vfyjxf/nee/network/packet/PacketSlotStackChange.java +++ b/src/main/java/com/github/vfyjxf/nee/network/packet/PacketSlotStackChange.java @@ -1,17 +1,16 @@ package com.github.vfyjxf.nee.network.packet; -import java.util.ArrayList; -import java.util.List; +import java.io.IOException; -import net.minecraft.item.ItemStack; +import javax.annotation.Nonnull; -import com.github.vfyjxf.nee.utils.ItemUtils; +import com.github.vfyjxf.nee.NotEnoughEnergistics; import appeng.api.storage.StorageName; +import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; import appeng.container.implementations.ContainerPatternTerm; import appeng.util.item.AEItemStack; -import cpw.mods.fml.common.network.ByteBufUtils; import cpw.mods.fml.common.network.simpleimpl.IMessage; import cpw.mods.fml.common.network.simpleimpl.IMessageHandler; import cpw.mods.fml.common.network.simpleimpl.MessageContext; @@ -24,41 +23,48 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; */ public class PacketSlotStackChange implements IMessage { - private ItemStack stack; - private List craftingSlots; + private IAEItemStack stack; + private Int2ObjectMap slotStacks; public PacketSlotStackChange() {} - public PacketSlotStackChange(ItemStack stack, List craftingSlots) { - this.stack = stack; - this.craftingSlots = craftingSlots; + public PacketSlotStackChange(@Nonnull Int2ObjectMap slotStacks) { + this.slotStacks = slotStacks; } - public ItemStack getStack() { + public IAEItemStack getStack() { return stack; } - public List getCraftingSlots() { - return craftingSlots; + public Int2ObjectMap getSlotStacks() { + return slotStacks; } @Override public void fromBytes(ByteBuf buf) { - this.stack = ItemUtils.loadItemStackFromNBT(ByteBufUtils.readTag(buf)); - int craftingSlotsSize = buf.readInt(); - this.craftingSlots = new ArrayList<>(craftingSlotsSize); - for (int i = 0; i < craftingSlotsSize; i++) { + int mapSize = buf.readInt(); + this.slotStacks = new Int2ObjectOpenHashMap<>(mapSize); + for (int i = 0; i < mapSize; i++) { int slotNumber = buf.readInt(); - craftingSlots.add(slotNumber); + try { + IAEItemStack stack = AEItemStack.loadItemStackFromPacket(buf); + this.slotStacks.put(slotNumber, stack); + } catch (IOException e) { + NotEnoughEnergistics.logger.error(e); + } } } @Override public void toBytes(ByteBuf buf) { - ByteBufUtils.writeTag(buf, ItemUtils.writeItemStackToNBT(this.stack, this.stack.stackSize)); - buf.writeInt(this.craftingSlots.size()); - for (Integer craftingSlot : this.craftingSlots) { - buf.writeInt(craftingSlot); + buf.writeInt(this.slotStacks.size()); + for (Int2ObjectMap.Entry entry : this.slotStacks.int2ObjectEntrySet()) { + buf.writeInt(entry.getIntKey()); + try { + entry.getValue().writeToPacket(buf); + } catch (IOException e) { + NotEnoughEnergistics.logger.error(e); + } } } @@ -66,12 +72,10 @@ public class PacketSlotStackChange implements IMessage { @Override public IMessage onMessage(PacketSlotStackChange message, MessageContext ctx) { - final IAEStack nextStack = AEItemStack.create(message.getStack()); - if (nextStack != null - && ctx.getServerHandler().playerEntity.openContainer instanceof ContainerPatternTerm cpt) { + if (ctx.getServerHandler().playerEntity.openContainer instanceof ContainerPatternTerm cpt) { final Int2ObjectMap> temp = new Int2ObjectOpenHashMap<>(); - for (int craftingSlot : message.getCraftingSlots()) { - temp.put(craftingSlot, nextStack); + for (Int2ObjectMap.Entry entry : message.getSlotStacks().int2ObjectEntrySet()) { + temp.put(entry.getIntKey(), entry.getValue()); } cpt.receiveSlotStacks(StorageName.CRAFTING_INPUT, temp); }