Skip to content

Commit

Permalink
namespace and encapsulate build actions
Browse files Browse the repository at this point in the history
  • Loading branch information
TechLord22 committed Dec 12, 2023
1 parent bfb172d commit 0ac9ad0
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 41 deletions.
23 changes: 10 additions & 13 deletions src/main/java/gregtech/api/recipes/RecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
import crafttweaker.CraftTweakerAPI;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.MustBeInvokedByOverriders;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;

import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -80,7 +80,6 @@ public class RecipeBuilder<R extends RecipeBuilder<R>> {
protected GTRecipeCategory category;
protected boolean isCTRecipe = false;
protected int parallel = 0;
protected @Nullable @UnmodifiableView List<RecipeBuildAction<R>> onBuildActions = null;
protected EnumValidationResult recipeStatus = EnumValidationResult.VALID;
protected @Nullable IRecipePropertyStorage recipePropertyStorage = null;
protected boolean recipePropertyStorageErrored = false;
Expand Down Expand Up @@ -131,8 +130,6 @@ protected RecipeBuilder(RecipeBuilder<R> recipeBuilder) {
this.EUt = recipeBuilder.EUt;
this.hidden = recipeBuilder.hidden;
this.category = recipeBuilder.category;
this.onBuildActions = recipeBuilder.onBuildActions == null ? null :
new ArrayList<>(recipeBuilder.onBuildActions);
this.recipePropertyStorage = recipeBuilder.recipePropertyStorage == null ? null :
recipeBuilder.recipePropertyStorage.copy();
if (this.recipePropertyStorage != null) {
Expand Down Expand Up @@ -898,11 +895,6 @@ protected static String getRequiredString(int max, int actual, @NotNull String t
return out;
}

protected R onBuild(@NotNull List<@NotNull RecipeBuildAction<R>> actions) {
this.onBuildActions = actions;
return (R) this;
}

/**
* @deprecated Obsolete. Does not need calling.
*/
Expand All @@ -913,11 +905,16 @@ protected R invalidateOnBuildAction() {
return (R) this;
}

/**
* Build and register the recipe, if valid.
* <strong>Do not call outside of the
* {@link net.minecraftforge.event.RegistryEvent.Register<net.minecraft.item.crafting.IRecipe>} event for recipes.
* </strong>
*/
@MustBeInvokedByOverriders
public void buildAndRegister() {
if (onBuildActions != null) {
for (RecipeBuildAction<R> action : onBuildActions) {
action.accept((R) this);
}
for (RecipeBuildAction<R> action : recipeMap.getBuildActions()) {
action.accept((R) this);
}
ValidationResult<Recipe> validationResult = build();
recipeMap.addRecipe(validationResult);
Expand Down
43 changes: 29 additions & 14 deletions src/main/java/gregtech/api/recipes/RecipeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.common.Optional.Method;
Expand All @@ -58,6 +59,7 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.UnmodifiableView;
import stanhebben.zenscript.annotations.Optional;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
Expand Down Expand Up @@ -121,7 +123,7 @@ public class RecipeMap<R extends RecipeBuilder<R>> {

private final Map<GTRecipeCategory, List<Recipe>> recipeByCategory = new Object2ObjectOpenHashMap<>();

private @Nullable List<RecipeBuildAction<R>> recipeBuildActions;
private final Map<ResourceLocation, RecipeBuildAction<R>> recipeBuildActions = new Object2ObjectOpenHashMap<>();
protected @Nullable SoundEvent sound;
private @Nullable RecipeMap<?> smallRecipeMap;

Expand Down Expand Up @@ -311,28 +313,41 @@ public RecipeMap<R> setChanceFunction(@NotNull ChanceBoostFunction function) {
/**
* Add a recipe build action to be performed upon this RecipeMap's builder's recipe registration.
*
* @param name the unique name of the action
* @param action the action to perform
* @return this
*/
public RecipeMap<R> onRecipeBuild(@NotNull RecipeBuildAction<R> action) {
if (recipeBuildActions == null) {
recipeBuildActions = new ArrayList<>();
public RecipeMap<R> onRecipeBuild(@NotNull ResourceLocation name, @NotNull RecipeBuildAction<R> action) {
if (recipeBuildActions.containsKey(name)) {
throw new IllegalArgumentException("Cannot register RecipeBuildAction with duplicate name: " + name);
}
recipeBuildActions.add(action);
recipeBuildActions.put(name, action);
return this;
}

/**
* Overwrite the RecipeMap's build actions with new ones.
* <p>
* <strong>Use with caution</strong>. You probably want {@link #onRecipeBuild(RecipeBuildAction)} instead.
*
* @see #onRecipeBuild(RecipeBuildAction)
* @param name the name of the build action to remove
*/
public void removeBuildAction(@NotNull ResourceLocation name) {
recipeBuildActions.remove(name);
}

/**
* Add a recipe build action to be performed upon this RecipeMap's builder's recipe registration.
*
* @param actions the actions to set
* @param actions the actions to perform
*/
public void setOnBuildActions(@NotNull List<@NotNull RecipeBuildAction<R>> actions) {
this.recipeBuildActions = actions;
@ApiStatus.Internal
protected void onRecipeBuild(@NotNull Map<ResourceLocation, RecipeBuildAction<R>> actions) {
recipeBuildActions.putAll(actions);
}

/**
* @return the build actions for this RecipeMap's default RecipeBuilder
*/
@ApiStatus.Internal
protected @UnmodifiableView @NotNull Collection<@NotNull RecipeBuildAction<R>> getBuildActions() {
return this.recipeBuildActions.values();
}

public RecipeMap<R> allowEmptyOutput() {
Expand Down Expand Up @@ -1345,7 +1360,7 @@ public String getUnlocalizedName() {
}

public R recipeBuilder() {
return recipeBuilderSample.copy().onBuild(recipeBuildActions);
return recipeBuilderSample.copy();
}

/**
Expand Down
18 changes: 11 additions & 7 deletions src/main/java/gregtech/api/recipes/RecipeMapBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@
import gregtech.api.recipes.ui.RecipeMapUI;
import gregtech.api.recipes.ui.RecipeMapUIFunction;

import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;

import it.unimi.dsi.fastutil.bytes.Byte2ObjectArrayMap;
import it.unimi.dsi.fastutil.bytes.Byte2ObjectMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static gregtech.api.recipes.ui.RecipeMapUI.computeOverlayKey;

Expand Down Expand Up @@ -44,7 +45,7 @@ public class RecipeMapBuilder<B extends RecipeBuilder<B>> {
private SoundEvent sound;
private boolean allowEmptyOutputs;

private @Nullable List<RecipeBuildAction<B>> buildActions;
private @Nullable Map<ResourceLocation, RecipeBuildAction<B>> buildActions;

/**
* @param unlocalizedName the name of the recipemap
Expand Down Expand Up @@ -255,14 +256,17 @@ public RecipeMapBuilder(@NotNull String unlocalizedName, @NotNull B defaultRecip
/**
* Add a recipe build action to be performed upon this RecipeMap's builder's recipe registration.
*
* @param name the unique name of the action
* @param action the action to perform
* @return this
*/
public @NotNull RecipeMapBuilder<B> onBuild(@NotNull RecipeBuildAction<B> action) {
public @NotNull RecipeMapBuilder<B> onBuild(@NotNull ResourceLocation name, @NotNull RecipeBuildAction<B> action) {
if (buildActions == null) {
buildActions = new ArrayList<>();
buildActions = new Object2ObjectOpenHashMap<>();
} else if (buildActions.containsKey(name)) {
throw new IllegalArgumentException("Cannot register RecipeBuildAction with duplicate name: " + name);
}
buildActions.add(action);
buildActions.put(name, action);
return this;
}

Expand All @@ -280,7 +284,7 @@ public RecipeMapBuilder(@NotNull String unlocalizedName, @NotNull B defaultRecip
recipeMap.allowEmptyOutput();
}
if (buildActions != null) {
recipeMap.setOnBuildActions(buildActions);
recipeMap.onRecipeBuild(buildActions);
}
return recipeMap;
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/gregtech/api/recipes/RecipeMaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import stanhebben.zenscript.annotations.ZenProperty;

import static gregtech.api.GTValues.*;
import static gregtech.api.util.GTUtility.gregtechId;

/**
* Notes:
Expand Down Expand Up @@ -123,7 +124,7 @@ public final class RecipeMaps {
.fluidOutputs(1)
.progressBar(GuiTextures.PROGRESS_BAR_ARC_FURNACE)
.sound(GTSoundEvents.ARC)
.onBuild(recipeBuilder -> {
.onBuild(gregtechId("arc_furnace_oxygen"), recipeBuilder -> {
if (recipeBuilder.getFluidInputs().isEmpty()) {
recipeBuilder.fluidInputs(Materials.Oxygen.getFluid(recipeBuilder.getDuration()));
}
Expand Down Expand Up @@ -151,7 +152,7 @@ public final class RecipeMaps {
.itemSlotOverlay(GuiTextures.CIRCUIT_OVERLAY, false)
.progressBar(GuiTextures.PROGRESS_BAR_CIRCUIT)
.sound(GTSoundEvents.ASSEMBLER)
.onBuild(recipeBuilder -> {
.onBuild(gregtechId("assembler_solder"), recipeBuilder -> {
var fluidInputs = recipeBuilder.getFluidInputs();
if (fluidInputs.size() == 1 && fluidInputs.get(0).getInputFluidStack().getFluid() ==
Materials.SolderingAlloy.getFluid()) {
Expand All @@ -160,7 +161,8 @@ public final class RecipeMaps {
recipeBuilder.copy().clearFluidInputs().fluidInputs(Materials.Tin.getFluid(amount * 2))
.buildAndRegister();
}

})
.onBuild(gregtechId("assembler_recycling"), recipeBuilder -> {
if (recipeBuilder.isWithRecycling()) {
// ignore input fluids for recycling
ItemStack outputStack = recipeBuilder.getOutputs().get(0);
Expand Down Expand Up @@ -195,7 +197,8 @@ public final class RecipeMaps {
@ZenProperty
public static final RecipeMap<AssemblyLineRecipeBuilder> ASSEMBLY_LINE_RECIPES = new RecipeMapAssemblyLine<>(
"assembly_line", new AssemblyLineRecipeBuilder(), AssemblyLineUI::new)
.onRecipeBuild(AssemblyLineManager::createDefaultResearchRecipe);
.onRecipeBuild(gregtechId("default_research_recipe"),
AssemblyLineManager::createDefaultResearchRecipe);

/**
* Example:
Expand Down Expand Up @@ -435,7 +438,7 @@ public final class RecipeMaps {
.fluidSlotOverlay(GuiTextures.VIAL_OVERLAY_2, true)
.progressBar(GuiTextures.PROGRESS_BAR_ARROW_MULTIPLE)
.sound(GTValues.FOOLS.get() ? GTSoundEvents.SCIENCE : GTSoundEvents.CHEMICAL_REACTOR)
.onBuild(recipeBuilder -> RecipeMaps.LARGE_CHEMICAL_RECIPES.recipeBuilder()
.onBuild(gregtechId("lcr_copy"), recipeBuilder -> RecipeMaps.LARGE_CHEMICAL_RECIPES.recipeBuilder()
.inputs(recipeBuilder.getInputs().toArray(new GTRecipeInput[0]))
.fluidInputs(recipeBuilder.getFluidInputs())
.outputs(recipeBuilder.getOutputs())
Expand Down Expand Up @@ -484,7 +487,7 @@ public final class RecipeMaps {
.itemSlotOverlay(GuiTextures.CIRCUIT_OVERLAY, false)
.progressBar(GuiTextures.PROGRESS_BAR_CIRCUIT_ASSEMBLER)
.sound(GTSoundEvents.ASSEMBLER)
.onBuild(recipeBuilder -> {
.onBuild(gregtechId("circuit_assembler_solder"), recipeBuilder -> {
if (recipeBuilder.getFluidInputs().isEmpty()) {
recipeBuilder.copy()
.fluidInputs(Materials.SolderingAlloy.getFluid(Math.max(1,
Expand Down Expand Up @@ -606,7 +609,7 @@ public final class RecipeMaps {
.itemSlotOverlay(GuiTextures.DUST_OVERLAY, true, true)
.progressBar(GuiTextures.PROGRESS_BAR_SLICE)
.sound(GTSoundEvents.CUT)
.onBuild(recipeBuilder -> {
.onBuild(gregtechId("cutter_fluid"), recipeBuilder -> {
if (recipeBuilder.getFluidInputs().isEmpty()) {
int duration = recipeBuilder.getDuration();
int eut = recipeBuilder.getEUt();
Expand Down

0 comments on commit 0ac9ad0

Please sign in to comment.