Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master-1.20-lts' into master-1.21
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Aug 21, 2024
2 parents a94f1f5 + 2285d21 commit ca64cc7
Show file tree
Hide file tree
Showing 3 changed files with 48 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

7 changes: 7 additions & 0 deletions resources/changelog/1.20.1-1.1.8.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.18.4 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 @@ -155,7 +156,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 @@ -167,7 +168,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 @@ -176,19 +177,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 NetworkCapability<ICraftingNetwork> getNetworkCapability() {
return Capabilities.CraftingNetwork.NETWORK;
}

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 @@ -199,7 +200,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 @@ -220,7 +221,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 @@ -247,7 +248,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 @@ -256,7 +259,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 @@ -265,8 +271,10 @@ public void update(INetwork network, IPartNetwork partNetwork, PartTarget target
}
}
}
slots.clear();
}

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

@Nullable
Expand Down Expand Up @@ -313,6 +321,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 @@ -448,7 +458,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 @@ -466,7 +476,7 @@ public void onErrorsChanged() {
}
if (this.partNetwork != null) {
for (int i = 0; i < getInventoryVariables().getContainerSize(); i++) {
reloadRecipe(i);
reloadRecipe(i, initialize);
}
}
}
Expand All @@ -481,7 +491,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 @@ -525,7 +535,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 @@ -585,7 +601,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 @@ -604,13 +620,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 ca64cc7

Please sign in to comment.