Recipes to make the wires when IC2 isn't installed

This commit is contained in:
malte0811 2018-08-11 21:30:19 +02:00
parent 1c24abda2c
commit 220b74ab94
10 changed files with 165 additions and 97 deletions

View File

@ -26,19 +26,21 @@ import net.minecraft.util.NonNullList;
import net.minecraft.world.World;
import net.minecraftforge.oredict.OreDictionary;
import net.minecraftforge.registries.IForgeRegistryEntry;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.Random;
public class RecipeCoilLength extends IForgeRegistryEntry.Impl<IRecipe> implements IRecipe {
public final ItemStack coil;
public final Ingredient cable;
public final List<Pair<Ingredient, Integer>> cables;
private final int maxLength;
public RecipeCoilLength(ItemStack coil, Ingredient cable) {
public RecipeCoilLength(ItemStack coil, List<Pair<Ingredient, Integer>> cables) {
this.coil = coil;
this.cable = cable;
this.cables = cables;
maxLength = ItemIC2Coil.getMaxWireLength(this.coil);
}
@ -81,26 +83,29 @@ public class RecipeCoilLength extends IForgeRegistryEntry.Impl<IRecipe> implemen
ret.set(i, currStack);
ItemIC2Coil.setLength(currStack, -length);
}
} else if (isCable(curr)) {
length--;
} else {
length -= getCableLength(curr);
}
}
return ret;
}
private int getLength(InventoryCrafting inv) {
int cableLength = 0;
int totalLength = 0;
for (int i = 0; i < inv.getSizeInventory(); i++) {
ItemStack curr = inv.getStackInSlot(i);
if (OreDictionary.itemMatches(curr, coil, false)) {
cableLength += ItemIC2Coil.getLength(curr);
} else if (isCable(curr)) {
cableLength++;
} else if (!curr.isEmpty()) {
return -1;
totalLength += ItemIC2Coil.getLength(curr);
} else {
int slotLength = getCableLength(curr);
if (slotLength>0) {
totalLength += slotLength;
} else if (!curr.isEmpty()) {
return -1;
}
}
}
return cableLength;
return totalLength;
}
@Nonnull
@ -109,8 +114,11 @@ public class RecipeCoilLength extends IForgeRegistryEntry.Impl<IRecipe> implemen
Random r = new Random();
NonNullList<Ingredient> ret = NonNullList.withSize(9, Ingredient.EMPTY);
for (int i = 0;i<ret.size();i++) {
ItemStack[] types = new ItemStack[cable.getMatchingStacks().length+1];
int length = types.length;
int length = 1;
for (Pair<Ingredient, Integer> cable:cables) {
length += cable.getLeft().getMatchingStacks().length;
}
ItemStack[] types = new ItemStack[length];
int cablePos = 0;
if (r.nextBoolean()) {
types[length-1] = coil;
@ -118,19 +126,23 @@ public class RecipeCoilLength extends IForgeRegistryEntry.Impl<IRecipe> implemen
types[0] = coil;
cablePos = 1;
}
System.arraycopy(cable.getMatchingStacks(), 0, types, cablePos, length-1);
for (Pair<Ingredient, Integer> cable : cables) {
ItemStack[] matching = cable.getLeft().getMatchingStacks();
System.arraycopy(matching, 0, types, cablePos, matching.length);
cablePos += matching.length;
}
ret.set(i, new UnmatchedIngredient(types));
}
return ret;
}
private boolean isCable(ItemStack stack) {
for (ItemStack curr:cable.getMatchingStacks()) {
if (ItemStack.areItemsEqual(stack, curr) && ItemStack.areItemStackTagsEqual(stack, curr)) {
return true;
private int getCableLength(ItemStack stack) {
for (Pair<Ingredient, Integer> ingred:cables) {
if (ingred.getLeft().apply(stack)) {
return ingred.getRight();
}
}
return false;
return 0;
}
//There is probably a better way to do this...

View File

@ -15,17 +15,32 @@
package malte0811.industrialWires.crafting.factories;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import malte0811.industrialWires.crafting.RecipeCoilLength;
import net.minecraft.item.crafting.Ingredient;
import net.minecraftforge.common.crafting.CraftingHelper;
import net.minecraftforge.common.crafting.IRecipeFactory;
import net.minecraftforge.common.crafting.JsonContext;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import java.util.ArrayList;
import java.util.List;
public class WireCoilFactory implements IRecipeFactory {
@Override
public RecipeCoilLength parse(JsonContext context, JsonObject json) {
JsonObject coil = json.getAsJsonObject("coil");
JsonObject cable = json.getAsJsonObject("cable");
return new RecipeCoilLength(CraftingHelper.getItemStack(coil, context), CraftingHelper.getIngredient(cable, context));
JsonArray cables = json.getAsJsonArray("cables");
List<Pair<Ingredient, Integer>> cablesList = new ArrayList<>(cables.size());
for (JsonElement ele:cables) {
JsonObject obj = ele.getAsJsonObject();
int length = obj.get("length").getAsInt();
Ingredient ingred = CraftingHelper.getIngredient(obj.getAsJsonObject("ingredient"), context);
cablesList.add(new ImmutablePair<>(ingred, length));
}
return new RecipeCoilLength(CraftingHelper.getItemStack(coil, context), cablesList);
}
}

View File

@ -1,18 +1,24 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:copper,insulation:0"
},
"cables": [
{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:copper,insulation:0"
}
},
{
"length": 8,
"ingredient": {
"item": "immersiveengineering:wirecoil",
"data": 0
}
}
],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 1
},
"conditions": [
{
"type": "forge:mod_loaded",
"modid": "ic2"
}
]
}
}

View File

@ -1,18 +1,24 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:copper,insulation:1"
},
"cables": [
{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:copper,insulation:1"
}
},
{
"length": 8,
"ingredient": {
"item": "immersiveengineering:wirecoil",
"data": 6
}
}
],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 6
},
"conditions": [
{
"type": "forge:mod_loaded",
"modid": "ic2"
}
]
}
}

View File

@ -1,10 +1,13 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:glass,insulation:0"
},
"cables": [{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:glass,insulation:0"
}
}],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 4

View File

@ -1,18 +1,24 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:gold,insulation:0"
},
"cables": [
{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:gold,insulation:0"
}
},
{
"length": 8,
"ingredient": {
"item": "immersiveengineering:wirecoil",
"data": 1
}
}
],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 2
},
"conditions": [
{
"type": "forge:mod_loaded",
"modid": "ic2"
}
]
}
}

View File

@ -1,18 +1,24 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:gold,insulation:2"
},
"cables": [
{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:gold,insulation:2"
}
},
{
"length": 8,
"ingredient": {
"item": "immersiveengineering:wirecoil",
"data": 7
}
}
],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 7
},
"conditions": [
{
"type": "forge:mod_loaded",
"modid": "ic2"
}
]
}
}

View File

@ -1,18 +1,24 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:iron,insulation:0"
},
"cables": [
{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:iron,insulation:0"
}
},
{
"length": 8,
"ingredient": {
"item": "immersiveengineering:wirecoil",
"data": 2
}
}
],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 3
},
"conditions": [
{
"type": "forge:mod_loaded",
"modid": "ic2"
}
]
}
}

View File

@ -1,10 +1,15 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:tin,insulation:0"
},
"cables": [
{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:tin,insulation:0"
}
}
],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 0

View File

@ -1,10 +1,13 @@
{
"type": "industrialwires:wire_coil",
"cable": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:tin,insulation:1"
},
"cables": [{
"length": 1,
"ingredient": {
"type": "industrialwires:ic2_item",
"name": "cable",
"variant": "type:tin,insulation:1"
}
}],
"coil": {
"item": "industrialwires:ic2_wire_coil",
"data": 5