-
Notifications
You must be signed in to change notification settings - Fork 205
Improve CraftingComponent API #2813
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
TechLord22
wants to merge
7
commits into
master
Choose a base branch
from
tc/crafting-component
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0138d23
improve CraftingComponent type safety and add a builder
TechLord22 cbbb3be
migrate to new API
TechLord22 94b97f9
remove GTValues.FALLBACK
TechLord22 fda4d81
fix event class
TechLord22 536e5ca
prevent setting null fallbacks
TechLord22 c59aefb
split into two maps
TechLord22 970bb97
fix builder overwrites
TechLord22 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
src/main/java/gregtech/api/recipes/crafting/CraftingComponent.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| package gregtech.api.recipes.crafting; | ||
|
|
||
| import gregtech.api.GTValues; | ||
| import gregtech.api.items.metaitem.MetaItem; | ||
| import gregtech.api.metatileentity.MetaTileEntity; | ||
| import gregtech.api.unification.material.Material; | ||
| import gregtech.api.unification.ore.OrePrefix; | ||
| import gregtech.api.unification.stack.UnificationEntry; | ||
|
|
||
| import net.minecraft.block.Block; | ||
| import net.minecraft.item.ItemStack; | ||
|
|
||
| import it.unimi.dsi.fastutil.ints.Int2ObjectMap; | ||
| import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| public final class CraftingComponent { | ||
|
|
||
| private final @NotNull Int2ObjectMap<ItemStack> itemEntries; | ||
| private final @NotNull Int2ObjectMap<String> stringEntries; | ||
| private @Nullable Object fallbackValue; | ||
|
|
||
| private CraftingComponent(@NotNull Int2ObjectMap<ItemStack> itemEntries, | ||
| @NotNull Int2ObjectMap<String> stringEntries, | ||
| @Nullable Object fallbackValue) { | ||
| this.itemEntries = itemEntries; | ||
| this.stringEntries = stringEntries; | ||
| this.fallbackValue = fallbackValue; | ||
| } | ||
|
|
||
| /** | ||
| * @param tier the tier of the ingredient | ||
| * @return the raw ingredient, which may be either an {@link ItemStack} or {@link String} | ||
| */ | ||
| public @Nullable Object getIngredient(int tier) { | ||
| ItemStack stack = itemEntries.get(tier); | ||
| if (stack != null) { | ||
| return stack; | ||
| } | ||
| String string = stringEntries.get(tier); | ||
| if (string != null) { | ||
| return string; | ||
| } | ||
| return fallbackValue; | ||
| } | ||
|
|
||
| /** | ||
| * Does not update the fallback ingredient | ||
| * | ||
| * @see #updateIngredients(CraftingComponent, boolean) | ||
| */ | ||
| @SuppressWarnings("unused") | ||
| public void updateIngredients(@NotNull CraftingComponent other) { | ||
| updateIngredients(other, false); | ||
| } | ||
|
|
||
| /** | ||
| * Add all ingredients from the provided Crafting Component to this ingredient, replacing existing values with | ||
| * new ones. | ||
| * | ||
| * @param other the component to update with | ||
| * @param updateFallback if the fallback ingredient should be updated with the provided component's ingredient | ||
| */ | ||
| @SuppressWarnings("unused") | ||
| public void updateIngredients(@NotNull CraftingComponent other, boolean updateFallback) { | ||
| itemEntries.putAll(other.itemEntries); | ||
| stringEntries.putAll(other.stringEntries); | ||
| if (updateFallback) { | ||
| if (other.fallbackValue == null) { | ||
| throw new IllegalArgumentException("Cannot update the fallback value to null"); | ||
| } | ||
| this.fallbackValue = other.fallbackValue; | ||
| } | ||
| } | ||
|
|
||
| public static class Builder { | ||
|
|
||
| private final @NotNull Int2ObjectMap<ItemStack> itemEntries = new Int2ObjectOpenHashMap<>(); | ||
| private final @NotNull Int2ObjectMap<String> stringEntries = new Int2ObjectOpenHashMap<>(); | ||
| private @Nullable Object fallbackValue; | ||
|
|
||
| /** | ||
| * Create a CraftingComponent without a fallback value. | ||
| */ | ||
| public Builder() {} | ||
|
|
||
| /** | ||
| * @param fallback the fallback ingredient | ||
| */ | ||
| public Builder(@NotNull ItemStack fallback) { | ||
| this.fallbackValue = fallback; | ||
| } | ||
|
|
||
| /** | ||
| * @see #Builder(ItemStack) | ||
| */ | ||
| public Builder(@NotNull MetaItem<?>.MetaValueItem fallback) { | ||
| this(fallback.getStackForm()); | ||
| } | ||
|
|
||
| /** | ||
| * @see #Builder(ItemStack) | ||
| */ | ||
| public Builder(@NotNull MetaTileEntity fallback) { | ||
| this(fallback.getStackForm()); | ||
| } | ||
|
|
||
| /** | ||
| * @see #Builder(Block, int) but defaults to {@link GTValues#W} for meta | ||
| */ | ||
| public Builder(@NotNull Block fallback) { | ||
| this(fallback, GTValues.W); | ||
| } | ||
|
|
||
| /** | ||
| * @see #Builder(ItemStack) | ||
| */ | ||
| public Builder(@NotNull Block fallback, int meta) { | ||
| this(new ItemStack(fallback, 1, meta)); | ||
| } | ||
|
|
||
| /** | ||
| * @param fallbackOreDict an OreDict string for the fallback ingredient | ||
| */ | ||
| public Builder(@NotNull String fallbackOreDict) { | ||
| this.fallbackValue = fallbackOreDict; | ||
| } | ||
|
|
||
| /** | ||
| * @see #Builder(String) | ||
| */ | ||
| public Builder(@NotNull OrePrefix fallbackPrefix, @NotNull Material fallbackMaterial) { | ||
| this(new UnificationEntry(fallbackPrefix, fallbackMaterial)); | ||
| } | ||
|
|
||
| /** | ||
| * @see #Builder(String) | ||
| */ | ||
| public Builder(@NotNull UnificationEntry fallback) { | ||
| this(fallback.toString()); | ||
| } | ||
|
|
||
| /** | ||
| * Add an entry | ||
| * | ||
| * @param tier the voltage tier for the entry, see {@link GTValues#V} | ||
| * @param stack the ingredient for the entry | ||
| * @return this | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull ItemStack stack) { | ||
| itemEntries.put(tier, stack); | ||
| stringEntries.remove(tier); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see #entry(int, ItemStack) | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull MetaItem<?>.MetaValueItem metaValueItem) { | ||
| return entry(tier, metaValueItem.getStackForm()); | ||
| } | ||
|
|
||
| /** | ||
| * @see #entry(int, ItemStack) | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull MetaTileEntity metaTileEntity) { | ||
| return entry(tier, metaTileEntity.getStackForm()); | ||
| } | ||
|
|
||
| /** | ||
| * @see #entry(int, Block, int) but defaults to {@link GTValues#W} for meta | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull Block block) { | ||
| return entry(tier, new ItemStack(block, 1, GTValues.W)); | ||
| } | ||
|
|
||
| /** | ||
| * @see #entry(int, ItemStack) | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull Block block, int meta) { | ||
| return entry(tier, new ItemStack(block, 1, meta)); | ||
| } | ||
|
|
||
| /** | ||
| * Add an entry | ||
| * | ||
| * @param tier the voltage tier for the entry, see {@link GTValues#V} | ||
| * @param oreDict the OreDict for the entry | ||
| * @return this | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull String oreDict) { | ||
| stringEntries.put(tier, oreDict); | ||
| itemEntries.remove(tier); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * @see #entry(int, String) | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull OrePrefix prefix, @NotNull Material material) { | ||
| return entry(tier, new UnificationEntry(prefix, material)); | ||
| } | ||
|
|
||
| /** | ||
| * @see #entry(int, String) | ||
| */ | ||
| public @NotNull Builder entry(int tier, @NotNull UnificationEntry unificationEntry) { | ||
| return entry(tier, unificationEntry.toString()); | ||
| } | ||
|
|
||
| public @NotNull CraftingComponent build() { | ||
| return new CraftingComponent(itemEntries, stringEntries, fallbackValue); | ||
| } | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
...tech/api/recipes/recipes/DummyRecipe.java → ...ech/api/recipes/crafting/DummyRecipe.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should there be a
getItemStack(int tier)method to directly get the tier's item stack so there's no need to cast this method? AgetString(int tier)would be good too.