-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CraftTweaker support for the Alloy Furnace (#37)
* Add CraftTweaker support for the Alloy Furnace * Improved AlloyRecipes API for the use of CTAlloyFurnace, improved documentation, and fixed some criticisms * add example javadoc. * Rename AlloyRecipes API to use register/unregister terminology and improved documentation for TeckleCTUtils. * Capitalization and documentation improvements * Make AlloyRecipes use its own API instead of direct list access. * Documentation and formatting fixes, remove direct use of fields in favor of Concrete Accessor. * Use IngredientStack#getInternal instead of reflection, merge CT version into 1 value
- Loading branch information
1 parent
01759ad
commit 573b85d
Showing
5 changed files
with
385 additions
and
16 deletions.
There are no files selected for viewing
This file contains 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 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 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
176 changes: 176 additions & 0 deletions
176
src/main/java/com/elytradev/teckle/compat/ct/CTAlloyFurnace.java
This file contains 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,176 @@ | ||
package com.elytradev.teckle.compat.ct; | ||
|
||
import com.elytradev.teckle.common.crafting.AlloyRecipe; | ||
import com.elytradev.teckle.common.crafting.AlloyRecipes; | ||
import crafttweaker.CraftTweakerAPI; | ||
import crafttweaker.IAction; | ||
import crafttweaker.annotations.ZenRegister; | ||
import crafttweaker.api.item.IIngredient; | ||
import crafttweaker.api.item.IItemStack; | ||
import crafttweaker.api.minecraft.CraftTweakerMC; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.NonNullList; | ||
import net.minecraft.util.Tuple; | ||
import stanhebben.zenscript.annotations.ZenClass; | ||
import stanhebben.zenscript.annotations.ZenMethod; | ||
|
||
/** | ||
* ZenScript class for accessing Alloy Furnace recipes. | ||
* | ||
* Exposes functions to register recipes, remove recipes by output, remove recipes by inputs, and remove all recipes. | ||
*/ | ||
@ZenClass("mods.teckle.alloy_furnace") | ||
@ZenRegister | ||
public class CTAlloyFurnace { | ||
|
||
/** | ||
* Creates and registers a recipe for the alloy furnace based on the data provided. | ||
* | ||
* @param output The ItemStack resulting from the recipe. | ||
* @param inputs An array of ingredients required to make the recipe. | ||
*/ | ||
@ZenMethod | ||
public static void addRecipe(IItemStack output, IIngredient[] inputs) { | ||
Tuple<Object, Integer>[] inputTuples = (Tuple<Object, Integer>[]) new Tuple[inputs.length]; | ||
|
||
if (inputs.length > 9) { | ||
throw new RuntimeException("Alloy Furnace cannot take more than 9 inputs; got " + inputs.length + " inputs instead"); | ||
} | ||
|
||
for (int i = 0; i < inputs.length; i++) { | ||
inputTuples[i] = TeckleCTUtils.convertIngredient(inputs[i]); | ||
} | ||
|
||
ItemStack outputStack = CraftTweakerMC.getItemStack(output); | ||
CraftTweakerAPI.apply(new Add(new AlloyRecipe(outputStack, inputTuples))); | ||
} | ||
|
||
/** | ||
* Created by addRecipe. Registers the internal recipe with the AlloyRecipes registry. | ||
*/ | ||
public static class Add implements IAction { | ||
private final AlloyRecipe recipe; | ||
|
||
public Add(AlloyRecipe recipe) { | ||
this.recipe = recipe; | ||
} | ||
|
||
@Override | ||
public void apply() { | ||
AlloyRecipes.getInstance().registerRecipe(recipe); | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Adding " + recipe.getCraftingResult().toString() + " recipe for Alloy Furnace"; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a Remove action that will remove all recipes with the matching output. | ||
* | ||
* @param output The output to be matched against. | ||
* Optionally compares NBT if it is specified. | ||
*/ | ||
@ZenMethod | ||
public static void removeRecipe(IItemStack output) { | ||
CraftTweakerAPI.apply(new Remove(CraftTweakerMC.getItemStack(output))); | ||
} | ||
|
||
/** | ||
* Removes every recipe from the AlloyRecipes registry that has the specified output. | ||
*/ | ||
public static class Remove implements IAction { | ||
private final ItemStack output; | ||
|
||
public Remove(ItemStack output) { | ||
this.output = output; | ||
} | ||
|
||
@Override | ||
public void apply() { | ||
AlloyRecipes | ||
.getInstance() | ||
.unregisterMatching( | ||
recipe -> TeckleCTUtils.stacksEqual(output, recipe.getCraftingResult(), output.hasTagCompound()) | ||
); | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Removing Alloy Furnace recipe for " + output.toString(); | ||
} | ||
} | ||
|
||
/** | ||
* Creates a RemoveInput action that will remove all recipes with the matching inputs. | ||
* | ||
* @param inputs The inputs that will be matched against existing recipes. | ||
* NBT will be individually compared if it is specified. | ||
*/ | ||
@ZenMethod | ||
public static void removeInputRecipe(IIngredient[] inputs) { | ||
Tuple<Object, Integer>[] inputTuples = (Tuple<Object, Integer>[]) new Tuple[inputs.length]; | ||
|
||
if (inputs.length > 9) { | ||
throw new RuntimeException("Alloy Furnace cannot take more than 9 inputs; got " + inputs.length + " inputs instead"); | ||
} | ||
|
||
for (int i = 0; i < inputs.length; i++) { | ||
inputTuples[i] = TeckleCTUtils.convertIngredient(inputs[i]); | ||
} | ||
|
||
NonNullList<Object> referenceInputs = (new AlloyRecipe(ItemStack.EMPTY, inputTuples)).getInputs(); | ||
|
||
CraftTweakerAPI.apply(new RemoveInput(referenceInputs)); | ||
} | ||
|
||
/** | ||
* Removes every recipe from the AlloyRecipes registry that has the specified inputs. | ||
*/ | ||
public static class RemoveInput implements IAction { | ||
private NonNullList<Object> inputs; | ||
|
||
public RemoveInput(NonNullList<Object> inputs) { | ||
this.inputs = inputs; | ||
} | ||
|
||
@Override | ||
public void apply() { | ||
AlloyRecipes | ||
.getInstance() | ||
.unregisterMatching( | ||
recipe -> TeckleCTUtils.recipeIngredientsMatch(recipe.getInputs(), inputs) | ||
); | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Removing matching Alloy Furnace recipes for given inputs: " + inputs; | ||
} | ||
} | ||
|
||
/** | ||
* Creates a RemoveAll action that will remove every recipe from the Alloy Furnace. | ||
* Useful if you want to disable the Alloy Furnace entirely, or want to rewrite all of its recipes. | ||
*/ | ||
@ZenMethod | ||
public static void removeAll() { | ||
CraftTweakerAPI.apply(new RemoveAll()); | ||
} | ||
|
||
/** | ||
* Removes all recipes from AlloyRecipes. | ||
*/ | ||
public static class RemoveAll implements IAction { | ||
@Override | ||
public void apply() { | ||
AlloyRecipes.getInstance().unregisterAll(); | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Removing all recipes from Alloy Furnace"; | ||
} | ||
} | ||
} |
Oops, something went wrong.