11package org .cyclops .integratedterminals .network .packet ;
22
33import com .google .common .collect .Lists ;
4+ import net .minecraft .client .Minecraft ;
45import net .minecraft .core .HolderLookup ;
56import net .minecraft .nbt .CompoundTag ;
67import net .minecraft .nbt .ListTag ;
1718import org .cyclops .commoncapabilities .api .ingredient .IngredientComponent ;
1819import org .cyclops .cyclopscore .network .CodecField ;
1920import org .cyclops .cyclopscore .network .PacketCodec ;
21+ import org .cyclops .integratedterminals .GeneralConfig ;
2022import org .cyclops .integratedterminals .Reference ;
2123import org .cyclops .integratedterminals .core .terminalstorage .TerminalStorageTabIngredientComponentClient ;
2224import org .cyclops .integratedterminals .core .terminalstorage .TerminalStorageTabIngredientComponentItemStackCrafting ;
@@ -46,6 +48,8 @@ public class TerminalStorageIngredientCraftingOptionsPacket extends PacketCodec<
4648 private boolean reset ;
4749 @ CodecField
4850 private boolean firstChannel ;
51+ @ CodecField
52+ private String ingredientComponentName ;
4953
5054 public TerminalStorageIngredientCraftingOptionsPacket () {
5155 super (ID );
@@ -56,7 +60,8 @@ public <T> TerminalStorageIngredientCraftingOptionsPacket(HolderLookup.Provider
5660 int channel ,
5761 Collection <HandlerWrappedTerminalCraftingOption <T >> craftingOptions ,
5862 boolean reset ,
59- boolean firstChannel ) {
63+ boolean firstChannel ,
64+ IngredientComponent <?, ?> ingredientComponent ) {
6065 super (ID );
6166 this .tabId = tabId ;
6267 this .channel = channel ;
@@ -68,43 +73,46 @@ public <T> TerminalStorageIngredientCraftingOptionsPacket(HolderLookup.Provider
6873 this .data .put ("craftingOptions" , list );
6974 this .reset = reset ;
7075 this .firstChannel = firstChannel ;
76+ this .ingredientComponentName = IngredientComponent .REGISTRY .getKey (ingredientComponent ).toString ();
7177 }
7278
7379 @ Override
7480 public boolean isAsync () {
75- return false ;
81+ return GeneralConfig . packetDeserializationEnableMultithreading ;
7682 }
7783
7884 @ Override
7985 @ OnlyIn (Dist .CLIENT )
8086 public void actionClient (Level world , Player player ) {
81- if (player .containerMenu instanceof ContainerTerminalStorageBase ) {
82- ContainerTerminalStorageBase container = ((ContainerTerminalStorageBase ) player .containerMenu );
83-
84-
85- TerminalStorageTabIngredientComponentClient <?, ?> tab = (TerminalStorageTabIngredientComponentClient <?, ?>) container .getTabClient (tabId );
86- IngredientComponent <?, ?> ingredientComponent = tab .getIngredientComponent ();
87-
88- ListTag list = this .data .getList ("craftingOptions" , Tag .TAG_COMPOUND );
89- List <HandlerWrappedTerminalCraftingOption <?>> craftingOptions = Lists .newArrayListWithExpectedSize (list .size ());
90- for (int i = 0 ; i < list .size (); i ++) {
91- HandlerWrappedTerminalCraftingOption <?> option = HandlerWrappedTerminalCraftingOption
92- .deserialize (world .registryAccess (), ingredientComponent , list .getCompound (i ));
93- craftingOptions .add (option );
94- }
95-
96- tab .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
87+ IngredientComponent <?, ?> ingredientComponent = IngredientComponent .REGISTRY .get (ResourceLocation .parse (ingredientComponentName ));
88+ if (ingredientComponentName == null ) {
89+ throw new IllegalArgumentException ("Could not find the ingredient component type " + ingredientComponentName );
90+ }
91+ ListTag list = this .data .getList ("craftingOptions" , Tag .TAG_COMPOUND );
92+ List <HandlerWrappedTerminalCraftingOption <?>> craftingOptions = Lists .newArrayListWithExpectedSize (list .size ());
93+ for (int i = 0 ; i < list .size (); i ++) {
94+ HandlerWrappedTerminalCraftingOption <?> option = HandlerWrappedTerminalCraftingOption
95+ .deserialize (world .registryAccess (), ingredientComponent , list .getCompound (i ));
96+ craftingOptions .add (option );
97+ }
9798
98- // Hard-coded crafting tab
99- // TODO: abstract this as "auxiliary" tabs
100- if (tabId .equals (IngredientComponents .ITEMSTACK .getName ().toString ())) {
101- TerminalStorageTabIngredientComponentClient <?, ?> tabCrafting = (TerminalStorageTabIngredientComponentClient <?, ?>) container
102- .getTabClient (TerminalStorageTabIngredientComponentItemStackCrafting .NAME .toString ());
103- tabCrafting .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
99+ // Run the following code in the render thread, since this packet runs in a different thread. (isAsync is true)
100+ Minecraft .getInstance ().execute (() -> {
101+ if (player .containerMenu instanceof ContainerTerminalStorageBase container ) {
102+ TerminalStorageTabIngredientComponentClient <?, ?> tab = (TerminalStorageTabIngredientComponentClient <?, ?>) container .getTabClient (tabId );
103+ tab .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
104+
105+ // Hard-coded crafting tab
106+ // TODO: abstract this as "auxiliary" tabs
107+ if (tabId .equals (IngredientComponents .ITEMSTACK .getName ().toString ())) {
108+ TerminalStorageTabIngredientComponentClient <?, ?> tabCrafting = (TerminalStorageTabIngredientComponentClient <?, ?>) container
109+ .getTabClient (TerminalStorageTabIngredientComponentItemStackCrafting .NAME .toString ());
110+ tabCrafting .addCraftingOptions (channel , (List ) craftingOptions , this .reset , this .firstChannel );
111+ }
112+
113+ container .refreshChannelStrings ();
104114 }
105-
106- container .refreshChannelStrings ();
107- }
115+ });
108116 }
109117
110118 @ Override
0 commit comments