Permalink
Browse files

Also accept material names as ingredients in recipes

  • Loading branch information...
PseudoKnight committed Aug 19, 2018
1 parent 4f70fba commit a34b3aff570ba895f15c707ec419cac7b62a6b24
@@ -1,10 +1,14 @@
package com.laytonsmith.abstraction;
import com.laytonsmith.abstraction.blocks.MCMaterial;
public interface MCFurnaceRecipe extends MCRecipe {
String getKey();
MCItemStack getInput();
void setInput(MCItemStack input);
void setInput(MCMaterial mat);
}
@@ -1,5 +1,7 @@
package com.laytonsmith.abstraction;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import java.util.Map;
public interface MCShapedRecipe extends MCRecipe {
@@ -12,5 +14,7 @@
void setIngredient(char key, MCItemStack ingredient);
void setIngredient(char key, MCMaterial mat);
void setShape(String[] shape);
}
@@ -1,5 +1,7 @@
package com.laytonsmith.abstraction;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import java.util.List;
public interface MCShapelessRecipe extends MCRecipe {
@@ -8,5 +10,7 @@
MCShapelessRecipe addIngredient(MCItemStack ingredient);
MCShapelessRecipe addIngredient(MCMaterial ingredient);
List<MCItemStack> getIngredients();
}
@@ -64,6 +64,14 @@ public static MCLocation GetLocation(MCWorld w, double x, double y, double z) {
return convertor.GetServerEventMixin();
}
public static MCMaterial GetMaterialFromLegacy(int type, int qty) {
return convertor.GetMaterialFromLegacy(type, qty);
}
public static MCMaterial GetMaterialFromLegacy(String name, int qty) {
return convertor.GetMaterialFromLegacy(name, qty);
}
public static MCItemStack GetItemStack(int type, int qty) {
return convertor.GetItemStack(type, qty);
}
@@ -2,7 +2,9 @@
import com.laytonsmith.abstraction.MCFurnaceRecipe;
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.enums.MCRecipeType;
import org.bukkit.Material;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
@@ -44,4 +46,9 @@ public MCItemStack getInput() {
public void setInput(MCItemStack input) {
fr.setInput(((ItemStack) input.getHandle()).getData());
}
@Override
public void setInput(MCMaterial mat) {
fr.setInput((Material) mat.getHandle());
}
}
@@ -2,9 +2,12 @@
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCShapedRecipe;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.enums.MCRecipeType;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
@@ -53,7 +56,12 @@ public MCItemStack getResult() {
@Override
public void setIngredient(char key, MCItemStack ingredient) {
r.setIngredient(key, ((ItemStack) ingredient.getHandle()).getData());
r.setIngredient(key, ((ItemStack) ingredient.getHandle()).getType());
}
@Override
public void setIngredient(char key, MCMaterial mat) {
r.setIngredient(key, (Material) mat.getHandle());
}
@Override
@@ -2,9 +2,12 @@
import com.laytonsmith.abstraction.MCItemStack;
import com.laytonsmith.abstraction.MCShapelessRecipe;
import com.laytonsmith.abstraction.blocks.MCMaterial;
import com.laytonsmith.abstraction.enums.MCRecipeType;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapelessRecipe;
@@ -48,7 +51,13 @@ public MCItemStack getResult() {
@Override
public MCShapelessRecipe addIngredient(MCItemStack ingredient) {
r.addIngredient(ingredient.getAmount(), ((ItemStack) ingredient.getHandle()).getData());
r.addIngredient(ingredient.getAmount(), ((ItemStack) ingredient.getHandle()).getType());
return this;
}
@Override
public MCShapelessRecipe addIngredient(MCMaterial ingredient) {
r.addIngredient((Material) ingredient.getHandle());
return this;
}
}
@@ -280,9 +280,9 @@ public MCItemStack item(Construct i, Target t, boolean legacy) {
mat = item.get("name", t).val();
if(mat.equals("MAP")) {
// special handling, ignore data until later
material = StaticLayer.GetConvertor().GetMaterialFromLegacy(mat, 0);
material = StaticLayer.GetMaterialFromLegacy(mat, 0);
} else {
material = StaticLayer.GetConvertor().GetMaterialFromLegacy(mat, data);
material = StaticLayer.GetMaterialFromLegacy(mat, data);
}
} else {
Construct type = item.get("type", t);
@@ -302,9 +302,9 @@ public MCItemStack item(Construct i, Target t, boolean legacy) {
int id = Static.getInt32(type, t);
if(id == 358) {
// special map handling, ignore data until later
material = StaticLayer.GetConvertor().GetMaterialFromLegacy(id, 0);
material = StaticLayer.GetMaterialFromLegacy(id, 0);
} else {
material = StaticLayer.GetConvertor().GetMaterialFromLegacy(id, data);
material = StaticLayer.GetMaterialFromLegacy(id, data);
}
}
if(material == null) {
@@ -1455,28 +1455,32 @@ public MCRecipe recipe(Construct c, Target t) {
throw new CREFormatException("Ingredients array is invalid.", t);
}
for(String key : shapedIngredients.stringKeySet()) {
MCItemStack is;
MCMaterial mat = null;
Construct ingredient = shapedIngredients.get(key, t);
if(ingredient instanceof CString) {
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "String ingredients may not be accurate.", t);
CString item = (CString) ingredient;
if(item.val().contains(":")) {
String[] split = item.val().split(":");
is = StaticLayer.GetItemStack(Integer.valueOf(split[0]), Integer.valueOf(split[1]), 1);
} else {
is = StaticLayer.GetItemStack(Integer.valueOf(item.val()), 1);
mat = StaticLayer.GetMaterial(ingredient.val());
if(mat == null) {
// maybe legacy item format
try {
if(ingredient.val().contains(":")) {
String[] split = ingredient.val().split(":");
mat = StaticLayer.GetMaterialFromLegacy(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
} else {
mat = StaticLayer.GetConvertor().getMaterial(Integer.valueOf(ingredient.val()));
}
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Numeric item formats (eg. \"0:0\" are deprecated.", t);
} catch (NumberFormatException ex) {}
}
} else if(ingredient instanceof CInt) {
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Integer ingredients may not be accurate.", t);
is = StaticLayer.GetItemStack(Static.getInt32(ingredient, t), 1);
mat = StaticLayer.GetConvertor().getMaterial(Static.getInt32(ingredient, t));
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Numeric item ingredients are deprecated.", t);
} else if(ingredient instanceof CArray) {
is = item(ingredient, t);
} else if(ingredient instanceof CNull) {
is = StaticLayer.GetItemStack("AIR", 0);
} else {
throw new CREFormatException("Item was not found", t);
mat = item(ingredient, t).getType();
}
if(mat == null) {
throw new CREFormatException("Ingredient is invalid: " + ingredient.val(), t);
}
((MCShapedRecipe) ret).setIngredient(key.charAt(0), is);
((MCShapedRecipe) ret).setIngredient(key.charAt(0), mat);
}
return ret;
@@ -1486,27 +1490,45 @@ public MCRecipe recipe(Construct c, Target t) {
throw new CREFormatException("Ingredients array is invalid.", t);
}
for(Construct ingredient : ingredients.asList()) {
MCItemStack is;
if(ingredient instanceof CString) {
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "String ingredients may not be accurate.", t);
if(ingredient.val().contains(":")) {
String[] split = ingredient.val().split(":");
is = StaticLayer.GetItemStack(Integer.valueOf(split[0]), Integer.valueOf(split[1]), 1);
} else {
is = StaticLayer.GetItemStack(Integer.valueOf(ingredient.val()), 1);
MCMaterial mat = StaticLayer.GetMaterial(ingredient.val());
if(mat == null) {
// maybe legacy item format
try {
if(ingredient.val().contains(":")) {
String[] split = ingredient.val().split(":");
mat = StaticLayer.GetMaterialFromLegacy(Integer.valueOf(split[0]), Integer.valueOf(split[1]));
} else {
mat = StaticLayer.GetConvertor().getMaterial(Integer.valueOf(ingredient.val()));
}
CHLog.GetLogger().w(CHLog.Tags.DEPRECATION, "Numeric item formats (eg. \"0:0\" are deprecated.", t);
} catch (NumberFormatException ex) {}
if(mat == null) {
throw new CREFormatException("Ingredient is invalid: " + ingredient.val(), t);
}
}
((MCShapelessRecipe) ret).addIngredient(mat);
} else if(ingredient instanceof CArray) {
is = item(ingredient, t);
((MCShapelessRecipe) ret).addIngredient(item(ingredient, t));
} else {
throw new CREFormatException("Item was not found", t);
}
((MCShapelessRecipe) ret).addIngredient(is);
}
return ret;
case FURNACE:
CArray is = Static.getArray(recipe.get("input", t), t);
((MCFurnaceRecipe) ret).setInput(item(is, t));
Construct input = recipe.get("input", t);
if(input instanceof CString) {
MCMaterial mat = StaticLayer.GetMaterial(input.val());
if(mat == null) {
throw new CREFormatException("Furnace input is invalid: " + input.val(), t);
}
((MCFurnaceRecipe) ret).setInput(mat);
} else if(input instanceof CArray) {
((MCFurnaceRecipe) ret).setInput(item(input, t));
} else {
throw new CREFormatException("Item was not found", t);
}
return ret;
default:
Oops, something went wrong.

0 comments on commit a34b3af

Please sign in to comment.