Skip to content

Commit 1c2a668

Browse files
committed
Merge remote-tracking branch 'origin/master-1.20-lts' into master-1.21-lts
2 parents a6d64aa + cb2eb9e commit 1c2a668

4 files changed

Lines changed: 58 additions & 44 deletions

File tree

src/main/java/org/cyclops/integratedterminals/GeneralConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public class GeneralConfig extends DummyConfig {
5656
public static boolean craftingPlannerEnableMultithreading = false;
5757
@ConfigurableProperty(category = "core", comment = "If client-directed packets should be serialized in a separate thread.", isCommandable = true, configLocation = ModConfig.Type.SERVER)
5858
public static boolean packetSerializationEnableMultithreading = true;
59+
@ConfigurableProperty(category = "core", comment = "If client-received packets should be deserialized in a separate thread.", isCommandable = true, configLocation = ModConfig.Type.CLIENT)
60+
public static boolean packetDeserializationEnableMultithreading = true;
5961

6062
@ConfigurableProperty(category = "general", comment = "The base energy usage for the crafting terminal.", minimalValue = 0, configLocation = ModConfig.Type.SERVER)
6163
public static int terminalCraftingBaseConsumption = 1;

src/main/java/org/cyclops/integratedterminals/core/terminalstorage/TerminalStorageTabIngredientComponentServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ private void sendCraftingOptionsToClient(int channel, Collection<HandlerWrappedT
384384
// Only allow collection of a max given size to be sent in a packet
385385
if (channeledCraftingOptions.size() <= GeneralConfig.terminalStoragePacketMaxRecipes) {
386386
IntegratedTerminals._instance.getPacketHandler().sendToPlayer(
387-
new TerminalStorageIngredientCraftingOptionsPacket(player.level().registryAccess(), this.getName().toString(), channel, channeledCraftingOptions, reset, firstChannel), player);
387+
new TerminalStorageIngredientCraftingOptionsPacket(player.level().registryAccess(), this.getName().toString(), channel, channeledCraftingOptions, reset, firstChannel, ingredientComponent), player);
388388
} else {
389389
List<Pair<Boolean, List<HandlerWrappedTerminalCraftingOption<T>>>> chunks = Lists.newArrayList();
390390
List<HandlerWrappedTerminalCraftingOption<T>> buffer = Lists.newArrayListWithExpectedSize(GeneralConfig.terminalStoragePacketMaxRecipes);

src/main/java/org/cyclops/integratedterminals/network/packet/TerminalStorageIngredientChangeEventPacket.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.cyclops.integratedterminals.network.packet;
22

3+
import net.minecraft.client.Minecraft;
34
import net.minecraft.core.HolderLookup;
45
import net.minecraft.nbt.CompoundTag;
56
import net.minecraft.network.RegistryFriendlyByteBuf;
@@ -17,6 +18,7 @@
1718
import org.cyclops.cyclopscore.network.CodecField;
1819
import org.cyclops.cyclopscore.network.PacketCodec;
1920
import org.cyclops.integrateddynamics.api.ingredient.IIngredientComponentStorageObservable;
21+
import org.cyclops.integratedterminals.GeneralConfig;
2022
import org.cyclops.integratedterminals.Reference;
2123
import org.cyclops.integratedterminals.core.terminalstorage.TerminalStorageTabIngredientComponentClient;
2224
import org.cyclops.integratedterminals.core.terminalstorage.TerminalStorageTabIngredientComponentItemStackCrafting;
@@ -61,30 +63,32 @@ public TerminalStorageIngredientChangeEventPacket(HolderLookup.Provider lookupPr
6163

6264
@Override
6365
public boolean isAsync() {
64-
return false;
66+
return GeneralConfig.packetDeserializationEnableMultithreading;
6567
}
6668

6769
@Override
6870
@OnlyIn(Dist.CLIENT)
6971
public void actionClient(Level world, Player player) {
70-
if(player.containerMenu instanceof ContainerTerminalStorageBase) {
71-
ContainerTerminalStorageBase container = ((ContainerTerminalStorageBase) player.containerMenu);
72-
IIngredientComponentStorageObservable.Change changeType = IIngredientComponentStorageObservable.Change.values()[changeData.getInt("changeType")];
73-
IngredientArrayList ingredients = IngredientCollections.deserialize(world.registryAccess(), changeData);
72+
IIngredientComponentStorageObservable.Change changeType = IIngredientComponentStorageObservable.Change.values()[changeData.getInt("changeType")];
73+
IngredientArrayList ingredients = IngredientCollections.deserialize(world.registryAccess(), changeData);
7474

75-
TerminalStorageTabIngredientComponentClient<?, ?> tab = (TerminalStorageTabIngredientComponentClient<?, ?>) container.getTabClient(tabId);
76-
tab.onChange(channel, changeType, ingredients, enabled);
75+
// Run the following code in the render thread, since this packet runs in a different thread. (isAsync is true)
76+
Minecraft.getInstance().execute(() -> {
77+
if(player.containerMenu instanceof ContainerTerminalStorageBase container) {
78+
TerminalStorageTabIngredientComponentClient<?, ?> tab = (TerminalStorageTabIngredientComponentClient<?, ?>) container.getTabClient(tabId);
79+
tab.onChange(channel, changeType, ingredients, enabled);
7780

78-
// Hard-coded crafting tab
79-
// TODO: abstract this as "auxiliary" tabs
80-
if (tabId.equals(IngredientComponents.ITEMSTACK.getName().toString())) {
81-
TerminalStorageTabIngredientComponentClient<?, ?> tabCrafting = (TerminalStorageTabIngredientComponentClient<?, ?>) container
82-
.getTabClient(TerminalStorageTabIngredientComponentItemStackCrafting.NAME.toString());
83-
tabCrafting.onChange(channel, changeType, ingredients, enabled);
84-
}
81+
// Hard-coded crafting tab
82+
// TODO: abstract this as "auxiliary" tabs
83+
if (tabId.equals(IngredientComponents.ITEMSTACK.getName().toString())) {
84+
TerminalStorageTabIngredientComponentClient<?, ?> tabCrafting = (TerminalStorageTabIngredientComponentClient<?, ?>) container
85+
.getTabClient(TerminalStorageTabIngredientComponentItemStackCrafting.NAME.toString());
86+
tabCrafting.onChange(channel, changeType, ingredients, enabled);
87+
}
8588

86-
container.refreshChannelStrings();
87-
}
89+
container.refreshChannelStrings();
90+
}
91+
});
8892
}
8993

9094
@Override

src/main/java/org/cyclops/integratedterminals/network/packet/TerminalStorageIngredientCraftingOptionsPacket.java

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.cyclops.integratedterminals.network.packet;
22

33
import com.google.common.collect.Lists;
4+
import net.minecraft.client.Minecraft;
45
import net.minecraft.core.HolderLookup;
56
import net.minecraft.nbt.CompoundTag;
67
import net.minecraft.nbt.ListTag;
@@ -17,6 +18,7 @@
1718
import org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;
1819
import org.cyclops.cyclopscore.network.CodecField;
1920
import org.cyclops.cyclopscore.network.PacketCodec;
21+
import org.cyclops.integratedterminals.GeneralConfig;
2022
import org.cyclops.integratedterminals.Reference;
2123
import org.cyclops.integratedterminals.core.terminalstorage.TerminalStorageTabIngredientComponentClient;
2224
import 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

Comments
 (0)