11package org .cyclops .integratedterminals .network .packet ;
22
33import com .google .common .collect .Lists ;
4+ import net .minecraft .client .Minecraft ;
45import net .minecraft .nbt .CompoundTag ;
56import net .minecraft .nbt .ListTag ;
67import net .minecraft .nbt .Tag ;
8+ import net .minecraft .resources .ResourceLocation ;
79import net .minecraft .server .level .ServerPlayer ;
810import net .minecraft .world .entity .player .Player ;
911import net .minecraft .world .level .Level ;
1315import org .cyclops .commoncapabilities .api .ingredient .IngredientComponent ;
1416import org .cyclops .cyclopscore .network .CodecField ;
1517import org .cyclops .cyclopscore .network .PacketCodec ;
18+ import org .cyclops .integratedterminals .GeneralConfig ;
1619import org .cyclops .integratedterminals .core .terminalstorage .TerminalStorageTabIngredientComponentClient ;
1720import org .cyclops .integratedterminals .core .terminalstorage .TerminalStorageTabIngredientComponentItemStackCrafting ;
1821import org .cyclops .integratedterminals .core .terminalstorage .crafting .HandlerWrappedTerminalCraftingOption ;
@@ -38,6 +41,8 @@ public class TerminalStorageIngredientCraftingOptionsPacket extends PacketCodec
3841 private boolean reset ;
3942 @ CodecField
4043 private boolean firstChannel ;
44+ @ CodecField
45+ private String ingredientComponentName ;
4146
4247 public TerminalStorageIngredientCraftingOptionsPacket () {
4348
@@ -47,7 +52,8 @@ public <T> TerminalStorageIngredientCraftingOptionsPacket(String tabId,
4752 int channel ,
4853 Collection <HandlerWrappedTerminalCraftingOption <T >> craftingOptions ,
4954 boolean reset ,
50- boolean firstChannel ) {
55+ boolean firstChannel ,
56+ IngredientComponent <?, ?> ingredientComponent ) {
5157 this .tabId = tabId ;
5258 this .channel = channel ;
5359 this .data = new CompoundTag ();
@@ -58,43 +64,46 @@ public <T> TerminalStorageIngredientCraftingOptionsPacket(String tabId,
5864 this .data .put ("craftingOptions" , list );
5965 this .reset = reset ;
6066 this .firstChannel = firstChannel ;
67+ this .ingredientComponentName = IngredientComponent .REGISTRY .getKey (ingredientComponent ).toString ();
6168 }
6269
6370 @ Override
6471 public boolean isAsync () {
65- return false ;
72+ return GeneralConfig . packetDeserializationEnableMultithreading ;
6673 }
6774
6875 @ Override
6976 @ OnlyIn (Dist .CLIENT )
7077 public void actionClient (Level world , Player player ) {
71- if (player .containerMenu instanceof ContainerTerminalStorageBase ) {
72- ContainerTerminalStorageBase container = ((ContainerTerminalStorageBase ) player .containerMenu );
73-
74-
75- TerminalStorageTabIngredientComponentClient <?, ?> tab = (TerminalStorageTabIngredientComponentClient <?, ?>) container .getTabClient (tabId );
76- IngredientComponent <?, ?> ingredientComponent = tab .getIngredientComponent ();
77-
78- ListTag list = this .data .getList ("craftingOptions" , Tag .TAG_COMPOUND );
79- List <HandlerWrappedTerminalCraftingOption <?>> craftingOptions = Lists .newArrayListWithExpectedSize (list .size ());
80- for (int i = 0 ; i < list .size (); i ++) {
81- HandlerWrappedTerminalCraftingOption <?> option = HandlerWrappedTerminalCraftingOption
82- .deserialize (ingredientComponent , list .getCompound (i ));
83- craftingOptions .add (option );
84- }
85-
86- tab .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
78+ IngredientComponent <?, ?> ingredientComponent = IngredientComponent .REGISTRY .getValue (new ResourceLocation (ingredientComponentName ));
79+ if (ingredientComponentName == null ) {
80+ throw new IllegalArgumentException ("Could not find the ingredient component type " + ingredientComponentName );
81+ }
82+ ListTag list = this .data .getList ("craftingOptions" , Tag .TAG_COMPOUND );
83+ List <HandlerWrappedTerminalCraftingOption <?>> craftingOptions = Lists .newArrayListWithExpectedSize (list .size ());
84+ for (int i = 0 ; i < list .size (); i ++) {
85+ HandlerWrappedTerminalCraftingOption <?> option = HandlerWrappedTerminalCraftingOption
86+ .deserialize (ingredientComponent , list .getCompound (i ));
87+ craftingOptions .add (option );
88+ }
8789
88- // Hard-coded crafting tab
89- // TODO: abstract this as "auxiliary" tabs
90- if (tabId .equals (IngredientComponents .ITEMSTACK .getName ().toString ())) {
91- TerminalStorageTabIngredientComponentClient <?, ?> tabCrafting = (TerminalStorageTabIngredientComponentClient <?, ?>) container
92- .getTabClient (TerminalStorageTabIngredientComponentItemStackCrafting .NAME .toString ());
93- tabCrafting .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
90+ // Run the following code in the render thread, since this packet runs in a different thread. (isAsync is true)
91+ Minecraft .getInstance ().execute (() -> {
92+ if (player .containerMenu instanceof ContainerTerminalStorageBase container ) {
93+ TerminalStorageTabIngredientComponentClient <?, ?> tab = (TerminalStorageTabIngredientComponentClient <?, ?>) container .getTabClient (tabId );
94+ tab .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
95+
96+ // Hard-coded crafting tab
97+ // TODO: abstract this as "auxiliary" tabs
98+ if (tabId .equals (IngredientComponents .ITEMSTACK .getName ().toString ())) {
99+ TerminalStorageTabIngredientComponentClient <?, ?> tabCrafting = (TerminalStorageTabIngredientComponentClient <?, ?>) container
100+ .getTabClient (TerminalStorageTabIngredientComponentItemStackCrafting .NAME .toString ());
101+ tabCrafting .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
102+ }
103+
104+ container .refreshChannelStrings ();
94105 }
95-
96- container .refreshChannelStrings ();
97- }
106+ });
98107 }
99108
100109 @ Override
0 commit comments