use of net.minecraft.item.crafting.Ingredient in project Wizardry by TeamWizardry.
the class RecipeShapedFluidFactory method parse.
@Override
public IRecipe parse(JsonContext context, JsonObject json) {
String group = JsonUtils.getString(json, "group", "");
Map<Character, Ingredient> ingredientMap = new HashMap<>();
ingredientMap.put(' ', Ingredient.EMPTY);
for (Entry<String, JsonElement> entry : JsonUtils.getJsonObject(json, "key").entrySet()) {
if (entry.getKey().length() != 1)
throw new JsonSyntaxException("Invalid key entry: '" + entry.getKey() + "' is an invalid symbol (must be 1 character only).");
if (" ".equals(entry.getKey()))
throw new JsonSyntaxException("Invalid key entry: ' ' is a reserved symbol.");
ingredientMap.put(entry.getKey().toCharArray()[0], CraftingHelper.getIngredient(entry.getValue(), context));
}
JsonArray jsonPattern = JsonUtils.getJsonArray(json, "pattern");
if (jsonPattern.size() == 0)
throw new JsonSyntaxException("Invalid pattern: empty pattern not allowed");
String[] pattern = new String[jsonPattern.size()];
for (int x = 0; x < pattern.length; x++) {
String line = JsonUtils.getString(jsonPattern.get(x), "pattern[" + x + "]");
if (x > 0 && pattern[0].length() != line.length())
throw new JsonSyntaxException("Invalid pattern: each row must be the same width");
pattern[x] = line;
}
ShapedPrimer primer = new ShapedPrimer();
primer.width = pattern[0].length();
primer.height = pattern.length;
primer.mirrored = JsonUtils.getBoolean(json, "mirrored", true);
primer.input = NonNullList.withSize(primer.width * primer.height, Ingredient.EMPTY);
Set<Character> keys = Sets.newHashSet(ingredientMap.keySet());
keys.remove(' ');
int x = 0;
for (String line : pattern) {
for (char c : line.toCharArray()) {
Ingredient ingredient = ingredientMap.get(c);
if (ingredient == null)
throw new JsonSyntaxException("Pattern references symbol '" + c + "' but it's not defined in the key");
primer.input.set(x++, ingredient);
keys.remove(c);
}
}
if (!keys.isEmpty())
throw new JsonSyntaxException("Key defineds symbols that aren't used in pattern: " + keys);
ItemStack result = CraftingHelper.getItemStack(JsonUtils.getJsonObject(json, "result"), context);
RecipeShapedFluid recipe = new RecipeShapedFluid(group.isEmpty() ? null : new ResourceLocation(group), result, primer);
return recipe;
}
use of net.minecraft.item.crafting.Ingredient in project Wizardry by TeamWizardry.
the class FluidRecipeLoader method processRecipes.
@SuppressWarnings("deprecation")
public void processRecipes(Map<String, FluidCrafter> recipeRegistry, Multimap<Ingredient, FluidCrafter> recipes) {
Wizardry.logger.info("<<========================================================================>>");
Wizardry.logger.info("> Starting fluid recipe loading.");
JsonContext context = new JsonContext("minecraft");
LinkedList<File> recipeFiles = new LinkedList<>();
Stack<File> toProcess = new Stack<>();
toProcess.push(directory);
while (!toProcess.isEmpty()) {
File file = toProcess.pop();
if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null)
for (File child : children) toProcess.push(child);
} else if (file.isFile())
if (file.getName().endsWith(".json"))
recipeFiles.add(file);
}
fileLoop: for (File file : recipeFiles) {
try {
if (!file.exists()) {
Wizardry.logger.error(" > SOMETHING WENT WRONG! " + file.getPath() + " can NOT be found. Ignoring file...");
continue;
}
JsonElement element;
try {
element = new JsonParser().parse(new FileReader(file));
} catch (FileNotFoundException e) {
Wizardry.logger.error(" > SOMETHING WENT WRONG! " + file.getPath() + " can NOT be found. Ignoring file...");
continue;
}
if (element == null) {
Wizardry.logger.error(" > SOMETHING WENT WRONG! Could not parse " + file.getPath() + ". Ignoring file...");
continue;
}
if (!element.isJsonObject()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT contain a JsonObject. Ignoring file...: " + element.toString());
continue;
}
JsonObject fileObject = element.getAsJsonObject();
List<Ingredient> extraInputs = new LinkedList<>();
Fluid fluid = ModFluids.MANA.getActual();
int duration = 100;
int required = 1;
boolean consume = false;
boolean explode = false;
boolean bubbling = true;
boolean harp = true;
if (recipeRegistry.containsKey(file.getPath())) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " already exists in the recipe map. Ignoring file...: " + element.toString());
continue;
}
if (!fileObject.has("output")) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT specify a recipe output. Ignoring file...: " + element.toString());
continue;
}
if (!fileObject.get("output").isJsonObject()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT provide a valid output. Ignoring file...: " + element.toString());
continue;
}
if (!fileObject.has("input")) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT provide an initial input item. Ignoring file...: " + element.toString());
continue;
}
JsonElement inputObject = fileObject.get("input");
Ingredient inputItem = CraftingHelper.getIngredient(inputObject, context);
if (inputItem == Ingredient.EMPTY) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT provide a valid input item. Ignoring file...: " + element.toString());
continue;
}
if (fileObject.has("extraInputs")) {
if (!fileObject.get("extraInputs").isJsonArray()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " has extra inputs NOT in a JsonArray format. Ignoring file...: " + element.toString());
continue;
}
JsonArray extraInputArray = fileObject.get("extraInputs").getAsJsonArray();
for (JsonElement extraInput : extraInputArray) {
Ingredient ingredient = CraftingHelper.getIngredient(extraInput, context);
if (ingredient == Ingredient.EMPTY) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT provide a valid extra input item. Ignoring file...: " + element.toString());
continue fileLoop;
}
extraInputs.add(ingredient);
}
}
if (fileObject.has("fluid")) {
if (!fileObject.get("fluid").isJsonPrimitive() || !fileObject.getAsJsonPrimitive("fluid").isString()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT give rfluid as a string. Ignoring file...: " + element.toString());
continue;
}
fluid = FluidRegistry.getFluid(fileObject.get("fluid").getAsString());
}
if (fileObject.has("duration")) {
if (!fileObject.get("duration").isJsonPrimitive() || !fileObject.getAsJsonPrimitive("duration").isNumber()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT give duration as a number. Ignoring file...:" + element.toString());
continue;
}
duration = fileObject.get("duration").getAsInt();
}
if (fileObject.has("required")) {
if (!fileObject.get("required").isJsonPrimitive() || !fileObject.getAsJsonPrimitive("required").isNumber()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT give required as a number. Ignoring file...: " + element.toString());
continue;
}
required = fileObject.get("required").getAsInt();
}
if (fileObject.has("consume")) {
if (!fileObject.get("consume").isJsonPrimitive()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT give consume as a boolean. Ignoring file...: " + element.toString());
continue;
}
consume = fileObject.get("consume").getAsBoolean();
}
if (fileObject.has("explode")) {
if (!fileObject.get("explode").isJsonPrimitive()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT give explode as a boolean. Ignoring file...: " + element.toString());
continue;
}
explode = fileObject.get("explode").getAsBoolean();
}
if (fileObject.has("harp")) {
if (!fileObject.get("harp").isJsonPrimitive()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT give harp as a boolean. Ignoring file...: " + element.toString());
continue;
}
harp = fileObject.get("harp").getAsBoolean();
}
if (fileObject.has("bubbling")) {
if (!fileObject.get("bubbling").isJsonPrimitive()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT give bubbling as a boolean. Ignoring file...: " + element.toString());
continue;
}
bubbling = fileObject.get("bubbling").getAsBoolean();
}
JsonElement typeElement = fileObject.get("type");
String type = typeElement == null ? "item" : typeElement.getAsString();
JsonObject output = fileObject.get("output").getAsJsonObject();
if (type.equalsIgnoreCase("item")) {
ItemStack outputItem = CraftingHelper.getItemStack(output, context);
if (outputItem.isEmpty()) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT provide a valid output item. Ignoring file...: " + element.toString());
continue;
}
FluidCrafter build = buildFluidCrafter(file.getPath(), outputItem, inputItem, extraInputs, fluid, duration, required, consume, explode, bubbling, harp);
recipeRegistry.put(file.getPath(), build);
recipes.put(inputItem, build);
} else if (type.equalsIgnoreCase("block")) {
IBlockState outputBlock;
JsonElement name = output.get("item");
if (name == null)
name = output.get("block");
if (name == null)
name = output.get("name");
Block block = name != null ? ForgeRegistries.BLOCKS.getValue(new ResourceLocation(name.getAsString())) : null;
if (block == null) {
Wizardry.logger.error(" > WARNING! " + file.getPath() + " does NOT provide a valid output block. Ignoring file...: " + element.toString());
continue;
}
int meta = 0;
JsonElement data = output.get("data");
if (data == null)
data = output.get("meta");
if (data != null && data.isJsonPrimitive() && data.getAsJsonPrimitive().isNumber())
meta = data.getAsInt();
outputBlock = block.getStateFromMeta(meta);
FluidCrafter build = buildFluidCrafter(file.getPath(), outputBlock, inputItem, extraInputs, fluid, duration, required, consume, explode, bubbling, harp);
recipeRegistry.put(file.getPath(), build);
recipes.put(inputItem, build);
} else
Wizardry.logger.error(" > WARNING! " + file.getPath() + " specifies an invalid recipe output type. Valid recipe types: \"item\" \"block\". Ignoring file...: " + element.toString());
} catch (Exception jsonException) {
Wizardry.logger.error(" > WARNING! Skipping " + file.getPath() + " due to error: ", jsonException);
}
}
Wizardry.logger.info("> Finished mana recipe loading.");
Wizardry.logger.info("<<========================================================================>>");
}
use of net.minecraft.item.crafting.Ingredient in project TechReborn by TechReborn.
the class RollingMachineRecipe method addRecipe.
public void addRecipe(ResourceLocation resourceLocation, ItemStack output, Object... components) {
String s = "";
int i = 0;
int j = 0;
int k = 0;
if (components[i] instanceof String[]) {
String[] as = (String[]) components[i++];
for (int l = 0; l < as.length; l++) {
String s2 = as[l];
k++;
j = s2.length();
s = (new StringBuilder()).append(s).append(s2).toString();
}
} else {
while (components[i] instanceof String) {
String s1 = (String) components[i++];
k++;
j = s1.length();
s = (new StringBuilder()).append(s).append(s1).toString();
}
}
HashMap<Character, ItemStack> hashmap = new HashMap<Character, ItemStack>();
for (; i < components.length; i += 2) {
Character character = (Character) components[i];
ItemStack itemstack1 = null;
if (components[i + 1] instanceof Item) {
itemstack1 = new ItemStack((Item) components[i + 1]);
} else if (components[i + 1] instanceof Block) {
itemstack1 = new ItemStack((Block) components[i + 1], 1, -1);
} else if (components[i + 1] instanceof ItemStack) {
itemstack1 = (ItemStack) components[i + 1];
}
hashmap.put(character, itemstack1);
}
NonNullList<Ingredient> recipeArray = NonNullList.create();
for (int i1 = 0; i1 < j * k; i1++) {
char c = s.charAt(i1);
if (hashmap.containsKey(c)) {
recipeArray.set(i1, CraftingHelper.getIngredient(((ItemStack) hashmap.get(c)).copy()));
} else {
recipeArray.set(i1, CraftingHelper.getIngredient(ItemStack.EMPTY));
}
}
recipes.put(resourceLocation, new ShapedRecipes("", j, k, recipeArray, output));
}
use of net.minecraft.item.crafting.Ingredient in project TechReborn by TechReborn.
the class GuiAutoCrafting method renderRecipe.
// Based of vanilla code
public void renderRecipe(IRecipe recipe, int x, int y) {
RenderHelper.enableGUIStandardItemLighting();
GlStateManager.enableAlpha();
mc.getTextureManager().bindTexture(RECIPE_BOOK_TEXTURE);
this.drawTexturedModalRect(x, y, 152, 78, 24, 24);
int recipeWidth = 3;
int recipeHeight = 3;
if (recipe instanceof ShapedRecipes) {
ShapedRecipes shapedrecipes = (ShapedRecipes) recipe;
recipeWidth = shapedrecipes.getWidth();
recipeHeight = shapedrecipes.getHeight();
}
if (recipe instanceof ShapedOreRecipe) {
ShapedOreRecipe shapedrecipes = (ShapedOreRecipe) recipe;
recipeWidth = shapedrecipes.getRecipeWidth();
recipeHeight = shapedrecipes.getRecipeHeight();
}
Iterator<Ingredient> ingredients = recipe.getIngredients().iterator();
for (int rHeight = 0; rHeight < recipeHeight; ++rHeight) {
int j1 = 3 + rHeight * 7;
for (int rWidth = 0; rWidth < recipeWidth; ++rWidth) {
if (ingredients.hasNext()) {
ItemStack[] aitemstack = ingredients.next().getMatchingStacks();
if (aitemstack.length != 0) {
int l1 = 3 + rWidth * 7;
GlStateManager.pushMatrix();
int i2 = (int) ((float) (x + l1) / 0.42F - 3.0F);
int j2 = (int) ((float) (y + j1) / 0.42F - 3.0F);
GlStateManager.scale(0.42F, 0.42F, 1.0F);
GlStateManager.enableLighting();
mc.getRenderItem().renderItemAndEffectIntoGUI(aitemstack[0], i2, j2);
GlStateManager.disableLighting();
GlStateManager.popMatrix();
}
}
}
}
GlStateManager.disableAlpha();
RenderHelper.disableStandardItemLighting();
}
use of net.minecraft.item.crafting.Ingredient in project artisan-worktables by codetaylor.
the class CTArtisanRecipe method getStacksShapeless.
/**
* @return the matrix items re-ordered to match the recipe's ingredient list order
*/
private MatchInfo getStacksShapeless(ICraftingContext context) {
List<IArtisanIngredient> ingredients = this.getIngredientList();
ICraftingMatrixStackHandler matrixHandler = context.getCraftingMatrixHandler();
List<ItemStack> itemList = new ArrayList<>(matrixHandler.getSlots());
List<Integer> indexList = new ArrayList<>(matrixHandler.getSlots());
IItemStack[] stacks = new IItemStack[ingredients.size()];
int[] indices = new int[ingredients.size()];
for (int i = 0; i < matrixHandler.getSlots(); i++) {
ItemStack itemStack = matrixHandler.getStackInSlot(i);
if (!itemStack.isEmpty()) {
itemList.add(itemStack);
indexList.add(i);
}
}
List<Ingredient> ingredientList = new ArrayList<>(ingredients.size());
for (IArtisanIngredient ingredient : ingredients) {
ingredientList.add(ingredient.toIngredient());
}
int[] matches = RecipeMatcher.findMatches(itemList, ingredientList);
for (int i = 0; i < matches.length; i++) {
stacks[matches[i]] = CraftTweakerMC.getIItemStack(itemList.get(i));
indices[matches[i]] = indexList.get(i);
}
return new MatchInfo(stacks, indices);
}
Aggregations