diff options
Diffstat (limited to 'src/main/java/gregtech/api/recipe')
47 files changed, 6030 insertions, 217 deletions
diff --git a/src/main/java/gregtech/api/recipe/BasicUIProperties.java b/src/main/java/gregtech/api/recipe/BasicUIProperties.java new file mode 100644 index 0000000000..fde86785b2 --- /dev/null +++ b/src/main/java/gregtech/api/recipe/BasicUIProperties.java @@ -0,0 +1,251 @@ +package gregtech.api.recipe; + +import java.awt.Rectangle; +import java.util.List; +import java.util.function.IntFunction; +import java.util.function.Supplier; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +import org.apache.commons.lang3.tuple.Pair; + +import com.gtnewhorizons.modularui.api.drawable.FallbackableUITexture; +import com.gtnewhorizons.modularui.api.drawable.IDrawable; +import com.gtnewhorizons.modularui.api.math.Pos2d; +import com.gtnewhorizons.modularui.api.math.Size; +import com.gtnewhorizons.modularui.common.widget.ProgressBar; + +import gregtech.api.gui.modularui.FallbackableSteamTexture; +import gregtech.api.gui.modularui.SteamTexture; +import gregtech.api.util.FieldsAreNonnullByDefault; +import gregtech.api.util.MethodsReturnNonnullByDefault; + +/** + * Data object to store properties, used to draw both basic machine GUI and NEI recipe GUI, mainly GUI widgets. + * Not all the info used to draw NEI are listed here, see also {@link NEIRecipeProperties}. + * <p> + * Use {@link #builder()} for creation. + */ +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +@FieldsAreNonnullByDefault +public final class BasicUIProperties { + + /** + * Starts constructing BasicUIProperties. + */ + public static BasicUIPropertiesBuilder builder() { + return new BasicUIPropertiesBuilder(); + } + + /** + * Creates new builder from this instance. + */ + public BasicUIPropertiesBuilder toBuilder() { + return new BasicUIPropertiesBuilder().maxItemInputs(maxItemInputs) + .maxItemOutputs(maxItemOutputs) + .maxFluidInputs(maxFluidInputs) + .maxFluidOutputs(maxFluidOutputs) + .slotOverlays(slotOverlays) + .slotOverlaysSteam(slotOverlaysSteam) + .progressBarTexture(progressBarTexture) + .progressBarTextureSteam(progressBarTextureSteam) + .progressBarDirection(progressBarDirection) + .progressBarSize(progressBarSize) + .progressBarPos(progressBarPos) + .useProgressBar(useProgressBar) + .useSpecialSlot(useSpecialSlot) + .neiTransferRect(neiTransferRect) + .neiTransferRectId(neiTransferRectId) + .specialTextures(specialTextures) + .specialTexturesSteam(specialTexturesSteam) + .logo(logo) + .logoSize(logoSize) + .logoPos(logoPos) + .itemInputPositionsGetter(itemInputPositionsGetter) + .itemOutputPositionsGetter(itemOutputPositionsGetter) + .specialItemPositionGetter(specialItemPositionGetter) + .fluidInputPositionsGetter(fluidInputPositionsGetter) + .fluidOutputPositionsGetter(fluidOutputPositionsGetter) + .amperage(amperage); + } + + /** + * How many item inputs does this recipemap usually has at most. + * It does not actually restrict number of items used in the recipe. + */ + public final int maxItemInputs; + /** + * How many item outputs does this recipemap usually has at most. + * It does not actually restrict number of items used in the recipe. + */ + public final int maxItemOutputs; + /** + * How many fluid inputs does this recipemap usually has at most. + * It does not actually restrict number of items used in the recipe. + */ + public final int maxFluidInputs; + /** + * How many fluid outputs does this recipemap usually has at most. + * It does not actually restrict number of items used in the recipe. + */ + public final int maxFluidOutputs; + + private final SlotOverlayGetter<IDrawable> slotOverlays; + private final SlotOverlayGetter<SteamTexture> slotOverlaysSteam; + + /** + * Progressbar used for BasicMachine GUI and NEI. + */ + @Nullable + public final FallbackableUITexture progressBarTexture; + /** + * Progressbar used for steam machine GUI. + */ + @Nullable + public final FallbackableSteamTexture progressBarTextureSteam; + /** + * Direction of progressbar animation. + */ + public final ProgressBar.Direction progressBarDirection; + /** + * Size of the progressbar. (20, 36) by default. + */ + public final Size progressBarSize; + /** + * Position of the progressbar. (78, 24) by default. + */ + public final Pos2d progressBarPos; + /** + * Image size in the direction of progressbar. Used for non-smooth rendering. + */ + public final int progressBarImageSize; + + /** + * If progressbar should be added. + */ + public final boolean useProgressBar; + + /** + * If special slot has its usage for this GUI. + */ + public final boolean useSpecialSlot; + + /** + * GUI area where clicking shows up all the recipes available. + */ + public final List<Rectangle> neiTransferRect; + /** + * ID used to open NEI recipe GUI when progressbar is clicked. + */ + @Nullable + public final String neiTransferRectId; + + /** + * Additional textures shown on GUI. + */ + public final List<Pair<IDrawable, Pair<Size, Pos2d>>> specialTextures; + /** + * Additional textures shown on steam machine GUI. + */ + public final List<Pair<SteamTexture, Pair<Size, Pos2d>>> specialTexturesSteam; + + /** + * Logo shown on GUI. GregTech logo by default. + */ + public final IDrawable logo; + /** + * Size of logo. (17, 17) by default. + */ + public final Size logoSize; + /** + * Position of logo. (152, 63) by default. + */ + public final Pos2d logoPos; + + public final IntFunction<List<Pos2d>> itemInputPositionsGetter; + public final IntFunction<List<Pos2d>> itemOutputPositionsGetter; + public final Supplier<Pos2d> specialItemPositionGetter; + public final IntFunction<List<Pos2d>> fluidInputPositionsGetter; + public final IntFunction<List<Pos2d>> fluidOutputPositionsGetter; + + /** + * Amperage for the recipemap. Even though this is placed at frontend because backend logic doesn't need it, + * some machine logic also use this variable. + */ + public final int amperage; + + BasicUIProperties(int maxItemInputs, int maxItemOutputs, int maxFluidInputs, int maxFluidOutputs, + SlotOverlayGetter<IDrawable> slotOverlays, SlotOverlayGetter<SteamTexture> slotOverlaysSteam, + @Nullable FallbackableUITexture progressBarTexture, @Nullable FallbackableSteamTexture progressBarTextureSteam, + ProgressBar.Direction progressBarDirection, Size progressBarSize, Pos2d progressBarPos, boolean useProgressBar, + boolean useSpecialSlot, List<Rectangle> neiTransferRect, @Nullable String neiTransferRectId, + List<Pair<IDrawable, Pair<Size, Pos2d>>> specialTextures, + List<Pair<SteamTexture, Pair<Size, Pos2d>>> specialTexturesSteam, IDrawable logo, Size logoSize, Pos2d logoPos, + IntFunction<List<Pos2d>> itemInputPositionsGetter, IntFunction<List<Pos2d>> itemOutputPositionsGetter, + Supplier<Pos2d> specialItemPositionGetter, IntFunction<List<Pos2d>> fluidInputPositionsGetter, + IntFunction<List<Pos2d>> fluidOutputPositionsGetter, int amperage) { + if (maxItemInputs < 0 || maxItemOutputs < 0 || maxFluidInputs < 0 || maxFluidOutputs < 0) { + throw new IllegalArgumentException( + "maxItemInputs, maxItemOutputs, maxFluidInputs and maxFluidOutputs cannot be negative"); + } + if (amperage < 1) { + throw new IllegalArgumentException("Amperage cannot be lower than 1"); + } + this.maxItemInputs = maxItemInputs; + this.maxItemOutputs = maxItemOutputs; + this.maxFluidInputs = maxFluidInputs; + this.maxFluidOutputs = maxFluidOutputs; + this.slotOverlays = slotOverlays; + this.slotOverlaysSteam = slotOverlaysSteam; + this.progressBarTexture = progressBarTexture; + this.progressBarTextureSteam = progressBarTextureSteam; + this.progressBarDirection = progressBarDirection; + this.progressBarSize = progressBarSize; + this.progressBarPos = progressBarPos; + this.useProgressBar = useProgressBar; + this.useSpecialSlot = useSpecialSlot; + this.neiTransferRect = neiTransferRect; + this.neiTransferRectId = neiTransferRectId; + this.specialTextures = specialTextures; + this.specialTexturesSteam = specialTexturesSteam; + this.logo = logo; + this.logoSize = logoSize; + this.logoPos = logoPos; + this.itemInputPositionsGetter = itemInputPositionsGetter; + this.itemOutputPositionsGetter = itemOutputPositionsGetter; + this.specialItemPositionGetter = specialItemPositionGetter; + this.fluidInputPositionsGetter = fluidInputPositionsGetter; + this.fluidOutputPositionsGetter = fluidOutputPositionsGetter; + this.amperage = amperage; + + this.progressBarImageSize = switch (progressBarDirection) { + case UP, DOWN -> progressBarSize.height; + case CIRCULAR_CW -> Math.max(progressBarSize.width, progressBarSize.height); + default -> progressBarSize.width; + }; + } + + /** + * Retrieves overlay for slot, with given matching conditions. + */ + @Nullable + public IDrawable getOverlayForSlot(int index, boolean isFluid, boolean isOutput, boolean isSpecial) { + return slotOverlays.apply(index, isFluid, isOutput, isSpecial); + } + + /** + * Retrieves overlay for slot of steam machines, with given matching conditions. + */ + @Nullable + public SteamTexture getOverlayForSlotSteam(int index, boolean isFluid, boolean isOutput, boolean isSpecial) { + return slotOverlaysSteam.apply(index, isFluid, isOutput, isSpecial); + } + + public interface SlotOverlayGetter<T> { + + @Nullable + T apply(int index, boolean isFluid, boolean isOutput, boolean isSpecial); + } +} diff --git a/src/main/java/gregtech/api/recipe/BasicUIPropertiesBuilder.java b/src/main/java/gregtech/api/recipe/BasicUIPropertiesBuilder.java new file mode 100644 index 0000000000..7be2c94b23 --- /dev/null +++ b/src/main/java/gregtech/api/recipe/BasicUIPropertiesBuilder.java @@ -0,0 +1,264 @@ +package gregtech.api.recipe; + +import java.awt.Rectangle; +import java.util.Collections; +import java.util.List; +import java.util.function.IntFunction; +import java.util.function.Supplier; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; + +import com.google.common.collect.ImmutableList; +import com.gtnewhorizons.modularui.api.drawable.FallbackableUITexture; +import com.gtnewhorizons.modularui.api.drawable.IDrawable; +import com.gtnewhorizons.modularui.api.math.Pos2d; +import com.gtnewhorizons.modularui.api.math.Size; +import com.gtnewhorizons.modularui.common.widget.ProgressBar; + +import gregtech.api.gui.modularui.FallbackableSteamTexture; +import gregtech.api.gui.modularui.GT_UITextures; +import gregtech.api.gui.modularui.SteamTexture; +import gregtech.api.util.MethodsReturnNonnullByDefault; +import gregtech.common.gui.modularui.UIHelper; + +/** + * Builder class for {@link BasicUIProperties}. + */ +@SuppressWarnings("UnusedReturnValue") +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +public final class BasicUIPropertiesBuilder { + + private int maxItemInputs, maxItemOutputs, maxFluidInputs, maxFluidOutputs; + + private BasicUIProperties.SlotOverlayGetter<IDrawable> slotOverlays = (index, isFluid, isOutput, isSpecial) -> null; + private BasicUIProperties.SlotOverlayGetter<SteamTexture> slotOverlaysSteam = (index, isFluid, isOutput, + isSpecial) -> null; + + @Nullable + private FallbackableUITexture progressBarTexture; + @Nullable + private FallbackableSteamTexture progressBarTextureSteam; + private ProgressBar.Direction progressBarDirection = ProgressBar.Direction.RIGHT; + private Size progressBarSize = new Size(20, 18); + private Pos2d progressBarPos = new Pos2d(78, 24); + + private boolean useProgressBar = true; + + private boolean useSpecialSlot; + + private final ImmutableList.Builder<Rectangle> neiTransferRect = ImmutableList.builder(); + @Nullable + private String neiTransferRectId; + + private final ImmutableList.Builder<Pair<IDrawable, Pair<Size, Pos2d>>> specialTextures = ImmutableList.builder(); + private final ImmutableList.Builder<Pair<SteamTexture, Pair<Size, Pos2d>>> specialTexturesSteam = ImmutableList + .builder(); + + private IDrawable logo = GT_UITextures.PICTURE_GT_LOGO_17x17_TRANSPARENT; + private Size logoSize = new Size(17, 17); + private Pos2d logoPos = new Pos2d(152, 63); + + private IntFunction<List<Pos2d>> itemInputPositionsGetter = UIHelper::getItemInputPositions; + private IntFunction<List<Pos2d>> itemOutputPositionsGetter = UIHelper::getItemOutputPositions; + private Supplier<Pos2d> specialItemPositionGetter = UIHelper::getSpecialItemPosition; + private IntFunction<List<Pos2d>> fluidInputPositionsGetter = UIHelper::getFluidInputPositions; + private IntFunction<List<Pos2d>> fluidOutputPositionsGetter = UIHelper::getFluidOutputPositions; + + private int amperage = 1; + + BasicUIPropertiesBuilder() {} + + public BasicUIProperties build() { + if (maxItemInputs == 0 && maxItemOutputs == 0 && maxFluidInputs == 0 && maxFluidOutputs == 0) { + throw new IllegalArgumentException("Set either of max I/O count"); + } + List<Rectangle> builtNEITransferRect = neiTransferRect.build(); + if (builtNEITransferRect.isEmpty()) { + builtNEITransferRect = Collections.singletonList( + new Rectangle( + progressBarPos.x - (16 / 2), + progressBarPos.y, + progressBarSize.width + 16, + progressBarSize.height)); + } + return new BasicUIProperties( + maxItemInputs, + maxItemOutputs, + maxFluidInputs, + maxFluidOutputs, + slotOverlays, + slotOverlaysSteam, + progressBarTexture, + progressBarTextureSteam, + progressBarDirection, + progressBarSize, + progressBarPos, + useProgressBar, + useSpecialSlot, + builtNEITransferRect, + neiTransferRectId, + specialTextures.build(), + specialTexturesSteam.build(), + logo, + logoSize, + logoPos, + itemInputPositionsGetter, + itemOutputPositionsGetter, + specialItemPositionGetter, + fluidInputPositionsGetter, + fluidOutputPositionsGetter, + amperage); + } + + public BasicUIPropertiesBuilder maxItemInputs(int maxItemInputs) { + this.maxItemInputs = maxItemInputs; + return this; + } + + public BasicUIPropertiesBuilder maxItemOutputs(int maxItemOutputs) { + this.maxItemOutputs = maxItemOutputs; + return this; + } + + public BasicUIPropertiesBuilder maxFluidInputs(int maxFluidInputs) { + this.maxFluidInputs = maxFluidInputs; + return this; + } + + public BasicUIPropertiesBuilder maxFluidOutputs(int maxFluidOutputs) { + this.maxFluidOutputs = maxFluidOutputs; + return this; + } + + public BasicUIPropertiesBuilder slotOverlays(BasicUIProperties.SlotOverlayGetter<IDrawable> slotOverlays) { + this.slotOverlays = slotOverlays; + return this; + } + + public BasicUIPropertiesBuilder slotOverlaysSteam( + BasicUIProperties.SlotOverlayGetter<SteamTexture> slotOverlaysSteam) { + this.slotOverlaysSteam = slotOverlaysSteam; + return this; + } + + public BasicUIPropertiesBuilder progressBarTexture(@Nullable FallbackableUITexture progressBarTexture) { + this.progressBarTexture = progressBarTexture; + return this; + } + + public BasicUIPropertiesBuilder progressBarTextureSteam( + @Nullable FallbackableSteamTexture progressBarTextureSteam) { + this.progressBarTextureSteam = progressBarTextureSteam; + return this; + } + + public BasicUIPropertiesBuilder progressBarDirection(ProgressBar.Direction progressBarDirection) { + this.progressBarDirection = progressBarDirection; + return this; + } + + public BasicUIPropertiesBuilder progressBarSize(Size progressBarSize) { + this.progressBarSize = progressBarSize; + return this; + } + + public BasicUIPropertiesBuilder progressBarPos(Pos2d progressBarPos) { + this.progressBarPos = progressBarPos; + return this; + } + + public BasicUIPropertiesBuilder useProgressBar(boolean useProgressBar) { + this.useProgressBar = useProgressBar; + return this; + } + + public BasicUIPropertiesBuilder useSpecialSlot(boolean useSpecialSlot) { + this.useSpecialSlot = useSpecialSlot; + return this; + } + + public BasicUIPropertiesBuilder addNEITransferRect(Rectangle neiTransferRect) { + this.neiTransferRect.add(neiTransferRect); + return this; + } + + BasicUIPropertiesBuilder neiTransferRect(List<Rectangle> neiTransferRect) { + this.neiTransferRect.addAll(neiTransferRect); + return this; + } + + public BasicUIPropertiesBuilder neiTransferRectId(@Nullable String neiTransferRectId) { + this.neiTransferRectId = neiTransferRectId; + return this; + } + + public BasicUIPropertiesBuilder addSpecialTexture(Size size, Pos2d pos, IDrawable texture) { + this.specialTextures.add(new ImmutablePair<>(texture, new ImmutablePair<>(size, pos))); + return this; + } + + BasicUIPropertiesBuilder specialTextures(List<Pair<IDrawable, Pair<Size, Pos2d>>> specialTextures) { + this.specialTextures.addAll(specialTextures); + return this; + } + + public BasicUIPropertiesBuilder addSpecialTextureSteam(Size size, Pos2d pos, SteamTexture texture) { + this.specialTexturesSteam.add(new ImmutablePair<>(texture, new ImmutablePair<>(size, pos))); + return this; + } + + BasicUIPropertiesBuilder specialTexturesSteam(List<Pair<SteamTexture, Pair<Size, Pos2d>>> specialTextures) { + this.specialTexturesSteam.addAll(specialTextures); + return this; + } + + public BasicUIPropertiesBuilder logo(IDrawable logo) { + this.logo = logo; + return this; + } + + public BasicUIPropertiesBuilder logoSize(Size logoSize) { + this.logoSize = logoSize; + return this; + } + + public BasicUIPropertiesBuilder logoPos(Pos2d logoPos) { + this.logoPos = logoPos; + return this; + } + + public BasicUIPropertiesBuilder itemInputPositionsGetter(IntFunction<List<Pos2d>> itemInputPositionsGetter) { + this.itemInputPositionsGetter = itemInputPositionsGetter; + return this; + } + + public BasicUIPropertiesBuilder itemOutputPositionsGetter(IntFunction<List<Pos2d>> itemOutputPositionsGetter) { + this.itemOutputPositionsGetter = itemOutputPositionsGetter; + return this; + } + + public BasicUIPropertiesBuilder specialItemPositionGetter(Supplier<Pos2d> specialItemPositionGetter) { + this.specialItemPositionGetter = specialItemPositionGetter; + return this; + } + + public BasicUIPropertiesBuilder fluidInputPositionsGetter(IntFunction<List<Pos2d>> fluidInputPositionsGetter) { + this.fluidInputPositionsGetter = fluidInputPositionsGetter; + return this; + } + + public BasicUIPropertiesBuilder fluidOutputPositionsGetter(IntFunction<List<Pos2d>> fluidOutputPositionsGetter) { + this.fluidOutputPositionsGetter = fluidOutputPositionsGetter; + return this; + } + + public BasicUIPropertiesBuilder amperage(int amperage) { + this.amperage = amperage; + return this; + } +} diff --git a/src/main/java/gregtech/api/recipe/FindRecipeQuery.java b/src/main/java/gregtech/api/recipe/FindRecipeQuery.java new file mode 100644 index 0000000000..77c0648688 --- /dev/null +++ b/src/main/java/gregtech/api/recipe/FindRecipeQuery.java @@ -0,0 +1,178 @@ +package gregtech.api.recipe; + +import java.util.function.Predicate; +import java.util.stream.Stream; + +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +import net.minecraft.item.ItemStack; +import net.minecraftforge.fluids.FluidStack; + +import gregtech.api.util.GT_Recipe; +import gregtech.api.util.MethodsReturnNonnullByDefault; + +// spotless:off spotless likes formatting @code to @code +/** + * Helper class for searching recipe. Retrieve instance with {@link RecipeMap#findRecipeQuery}. + * <p> + * It features fluent API, so you can find recipes like this: + * + * <pre> + * {@code + * GT_Recipe recipe = recipeMap.findRecipeQuery() + * .items(inputItems) + * .fluids(inputFluids) + * .find(); + * } + * </pre> + */ +// spotless:on +@SuppressWarnings("unused") +@ParametersAreNonnullByDefault +@MethodsReturnNonnullByDefault +public final class FindRecipeQuery { + + private static final Predicate<GT_Recipe> ALWAYS = r -> true; + + private final RecipeMap<?> recipeMap; + + @Nullable + private ItemStack[] items; + @Nullable + private FluidStack[] fluids; + @Nullable + private ItemStack specialSlot; + private Predicate<GT_Recipe> filter = ALWAYS; + private long voltage = Integer.MAX_VALUE; + @Nullable + private GT_Recipe cachedRecipe; + private boolean notUnificated; + private boolean dontCheckStackSizes; + private boolean forCollisionCheck; + + FindRecipeQuery(RecipeMap<?> recipeMap) { + this.recipeMap = recipeMap; + } + + // region executors + + /** + * @return The first matched recipe, or null if not found. + */ + @Nullable + public GT_Recipe find() { + return findAll().findFirst() + .orElse(null); + } + + /** |
