Skip to content

Commit

Permalink
fix: disable random recipe registering for most fast machines
Browse files Browse the repository at this point in the history
  • Loading branch information
ybw0014 committed Jun 1, 2024
1 parent 81cd2ca commit c0097f4
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void registerRecipes() {
RawRecipe rawRecipe = new RawRecipe(recipe.getKey(), new ItemStack[] {recipe.getValue()});
rawRecipes.add(rawRecipe);
}
RecipeUtils.registerRecipes(recipes, rawRecipes);
RecipeUtils.registerRecipes(recipes, rawRecipes, false);
} catch (Exception ex) {
FastMachines.log(Level.SEVERE, ex, "An error has occurred while registering recipes for {0}", getClass().getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public FastSlimeFrameFoundry(SlimefunItemStack item, RecipeType recipeType, Item
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SFrameStacks.FOUNDRY.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SFrameStacks.FOUNDRY.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void registerRecipes() {
var altar = (AncientAltar) SlimefunItems.ANCIENT_ALTAR.getItem();
List<RawRecipe> rawRecipes = new ArrayList<>();
for (var recipe : altar.getRecipes()) {
// TODO: figure out the proper way to handle spawners
if (recipe.getOutput().getType() == Material.SPAWNER) {
continue;
}
Expand All @@ -42,7 +41,7 @@ public void registerRecipes() {
RawRecipe rawRecipe = new RawRecipe(input.toArray(new ItemStack[9]), new ItemStack[] {recipe.getOutput()});
rawRecipes.add(rawRecipe);
}
RecipeUtils.registerRecipes(recipes, rawRecipes);
RecipeUtils.registerRecipes(recipes, rawRecipes, false);
} catch (Exception ex) {
FastMachines.log(Level.SEVERE, ex, "An error has occurred while registering recipes for {0}", getClass().getSimpleName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastArmorForge(SlimefunItemStack item, RecipeType recipeType, ItemStack[]
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ARMOR_FORGE.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ARMOR_FORGE.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastComposter(SlimefunItemStack item, RecipeType recipeType, ItemStack[]
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerDisplayRecipes(recipes, SlimefunItems.COMPOSTER.getItemId());
RecipeUtils.registerDisplayRecipes(recipes, SlimefunItems.COMPOSTER.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastCompressor(SlimefunItemStack item, RecipeType recipeType, ItemStack[]
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.COMPRESSOR.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.COMPRESSOR.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastEnhancedCraftingTable(SlimefunItemStack item, RecipeType recipeType,
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ENHANCED_CRAFTING_TABLE.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ENHANCED_CRAFTING_TABLE.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastGrindStone(SlimefunItemStack item, RecipeType recipeType, ItemStack[]
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.GRIND_STONE.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.GRIND_STONE.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastJuicer(SlimefunItemStack item, RecipeType recipeType, ItemStack[] rec
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.JUICER.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.JUICER.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastMagicWorkbench(SlimefunItemStack item, RecipeType recipeType, ItemSta
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.MAGIC_WORKBENCH.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.MAGIC_WORKBENCH.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastOreCrusher(SlimefunItemStack item, RecipeType recipeType, ItemStack[]
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ORE_CRUSHER.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ORE_CRUSHER.getItemId(), true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastOreWasher(SlimefunItemStack item, RecipeType recipeType, ItemStack[]
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ORE_WASHER.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.ORE_WASHER.getItemId(), true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastPanningMachine(SlimefunItemStack item, RecipeType recipeType, ItemSta
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerDisplayRecipes(recipes, SlimefunItems.AUTOMATED_PANNING_MACHINE.getItemId());
RecipeUtils.registerDisplayRecipes(recipes, SlimefunItems.AUTOMATED_PANNING_MACHINE.getItemId(), true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastPressureChamber(SlimefunItemStack item, RecipeType recipeType, ItemSt
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.PRESSURE_CHAMBER.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.PRESSURE_CHAMBER.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastSmeltery(SlimefunItemStack item, RecipeType recipeType, ItemStack[] r
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.SMELTERY.getItemId());
RecipeUtils.registerMultiblockMachineRecipes(recipes, SlimefunItems.SMELTERY.getItemId(), false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public FastTableSaw(SlimefunItemStack item, RecipeType recipeType, ItemStack[] r
@Override
public void registerRecipes() {
FastMachines.debug("Registering recipes for {0}", getClass().getSimpleName());
RecipeUtils.registerDisplayRecipes(recipes, SlimefunItems.TABLE_SAW.getItemId());
RecipeUtils.registerDisplayRecipes(recipes, SlimefunItems.TABLE_SAW.getItemId(), false);
}

@Override
Expand Down
87 changes: 51 additions & 36 deletions src/main/java/net/guizhanss/fastmachines/utils/RecipeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,12 @@ private static boolean appendRandomRecipe(List<IRecipe> recipes, ItemStack input
/**
* Register recipes.
*
* @param recipes The {@link List} that stores {@link IRecipe}s.
* @param pendingRecipes The {@link List} of recipes that need to be registered. The first element in pair is input, and the second
* element in pair is output.
* @param recipes The {@link List} that stores {@link IRecipe}s.
* @param pendingRecipes The {@link List} of recipes that need to be registered. The first element in pair is input, and the second
* element in pair is output.
* @param enableRandomRecipes Whether to enable random recipes.
*/
public static void registerRecipes(List<IRecipe> recipes, List<RawRecipe> pendingRecipes) {
public static void registerRecipes(List<IRecipe> recipes, List<RawRecipe> pendingRecipes, boolean enableRandomRecipes) {
ItemStack[] lastInput = new ItemStack[0];
List<ItemStack> storedOutput = new ArrayList<>();

Expand Down Expand Up @@ -182,40 +183,54 @@ public static void registerRecipes(List<IRecipe> recipes, List<RawRecipe> pendin
FastMachines.debug("processing raw recipe: input={0}, output={1}",
Arrays.toString(input), Arrays.toString(output));

if (Arrays.equals(input, lastInput, ITEM_COMPARATOR)) {
// this recipe still belongs to a random recipe, add current output to stored items
FastMachines.debug("input matches last recipe, adding to stored output");
storedOutput.add(output[0]);
} else if (storedOutput.isEmpty()) {
// initial state, store it
FastMachines.debug("empty output, storing current recipe");
lastInput = input;
storedOutput.add(output[0]);
if (enableRandomRecipes) {
if (Arrays.equals(input, lastInput, ITEM_COMPARATOR)) {
// this recipe still belongs to a random recipe, add current output to stored items
FastMachines.debug("input matches last recipe, adding to stored output");
storedOutput.add(output[0]);
} else if (storedOutput.isEmpty()) {
// initial state, store it
FastMachines.debug("empty output, storing current recipe");
lastInput = input;
storedOutput.add(output[0]);
} else {
// current input is different with stored input
registerRecipeHelper(recipes, lastInput, storedOutput);

// clear store, put current one in
lastInput = input;
storedOutput.clear();
storedOutput.add(output[0]);
}
} else {
// current input is different with stored input
registerRecipeHelper(recipes, lastInput, storedOutput);

// clear store, put current one in
lastInput = input;
storedOutput.clear();
storedOutput.add(output[0]);
// no random recipes enabled, register each recipe directly
IRecipe iRecipe = new StandardRecipe(output[0], input);
FastMachines.debug("registering standard recipe: {0}", iRecipe);
recipes.add(iRecipe);
}
}

// the last recipe is not processed yet.
registerRecipeHelper(recipes, lastInput, storedOutput);
if (enableRandomRecipes) {
// the last recipe is not processed yet.
registerRecipeHelper(recipes, lastInput, storedOutput);
}
}

private static void registerRecipeHelper(List<IRecipe> recipes, ItemStack[] lastInput, List<ItemStack> storedOutput) {
var firstValidInput = Arrays.stream(lastInput).filter(Objects::nonNull).findFirst();
if (firstValidInput.isEmpty()) {
FastMachines.log(Level.WARNING, "Unexpected empty input, ignoring");
var validInputs = Arrays.stream(lastInput).filter(Objects::nonNull).toList();
if (validInputs.isEmpty()) {
FastMachines.log(Level.WARNING, "Unexpected empty input");
return;
}
if (validInputs.size() > 1) {
IRecipe iRecipe = new StandardRecipe(storedOutput.get(0), lastInput);
FastMachines.debug("registering standard recipe: {0}", iRecipe);
return;
}
if (!appendRandomRecipe(recipes, firstValidInput.get(), storedOutput)) {
if (!appendRandomRecipe(recipes, validInputs.get(0), storedOutput)) {
IRecipe iRecipe;
if (storedOutput.size() > 1) {
iRecipe = new RandomRecipe(firstValidInput.get(), storedOutput);
iRecipe = new RandomRecipe(validInputs.get(0), storedOutput);
FastMachines.debug("registering random recipe: {0}", iRecipe);
} else {
iRecipe = new StandardRecipe(storedOutput.get(0), lastInput);
Expand All @@ -232,7 +247,7 @@ private static void registerRecipeHelper(List<IRecipe> recipes, ItemStack[] last
* @param id The id of the {@link MultiBlockMachine}.
*/
@ParametersAreNonnullByDefault
public static void registerMultiblockMachineRecipes(List<IRecipe> recipes, String id) {
public static void registerMultiblockMachineRecipes(List<IRecipe> recipes, String id, boolean enableRandomRecipes) {
Preconditions.checkArgument(recipes != null, MSG_RECIPE_NULL);
Preconditions.checkArgument(id != null, MSG_ID_NULL);

Expand All @@ -258,7 +273,7 @@ public static void registerMultiblockMachineRecipes(List<IRecipe> recipes, Strin
ItemStack[] output = recipeList.get(i + 1);
pendingRecipes.add(new RawRecipe(input, output));
}
registerRecipes(recipes, pendingRecipes);
registerRecipes(recipes, pendingRecipes, enableRandomRecipes);
}

/**
Expand All @@ -268,7 +283,7 @@ public static void registerMultiblockMachineRecipes(List<IRecipe> recipes, Strin
* @param id The id of the {@link MultiBlockMachine}.
*/
@ParametersAreNonnullByDefault
public static void registerMachineRecipes(List<IRecipe> recipes, String id) {
public static void registerMachineRecipes(List<IRecipe> recipes, String id, boolean enableRandomRecipes) {
Preconditions.checkArgument(recipes != null, MSG_RECIPE_NULL);
Preconditions.checkArgument(id != null, MSG_ID_NULL);

Expand All @@ -295,19 +310,19 @@ public static void registerMachineRecipes(List<IRecipe> recipes, String id) {
pendingRecipes.add(new RawRecipe(input, output));
}
}
registerRecipes(recipes, pendingRecipes);
registerRecipes(recipes, pendingRecipes, enableRandomRecipes);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
FastMachines.log(Level.WARNING, "Failed to retrieve machine recipes from {0}, attempting to use backup method.", id);
if (machineItem instanceof RecipeDisplayItem recipeDisplayItem) {
registerDisplayRecipes(recipes, recipeDisplayItem.getDisplayRecipes());
registerDisplayRecipes(recipes, recipeDisplayItem.getDisplayRecipes(), enableRandomRecipes);
FastMachines.log(Level.INFO, "Backup method succeeded. You can ignore the above warning.");
} else {
FastMachines.log(Level.SEVERE, "Backup method failed, please report this.");
}
}
}

public static void registerDisplayRecipes(List<IRecipe> recipes, String id) {
public static void registerDisplayRecipes(List<IRecipe> recipes, String id, boolean enableRandomRecipes) {
Preconditions.checkArgument(recipes != null, MSG_RECIPE_NULL);
Preconditions.checkArgument(id != null, MSG_ID_NULL);

Expand All @@ -322,7 +337,7 @@ public static void registerDisplayRecipes(List<IRecipe> recipes, String id) {

FastMachines.debug("Registering recipes from display recipes: {0}", id);

registerDisplayRecipes(recipes, recipeDisplayItem.getDisplayRecipes());
registerDisplayRecipes(recipes, recipeDisplayItem.getDisplayRecipes(), enableRandomRecipes);
}

/**
Expand All @@ -332,7 +347,7 @@ public static void registerDisplayRecipes(List<IRecipe> recipes, String id) {
* @param displayRecipes The display recipes from a {@link RecipeDisplayItem}.
*/
@ParametersAreNonnullByDefault
public static void registerDisplayRecipes(List<IRecipe> recipes, List<ItemStack> displayRecipes) {
public static void registerDisplayRecipes(List<IRecipe> recipes, List<ItemStack> displayRecipes, boolean enableRandomRecipes) {
Preconditions.checkArgument(recipes != null, MSG_RECIPE_NULL);
Preconditions.checkArgument(displayRecipes != null, "displayRecipes cannot be null");

Expand All @@ -350,7 +365,7 @@ public static void registerDisplayRecipes(List<IRecipe> recipes, List<ItemStack>

pendingRecipes.add(new RawRecipe(input, output));
}
registerRecipes(recipes, pendingRecipes);
registerRecipes(recipes, pendingRecipes, enableRandomRecipes);
}

/**
Expand Down

0 comments on commit c0097f4

Please sign in to comment.