mirror of
https://github.com/Sollace/Unicopia.git
synced 2025-02-25 14:14:32 +01:00
Recipes can now handle multiple inputs/outputs
This commit is contained in:
parent
36eb492a6e
commit
a292cbb9fa
15 changed files with 152 additions and 71 deletions
|
@ -0,0 +1,115 @@
|
||||||
|
package com.minelittlepony.unicopia.enchanting;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
|
import com.minelittlepony.unicopia.spell.SpellRegistry;
|
||||||
|
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
public interface SpellIngredient {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
static SpellIngredient parse(JsonElement json) {
|
||||||
|
if (json.isJsonArray()) {
|
||||||
|
return Compound.parse(json.getAsJsonArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
return Single.parse(json.getAsJsonObject());
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean matches(ItemStack other, int materialMult);
|
||||||
|
|
||||||
|
class Compound implements SpellIngredient {
|
||||||
|
private final List<SpellIngredient> items;
|
||||||
|
|
||||||
|
Compound(List<SpellIngredient> items) {
|
||||||
|
this.items = items;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(ItemStack other, int materialMult) {
|
||||||
|
return items.stream().anyMatch(item -> item.matches(other, materialMult));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
static SpellIngredient parse(JsonArray json) {
|
||||||
|
if (json.size() > 0) {
|
||||||
|
List<SpellIngredient> items = Lists.newArrayList();
|
||||||
|
|
||||||
|
for (JsonElement j : json) {
|
||||||
|
SpellIngredient item = SpellIngredient.parse(j);
|
||||||
|
|
||||||
|
if (item != null) {
|
||||||
|
items.add(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!items.isEmpty()) {
|
||||||
|
return new Compound(items);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Single implements SpellIngredient {
|
||||||
|
|
||||||
|
private final ItemStack contained;
|
||||||
|
private final boolean ignoreMeta;
|
||||||
|
|
||||||
|
Single(ItemStack stack, boolean meta) {
|
||||||
|
contained = stack;
|
||||||
|
ignoreMeta = meta;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean matches(ItemStack other, int materialMult) {
|
||||||
|
if (other.isEmpty() != contained.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
} else if (other.isEmpty()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (other.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (contained.getItem() == other.getItem()
|
||||||
|
&& (ignoreMeta || other.getMetadata() == contained.getMetadata())
|
||||||
|
&& ItemStack.areItemStackTagsEqual(contained, other)) {
|
||||||
|
return other.getCount() >= (materialMult * contained.getCount());
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public static Single parse(JsonObject json) {
|
||||||
|
Item item = json.has("item") ? Item.getByNameOrId(json.get("item").getAsString()) : null;
|
||||||
|
|
||||||
|
if (item != null) {
|
||||||
|
int metadata = Math.max(0, json.has("data") ? json.get("data").getAsInt() : 0);
|
||||||
|
int size = Math.max(1, json.has("count") ? json.get("count").getAsInt() : 1);
|
||||||
|
String spell = json.has("spell") ? json.get("spell").getAsString() : null;
|
||||||
|
|
||||||
|
ItemStack stack = new ItemStack(item, size, metadata);
|
||||||
|
|
||||||
|
if (spell != null) {
|
||||||
|
stack = SpellRegistry.instance().enchantStack(stack, spell);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Single(stack, !json.has("data"));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,70 +1,63 @@
|
||||||
package com.minelittlepony.unicopia.enchanting;
|
package com.minelittlepony.unicopia.enchanting;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import com.google.gson.JsonElement;
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
|
import com.google.gson.JsonParseException;
|
||||||
import com.minelittlepony.unicopia.init.UItems;
|
import com.minelittlepony.unicopia.init.UItems;
|
||||||
import com.minelittlepony.unicopia.inventory.InventorySpellBook;
|
import com.minelittlepony.unicopia.inventory.InventorySpellBook;
|
||||||
import com.minelittlepony.unicopia.spell.SpellRegistry;
|
import com.minelittlepony.unicopia.spell.SpellRegistry;
|
||||||
|
|
||||||
import net.minecraft.inventory.InventoryCrafting;
|
import net.minecraft.inventory.InventoryCrafting;
|
||||||
import net.minecraft.item.Item;
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
import net.minecraft.item.crafting.IRecipe;
|
import net.minecraft.item.crafting.IRecipe;
|
||||||
import net.minecraft.util.NonNullList;
|
import net.minecraft.util.NonNullList;
|
||||||
import net.minecraft.util.ResourceLocation;
|
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
|
|
||||||
import net.minecraftforge.registries.IForgeRegistryEntry.Impl;
|
import net.minecraftforge.registries.IForgeRegistryEntry.Impl;
|
||||||
|
|
||||||
public class SpellRecipe extends Impl<IRecipe> implements IRecipe {
|
public class SpellRecipe extends Impl<IRecipe> implements IRecipe {
|
||||||
|
|
||||||
private final ResourceLocation spellitem;
|
private final SpellIngredient spellitem;
|
||||||
|
|
||||||
private final String spellId;
|
private final String spellId;
|
||||||
|
|
||||||
private final NonNullList<RecipeItem> ingredients;
|
private final NonNullList<SpellIngredient> ingredients;
|
||||||
|
|
||||||
public static IRecipe deserialize(JsonObject json) {
|
public static IRecipe deserialize(JsonObject json) {
|
||||||
|
|
||||||
NonNullList<RecipeItem> ingredients = NonNullList.create();
|
NonNullList<SpellIngredient> ingredients = NonNullList.create();
|
||||||
|
|
||||||
for (JsonElement i : json.get("ingredients").getAsJsonArray()) {
|
for (JsonElement i : json.get("ingredients").getAsJsonArray()) {
|
||||||
JsonObject o = i.getAsJsonObject();
|
SpellIngredient ingredient = SpellIngredient.parse(i);
|
||||||
|
|
||||||
Item item = o.has("item") ? Item.getByNameOrId(o.get("item").getAsString()) : null;
|
if (ingredient != null) {
|
||||||
|
ingredients.add(ingredient);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (item != null) {
|
if (ingredients.isEmpty()) {
|
||||||
int metadata = Math.max(0, o.has("data") ? o.get("data").getAsInt() : 0);
|
throw new JsonParseException("Recipe cannot have 0 ingredients");
|
||||||
int size = Math.max(1, o.has("count") ? o.get("count").getAsInt() : 1);
|
|
||||||
String spell = o.has("spell") ? o.get("spell").getAsString() : null;
|
|
||||||
|
|
||||||
ItemStack stack = new ItemStack(item, size, metadata);
|
|
||||||
|
|
||||||
if (spell != null) {
|
|
||||||
stack = SpellRegistry.instance().enchantStack(stack, spell);
|
|
||||||
}
|
|
||||||
|
|
||||||
ingredients.add(new RecipeItem(stack, !o.has("data")));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
json = json.get("result").getAsJsonObject();
|
json = json.get("result").getAsJsonObject();
|
||||||
|
|
||||||
String spellId = json.get("spell").getAsString();
|
String spellId = json.get("spell").getAsString();
|
||||||
|
|
||||||
Item spellitem = json.has("item") ? Item.getByNameOrId(json.get("item").getAsString()) : null;
|
SpellIngredient result = SpellIngredient.parse(json.get("item"));
|
||||||
if (spellitem == null) {
|
|
||||||
spellitem = UItems.spell;
|
if (result == null) {
|
||||||
|
throw new JsonParseException("Recipe cannot have no enchantable input");
|
||||||
}
|
}
|
||||||
|
|
||||||
return new SpellRecipe(spellitem, spellId, ingredients);
|
return new SpellRecipe(result, spellId, ingredients);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpellRecipe(Item spellitem, String spellName, NonNullList<RecipeItem> ingredients) {
|
public SpellRecipe(SpellIngredient spellitem, String spellName, NonNullList<SpellIngredient> ingredients) {
|
||||||
this.spellitem = spellitem.getRegistryName();
|
this.spellitem = spellitem;
|
||||||
this.spellId = spellName;
|
this.spellId = spellName;
|
||||||
this.ingredients = ingredients;
|
this.ingredients = ingredients;
|
||||||
}
|
}
|
||||||
|
@ -77,13 +70,13 @@ public class SpellRecipe extends Impl<IRecipe> implements IRecipe {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!spellitem.equals(enchantedStack.getItem().getRegistryName())) {
|
if (!spellitem.matches(enchantedStack, enchantedStack.getCount())) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int materialMult = enchantedStack.getCount();
|
int materialMult = enchantedStack.getCount();
|
||||||
|
|
||||||
ArrayList<RecipeItem> toMatch = Lists.newArrayList(ingredients);
|
ArrayList<SpellIngredient> toMatch = Lists.newArrayList(ingredients);
|
||||||
|
|
||||||
for (int i = 0; i < inv.getSizeInventory(); i++) {
|
for (int i = 0; i < inv.getSizeInventory(); i++) {
|
||||||
ItemStack stack = inv.getStackInSlot(i);
|
ItemStack stack = inv.getStackInSlot(i);
|
||||||
|
@ -97,7 +90,7 @@ public class SpellRecipe extends Impl<IRecipe> implements IRecipe {
|
||||||
return toMatch.isEmpty();
|
return toMatch.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean removeMatch(ArrayList<RecipeItem> toMatch, ItemStack stack, int materialMult) {
|
private boolean removeMatch(List<SpellIngredient> toMatch, ItemStack stack, int materialMult) {
|
||||||
return toMatch.stream()
|
return toMatch.stream()
|
||||||
.filter(s -> s.matches(stack, materialMult))
|
.filter(s -> s.matches(stack, materialMult))
|
||||||
.findFirst()
|
.findFirst()
|
||||||
|
@ -123,6 +116,7 @@ public class SpellRecipe extends Impl<IRecipe> implements IRecipe {
|
||||||
@Override
|
@Override
|
||||||
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
|
public NonNullList<ItemStack> getRemainingItems(InventoryCrafting inv) {
|
||||||
NonNullList<ItemStack> remainers = NonNullList.<ItemStack>withSize(inv.getSizeInventory(), ItemStack.EMPTY);
|
NonNullList<ItemStack> remainers = NonNullList.<ItemStack>withSize(inv.getSizeInventory(), ItemStack.EMPTY);
|
||||||
|
|
||||||
for (int i = 0; i < remainers.size(); i++) {
|
for (int i = 0; i < remainers.size(); i++) {
|
||||||
ItemStack stack = inv.getStackInSlot(i);
|
ItemStack stack = inv.getStackInSlot(i);
|
||||||
|
|
||||||
|
@ -130,37 +124,9 @@ public class SpellRecipe extends Impl<IRecipe> implements IRecipe {
|
||||||
remainers.set(i, new ItemStack(stack.getItem().getContainerItem()));
|
remainers.set(i, new ItemStack(stack.getItem().getContainerItem()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return remainers;
|
return remainers;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class RecipeItem {
|
|
||||||
|
|
||||||
private final ItemStack contained;
|
|
||||||
private final boolean ignoreMeta;
|
|
||||||
|
|
||||||
RecipeItem(ItemStack stack, boolean meta) {
|
|
||||||
contained = stack;
|
|
||||||
ignoreMeta = meta;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean matches(ItemStack other, int materialMult) {
|
|
||||||
if (other.isEmpty() != contained.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
} else if (other.isEmpty()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (other.isEmpty()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (contained.getItem() == other.getItem()
|
|
||||||
&& (ignoreMeta || other.getMetadata() == contained.getMetadata())
|
|
||||||
&& ItemStack.areItemStackTagsEqual(contained, other)) {
|
|
||||||
return other.getCount() >= (materialMult * contained.getCount());
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "minecraft:redstone" }
|
{ "item": "minecraft:redstone" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "charge"
|
"spell": "charge"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "minecraft:red_flower", "data": 0 }
|
{ "item": "minecraft:red_flower", "data": 0 }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "drake"
|
"spell": "drake"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "minecraft:lava_bucket" }
|
{ "item": "minecraft:lava_bucket" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "fire"
|
"spell": "fire"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "minecraft:water_bucket" }
|
{ "item": "minecraft:water_bucket" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "ice"
|
"spell": "ice"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "minecraft:lava_bucket" }
|
{ "item": "minecraft:lava_bucket" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:corrupted_gem",
|
"item": { "item": "unicopia:corrupted_gem" },
|
||||||
"spell": "inferno"
|
"spell": "inferno"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "unicopia:apple_green" }
|
{ "item": "unicopia:apple_green" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "light"
|
"spell": "light"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "minecraft:rotten_flesh" }
|
{ "item": "minecraft:rotten_flesh" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:corrupted_gem",
|
"item": { "item": "unicopia:corrupted_gem" },
|
||||||
"spell": "necromancy"
|
"spell": "necromancy"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@
|
||||||
{ "item": "unicopia:gem", "spell": "fire" }
|
{ "item": "unicopia:gem", "spell": "fire" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "portal"
|
"spell": "portal"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,7 +7,7 @@
|
||||||
{ "item": "minecraft:egg" }
|
{ "item": "minecraft:egg" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:corrupted_gem",
|
"item": { "item": "unicopia:corrupted_gem" },
|
||||||
"spell": "shield"
|
"spell": "shield"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "unicopia:gem", "spell": "light" }
|
{ "item": "unicopia:gem", "spell": "light" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "reveal"
|
"spell": "reveal"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "minecraft:blaze_powder" }
|
{ "item": "minecraft:blaze_powder" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "shield"
|
"spell": "shield"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -5,7 +5,7 @@
|
||||||
{ "item": "unicopia:gem", "spell": "fire" }
|
{ "item": "unicopia:gem", "spell": "fire" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:corrupted_gem",
|
"item": { "item": "unicopia:corrupted_gem" },
|
||||||
"spell": "vortex"
|
"spell": "vortex"
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -6,7 +6,7 @@
|
||||||
{ "item": "unicopia:gem", "spell": "portal" }
|
{ "item": "unicopia:gem", "spell": "portal" }
|
||||||
],
|
],
|
||||||
"result": {
|
"result": {
|
||||||
"item": "unicopia:gem",
|
"item": { "item": "unicopia:gem" },
|
||||||
"spell": "vortex"
|
"spell": "vortex"
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue