From caa7392df580c91b9af087b90dd3f479157f6245 Mon Sep 17 00:00:00 2001 From: raoulvdberge Date: Tue, 15 Oct 2019 19:16:51 +0200 Subject: [PATCH] Re-add JEI transfer --- ...id.java => GridRecipeTransferHandler.java} | 47 ++--- .../integration/jei/RSJeiPlugin.java | 8 +- .../MessageGridProcessingTransfer.java.off | 172 ----------------- .../network/MessageGridTransfer.java.off | 82 -------- .../network/NetworkHandler.java | 2 + .../grid/GridProcessingTransferMessage.java | 176 ++++++++++++++++++ .../network/grid/GridTransferMessage.java | 92 +++++++++ .../screen/widget/ScrollbarWidget.java | 15 +- 8 files changed, 314 insertions(+), 280 deletions(-) rename src/main/java/com/raoulvdberge/refinedstorage/integration/jei/{RecipeTransferHandlerGrid.java => GridRecipeTransferHandler.java} (65%) mode change 100755 => 100644 delete mode 100644 src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridProcessingTransfer.java.off delete mode 100644 src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridTransfer.java.off create mode 100644 src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridProcessingTransferMessage.java create mode 100644 src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridTransferMessage.java diff --git a/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/RecipeTransferHandlerGrid.java b/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/GridRecipeTransferHandler.java old mode 100755 new mode 100644 similarity index 65% rename from src/main/java/com/raoulvdberge/refinedstorage/integration/jei/RecipeTransferHandlerGrid.java rename to src/main/java/com/raoulvdberge/refinedstorage/integration/jei/GridRecipeTransferHandler.java index 9cda174b87..0cfdf0ea00 --- a/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/RecipeTransferHandlerGrid.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/GridRecipeTransferHandler.java @@ -1,21 +1,20 @@ package com.raoulvdberge.refinedstorage.integration.jei; -/* + import com.raoulvdberge.refinedstorage.RS; import com.raoulvdberge.refinedstorage.api.network.grid.GridType; import com.raoulvdberge.refinedstorage.api.network.grid.IGrid; -import com.raoulvdberge.refinedstorage.apiimpl.network.node.NetworkNodeGrid; -import com.raoulvdberge.refinedstorage.container.ContainerGrid; -import com.raoulvdberge.refinedstorage.network.MessageGridProcessingTransfer; -import com.raoulvdberge.refinedstorage.network.MessageGridTransfer; -import mezz.jei.api.gui.IGuiIngredient; +import com.raoulvdberge.refinedstorage.apiimpl.network.node.GridNetworkNode; +import com.raoulvdberge.refinedstorage.container.GridContainer; +import com.raoulvdberge.refinedstorage.network.grid.GridProcessingTransferMessage; +import com.raoulvdberge.refinedstorage.network.grid.GridTransferMessage; +import mezz.jei.api.constants.VanillaRecipeCategoryUid; import mezz.jei.api.gui.IRecipeLayout; -import mezz.jei.api.recipe.VanillaRecipeCategoryUid; +import mezz.jei.api.gui.ingredient.IGuiIngredient; import mezz.jei.api.recipe.transfer.IRecipeTransferError; import mezz.jei.api.recipe.transfer.IRecipeTransferHandler; -import net.minecraft.client.Minecraft; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.inventory.Container; -import net.minecraft.inventory.InventoryCrafting; +import net.minecraft.inventory.CraftingInventory; +import net.minecraft.inventory.container.Container; import net.minecraft.item.ItemStack; import net.minecraftforge.fluids.FluidStack; @@ -23,9 +22,9 @@ import java.util.List; import java.util.stream.Collectors; -public class RecipeTransferHandlerGrid implements IRecipeTransferHandler { - public static final long TRANSFER_SCROLL_DELAY_MS = 200; - public static long LAST_TRANSFER; +public class GridRecipeTransferHandler implements IRecipeTransferHandler { + public static final long TRANSFER_SCROLLBAR_DELAY_MS = 200; + public static long LAST_TRANSFER_TIME; private static final IRecipeTransferError ERROR_CANNOT_TRANSFER = new IRecipeTransferError() { @Override @@ -34,24 +33,24 @@ public Type getType() { } @Override - public void showError(Minecraft minecraft, int mouseX, int mouseY, IRecipeLayout recipeLayout, int recipeX, int recipeY) { - // NO OP + public void showError(int i, int i1, IRecipeLayout iRecipeLayout, int i2, int i3) { + } }; @Override public Class getContainerClass() { - return ContainerGrid.class; + return GridContainer.class; } @Override public IRecipeTransferError transferRecipe(Container container, IRecipeLayout recipeLayout, PlayerEntity player, boolean maxTransfer, boolean doTransfer) { - IGrid grid = ((ContainerGrid) container).getGrid(); + IGrid grid = ((GridContainer) container).getGrid(); if (doTransfer) { - LAST_TRANSFER = System.currentTimeMillis(); + LAST_TRANSFER_TIME = System.currentTimeMillis(); - if (grid.getGridType() == GridType.PATTERN && ((NetworkNodeGrid) grid).isProcessingPattern()) { + if (grid.getGridType() == GridType.PATTERN && ((GridNetworkNode) grid).isProcessingPattern()) { List inputs = new LinkedList<>(); List outputs = new LinkedList<>(); @@ -82,12 +81,15 @@ public IRecipeTransferError transferRecipe(Container container, IRecipeLayout re } } - RS.INSTANCE.network.sendToServer(new MessageGridProcessingTransfer(inputs, outputs, fluidInputs, fluidOutputs)); + RS.NETWORK_HANDLER.sendToServer(new GridProcessingTransferMessage(inputs, outputs, fluidInputs, fluidOutputs)); } else { - RS.INSTANCE.network.sendToServer(new MessageGridTransfer(recipeLayout.getItemStacks().getGuiIngredients(), container.inventorySlots.stream().filter(s -> s.inventory instanceof InventoryCrafting).collect(Collectors.toList()))); + RS.NETWORK_HANDLER.sendToServer(new GridTransferMessage( + recipeLayout.getItemStacks().getGuiIngredients(), + container.inventorySlots.stream().filter(s -> s.inventory instanceof CraftingInventory).collect(Collectors.toList()) + )); } } else { - if (grid.getGridType() == GridType.PATTERN && ((NetworkNodeGrid) grid).isProcessingPattern()) { + if (grid.getGridType() == GridType.PATTERN && ((GridNetworkNode) grid).isProcessingPattern()) { if (recipeLayout.getRecipeCategory().getUid().equals(VanillaRecipeCategoryUid.CRAFTING)) { return ERROR_CANNOT_TRANSFER; } @@ -101,4 +103,3 @@ public IRecipeTransferError transferRecipe(Container container, IRecipeLayout re return null; } } -*/ \ No newline at end of file diff --git a/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/RSJeiPlugin.java b/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/RSJeiPlugin.java index 5b65f1a144..11337b31cb 100755 --- a/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/RSJeiPlugin.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/integration/jei/RSJeiPlugin.java @@ -5,6 +5,7 @@ import mezz.jei.api.IModPlugin; import mezz.jei.api.JeiPlugin; import mezz.jei.api.registration.IGuiHandlerRegistration; +import mezz.jei.api.registration.IRecipeTransferRegistration; import mezz.jei.api.runtime.IJeiRuntime; import net.minecraft.util.ResourceLocation; @@ -14,8 +15,6 @@ public class RSJeiPlugin implements IModPlugin { public static IJeiRuntime RUNTIME; - // TODO registry.getRecipeTransferRegistry().addUniversalRecipeTransferHandler(new RecipeTransferHandlerGrid()); - // TODO registry.addRecipeRegistryPlugin(new RecipeRegistryPluginCover()); // TODO registry.addRecipeRegistryPlugin(new RecipeRegistryPluginHollowCover()); @@ -27,6 +26,11 @@ public ResourceLocation getPluginUid() { return ID; } + @Override + public void registerRecipeTransferHandlers(IRecipeTransferRegistration registration) { + registration.addUniversalRecipeTransferHandler(new GridRecipeTransferHandler()); + } + @Override public void registerGuiHandlers(IGuiHandlerRegistration registration) { registration.addGuiContainerHandler(BaseScreen.class, new GuiContainerHandler()); diff --git a/src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridProcessingTransfer.java.off b/src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridProcessingTransfer.java.off deleted file mode 100644 index 012b72ec42..0000000000 --- a/src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridProcessingTransfer.java.off +++ /dev/null @@ -1,172 +0,0 @@ -package com.raoulvdberge.refinedstorage.network; - -import com.raoulvdberge.refinedstorage.api.network.grid.GridType; -import com.raoulvdberge.refinedstorage.api.network.grid.IGrid; -import com.raoulvdberge.refinedstorage.apiimpl.network.node.NetworkNodeGrid; -import com.raoulvdberge.refinedstorage.container.GridContainer; -import com.raoulvdberge.refinedstorage.inventory.fluid.FluidInventory; -import com.raoulvdberge.refinedstorage.inventory.item.ItemHandlerBase; -import com.raoulvdberge.refinedstorage.util.StackUtils; -import io.netty.buffer.ByteBuf; -import net.minecraft.entity.player.ServerPlayerEntity; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fluids.Fluid; -import net.minecraftforge.fluids.FluidStack; -import net.minecraftforge.fml.common.network.simpleimpl.IMessage; - -import java.util.ArrayList; -import java.util.Collection; - -public class MessageGridProcessingTransfer extends MessageHandlerPlayerToServer implements IMessage { - private Collection inputs; - private Collection outputs; - - private Collection fluidInputs; - private Collection fluidOutputs; - - public MessageGridProcessingTransfer() { - } - - public MessageGridProcessingTransfer(Collection inputs, Collection outputs, Collection fluidInputs, Collection fluidOutputs) { - this.inputs = inputs; - this.outputs = outputs; - this.fluidInputs = fluidInputs; - this.fluidOutputs = fluidOutputs; - } - - @Override - public void fromBytes(ByteBuf buf) { - int size = buf.readInt(); - - this.inputs = new ArrayList<>(size); - - for (int i = 0; i < size; i++) { - this.inputs.add(StackUtils.readItemStack(buf)); - } - - size = buf.readInt(); - - this.outputs = new ArrayList<>(size); - - for (int i = 0; i < size; i++) { - this.outputs.add(StackUtils.readItemStack(buf)); - } - - size = buf.readInt(); - - this.fluidInputs = new ArrayList<>(size); - - for (int i = 0; i < size; i++) { - this.fluidInputs.add(StackUtils.readFluidStack(buf)); - } - - size = buf.readInt(); - - this.fluidOutputs = new ArrayList<>(size); - - for (int i = 0; i < size; i++) { - this.fluidOutputs.add(StackUtils.readFluidStack(buf)); - } - } - - @Override - public void toBytes(ByteBuf buf) { - buf.writeInt(inputs.size()); - - for (ItemStack stack : inputs) { - StackUtils.writeItemStack(buf, stack); - } - - buf.writeInt(outputs.size()); - - for (ItemStack stack : outputs) { - StackUtils.writeItemStack(buf, stack); - } - - buf.writeInt(fluidInputs.size()); - - for (FluidStack stack : fluidInputs) { - StackUtils.writeFluidStack(buf, stack); - } - - buf.writeInt(fluidOutputs.size()); - - for (FluidStack stack : fluidOutputs) { - StackUtils.writeFluidStack(buf, stack); - } - } - - @Override - public void handle(MessageGridProcessingTransfer message, ServerPlayerEntity player) { - if (player.openContainer instanceof ContainerGrid) { - IGrid grid = ((ContainerGrid) player.openContainer).getGrid(); - - if (grid.getGridType() == GridType.PATTERN) { - ItemHandlerBase handler = ((NetworkNodeGrid) grid).getProcessingMatrix(); - FluidInventory handlerFluid = ((NetworkNodeGrid) grid).getProcessingMatrixFluids(); - - clearInputsAndOutputs(handler); - clearInputsAndOutputs(handlerFluid); - - setInputs(handler, message.inputs); - setOutputs(handler, message.outputs); - - setFluidInputs(handlerFluid, message.fluidInputs); - setFluidOutputs(handlerFluid, message.fluidOutputs); - } - } - } - - private void clearInputsAndOutputs(ItemHandlerBase handler) { - for (int i = 0; i < 9 * 2; ++i) { - handler.setStackInSlot(i, ItemStack.EMPTY); - } - } - - private void clearInputsAndOutputs(FluidInventory handler) { - for (int i = 0; i < 9 * 2; ++i) { - handler.setFluid(i, null); - } - } - - private void setInputs(ItemHandlerBase handler, Collection stacks) { - setSlots(handler, stacks, 0, 9); - } - - private void setOutputs(ItemHandlerBase handler, Collection stacks) { - setSlots(handler, stacks, 9, 18); - } - - private void setSlots(ItemHandlerBase handler, Collection stacks, int begin, int end) { - for (ItemStack stack : stacks) { - handler.setStackInSlot(begin, stack); - - begin++; - - if (begin >= end) { - break; - } - } - } - - private void setFluidInputs(FluidInventory inventory, Collection stacks) { - setFluidSlots(inventory, stacks, 0, 9); - } - - private void setFluidOutputs(FluidInventory inventory, Collection stacks) { - setFluidSlots(inventory, stacks, 9, 18); - } - - private void setFluidSlots(FluidInventory inventory, Collection stacks, int begin, int end) { - for (FluidStack stack : stacks) { - - inventory.setFluid(begin, stack.copy()); - - begin++; - - if (begin >= end) { - break; - } - } - } -} diff --git a/src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridTransfer.java.off b/src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridTransfer.java.off deleted file mode 100644 index 4f2a91e588..0000000000 --- a/src/main/java/com/raoulvdberge/refinedstorage/network/MessageGridTransfer.java.off +++ /dev/null @@ -1,82 +0,0 @@ -package com.raoulvdberge.refinedstorage.network; - -import com.raoulvdberge.refinedstorage.api.network.grid.GridType; -import com.raoulvdberge.refinedstorage.api.network.grid.IGrid; -import com.raoulvdberge.refinedstorage.container.GridContainer; -import com.raoulvdberge.refinedstorage.util.StackUtils; -import io.netty.buffer.ByteBuf; -import mezz.jei.api.gui.IGuiIngredient; -import net.minecraft.entity.player.ServerPlayerEntity; -import net.minecraft.inventory.Slot; -import net.minecraft.item.ItemStack; -import net.minecraftforge.fml.common.network.simpleimpl.IMessage; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -public class MessageGridTransfer extends MessageHandlerPlayerToServer implements IMessage { - private Map> inputs; - private List slots; - - private ItemStack[][] recipe = new ItemStack[9][]; - - public MessageGridTransfer() { - } - - public MessageGridTransfer(Map> inputs, List slots) { - this.inputs = inputs; - this.slots = slots; - } - - @Override - public void fromBytes(ByteBuf buf) { - int slots = buf.readInt(); - - for (int i = 0; i < slots; ++i) { - int ingredients = buf.readInt(); - - recipe[i] = new ItemStack[ingredients]; - - for (int j = 0; j < ingredients; ++j) { - recipe[i][j] = StackUtils.readItemStack(buf); - } - } - } - - @Override - public void toBytes(ByteBuf buf) { - buf.writeInt(slots.size()); - - for (Slot slot : slots) { - IGuiIngredient ingredient = inputs.get(slot.getSlotIndex() + 1); - - List ingredients = new ArrayList<>(); - - if (ingredient != null) { - for (ItemStack possibleStack : ingredient.getAllIngredients()) { - if (possibleStack != null) { - ingredients.add(possibleStack); - } - } - } - - buf.writeInt(ingredients.size()); - - for (ItemStack possibleStack : ingredients) { - StackUtils.writeItemStack(buf, possibleStack); - } - } - } - - @Override - public void handle(MessageGridTransfer message, ServerPlayerEntity player) { - if (player.openContainer instanceof ContainerGrid) { - IGrid grid = ((ContainerGrid) player.openContainer).getGrid(); - - if (grid.getGridType() == GridType.CRAFTING || grid.getGridType() == GridType.PATTERN) { - grid.onRecipeTransfer(player, message.recipe); - } - } - } -} diff --git a/src/main/java/com/raoulvdberge/refinedstorage/network/NetworkHandler.java b/src/main/java/com/raoulvdberge/refinedstorage/network/NetworkHandler.java index dd0be013d8..b3143afe27 100644 --- a/src/main/java/com/raoulvdberge/refinedstorage/network/NetworkHandler.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/network/NetworkHandler.java @@ -43,6 +43,8 @@ public void register() { handler.registerMessage(id++, GridFluidDeltaMessage.class, GridFluidDeltaMessage::encode, GridFluidDeltaMessage::decode, GridFluidDeltaMessage::handle); handler.registerMessage(id++, GridFluidInsertHeldMessage.class, GridFluidInsertHeldMessage::encode, GridFluidInsertHeldMessage::decode, GridFluidInsertHeldMessage::handle); handler.registerMessage(id++, GridFluidPullMessage.class, GridFluidPullMessage::encode, GridFluidPullMessage::decode, GridFluidPullMessage::handle); + handler.registerMessage(id++, GridTransferMessage.class, GridTransferMessage::encode, GridTransferMessage::decode, GridTransferMessage::handle); + handler.registerMessage(id++, GridProcessingTransferMessage.class, GridProcessingTransferMessage::encode, GridProcessingTransferMessage::decode, GridProcessingTransferMessage::handle); } public void sendToServer(Object message) { diff --git a/src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridProcessingTransferMessage.java b/src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridProcessingTransferMessage.java new file mode 100644 index 0000000000..6aa9025dcf --- /dev/null +++ b/src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridProcessingTransferMessage.java @@ -0,0 +1,176 @@ +package com.raoulvdberge.refinedstorage.network.grid; + +import com.raoulvdberge.refinedstorage.api.network.grid.GridType; +import com.raoulvdberge.refinedstorage.api.network.grid.IGrid; +import com.raoulvdberge.refinedstorage.apiimpl.network.node.GridNetworkNode; +import com.raoulvdberge.refinedstorage.container.GridContainer; +import com.raoulvdberge.refinedstorage.inventory.fluid.FluidInventory; +import com.raoulvdberge.refinedstorage.inventory.item.BaseItemHandler; +import com.raoulvdberge.refinedstorage.util.StackUtils; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fluids.FluidStack; +import net.minecraftforge.fml.network.NetworkEvent; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Supplier; + +public class GridProcessingTransferMessage { + private Collection inputs; + private Collection outputs; + private Collection fluidInputs; + private Collection fluidOutputs; + + public GridProcessingTransferMessage(Collection inputs, Collection outputs, Collection fluidInputs, Collection fluidOutputs) { + this.inputs = inputs; + this.outputs = outputs; + this.fluidInputs = fluidInputs; + this.fluidOutputs = fluidOutputs; + } + + public static GridProcessingTransferMessage decode(PacketBuffer buf) { + int size = buf.readInt(); + + List inputs = new ArrayList<>(size); + + for (int i = 0; i < size; i++) { + inputs.add(StackUtils.readItemStack(buf)); + } + + size = buf.readInt(); + + List outputs = new ArrayList<>(size); + + for (int i = 0; i < size; i++) { + outputs.add(StackUtils.readItemStack(buf)); + } + + size = buf.readInt(); + + List fluidInputs = new ArrayList<>(size); + + for (int i = 0; i < size; i++) { + fluidInputs.add(FluidStack.readFromPacket(buf)); + } + + size = buf.readInt(); + + List fluidOutputs = new ArrayList<>(size); + + for (int i = 0; i < size; i++) { + fluidOutputs.add(FluidStack.readFromPacket(buf)); + } + + return new GridProcessingTransferMessage(inputs, outputs, fluidInputs, fluidOutputs); + } + + public static void encode(GridProcessingTransferMessage message, PacketBuffer buf) { + buf.writeInt(message.inputs.size()); + + for (ItemStack stack : message.inputs) { + StackUtils.writeItemStack(buf, stack); + } + + buf.writeInt(message.outputs.size()); + + for (ItemStack stack : message.outputs) { + StackUtils.writeItemStack(buf, stack); + } + + buf.writeInt(message.fluidInputs.size()); + + for (FluidStack stack : message.fluidInputs) { + stack.writeToPacket(buf); + } + + buf.writeInt(message.fluidOutputs.size()); + + for (FluidStack stack : message.fluidOutputs) { + stack.writeToPacket(buf); + } + } + + public static void handle(GridProcessingTransferMessage message, Supplier ctx) { + PlayerEntity player = ctx.get().getSender(); + + if (player != null) { + ctx.get().enqueueWork(() -> { + if (player.openContainer instanceof GridContainer) { + IGrid grid = ((GridContainer) player.openContainer).getGrid(); + + if (grid.getGridType() == GridType.PATTERN) { + BaseItemHandler handler = ((GridNetworkNode) grid).getProcessingMatrix(); + FluidInventory handlerFluid = ((GridNetworkNode) grid).getProcessingMatrixFluids(); + + clearInputsAndOutputs(handler); + clearInputsAndOutputs(handlerFluid); + + setInputs(handler, message.inputs); + setOutputs(handler, message.outputs); + + setFluidInputs(handlerFluid, message.fluidInputs); + setFluidOutputs(handlerFluid, message.fluidOutputs); + } + } + }); + } + + ctx.get().setPacketHandled(true); + } + + private static void clearInputsAndOutputs(BaseItemHandler handler) { + for (int i = 0; i < 9 * 2; ++i) { + handler.setStackInSlot(i, ItemStack.EMPTY); + } + } + + private static void clearInputsAndOutputs(FluidInventory handler) { + for (int i = 0; i < 9 * 2; ++i) { + handler.setFluid(i, FluidStack.EMPTY); + } + } + + private static void setInputs(BaseItemHandler handler, Collection stacks) { + setSlots(handler, stacks, 0, 9); + } + + private static void setOutputs(BaseItemHandler handler, Collection stacks) { + setSlots(handler, stacks, 9, 18); + } + + private static void setSlots(BaseItemHandler handler, Collection stacks, int begin, int end) { + for (ItemStack stack : stacks) { + handler.setStackInSlot(begin, stack); + + begin++; + + if (begin >= end) { + break; + } + } + } + + private static void setFluidInputs(FluidInventory inventory, Collection stacks) { + setFluidSlots(inventory, stacks, 0, 9); + } + + private static void setFluidOutputs(FluidInventory inventory, Collection stacks) { + setFluidSlots(inventory, stacks, 9, 18); + } + + private static void setFluidSlots(FluidInventory inventory, Collection stacks, int begin, int end) { + for (FluidStack stack : stacks) { + + inventory.setFluid(begin, stack.copy()); + + begin++; + + if (begin >= end) { + break; + } + } + } +} diff --git a/src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridTransferMessage.java b/src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridTransferMessage.java new file mode 100644 index 0000000000..47c6cbd319 --- /dev/null +++ b/src/main/java/com/raoulvdberge/refinedstorage/network/grid/GridTransferMessage.java @@ -0,0 +1,92 @@ +package com.raoulvdberge.refinedstorage.network.grid; + +import com.raoulvdberge.refinedstorage.api.network.grid.GridType; +import com.raoulvdberge.refinedstorage.api.network.grid.IGrid; +import com.raoulvdberge.refinedstorage.container.GridContainer; +import com.raoulvdberge.refinedstorage.util.StackUtils; +import mezz.jei.api.gui.ingredient.IGuiIngredient; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.inventory.container.Slot; +import net.minecraft.item.ItemStack; +import net.minecraft.network.PacketBuffer; +import net.minecraftforge.fml.network.NetworkEvent; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.function.Supplier; + +public class GridTransferMessage { + private Map> inputs; + private List slots; + + private ItemStack[][] recipe = new ItemStack[9][]; + + public GridTransferMessage() { + } + + public GridTransferMessage(Map> inputs, List slots) { + this.inputs = inputs; + this.slots = slots; + } + + public static GridTransferMessage decode(PacketBuffer buf) { + GridTransferMessage msg = new GridTransferMessage(); + + int slots = buf.readInt(); + + for (int i = 0; i < slots; ++i) { + int ingredients = buf.readInt(); + + msg.recipe[i] = new ItemStack[ingredients]; + + for (int j = 0; j < ingredients; ++j) { + msg.recipe[i][j] = StackUtils.readItemStack(buf); + } + } + + return msg; + } + + public static void encode(GridTransferMessage message, PacketBuffer buf) { + buf.writeInt(message.slots.size()); + + for (Slot slot : message.slots) { + IGuiIngredient ingredient = message.inputs.get(slot.getSlotIndex() + 1); + + List ingredients = new ArrayList<>(); + + if (ingredient != null) { + for (ItemStack possibleStack : ingredient.getAllIngredients()) { + if (possibleStack != null) { + ingredients.add(possibleStack); + } + } + } + + buf.writeInt(ingredients.size()); + + for (ItemStack possibleStack : ingredients) { + StackUtils.writeItemStack(buf, possibleStack); + } + } + } + + public static void handle(GridTransferMessage message, Supplier ctx) { + PlayerEntity player = ctx.get().getSender(); + + if (player != null) { + ctx.get().enqueueWork(() -> { + if (player.openContainer instanceof GridContainer) { + IGrid grid = ((GridContainer) player.openContainer).getGrid(); + + if (grid.getGridType() == GridType.CRAFTING || grid.getGridType() == GridType.PATTERN) { + grid.onRecipeTransfer(player, message.recipe); + } + } + }); + } + + ctx.get().setPacketHandled(true); + } +} diff --git a/src/main/java/com/raoulvdberge/refinedstorage/screen/widget/ScrollbarWidget.java b/src/main/java/com/raoulvdberge/refinedstorage/screen/widget/ScrollbarWidget.java index 2f66a7537f..8729398696 100644 --- a/src/main/java/com/raoulvdberge/refinedstorage/screen/widget/ScrollbarWidget.java +++ b/src/main/java/com/raoulvdberge/refinedstorage/screen/widget/ScrollbarWidget.java @@ -1,6 +1,8 @@ package com.raoulvdberge.refinedstorage.screen.widget; import com.raoulvdberge.refinedstorage.RS; +import com.raoulvdberge.refinedstorage.integration.jei.GridRecipeTransferHandler; +import com.raoulvdberge.refinedstorage.integration.jei.JeiIntegration; import com.raoulvdberge.refinedstorage.screen.BaseScreen; import com.raoulvdberge.refinedstorage.util.RenderUtils; import net.minecraft.client.gui.IGuiEventListener; @@ -65,6 +67,13 @@ public boolean mouseClicked(double mx, double my, int button) { my -= screen.getGuiTop(); if (button == 0 && RenderUtils.inBounds(x, y, width, height, mx, my)) { + // Prevent accidental scrollbar click after clicking recipe transfer button + if (JeiIntegration.isLoaded() && System.currentTimeMillis() - GridRecipeTransferHandler.LAST_TRANSFER_TIME <= GridRecipeTransferHandler.TRANSFER_SCROLLBAR_DELAY_MS) { + return false; + } + + updateOffset(my); + clicked = true; return true; @@ -79,10 +88,14 @@ public void mouseMoved(double mx, double my) { my -= screen.getGuiTop(); if (clicked && RenderUtils.inBounds(x, y, width, height, mx, my)) { - setOffset((int) Math.floor((float) (my - y) / (float) (height - SCROLLER_HEIGHT) * (float) maxOffset)); + updateOffset(my); } } + private void updateOffset(double my) { + setOffset((int) Math.floor((float) (my - y) / (float) (height - SCROLLER_HEIGHT) * (float) maxOffset)); + } + @Override public boolean mouseReleased(double mx, double my, int button) { if (clicked) {