Skip to content

Commit

Permalink
Add CraftTweaker support for the Alloy Furnace (#37)
Browse files Browse the repository at this point in the history
* 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
coderbot16 authored and TheCodedOne committed Nov 4, 2018
1 parent 01759ad commit 573b85d
Show file tree
Hide file tree
Showing 5 changed files with 385 additions and 16 deletions.
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ mc_version=1.12.2
jei_version=4.8.0.114
#MCMP Stuff
mcmp_version=2.3.4_49
#CT Stuff
ct_version=1.12-4.0.12.323
#Eytra Stuff
pdp_version=MC1.12_ver1.1.1
concrete_version=0.3.3-SNAPSHOT
Expand Down
8 changes: 8 additions & 0 deletions project.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ if (!ext.early) {
// MCMP Maven
url "http://maven.amadornes.com/"
}
maven {
// CraftTweaker2 Maven
name "Jared"
url "http://maven.blamejared.com/"
}
}

dependencies {
Expand All @@ -50,5 +55,8 @@ if (!ext.early) {

// MCMP
deobfCompile "MCMultiPart2:MCMultiPart-exp:${mcmp_version}"

// CraftTweaker
compile "CraftTweaker2:CraftTweaker2-MC1120-Main:${ct_version}"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.elytradev.teckle.common.item.ItemIngot;
import com.elytradev.teckle.common.item.ItemSiliconWafer;
import com.google.common.base.Charsets;
import com.google.common.base.Predicate;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;
import com.google.gson.Gson;
Expand All @@ -47,9 +48,6 @@ public class AlloyRecipes {
private static final AlloyRecipes INSTANCE = new AlloyRecipes();
private List<AlloyRecipe> recipes = new ArrayList<>();

public AlloyRecipes() {
}

public static AlloyRecipes getInstance() {
return INSTANCE;
}
Expand All @@ -58,53 +56,79 @@ public List<AlloyRecipe> getRecipes() {
return Lists.newArrayList(recipes);
}

public void init() {
/**
* Unregisters all the currently registered Alloy Recipes.
*/
public void unregisterAll() {
recipes.clear();
}

/**
* Registers the given recipe for use with the Alloy Furnace.
*
* @param recipe the AlloyRecipe to register.
*/
public void registerRecipe(AlloyRecipe recipe) {
recipes.add(recipe);
}

/**
* Removes all recipes matching the given predicate.
*
* @param matcher the predicate to check each recipe against.
*/
public void unregisterMatching(Predicate<AlloyRecipe> matcher) {
recipes.removeIf(matcher);
}

public void init() {
unregisterAll();

AlloyRecipe siliconBouleRecipe = new AlloyRecipe(
new ItemStack(TeckleObjects.itemSiliconBoule),
new Tuple<>("sand", 8),
new Tuple<>("coal", 8));
recipes.add(siliconBouleRecipe);
registerRecipe(siliconBouleRecipe);

AlloyRecipe redDopedWaferRecipe = new AlloyRecipe(
new ItemStack(TeckleObjects.itemSiliconWafer, 1, ItemSiliconWafer.WaferType.RED.getMetadata()),
new Tuple<>("dustRedstone", 4),
new Tuple<>(new ItemStack(TeckleObjects.itemSiliconWafer, 1, 0), null));
recipes.add(redDopedWaferRecipe);
registerRecipe(redDopedWaferRecipe);

AlloyRecipe blueDopedWaferRecipe = new AlloyRecipe(
new ItemStack(TeckleObjects.itemSiliconWafer, 1, ItemSiliconWafer.WaferType.BLUE.getMetadata()),
new Tuple<>("dustNikolite", 4),
new Tuple<>(new ItemStack(TeckleObjects.itemSiliconWafer, 1, 0), null));
recipes.add(blueDopedWaferRecipe);
registerRecipe(blueDopedWaferRecipe);

AlloyRecipe brassIngotRecipe = new AlloyRecipe(
new ItemStack(TeckleObjects.itemIngot, 4, ItemIngot.IngotType.BRASS.getMetadata()),
new Tuple<>("ingotTin", 1),
new Tuple<>("ingotCopper", 3)
);
recipes.add(brassIngotRecipe);
registerRecipe(brassIngotRecipe);

AlloyRecipe redAlloyIngotRecipe = new AlloyRecipe(
new ItemStack(TeckleObjects.itemIngot, 4, ItemIngot.IngotType.RED_ALLOY.getMetadata()),
new Tuple<>("ingotCopper", 1),
new Tuple<>("dustRedstone", 4)
);
recipes.add(redAlloyIngotRecipe);
registerRecipe(redAlloyIngotRecipe);

AlloyRecipe redAlloyIngotRecipeAlt = new AlloyRecipe(
new ItemStack(TeckleObjects.itemIngot, 4, ItemIngot.IngotType.RED_ALLOY.getMetadata()),
new Tuple<>("ingotIron", 1),
new Tuple<>("dustRedstone", 4)
);
recipes.add(redAlloyIngotRecipeAlt);
registerRecipe(redAlloyIngotRecipeAlt);

AlloyRecipe blueAlloyIngotRecipe = new AlloyRecipe(
new ItemStack(TeckleObjects.itemIngot, 4, ItemIngot.IngotType.BLUE_ALLOY.getMetadata()),
new Tuple<>("ingotSilver", 1),
new Tuple<>("dustNikolite", 4)
);
recipes.add(blueAlloyIngotRecipe);
registerRecipe(blueAlloyIngotRecipe);

// Adds all the vanilla recipes to the alloy furnace.
if (TeckleMod.CONFIG.importFurnaceRecipes)
Expand Down Expand Up @@ -170,7 +194,7 @@ public void init() {
Tuple<Object, Integer>[] inputsArray = new Tuple[inputs.size()];
inputsArray = inputs.toArray(inputsArray);
AlloyRecipe loadedRecipe = new AlloyRecipe(outputStack, inputsArray);
recipes.add(loadedRecipe);
registerRecipe(loadedRecipe);
}
}
}
Expand Down Expand Up @@ -198,10 +222,6 @@ private AlloyRecipe convertFurnaceRecipe(Map.Entry<ItemStack, ItemStack> furnace
return new AlloyRecipe(furnaceRecipe.getValue(), new Tuple<>(furnaceRecipe.getKey(), null));
}

public void clear() {
recipes.clear();
}

/**
* Data that we deserialize from JSON, uses getters and setters to prevent null results on certain optional vars.
*/
Expand Down
176 changes: 176 additions & 0 deletions src/main/java/com/elytradev/teckle/compat/ct/CTAlloyFurnace.java
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";
}
}
}
Loading

0 comments on commit 573b85d

Please sign in to comment.