ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/recipe/CrusherRecipeRegistry.java

132 lines
5.5 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.recipe;
2016-01-05 04:47:35 +01:00
import java.util.ArrayList;
import java.util.List;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
import de.ellpeck.actuallyadditions.common.config.values.ConfigStringListValues;
import de.ellpeck.actuallyadditions.common.util.StackUtil;
import net.minecraft.item.Item;
2016-01-05 04:47:35 +01:00
import net.minecraft.item.ItemStack;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.oredict.OreIngredient;
2019-05-02 09:10:29 +02:00
public final class CrusherRecipeRegistry {
2016-01-05 04:47:35 +01:00
public static final ArrayList<SearchCase> SEARCH_CASES = new ArrayList<>();
2016-01-05 04:47:35 +01:00
2019-05-02 09:10:29 +02:00
public static void registerFinally() {
2019-02-27 19:53:05 +01:00
ArrayList<String> oresNoResult = new ArrayList<>();
2016-05-19 20:05:12 +02:00
int recipeStartedAt = ActuallyAdditionsAPI.CRUSHER_RECIPES.size();
2016-01-05 04:47:35 +01:00
2019-05-02 09:10:29 +02:00
for (String ore : OreDictionary.getOreNames()) {
if (!hasException(ore)) {
for (SearchCase theCase : SEARCH_CASES) {
if (ore.length() > theCase.theCase.length()) {
if (ore.substring(0, theCase.theCase.length()).equals(theCase.theCase)) {
String outputOre = theCase.resultPreString + ore.substring(theCase.theCase.length());
List<ItemStack> outputs = OreDictionary.getOres(outputOre, false);
ItemStack output = outputs.isEmpty() ? ItemStack.EMPTY : outputs.get(0).copy();
output.setCount(theCase.resultAmount);
2019-05-02 09:10:29 +02:00
if (output.isEmpty()) {
if (!oresNoResult.contains(ore)) {
oresNoResult.add(ore);
}
} else ActuallyAdditionsAPI.addCrusherRecipe(new OreIngredient(ore), output, StackUtil.getEmpty(), 0);
2016-01-05 04:47:35 +01:00
}
}
}
}
}
2019-02-27 19:53:05 +01:00
ArrayList<String> addedRecipes = new ArrayList<>();
2019-05-02 09:10:29 +02:00
for (int i = recipeStartedAt; i < ActuallyAdditionsAPI.CRUSHER_RECIPES.size(); i++) {
2016-05-19 20:05:12 +02:00
CrusherRecipe recipe = ActuallyAdditionsAPI.CRUSHER_RECIPES.get(i);
2019-05-02 09:10:29 +02:00
addedRecipes.add(recipe.getInput().getMatchingStacks() + " -> " + recipe.getOutputOne());
2016-01-05 04:47:35 +01:00
}
2019-05-02 09:10:29 +02:00
ActuallyAdditions.LOGGER.debug("Added " + addedRecipes.size() + " Crusher Recipes automatically: " + addedRecipes);
ActuallyAdditions.LOGGER.debug("Couldn't add " + oresNoResult.size() + " Crusher Recipes automatically, either because the inputs were missing outputs, or because they exist already: " + oresNoResult);
removeDuplicateRecipes();
2016-01-05 04:47:35 +01:00
}
2019-02-27 19:53:05 +01:00
public static void removeDuplicateRecipes() {
ArrayList<CrusherRecipe> usable = new ArrayList<>();
ArrayList<CrusherRecipe> removed = new ArrayList<>();
2019-05-02 09:10:29 +02:00
for (CrusherRecipe r : ActuallyAdditionsAPI.CRUSHER_RECIPES) {
boolean canUse = true;
2019-05-02 09:10:29 +02:00
if (r.getInput().getMatchingStacks().length == 0) canUse = false;
else for (CrusherRecipe re : usable) {
if (re.getInput().apply(r.getInput().getMatchingStacks()[0])) canUse = false;
}
2019-02-27 19:53:05 +01:00
2019-05-02 09:10:29 +02:00
if (canUse) usable.add(r);
else removed.add(r);
}
2019-02-27 19:53:05 +01:00
ActuallyAdditionsAPI.CRUSHER_RECIPES.clear();
ActuallyAdditionsAPI.CRUSHER_RECIPES.addAll(usable);
ActuallyAdditions.LOGGER.debug(String.format("Removed %s crusher recipes that had dupliate inputs, %s remain.", removed.size(), usable.size()));
2019-02-27 19:53:05 +01:00
}
2016-01-05 04:47:35 +01:00
2019-05-02 09:10:29 +02:00
public static boolean hasBlacklistedOutput(ItemStack output, String[] config) {
if (StackUtil.isValid(output)) {
Item item = output.getItem();
2019-05-02 09:10:29 +02:00
if (item != null) {
String reg = item.getRegistryName().toString();
2019-05-02 09:10:29 +02:00
for (String conf : config) {
String confReg = conf;
int meta = 0;
2019-05-02 09:10:29 +02:00
if (conf.contains("@")) {
try {
String[] split = conf.split("@");
confReg = split[0];
meta = Integer.parseInt(split[1]);
2019-05-02 09:10:29 +02:00
} catch (Exception e) {
ActuallyAdditions.LOGGER.warn("A config option appears to be incorrect: The entry " + conf + " can't be parsed!");
}
}
2019-05-02 09:10:29 +02:00
if (reg.equals(confReg) && output.getItemDamage() == meta) { return true; }
}
return false;
}
}
return true;
}
2019-05-02 09:10:29 +02:00
public static boolean hasException(String ore) {
for (String conf : ConfigStringListValues.CRUSHER_RECIPE_EXCEPTIONS.getValue()) {
if (conf.equals(ore)) { return true; }
2016-01-05 04:47:35 +01:00
}
return false;
}
2019-05-02 09:10:29 +02:00
public static CrusherRecipe getRecipeFromInput(ItemStack input) {
for (CrusherRecipe recipe : ActuallyAdditionsAPI.CRUSHER_RECIPES)
if (recipe.matches(input)) return recipe;
2016-01-05 04:47:35 +01:00
return null;
}
2019-05-02 09:10:29 +02:00
public static class SearchCase {
2016-01-05 04:47:35 +01:00
2016-05-19 20:05:12 +02:00
final String theCase;
final int resultAmount;
final String resultPreString;
2016-01-05 04:47:35 +01:00
2019-05-02 09:10:29 +02:00
public SearchCase(String theCase, int resultAmount) {
2016-01-05 04:47:35 +01:00
this(theCase, resultAmount, "dust");
}
2019-05-02 09:10:29 +02:00
public SearchCase(String theCase, int resultAmount, String resultPreString) {
2016-01-05 04:47:35 +01:00
this.theCase = theCase;
this.resultAmount = resultAmount;
this.resultPreString = resultPreString;
}
}
}