Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master-1.19-lts' into master-1.2…
Browse files Browse the repository at this point in the history
…0-lts
  • Loading branch information
rubensworks committed Aug 21, 2024
2 parents 4a8ef8c + d6f8cba commit c84cf94
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 17 deletions.
7 changes: 7 additions & 0 deletions resources/changelog/1.19.2-1.1.7.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
As always, don't forget to backup your world before updating!
Requires CyclopsCore version 1.17.0 or higher.

Fixes:
* Fix dynamic recipes in crafting interfaces broken after reload
Related to CyclopsMC/IntegratedCrafting#110

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntArraySet;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag;
Expand Down Expand Up @@ -160,7 +161,7 @@ protected PartTypeInterfaceCrafting.State constructDefaultState() {
@Override
public void afterNetworkReAlive(INetwork network, IPartNetwork partNetwork, PartTarget target, PartTypeInterfaceCrafting.State state) {
super.afterNetworkReAlive(network, partNetwork, target, state);
addTargetToNetwork(network, target, state);
addTargetToNetwork(network, target, state, true);
}

@Override
Expand All @@ -172,7 +173,7 @@ public void onNetworkRemoval(INetwork network, IPartNetwork partNetwork, PartTar
@Override
public void onNetworkAddition(INetwork network, IPartNetwork partNetwork, PartTarget target, PartTypeInterfaceCrafting.State state) {
super.onNetworkAddition(network, partNetwork, target, state);
addTargetToNetwork(network, target, state);
addTargetToNetwork(network, target, state, true);
}

@Override
Expand All @@ -181,19 +182,19 @@ public void setPriorityAndChannel(INetwork network, IPartNetwork partNetwork, Pa
// so we have to re-add it.
removeTargetFromNetwork(network, target.getTarget(), state);
super.setPriorityAndChannel(network, partNetwork, target, state, priority, channel);
addTargetToNetwork(network, target, state);
addTargetToNetwork(network, target, state, false);
}

protected Capability<ICraftingNetwork> getNetworkCapability() {
return CraftingNetworkConfig.CAPABILITY;
}

protected void addTargetToNetwork(INetwork network, PartTarget pos, PartTypeInterfaceCrafting.State state) {
protected void addTargetToNetwork(INetwork network, PartTarget pos, PartTypeInterfaceCrafting.State state, boolean initialize) {
network.getCapability(getNetworkCapability())
.ifPresent(craftingNetwork -> {
int channelCrafting = state.getChannelCrafting();
state.setTarget(pos);
state.setNetworks(network, craftingNetwork, NetworkHelpers.getPartNetworkChecked(network), channelCrafting, ValueDeseralizationContext.of(pos.getCenter().getPos().getLevel(true)));
state.setNetworks(network, craftingNetwork, NetworkHelpers.getPartNetworkChecked(network), channelCrafting, ValueDeseralizationContext.of(pos.getCenter().getPos().getLevel(true)), initialize);
state.setShouldAddToCraftingNetwork(true);
});
}
Expand All @@ -204,7 +205,7 @@ protected void removeTargetFromNetwork(INetwork network, PartPos pos, PartTypeIn
network.getCapability(getNetworkCapability())
.ifPresent(n -> n.removeCraftingInterface(state.getChannelCrafting(), state));
}
state.setNetworks(null, null, null, -1, null);
state.setNetworks(null, null, null, -1, null, false);
state.setTarget(null);
}

Expand All @@ -225,7 +226,7 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target
// Init network data in part state if it has not been done yet.
// This can occur when the part chunk is being reloaded.
if (state.getCraftingNetwork() == null) {
addTargetToNetwork(network, target, state);
addTargetToNetwork(network, target, state, false);
}

int channel = state.getChannelCrafting();
Expand All @@ -252,7 +253,9 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target
if (!slots.isEmpty()) {
ICraftingNetwork craftingNetwork = network.getCapability(getNetworkCapability()).orElse(null);
if (craftingNetwork != null) {
for (Integer slot : slots) {
IntSet slotsCopy = new IntOpenHashSet(slots); // Create a copy, to allow insertion into slots during this loop
slots.clear();
for (Integer slot : slotsCopy) {
// Remove the old recipe from the network
Int2ObjectMap<IRecipeDefinition> recipes = state.getRecipesIndexed();
IRecipeDefinition oldRecipe = recipes.get(slot);
Expand All @@ -261,7 +264,10 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target
}

// Reload the recipe in the slot
state.reloadRecipe(slot);
// We simulate initialization for the first two ticks, as dependency variables may still be loading,
// and errored may only go away after these dependencies are fully loaded.
// Related to CyclopsMC/IntegratedCrafting#110
state.reloadRecipe(slot, state.ticksAfterReload <= 1);

// Add the new recipe to the network
IRecipeDefinition newRecipe = recipes.get(slot);
Expand All @@ -270,8 +276,10 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target
}
}
}
slots.clear();
}

// Internal tick counter
state.ticksAfterReload++;
}

@Nullable
Expand Down Expand Up @@ -318,6 +326,8 @@ public void addDrops(PartTarget target, State state, List<ItemStack> itemStacks,
public static class State extends PartStateBase<PartTypeInterfaceCrafting>
implements ICraftingInterface, ICraftingResultsSink {

protected int ticksAfterReload = 0;

private final CraftingJobHandler craftingJobHandler;
private final SimpleInventory inventoryVariables;
private final List<InventoryVariableEvaluator<ValueObjectTypeRecipe.ValueRecipe>> variableEvaluators;
Expand Down Expand Up @@ -453,7 +463,7 @@ public int getChannelCrafting() {
return channelCrafting;
}

public void reloadRecipes() {
public void reloadRecipes(boolean initialize) {
this.currentRecipes.clear();
this.recipeSlotMessages.clear();
this.recipeSlotValidated.clear();
Expand All @@ -471,7 +481,7 @@ public void onErrorsChanged() {
}
if (this.partNetwork != null) {
for (int i = 0; i < getInventoryVariables().getContainerSize(); i++) {
reloadRecipe(i);
reloadRecipe(i, initialize);
}
}
}
Expand All @@ -486,7 +496,7 @@ private void setLocalErrors(int slot, List<MutableComponent> errors) {
}
}

protected void reloadRecipe(int slot) {
protected void reloadRecipe(int slot, boolean initialize) {
this.currentRecipes.remove(slot);
if (this.recipeSlotMessages.size() > slot) {
this.recipeSlotMessages.remove(slot);
Expand Down Expand Up @@ -530,7 +540,13 @@ protected void reloadRecipe(int slot) {
this.recipeSlotMessages.put(slot, e.getErrorMessage());
}
} else {
this.recipeSlotMessages.put(slot, Component.translatable("gui.integratedcrafting.partinterface.slot.message.norecipe"));
// If we're initializing, the variable might be referencing other variables that are not yet loaded.
// So let's retry once in the next tick.
if (initialize && evaluator.hasVariable()) {
this.delayedReloadRecipe(slot);
} else {
this.recipeSlotMessages.put(slot, Component.translatable("gui.integratedcrafting.partinterface.slot.message.norecipe"));
}
}

try {
Expand Down Expand Up @@ -596,7 +612,7 @@ public void onDirty() {

// Recalculate recipes
if (getTarget() != null && !getTarget().getCenter().getPos().getLevel(true).isClientSide) {
reloadRecipes();
reloadRecipes(false);
}

// Re-register to the network, to force an update for all new recipes
Expand All @@ -615,13 +631,14 @@ public PartTarget getTarget() {

public void setNetworks(@Nullable INetwork network, @Nullable ICraftingNetwork craftingNetwork,
@Nullable IPartNetwork partNetwork, int channel,
@Nullable ValueDeseralizationContext valueDeseralizationContext) {
@Nullable ValueDeseralizationContext valueDeseralizationContext,
boolean initialize) {
this.network = network;
this.craftingNetwork = craftingNetwork;
this.partNetwork = partNetwork;
this.channel = channel;
this.valueDeseralizationContext = valueDeseralizationContext;
reloadRecipes();
reloadRecipes(initialize);
if (network != null) {
this.getCraftingJobHandler().reRegisterObservers(network);
}
Expand Down

0 comments on commit c84cf94

Please sign in to comment.