4444import org .cyclops .integratedterminals .api .terminalstorage .crafting .ITerminalCraftingOption ;
4545import org .cyclops .integratedterminals .api .terminalstorage .crafting .ITerminalStorageTabIngredientCraftingHandler ;
4646import org .cyclops .integratedterminals .capability .ingredient .IngredientComponentTerminalStorageHandlerConfig ;
47+ import org .cyclops .integratedterminals .core .terminalstorage .crafting .CraftingOptionDelta ;
4748import org .cyclops .integratedterminals .core .terminalstorage .crafting .HandlerWrappedTerminalCraftingOption ;
4849import org .cyclops .integratedterminals .core .terminalstorage .crafting .TerminalStorageTabIngredientCraftingHandlers ;
4950import org .cyclops .integratedterminals .network .packet .TerminalStorageIngredientChangeEventPacket ;
@@ -145,6 +146,8 @@ protected void initChannel(int channel) {
145146 // We assume that crafting options don't change that often,
146147 // so we don't have any observers that listen on recipe index changes.
147148 // Consequence is: players will have to re-open the terminal when they want to see recipe changes.
149+ // TODO: introduce a change listener system for the recipe index? Will become too expensive to do diff every tick...
150+ // TODO: No: only send it once to the client on init, and keep change index as future work if needed.
148151 List <HandlerWrappedTerminalCraftingOption <T >> channeledCraftingOptions = Lists .newArrayList ();
149152 for (ITerminalStorageTabIngredientCraftingHandler handler : TerminalStorageTabIngredientCraftingHandlers .REGISTRY .getHandlers ()) {
150153 Collection <ITerminalCraftingOption <T >> options = handler .getCraftingOptions (this , channel );
@@ -155,10 +158,11 @@ protected void initChannel(int channel) {
155158 }
156159 }
157160 this .craftingOptions .put (channel , channeledCraftingOptions );
158- boolean firstChannel = true ;
159- if (channeledCraftingOptions .size () > 0 ) {
160- this .sendCraftingOptionsToClient (channel , channeledCraftingOptions , false , firstChannel );
161- firstChannel = false ;
161+ if (!channeledCraftingOptions .isEmpty ()) {
162+ List <CraftingOptionDelta <T >> channeledCraftingDeltaOptions = channeledCraftingOptions .stream ()
163+ .map (option -> new CraftingOptionDelta <>(option , IIngredientComponentStorageObservable .Change .ADDITION ))
164+ .toList ();
165+ this .sendCraftingOptionsToClient (channel , channeledCraftingDeltaOptions );
162166 }
163167 }
164168
@@ -263,7 +267,6 @@ public void onChange(IIngredientComponentStorageObservable.StorageChangeEvent<T,
263267 }
264268
265269 protected void reApplyFilter (@ Nullable IIngredientComponentStorageObservable .StorageChangeEvent <T , M > event ) {
266- boolean firstChannel = true ;
267270 for (int channel : event == null ? this .unfilteredIngredientsViews .keySet () : Collections .singleton (event .getChannel ())) {
268271 Predicate <T > ingredientsFilter = getIngredientsFilter ();
269272 if (ingredientsFilter != null || event == null ) {
@@ -304,6 +307,7 @@ protected void reApplyFilter(@Nullable IIngredientComponentStorageObservable.Sto
304307 }
305308
306309 // Filter crafting options and re-send to client
310+ // TODO: don't do this!!! Use change events as well here!!!
307311 Collection <HandlerWrappedTerminalCraftingOption <T >> channeledCraftingOptions = this .craftingOptions .get (channel );
308312 if (channeledCraftingOptions != null ) {
309313 Collection <HandlerWrappedTerminalCraftingOption <T >> channeledCraftingOptionsFiltered ;
@@ -323,10 +327,8 @@ protected void reApplyFilter(@Nullable IIngredientComponentStorageObservable.Sto
323327 } else {
324328 channeledCraftingOptionsFiltered = channeledCraftingOptions ;
325329 }
326- this .sendCraftingOptionsToClient (channel , channeledCraftingOptionsFiltered , true , firstChannel );
330+ this .sendCraftingOptionsToClient (channel , channeledCraftingOptionsFiltered );
327331 }
328-
329- firstChannel = false ;
330332 }
331333
332334 initialized = true ;
@@ -375,37 +377,35 @@ protected void sendToClient(IIngredientComponentStorageObservable.StorageChangeE
375377 }
376378 }
377379
378- private void sendCraftingOptionsToClient (int channel , Collection <HandlerWrappedTerminalCraftingOption <T >> channeledCraftingOptions ,
379- boolean reset , boolean firstChannel ) {
380+ private void sendCraftingOptionsToClient (int channel , Collection <CraftingOptionDelta <T >> channeledCraftingOptions ) {
380381 // Only allow collection of a max given size to be sent in a packet
381382 if (channeledCraftingOptions .size () <= GeneralConfig .terminalStoragePacketMaxRecipes ) {
382383 IntegratedTerminals ._instance .getPacketHandler ().sendToPlayer (
383- new TerminalStorageIngredientCraftingOptionsPacket (this .getName ().toString (), channel , channeledCraftingOptions , reset , firstChannel ), player );
384+ new TerminalStorageIngredientCraftingOptionsPacket (this .getName ().toString (), channel , channeledCraftingOptions ), player );
384385 } else {
385- List <Pair < Boolean , List <HandlerWrappedTerminalCraftingOption < T > >>> chunks = Lists .newArrayList ();
386- List <HandlerWrappedTerminalCraftingOption <T >> buffer = Lists .newArrayListWithExpectedSize (GeneralConfig .terminalStoragePacketMaxRecipes );
386+ List <List <CraftingOptionDelta < T >>> chunks = Lists .newArrayList ();
387+ List <CraftingOptionDelta <T >> buffer = Lists .newArrayListWithExpectedSize (GeneralConfig .terminalStoragePacketMaxRecipes );
387388
388- for (HandlerWrappedTerminalCraftingOption <T > instance : channeledCraftingOptions ) {
389+ for (CraftingOptionDelta <T > instance : channeledCraftingOptions ) {
389390 buffer .add (instance );
390391
391392 // If our buffer reaches its capacity,
392393 // flush it, and create a new buffer
393394 if (buffer .size () == GeneralConfig .terminalStoragePacketMaxRecipes ) {
394- chunks .add (Pair .of (reset , buffer ));
395- reset = false ; // Only reset in first packet
395+ chunks .add (buffer );
396396 buffer = Lists .newArrayListWithExpectedSize (GeneralConfig .terminalStoragePacketMaxRecipes );
397397 }
398398 }
399399 // Our buffer can contain some remaining instances, make sure to flush them as well.
400400 if (!buffer .isEmpty ()) {
401- chunks .add (Pair . of ( reset , buffer ) );
401+ chunks .add (buffer );
402402 }
403403
404- for (Pair < Boolean , List <HandlerWrappedTerminalCraftingOption < T > >> chunk : chunks ) {
404+ for (List <CraftingOptionDelta < T >> chunk : chunks ) {
405405 if (GeneralConfig .packetSerializationEnableMultithreading ) {
406- PACKET_SERIALIZER .execute (() -> sendCraftingOptionsToClient (channel , chunk . getRight (), chunk . getLeft (), firstChannel ));
406+ PACKET_SERIALIZER .execute (() -> sendCraftingOptionsToClient (channel , chunk ));
407407 } else {
408- sendCraftingOptionsToClient (channel , chunk . getRight (), chunk . getLeft (), firstChannel );
408+ sendCraftingOptionsToClient (channel , chunk );
409409 }
410410 }
411411 }
0 commit comments