From 6971051b904bc85cd6be704b7b230a0df1928389 Mon Sep 17 00:00:00 2001 From: Arthur Uzulin Date: Mon, 30 Sep 2019 08:09:39 +1000 Subject: [PATCH] Added automatic recipe loaders to crafting and smelting book content JSON tag "recipe" allows you to specify the recipe ID to be automatically loaded rather than having to manually fill the grid[][], input and result --- .../client/book/data/BookLoadException.java | 7 ++ .../data/content/ContentBlockInteraction.java | 4 +- .../book/data/content/ContentCrafting.java | 60 +++++++++++- .../book/data/content/ContentError.java | 15 +++ .../book/data/content/ContentSmelting.java | 94 ++++++++++++------- 5 files changed, 145 insertions(+), 35 deletions(-) create mode 100644 src/main/java/slimeknights/mantle/client/book/data/BookLoadException.java diff --git a/src/main/java/slimeknights/mantle/client/book/data/BookLoadException.java b/src/main/java/slimeknights/mantle/client/book/data/BookLoadException.java new file mode 100644 index 0000000..b080b05 --- /dev/null +++ b/src/main/java/slimeknights/mantle/client/book/data/BookLoadException.java @@ -0,0 +1,7 @@ +package slimeknights.mantle.client.book.data; + +public class BookLoadException extends RuntimeException { + public BookLoadException(String message){ + super(message); + } +} diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java index 9e1d51c..8738de1 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentBlockInteraction.java @@ -46,11 +46,11 @@ public class ContentBlockInteraction extends PageContent { list.add(new ElementImage(x, y, IMG_SMITHING.width, IMG_SMITHING.height, IMG_SMITHING, book.appearance.slotColor)); - if (this.input != null && !this.input.id.equals("")) { + if (this.input != null && !this.input.getItems().isEmpty()) { list.add(new ElementItem(x + INPUT_X, y + INPUT_Y, ITEM_SCALE, this.input.getItems(), this.input.action)); } - if (this.block != null && !this.block.id.equals("")) { + if (this.block != null && !this.block.getItems().isEmpty()) { list.add(new ElementItem(x + BLOCK_X, y + BLOCK_Y, BLOCK_SCALE, this.block.getItems(), this.block.action)); } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java index 539d232..631a315 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentCrafting.java @@ -1,6 +1,16 @@ package slimeknights.mantle.client.book.data.content; +import net.minecraft.client.Minecraft; +import net.minecraft.item.ItemStack; +import net.minecraft.item.crafting.ICraftingRecipe; +import net.minecraft.item.crafting.IRecipe; +import net.minecraft.item.crafting.Ingredient; +import net.minecraft.util.NonNullList; +import net.minecraft.util.ResourceLocation; +import net.minecraftforge.common.crafting.IShapedRecipe; +import org.apache.commons.lang3.StringUtils; import slimeknights.mantle.client.book.data.BookData; +import slimeknights.mantle.client.book.data.BookLoadException; import slimeknights.mantle.client.book.data.element.ImageData; import slimeknights.mantle.client.book.data.element.ItemStackData; import slimeknights.mantle.client.book.data.element.TextData; @@ -34,6 +44,7 @@ public class ContentCrafting extends PageContent { public ItemStackData[][] grid; public ItemStackData result; public TextData[] description; + public String recipe; @Override public void build(BookData book, ArrayList list, boolean rightSide) { @@ -65,7 +76,7 @@ public class ContentCrafting extends PageContent { if (this.grid != null) { for (int i = 0; i < this.grid.length; i++) { for (int j = 0; j < this.grid[i].length; j++) { - if (this.grid[i][j].id.equals("")) { + if (this.grid[i][j] == null || this.grid[i][j].getItems().isEmpty()) { continue; } list.add(new ElementItem(x + SLOT_MARGIN + (SLOT_PADDING + Math.round(ElementItem.ITEM_SIZE_HARDCODED * ITEM_SCALE)) * j, y + SLOT_MARGIN + (SLOT_PADDING + Math.round(ElementItem.ITEM_SIZE_HARDCODED * ITEM_SCALE)) * i, ITEM_SCALE, this.grid[i][j].getItems(), this.grid[i][j].action)); @@ -81,4 +92,51 @@ public class ContentCrafting extends PageContent { list.add(new ElementText(0, height + 5, BookScreen.PAGE_WIDTH, BookScreen.PAGE_HEIGHT - height - 5, this.description)); } } + + @Override + public void load() { + super.load(); + + if(!StringUtils.isEmpty(recipe) && ResourceLocation.isResouceNameValid(recipe)) { + int w = 0, h = 0; + switch(grid_size.toLowerCase()) { + case "large": + w = h = 3; + break; + case "small": + w = h = 2; + break; + } + + IRecipe recipe = Minecraft.getInstance().world.getRecipeManager().getRecipe(new ResourceLocation(this.recipe)).orElse(null); + if(recipe instanceof ICraftingRecipe) { + if(!recipe.canFit(w, h)) { + throw new BookLoadException("Recipe " + this.recipe + " cannot fit in a " + w + "x" + h + " crafting grid"); + } + + result = ItemStackData.getItemStackData(recipe.getRecipeOutput()); + + NonNullList ingredients = recipe.getIngredients(); + + if(recipe instanceof IShapedRecipe) { + IShapedRecipe shaped = (IShapedRecipe) recipe; + + grid = new ItemStackData[shaped.getRecipeHeight()][shaped.getRecipeWidth()]; + + for(int y = 0; y < grid.length; y++) { + for(int x = 0; x < grid[y].length; x++){ + grid[y][x] = ItemStackData.getItemStackData(NonNullList.from(ItemStack.EMPTY, ingredients.get(x + y * grid[y].length).getMatchingStacks())); + } + } + + return; + } + + grid = new ItemStackData[h][w]; + for(int i = 0; i < ingredients.size(); i++){ + grid[i / h][i % w] = ItemStackData.getItemStackData(NonNullList.from(ItemStack.EMPTY, ingredients.get(i).getMatchingStacks())); + } + } + } + } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java index 5061bdd..19227c0 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentError.java @@ -3,6 +3,7 @@ package slimeknights.mantle.client.book.data.content; import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.OnlyIn; import slimeknights.mantle.client.book.data.BookData; +import slimeknights.mantle.client.book.data.BookLoadException; import slimeknights.mantle.client.book.data.element.TextData; import slimeknights.mantle.client.screen.book.BookScreen; import slimeknights.mantle.client.screen.book.element.BookElement; @@ -29,6 +30,11 @@ public class ContentError extends PageContent { public void build(BookData book, ArrayList list, boolean rightSide) { this.addTitle(list, "Error"); + if(exception instanceof BookLoadException) { + buildSimple(list); + return; + } + StackTraceElement[] stackTrace = null; if (this.exception != null) { stackTrace = this.exception.getStackTrace(); @@ -63,4 +69,13 @@ public class ContentError extends PageContent { list.add(new ElementText(0, TITLE_HEIGHT, BookScreen.PAGE_WIDTH, BookScreen.PAGE_HEIGHT - TITLE_HEIGHT, text)); } + + public void buildSimple(ArrayList list) { + TextData[] text = new TextData[1]; + + text[0] = new TextData(exception.getMessage()); + text[0].color = "dark_red"; + + list.add(new ElementText(0, TITLE_HEIGHT, BookScreen.PAGE_WIDTH, BookScreen.PAGE_HEIGHT - TITLE_HEIGHT, text)); + } } diff --git a/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java b/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java index d854f35..717ca8e 100644 --- a/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java +++ b/src/main/java/slimeknights/mantle/client/book/data/content/ContentSmelting.java @@ -1,9 +1,14 @@ package slimeknights.mantle.client.book.data.content; import net.minecraft.block.Blocks; +import net.minecraft.client.Minecraft; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.item.crafting.AbstractCookingRecipe; +import net.minecraft.item.crafting.IRecipe; import net.minecraft.util.NonNullList; +import net.minecraft.util.ResourceLocation; +import org.apache.commons.lang3.StringUtils; import slimeknights.mantle.client.book.data.BookData; import slimeknights.mantle.client.book.data.element.ImageData; import slimeknights.mantle.client.book.data.element.ItemStackData; @@ -19,6 +24,7 @@ import java.util.ArrayList; import static slimeknights.mantle.client.screen.book.Textures.TEX_SMELTING; public class ContentSmelting extends PageContent { + private static final NonNullList FUELS; public static final transient int TEX_SIZE = 128; public static final transient ImageData IMG_SMELTING = new ImageData(TEX_SMELTING, 0, 0, 110, 114, TEX_SIZE, TEX_SIZE); @@ -35,7 +41,10 @@ public class ContentSmelting extends PageContent { public String title = "Smelting"; public ItemStackData input; public ItemStackData result; + public ItemStackData fuel; + public int cookTime = 200; public TextData[] description; + public String recipe; @Override public void build(BookData book, ArrayList list, boolean rightSide) { @@ -47,11 +56,11 @@ public class ContentSmelting extends PageContent { list.add(new ElementText(0, 0, BookScreen.PAGE_WIDTH, 9, tdTitle)); list.add(new ElementImage(x, y, IMG_SMELTING.width, IMG_SMELTING.height, IMG_SMELTING, book.appearance.slotColor)); - if (this.input != null && !this.input.id.equals("")) { + if (this.input != null && !this.input.getItems().isEmpty()) { list.add(new ElementItem(x + INPUT_X, y + INPUT_Y, ITEM_SCALE, this.input.getItems(), this.input.action)); } - if (this.result != null && !this.result.id.equals("")) { + if (this.result != null && !this.result.getItems().isEmpty()) { list.add(new ElementItem(x + RESULT_X, y + RESULT_Y, ITEM_SCALE, this.result.getItems(), this.result.action)); } @@ -65,36 +74,57 @@ public class ContentSmelting extends PageContent { public NonNullList getFuelsList() { //TODO ask JEI for fuel list if it is present - NonNullList fuels = NonNullList.withSize(28, ItemStack.EMPTY); - fuels.set(0, new ItemStack(Blocks.OAK_SLAB)); - fuels.set(1, new ItemStack(Blocks.SPRUCE_SLAB)); - fuels.set(2, new ItemStack(Blocks.BIRCH_SLAB)); - fuels.set(3, new ItemStack(Blocks.JUNGLE_SLAB)); - fuels.set(4, new ItemStack(Blocks.ACACIA_SLAB)); - fuels.set(5, new ItemStack(Blocks.DARK_OAK_SLAB)); - fuels.set(6, new ItemStack(Blocks.OAK_PLANKS)); - fuels.set(7, new ItemStack(Blocks.SPRUCE_PLANKS)); - fuels.set(8, new ItemStack(Blocks.BIRCH_PLANKS)); - fuels.set(9, new ItemStack(Blocks.JUNGLE_PLANKS)); - fuels.set(10, new ItemStack(Blocks.ACACIA_PLANKS)); - fuels.set(11, new ItemStack(Blocks.DARK_OAK_PLANKS)); - fuels.set(12, new ItemStack(Blocks.COAL_BLOCK)); - fuels.set(13, new ItemStack(Items.WOODEN_PICKAXE)); - fuels.set(14, new ItemStack(Items.WOODEN_SWORD)); - fuels.set(15, new ItemStack(Items.WOODEN_HOE)); - fuels.set(16, new ItemStack(Items.STICK)); - fuels.set(17, new ItemStack(Items.COAL)); - fuels.set(18, new ItemStack(Items.LAVA_BUCKET)); - fuels.set(19, new ItemStack(Blocks.OAK_SAPLING)); - fuels.set(20, new ItemStack(Blocks.SPRUCE_SAPLING)); - fuels.set(21, new ItemStack(Blocks.BIRCH_SAPLING)); - fuels.set(22, new ItemStack(Blocks.JUNGLE_SAPLING)); - fuels.set(23, new ItemStack(Blocks.ACACIA_SAPLING)); - fuels.set(24, new ItemStack(Blocks.DARK_OAK_SAPLING)); - fuels.set(25, new ItemStack(Items.BLAZE_ROD)); - fuels.set(26, new ItemStack(Items.WOODEN_SHOVEL)); - fuels.set(27, new ItemStack(Items.WOODEN_AXE)); + if(fuel != null) { + return fuel.getItems(); + } - return fuels; + return FUELS; + } + + @Override + public void load() { + super.load(); + + if(!StringUtils.isEmpty(recipe) && ResourceLocation.isResouceNameValid(recipe)) { + IRecipe recipe = Minecraft.getInstance().world.getRecipeManager().getRecipe(new ResourceLocation(this.recipe)).orElse(null); + + if(recipe instanceof AbstractCookingRecipe) { + input = ItemStackData.getItemStackData(NonNullList.from(ItemStack.EMPTY, recipe.getIngredients().get(0).getMatchingStacks())); + cookTime = ((AbstractCookingRecipe)recipe).getCookTime(); + result = ItemStackData.getItemStackData(recipe.getRecipeOutput()); + } + } + } + + static { + FUELS = NonNullList.from(ItemStack.EMPTY, + new ItemStack(Blocks.OAK_SLAB), + new ItemStack(Blocks.SPRUCE_SLAB), + new ItemStack(Blocks.BIRCH_SLAB), + new ItemStack(Blocks.JUNGLE_SLAB), + new ItemStack(Blocks.ACACIA_SLAB), + new ItemStack(Blocks.DARK_OAK_SLAB), + new ItemStack(Blocks.OAK_PLANKS), + new ItemStack(Blocks.SPRUCE_PLANKS), + new ItemStack(Blocks.BIRCH_PLANKS), + new ItemStack(Blocks.JUNGLE_PLANKS), + new ItemStack(Blocks.ACACIA_PLANKS), + new ItemStack(Blocks.DARK_OAK_PLANKS), + new ItemStack(Blocks.COAL_BLOCK), + new ItemStack(Items.WOODEN_PICKAXE), + new ItemStack(Items.WOODEN_SWORD), + new ItemStack(Items.WOODEN_HOE), + new ItemStack(Items.STICK), + new ItemStack(Items.COAL), + new ItemStack(Items.LAVA_BUCKET), + new ItemStack(Blocks.OAK_SAPLING), + new ItemStack(Blocks.SPRUCE_SAPLING), + new ItemStack(Blocks.BIRCH_SAPLING), + new ItemStack(Blocks.JUNGLE_SAPLING), + new ItemStack(Blocks.ACACIA_SAPLING), + new ItemStack(Blocks.DARK_OAK_SAPLING), + new ItemStack(Items.BLAZE_ROD), + new ItemStack(Items.WOODEN_SHOVEL), + new ItemStack(Items.WOODEN_AXE)); } }