Skip to content

Commit

Permalink
Fix crash on world reload when an EBF is running
Browse files Browse the repository at this point in the history
Fixes #12

Minor code cleanup
  • Loading branch information
Exaxxion committed Jun 9, 2024
1 parent f75b52c commit e969818
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import net.minecraftforge.items.IItemHandler;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -74,7 +75,7 @@ public MultiblockRecipeLogic getRecipeMapWorkable() {
* Performs extra checks for validity of given recipe before multiblock
* will start it's processing.
*/
public boolean checkRecipe(Recipe recipe, boolean consumeIfSuccess) {
public boolean checkRecipe(@Nullable Recipe recipe, boolean consumeIfSuccess) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@
import gregtech.common.blocks.BlockWireCoil.CoilType;
import gregtech.common.blocks.MetaBlocks;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.TextComponentTranslation;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nonnull;
import java.util.List;
Expand All @@ -36,7 +38,10 @@ public class MetaTileEntityElectricBlastFurnace extends RecipeMapMultiblockContr
MultiblockAbility.INPUT_ENERGY
};

/** The current blast furnace temperature */
private int blastFurnaceTemperature;
/** The temperature required by the actively running recipe */
private int recipeTemperature;

public MetaTileEntityElectricBlastFurnace(ResourceLocation metaTileEntityId) {
super(metaTileEntityId, RecipeMaps.BLAST_RECIPES);
Expand Down Expand Up @@ -68,16 +73,29 @@ public void invalidateStructure() {
}

@Override
public boolean checkRecipe(Recipe recipe, boolean consumeIfSuccess) {
return this.blastFurnaceTemperature >= recipe.getProperty(BlastTemperatureProperty.getInstance(),0);
public boolean checkRecipe(@Nullable Recipe recipe, boolean consumeIfSuccess) {
// Recipe will be null during Jam checks when a world has been reloaded after starting a Recipe.
int requiredTemperature = (recipe == null)
// fall back to the recorded value (which will be 0 for any Recipe started before this change)
? recipeTemperature
// otherwise, get it as normal from the Recipe
: recipe.getProperty(BlastTemperatureProperty.getInstance(), 0);

// Verify sufficient coil heat
if (this.blastFurnaceTemperature >= requiredTemperature) {
// If this is a new recipe, record its temperature
if (consumeIfSuccess)
recipeTemperature = requiredTemperature;
return true;
}
return false;
}

public static Predicate<BlockWorldState> heatingCoilPredicate() {
return blockWorldState -> {
IBlockState blockState = blockWorldState.getBlockState();
if (!(blockState.getBlock() instanceof BlockWireCoil))
if (!(blockState.getBlock() instanceof BlockWireCoil blockWireCoil))
return false;
BlockWireCoil blockWireCoil = (BlockWireCoil) blockState.getBlock();
CoilType coilType = blockWireCoil.getState(blockState);
CoilType currentCoilType = blockWorldState.getMatchContext().getOrPut("CoilType", coilType);
return currentCoilType.getName().equals(coilType.getName());
Expand Down Expand Up @@ -113,4 +131,17 @@ public ICubeRenderer getBaseTexture(IMultiblockPart sourcePart) {
protected OrientedOverlayRenderer getFrontOverlay() {
return Textures.BLAST_FURNACE_OVERLAY;
}

@Override
public NBTTagCompound writeToNBT(NBTTagCompound data) {
if (getRecipeMapWorkable().isActive())
data.setInteger("RecipeTemp", recipeTemperature);
return super.writeToNBT(data);
}

@Override
public void readFromNBT(NBTTagCompound data) {
recipeTemperature = data.getInteger("RecipeTemp");
super.readFromNBT(data);
}
}

0 comments on commit e969818

Please sign in to comment.