1+ package com .circulation .random_complement .mixin .nee .new_patten_gui ;
2+
3+ import appeng .api .networking .IGridNode ;
4+ import appeng .container .implementations .ContainerPatternTerm ;
5+ import appeng .util .helpers .ItemHandlerUtil ;
6+ import appeng .util .inv .WrapperInvItemHandler ;
7+ import com .github .vfyjxf .nee .network .packet .PacketRecipeTransfer ;
8+ import net .minecraft .entity .player .EntityPlayerMP ;
9+ import net .minecraft .inventory .Container ;
10+ import net .minecraft .item .ItemStack ;
11+ import net .minecraft .nbt .NBTTagCompound ;
12+ import net .minecraftforge .fml .common .network .simpleimpl .IMessage ;
13+ import net .minecraftforge .fml .common .network .simpleimpl .MessageContext ;
14+ import net .minecraftforge .items .IItemHandler ;
15+ import org .spongepowered .asm .mixin .Mixin ;
16+ import org .spongepowered .asm .mixin .Overwrite ;
17+ import org .spongepowered .asm .mixin .Shadow ;
18+
19+ @ Mixin (PacketRecipeTransfer .Handler .class )
20+ public abstract class MixinPacketRecipeTransfer {
21+
22+ @ Shadow (remap = false )
23+ protected abstract void setCraftingRecipe (ContainerPatternTerm container , boolean craftingMode );
24+
25+ /**
26+ * @author circulation
27+ * @reason 防止出现数组越界
28+ */
29+ @ Overwrite (remap = false )
30+ public IMessage onMessage (PacketRecipeTransfer message , MessageContext ctx ) {
31+ EntityPlayerMP player = ctx .getServerHandler ().player ;
32+ Container container = player .openContainer ;
33+ player .getServerWorld ().addScheduledTask (() -> {
34+ if (container instanceof ContainerPatternTerm cct ) {
35+ IItemHandler craftMatrix = cct .getInventoryByName ("crafting" );
36+
37+ this .setCraftingRecipe (cct , message .getCraftingMode ());
38+ ItemStack [] recipeInputs = new ItemStack [craftMatrix .getSlots ()];
39+ ItemStack [] recipeOutputs = null ;
40+
41+ for (int i = 0 ; i < recipeInputs .length ; ++i ) {
42+ NBTTagCompound currentStack = message .getInput ().getCompoundTag ("#" + i );
43+ recipeInputs [i ] = currentStack .isEmpty () ? ItemStack .EMPTY : new ItemStack (currentStack );
44+ }
45+
46+ if (!message .getOutput ().isEmpty ()) {
47+ recipeOutputs = new ItemStack [cct .getInventoryByName ("output" ).getSlots ()];
48+
49+ for (int i = 0 ; i < recipeOutputs .length ; ++i ) {
50+ NBTTagCompound currentStack = message .getOutput ().getCompoundTag ("O" + i );
51+ recipeOutputs [i ] = currentStack .isEmpty () ? ItemStack .EMPTY : new ItemStack (currentStack );
52+ }
53+ }
54+
55+ IGridNode node = cct .getNetworkNode ();
56+ if (node == null ) {
57+ return ;
58+ }
59+
60+ if (message .getInput () != null ) {
61+ for (int i = 0 ; i < craftMatrix .getSlots (); ++i ) {
62+ ItemStack currentItem = ItemStack .EMPTY ;
63+ if (recipeInputs [i ] != null ) {
64+ currentItem = recipeInputs [i ].copy ();
65+ }
66+
67+ ItemHandlerUtil .setStackInSlot (craftMatrix , i , currentItem );
68+ }
69+
70+ if (recipeOutputs != null && !message .getCraftingMode ()) {
71+ IItemHandler outputMatrix = cct .getInventoryByName ("output" );
72+
73+ for (int i = 0 ; i < outputMatrix .getSlots (); ++i ) {
74+ ItemStack currentItem = ItemStack .EMPTY ;
75+ if (recipeOutputs [i ] != null ) {
76+ currentItem = recipeOutputs [i ].copy ();
77+ }
78+
79+ ItemHandlerUtil .setStackInSlot (outputMatrix , i , currentItem );
80+ }
81+ }
82+
83+ container .onCraftMatrixChanged (new WrapperInvItemHandler (craftMatrix ));
84+ }
85+ }
86+
87+ });
88+ return null ;
89+ }
90+ }
0 commit comments