Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/main/java/gregtech/api/GTValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,6 @@ public static boolean isDeobfEnvironment() {
return isDeobf;
}

/**
* Default fallback value used for Map keys.
* Currently only used in {@link gregtech.loaders.recipe.CraftingComponent}.
*/
public static final int FALLBACK = -1;

public static Supplier<Boolean> FOOLS = () -> {
String[] yearMonthDay = LocalDate.now().toString().split("-");
return ConfigHolder.misc.specialEvents && yearMonthDay[1].equals("04") && yearMonthDay[2].equals("01");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/recipes/ModHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.toolitem.IGTTool;
import gregtech.api.items.toolitem.ToolHelper;
import gregtech.api.recipes.recipes.DummyRecipe;
import gregtech.api.recipes.crafting.DummyRecipe;
import gregtech.api.unification.OreDictUnifier;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.Materials;
Expand Down
216 changes: 216 additions & 0 deletions src/main/java/gregtech/api/recipes/crafting/CraftingComponent.java
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) {
Copy link
Member

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? A getString(int tier) would be good too.

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);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package gregtech.api.recipes.recipes;
package gregtech.api.recipes.crafting;

import net.minecraft.inventory.InventoryCrafting;
import net.minecraft.item.ItemStack;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/gregtech/common/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import gregtech.api.metatileentity.registry.MTERegistry;
import gregtech.api.recipes.GTRecipeInputCache;
import gregtech.api.recipes.ModHandler;
import gregtech.api.recipes.crafting.CraftingComponent;
import gregtech.api.recipes.ingredients.GTRecipeOreInput;
import gregtech.api.recipes.properties.impl.FusionEUToStartProperty;
import gregtech.api.unification.material.Material;
Expand Down Expand Up @@ -47,7 +48,7 @@
import gregtech.integration.groovy.GroovyScriptModule;
import gregtech.loaders.MaterialInfoLoader;
import gregtech.loaders.OreDictionaryLoader;
import gregtech.loaders.recipe.CraftingComponent;
import gregtech.loaders.recipe.CraftingComponents;
import gregtech.loaders.recipe.GTRecipeManager;
import gregtech.modules.GregTechModules;

Expand Down Expand Up @@ -335,7 +336,7 @@ public static void registerItems(RegistryEvent.Register<Item> event) {
@SubscribeEvent(priority = EventPriority.HIGHEST)
public static void initComponents(RegistryEvent.Register<IRecipe> event) {
GTRecipeInputCache.enableCache();
CraftingComponent.initializeComponents();
CraftingComponents.initializeComponents();
MinecraftForge.EVENT_BUS.post(new GregTechAPI.RegisterEvent<>(null, CraftingComponent.class));
}

Expand Down
Loading
Loading