Skip to content
85 changes: 85 additions & 0 deletions src/main/java/gregtech/api/recipes/ModHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.Materials;
import gregtech.api.unification.ore.OrePrefix;
import gregtech.api.unification.stack.ItemMaterialInfo;
import gregtech.api.unification.stack.MaterialStack;
import gregtech.api.unification.stack.UnificationEntry;
import gregtech.api.util.DummyContainer;
Expand Down Expand Up @@ -39,6 +40,7 @@
import javax.annotation.Nullable;
import java.lang.reflect.Field;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -225,6 +227,10 @@ public static void addMirroredShapedRecipe(String regName, ItemStack result, Obj
* </ul>
*/
public static void addShapedRecipe(String regName, ItemStack result, Object... recipe) {
addShapedRecipe(false, regName, result, recipe);
}

public static void addShapedRecipe(boolean withUnificationData, String regName, ItemStack result, Object... recipe) {
boolean skip = false;
if (result.isEmpty()) {
GTLog.logger.error("Result cannot be an empty ItemStack. Recipe: {}", regName);
Expand All @@ -241,6 +247,8 @@ public static void addShapedRecipe(String regName, ItemStack result, Object... r
.setMirrored(false) //make all recipes not mirrored by default
.setRegistryName(regName);
ForgeRegistries.RECIPES.register(shapedOreRecipe);

if (withUnificationData) OreDictUnifier.registerOre(result, getRecyclingIngredients(recipe));
}

public static void addShapedEnergyTransferRecipe(String regName, ItemStack result, Predicate<ItemStack> chargePredicate, boolean overrideCharge, boolean transferMaxCharge, Object... recipe) {
Expand Down Expand Up @@ -337,6 +345,83 @@ public static Object finalizeIngredient(Object ingredient) {
return ingredient;
}

public static ItemMaterialInfo getRecyclingIngredients(Object... recipe) {
Map<Character, Integer> inputCountMap = new HashMap<>();
Map<Material, Long> materialStacksExploded = new HashMap<>();

int itr = 0;
while (recipe[itr] instanceof String) {
String s = (String) recipe[itr];
for (char c : s.toCharArray()) {
if (getToolNameByCharacter(c) != null) continue; // skip tools
int count = inputCountMap.getOrDefault(c, 0);
inputCountMap.put(c, count + 1);
}
itr++;
}

char lastChar = ' ';
for (int i = itr; i < recipe.length; i++) {
Object ingredient = recipe[i];

// Track the current working ingredient symbol
if (ingredient instanceof Character) {
lastChar = (char) ingredient;
continue;
Comment thread
serenibyss marked this conversation as resolved.
}

// Should never happen if recipe is formatted correctly
// In the case that it isn't, this error should be handled
// by an earlier method call parsing the recipe.
if (lastChar == ' ') return null;
Comment thread
serenibyss marked this conversation as resolved.

ItemStack stack;
if (ingredient instanceof MetaItem.MetaValueItem) {
stack = ((MetaItem<?>.MetaValueItem) ingredient).getStackForm();
} else if (ingredient instanceof UnificationEntry) {
stack = OreDictUnifier.get((UnificationEntry) ingredient);
} else if (ingredient instanceof ItemStack) {
stack = (ItemStack) ingredient;
} else if (ingredient instanceof Item) {
stack = new ItemStack((Item) ingredient, 1);
} else if (ingredient instanceof Block) {
stack = new ItemStack((Block) ingredient, 1);
} else if (ingredient instanceof String) {
stack = OreDictUnifier.get((String) ingredient);
} else continue; // throw out bad entries

BiConsumer<MaterialStack, Character> func = (ms, c) -> {
long amount = materialStacksExploded.getOrDefault(ms.material, 0L);
materialStacksExploded.put(ms.material, (ms.amount * inputCountMap.get(c)) + amount);
};

// First try to get ItemMaterialInfo
ItemMaterialInfo info = OreDictUnifier.getMaterialInfo(stack);
if (info != null) {
for (MaterialStack ms : info.getMaterials()) func.accept(ms, lastChar);
continue;
}

// Then try to get a single Material (UnificationEntry needs this, for example)
MaterialStack materialStack = OreDictUnifier.getMaterial(stack);
Comment thread
serenibyss marked this conversation as resolved.
if (materialStack != null) func.accept(materialStack, lastChar);

// Gather any secondary materials if this item has an OrePrefix
OrePrefix prefix = OreDictUnifier.getPrefix(stack);
if (prefix != null && !prefix.secondaryMaterials.isEmpty()) {
for (MaterialStack ms : prefix.secondaryMaterials) {
func.accept(ms, lastChar);
}
}
}

return new ItemMaterialInfo(materialStacksExploded.entrySet().stream()
.map(e -> new MaterialStack(e.getKey(), e.getValue()))
.sorted(Comparator.comparingLong(m -> -m.amount))
.collect(Collectors.toList())
);
}

/**
* Add Shapeless Crafting Recipes
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/gregtech/api/recipes/RecipeMaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class RecipeMaps {


@ZenProperty
public static final RecipeMap<SimpleRecipeBuilder> MACERATOR_RECIPES = new RecipeMap<>("macerator", 1, 1, 1, 3, 0, 0, 0, 0, new SimpleRecipeBuilder().duration(150).EUt(2), false)
public static final RecipeMap<SimpleRecipeBuilder> MACERATOR_RECIPES = new RecipeMap<>("macerator", 1, 1, 1, 4, 0, 0, 0, 0, new SimpleRecipeBuilder().duration(150).EUt(2), false)
.setSlotOverlay(false, false, GuiTextures.CRUSHED_ORE_OVERLAY)
.setSlotOverlay(true, false, GuiTextures.DUST_OVERLAY)
.setProgressBar(GuiTextures.PROGRESS_BAR_MACERATE, MoveType.HORIZONTAL)
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/gregtech/api/unification/OreDictUnifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,14 @@ public static MaterialStack getMaterial(ItemStack itemStack) {
}
}
ItemMaterialInfo info = materialUnificationInfo.get(simpleItemStack);
return info == null ? null : info.material.copy();
return info == null ? null : info.getMaterial().copy();
}

@Nullable
public static ItemMaterialInfo getMaterialInfo(ItemStack itemStack) {
if (itemStack.isEmpty()) return null;
ItemAndMetadata simpleItemStack = new ItemAndMetadata(itemStack);
return materialUnificationInfo.get(simpleItemStack);
}

@Nullable
Expand Down Expand Up @@ -239,7 +246,8 @@ public static ItemStack get(OrePrefix orePrefix, Material material, int stackSiz

public static ItemStack get(String oreDictName) {
List<ItemStack> itemStacks = oreDictNameStacks.get(oreDictName);
return itemStacks.size() > 0 ? itemStacks.get(0).copy() : ItemStack.EMPTY;
if (itemStacks == null || itemStacks.size() == 0) return ItemStack.EMPTY;
return itemStacks.get(0).copy();
}

public static List<Entry<ItemStack, ItemMaterialInfo>> getAllItemInfos() {
Expand Down Expand Up @@ -267,21 +275,40 @@ else if ((materialAmount * 9) >= M)
return ItemStack.EMPTY;
}


public static ItemStack getDust(MaterialStack materialStack) {
return getDust(materialStack.material, materialStack.amount);
}

public static ItemStack getIngot(Material material, long materialAmount) {
if (!material.hasProperty(PropertyKey.INGOT) || materialAmount <= 0)
return ItemStack.EMPTY;
if (materialAmount % (M * 9) == 0)
return get(OrePrefix.block, material, (int) (materialAmount / (M * 9)));
if (materialAmount % M == 0 || materialAmount >= M * 16)
return get(OrePrefix.ingot, material, (int) (materialAmount / M));
else if ((materialAmount * 9) >= M)
return get(OrePrefix.nugget, material, (int) ((materialAmount * 9) / M));
return ItemStack.EMPTY;
}

public static ItemStack getIngot(MaterialStack materialStack) {
return getIngot(materialStack.material, materialStack.amount);
}

/**
* Returns an Ingot of the material if it exists. Otherwise it returns a Dust.
* Returns ItemStack.EMPTY if neither exist.
*/
public static ItemStack getIngotOrDust(Material material, long materialAmount) {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to not return an ItemStack.EMPTY for some Arc Furnace recipes (Ash, in particular)

ItemStack ingotStack = getIngot(material, materialAmount);
if (ingotStack != ItemStack.EMPTY) return ingotStack;
return getDust(material, materialAmount);
}

public static ItemStack getIngotOrDust(MaterialStack materialStack) {
return getIngotOrDust(materialStack.material, materialStack.amount);
}
Comment on lines +308 to +310
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this method, when anything that can call this method can automatically call the method's redirect, as it has access to the materialStack.amount and materialStack.material already. This seems like a useless method


synchronized private static <T> void addAndSort(List<T> list, T itemToAdd, Comparator<T> comparator) {
list.add(itemToAdd);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ public static void register() {
Brick = new Material.Builder(2524, "brick")
.dust(1)
.color(0x9B5643).iconSet(ROUGH)
.flags(EXCLUDE_BLOCK_CRAFTING_RECIPES, DECOMPOSITION_BY_CENTRIFUGING)
.flags(EXCLUDE_BLOCK_CRAFTING_RECIPES, NO_SMELTING, DECOMPOSITION_BY_CENTRIFUGING)
.components(Clay, 1)
.build();

Fireclay = new Material.Builder(2525, "fireclay")
.dust()
.color(0xADA09B).iconSet(ROUGH)
.flags(DECOMPOSITION_BY_CENTRIFUGING)
.flags(DECOMPOSITION_BY_CENTRIFUGING, NO_SMELTING)
.components(Clay, 1, Brick, 1)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public static void register() {
RawRubber = new Material.Builder(1002, "raw_rubber")
.dust()
.color(0xCCC789)
.flags(DISABLE_DECOMPOSITION)
.flags(DISABLE_DECOMPOSITION, FLAMMABLE)
.components(Carbon, 5, Hydrogen, 8)
.build();

RawStyreneButadieneRubber = new Material.Builder(1003, "raw_styrene_butadiene_rubber")
.dust()
.color(0x54403D).iconSet(SHINY)
.flags(DISABLE_DECOMPOSITION)
.flags(DISABLE_DECOMPOSITION, FLAMMABLE)
.components(Carbon, 20, Hydrogen, 26)
.build()
.setFormula("(C4H6)3C8H8", true);
Expand All @@ -57,22 +57,22 @@ public static void register() {
ReinforcedEpoxyResin = new Material.Builder(1006, "reinforced_epoxy_resin")
.ingot().fluid()
.color(0xA07A10)
.flags(STD_METAL, DISABLE_DECOMPOSITION, NO_SMASHING, GENERATE_FINE_WIRE, GENERATE_ROD)
.flags(STD_METAL, DISABLE_DECOMPOSITION, NO_SMASHING, GENERATE_FINE_WIRE, GENERATE_ROD, FLAMMABLE)
.components(Carbon, 6, Hydrogen, 4, Oxygen, 1)
.build();

PolyvinylChloride = new Material.Builder(1007, "polyvinyl_chloride")
.ingot().fluid()
.color(0xD7E6E6)
.flags(EXT_METAL, GENERATE_FOIL, DISABLE_DECOMPOSITION, NO_SMASHING)
.flags(EXT_METAL, GENERATE_FOIL, DISABLE_DECOMPOSITION, NO_SMASHING, FLAMMABLE)
.components(Carbon, 2, Hydrogen, 3, Chlorine, 1)
.itemPipeProperties(512, 4)
.build();

PolyphenyleneSulfide = new Material.Builder(1008, "polyphenylene_sulfide")
.ingot().fluid()
.color(0xAA8800)
.flags(EXT_METAL, DISABLE_DECOMPOSITION, GENERATE_FOIL)
.flags(EXT_METAL, DISABLE_DECOMPOSITION, GENERATE_FOIL, FLAMMABLE)
.components(Carbon, 6, Hydrogen, 4, Sulfur, 1)
.build();

Expand All @@ -85,15 +85,15 @@ public static void register() {
Polybenzimidazole = new Material.Builder(1010, "polybenzimidazole")
.ingot().fluid()
.color(0x2D2D2D)
.flags(EXCLUDE_BLOCK_CRAFTING_RECIPES, NO_SMASHING, DISABLE_DECOMPOSITION, GENERATE_FOIL)
.flags(EXCLUDE_BLOCK_CRAFTING_RECIPES, NO_SMASHING, DISABLE_DECOMPOSITION, GENERATE_FOIL, FLAMMABLE)
.components(Carbon, 20, Hydrogen, 12, Nitrogen, 4)
.fluidPipeProperties(1000, 100, true)
.build();

Polydimethylsiloxane = new Material.Builder(1011, "polydimethylsiloxane")
.dust()
.color(0xF5F5F5)
.flags(DISABLE_DECOMPOSITION)
.flags(DISABLE_DECOMPOSITION, FLAMMABLE)
.components(Carbon, 2, Hydrogen, 6, Oxygen, 1, Silicon, 1)
.build();

Expand All @@ -108,7 +108,7 @@ public static void register() {
Epoxy = new Material.Builder(1013, "epoxy")
.ingot(1).fluid()
.color(0xC88C14)
.flags(EXT2_METAL, DISABLE_DECOMPOSITION, NO_SMASHING)
.flags(EXT2_METAL, DISABLE_DECOMPOSITION, NO_SMASHING, FLAMMABLE)
.components(Carbon, 21, Hydrogen, 25, Chlorine, 1, Oxygen, 5)
.build();

Expand All @@ -117,14 +117,14 @@ public static void register() {
Polycaprolactam = new Material.Builder(1015, "polycaprolactam")
.ingot(1).fluid()
.color(0x323232)
.flags(STD_METAL, DISABLE_DECOMPOSITION, NO_SMASHING, GENERATE_FOIL)
.flags(STD_METAL, DISABLE_DECOMPOSITION, NO_SMASHING, GENERATE_FOIL, FLAMMABLE)
.components(Carbon, 6, Hydrogen, 11, Nitrogen, 1, Oxygen, 1)
.build();

Polytetrafluoroethylene = new Material.Builder(1016, "polytetrafluoroethylene")
.ingot(1).fluid()
.color(0x646464)
.flags(STD_METAL, GENERATE_FRAME, DISABLE_DECOMPOSITION, NO_SMASHING, GENERATE_FOIL)
.flags(STD_METAL, GENERATE_FRAME, DISABLE_DECOMPOSITION, NO_SMASHING, GENERATE_FOIL, FLAMMABLE)
.components(Carbon, 2, Fluorine, 4)
.fluidPipeProperties(600, 80, true)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public static void register() {
Stone = new Material.Builder(1599, "stone")
.dust(1)
.color(0xCDCDCD).iconSet(ROUGH)
.flags(MORTAR_GRINDABLE, GENERATE_GEAR, NO_SMASHING)
.flags(MORTAR_GRINDABLE, GENERATE_GEAR, NO_SMASHING, NO_SMELTING)
.build();

Lava = new Material.Builder(1600, "lava")
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/gregtech/api/unification/ore/OrePrefix.java
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,12 @@ public static class Conditions {
pipeNormalRestrictive.addSecondaryMaterial(new MaterialStack(Materials.Iron, ring.materialAmount * 2));
pipeLargeRestrictive.addSecondaryMaterial(new MaterialStack(Materials.Iron, ring.materialAmount * 2));

cableGtSingle.addSecondaryMaterial(new MaterialStack(Materials.Rubber, plate.materialAmount));
cableGtDouble.addSecondaryMaterial(new MaterialStack(Materials.Rubber, plate.materialAmount));
cableGtQuadruple.addSecondaryMaterial(new MaterialStack(Materials.Rubber, plate.materialAmount * 2));
cableGtOctal.addSecondaryMaterial(new MaterialStack(Materials.Rubber, plate.materialAmount * 3));
cableGtHex.addSecondaryMaterial(new MaterialStack(Materials.Rubber, plate.materialAmount * 5));

plateDouble.setIgnored(Materials.BorosilicateGlass);
plate.setIgnored(Materials.BorosilicateGlass);
foil.setIgnored(Materials.BorosilicateGlass);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,34 @@

import com.google.common.collect.ImmutableList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ItemMaterialInfo {

public final MaterialStack material;
public final ImmutableList<MaterialStack> additionalComponents;
private final List<MaterialStack> materials = new ArrayList<>();

public ItemMaterialInfo(MaterialStack... materials) {
this.materials.addAll(Arrays.asList(materials));
}

public ItemMaterialInfo(List<MaterialStack> materials) {
this.materials.addAll(materials);
}

public ItemMaterialInfo(MaterialStack material, MaterialStack... additionalComponents) {
this.material = material;
this.additionalComponents = ImmutableList.copyOf(additionalComponents);
/**
* Returns the first MaterialStack in the "materials" list
*/
public MaterialStack getMaterial() {
return materials.size() == 0 ? null : materials.get(0);
}

/**
* Returns all MaterialStacks associated with this Object.
*/
public ImmutableList<MaterialStack> getMaterials() {
return ImmutableList.copyOf(materials);
}

@Override
Expand All @@ -18,21 +38,16 @@ public boolean equals(Object o) {
if (o == null || getClass() != o.getClass()) return false;

ItemMaterialInfo that = (ItemMaterialInfo) o;

if (!material.equals(that.material)) return false;
return additionalComponents.equals(that.additionalComponents);
return materials.equals(that.materials);
}

@Override
public int hashCode() {
int result = material.hashCode();
result = 31 * result + additionalComponents.hashCode();
return result;
return materials.hashCode();
}

@Override
public String toString() {
return material.material.toCamelCaseString();
return materials.size() == 0 ? "" : materials.get(0).material.toCamelCaseString();
}

}
}
1 change: 0 additions & 1 deletion src/main/java/gregtech/api/util/GTUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -1035,5 +1035,4 @@ public static long mean(@Nonnull long[] values) {
public static double getMeanTickTime(@Nonnull World world) {
return mean(Objects.requireNonNull(world.getMinecraftServer()).tickTimeArray) * 1.0E-6D;
}

}
Loading