2020-09-09 16:48:43 +02:00
|
|
|
package de.ellpeck.actuallyadditions.booklet.page;
|
2016-11-10 21:07:15 +01:00
|
|
|
|
2019-05-02 09:08:15 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
2016-11-10 23:37:40 +01:00
|
|
|
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
|
2020-09-09 16:48:43 +02:00
|
|
|
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
|
|
|
|
import de.ellpeck.actuallyadditions.booklet.gui.GuiBooklet;
|
|
|
|
import de.ellpeck.actuallyadditions.common.util.RefHelp;
|
|
|
|
import de.ellpeck.actuallyadditions.common.util.StackUtil;
|
|
|
|
import de.ellpeck.actuallyadditions.common.util.StringUtil;
|
|
|
|
import de.ellpeck.actuallyadditions.common.util.Util;
|
|
|
|
import de.ellpeck.actuallyadditions.common.util.crafting.BlankRecipe;
|
2016-11-10 23:37:40 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-11-10 21:07:15 +01:00
|
|
|
import net.minecraft.item.crafting.IRecipe;
|
2017-06-17 00:48:49 +02:00
|
|
|
import net.minecraft.item.crafting.Ingredient;
|
2016-11-10 23:37:40 +01:00
|
|
|
import net.minecraft.item.crafting.ShapedRecipes;
|
|
|
|
import net.minecraft.item.crafting.ShapelessRecipes;
|
2016-11-11 16:37:45 +01:00
|
|
|
import net.minecraftforge.fml.client.config.GuiUtils;
|
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2016-11-10 23:37:40 +01:00
|
|
|
import net.minecraftforge.oredict.ShapedOreRecipe;
|
|
|
|
import net.minecraftforge.oredict.ShapelessOreRecipe;
|
2016-11-10 21:07:15 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class PageCrafting extends BookletPage {
|
2016-11-10 21:07:15 +01:00
|
|
|
|
2016-11-16 20:31:16 +01:00
|
|
|
private final List<IRecipe> recipes;
|
2016-11-11 18:55:32 +01:00
|
|
|
private int recipeAt;
|
2016-11-11 16:37:45 +01:00
|
|
|
private String recipeTypeLocKey;
|
2016-11-10 23:37:40 +01:00
|
|
|
private boolean isWildcard;
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PageCrafting(int localizationKey, int priority, List<IRecipe> recipes) {
|
2016-11-22 22:54:35 +01:00
|
|
|
super(localizationKey, priority);
|
2016-11-10 23:37:40 +01:00
|
|
|
this.recipes = recipes;
|
2016-11-10 21:07:15 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PageCrafting(int localizationKey, List<IRecipe> recipes) {
|
2016-11-22 22:54:35 +01:00
|
|
|
this(localizationKey, 0, recipes);
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PageCrafting(int localizationKey, IRecipe... recipes) {
|
2016-11-22 22:54:35 +01:00
|
|
|
this(localizationKey, 0, recipes);
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public PageCrafting(int localizationKey, int priority, IRecipe... recipes) {
|
2016-11-22 22:54:35 +01:00
|
|
|
this(localizationKey, priority, Arrays.asList(recipes));
|
2016-11-10 21:07:15 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public BookletPage setWildcard() {
|
2016-11-10 23:37:40 +01:00
|
|
|
this.isWildcard = true;
|
2016-11-10 21:07:15 +01:00
|
|
|
return this;
|
|
|
|
}
|
2016-11-10 23:37:40 +01:00
|
|
|
|
2016-11-11 16:37:45 +01:00
|
|
|
@Override
|
|
|
|
@SideOnly(Side.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public void drawScreenPre(GuiBookletBase gui, int startX, int startY, int mouseX, int mouseY, float partialTicks) {
|
2016-11-11 16:37:45 +01:00
|
|
|
super.drawScreenPre(gui, startX, startY, mouseX, mouseY, partialTicks);
|
|
|
|
|
|
|
|
gui.mc.getTextureManager().bindTexture(GuiBooklet.RES_LOC_GADGETS);
|
2019-05-02 09:10:29 +02:00
|
|
|
GuiUtils.drawTexturedModalRect(startX + 5, startY + 6, 20, 0, 116, 54, 0);
|
2016-11-11 16:37:45 +01:00
|
|
|
|
2019-08-30 00:51:29 +02:00
|
|
|
if (recipeTypeLocKey != null) gui.renderScaledAsciiString("(" + StringUtil.localize(this.recipeTypeLocKey) + ")", startX + 6, startY + 65, 0, false, gui.getMediumFontSize());
|
2016-11-11 16:37:45 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
PageTextOnly.renderTextToPage(gui, this, startX + 6, startY + 80);
|
2016-11-11 16:37:45 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 23:37:40 +01:00
|
|
|
@Override
|
2016-11-11 18:55:32 +01:00
|
|
|
@SideOnly(Side.CLIENT)
|
2019-05-02 09:10:29 +02:00
|
|
|
public void updateScreen(GuiBookletBase gui, int startX, int startY, int pageTimer) {
|
2016-11-11 18:55:32 +01:00
|
|
|
super.updateScreen(gui, startX, startY, pageTimer);
|
2016-11-10 23:37:40 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (pageTimer % 20 == 0) {
|
2016-11-11 18:55:32 +01:00
|
|
|
this.findRecipe(gui, startX, startY);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private void findRecipe(GuiBookletBase gui, int startX, int startY) {
|
|
|
|
if (!this.recipes.isEmpty()) {
|
2016-11-11 18:55:32 +01:00
|
|
|
IRecipe recipe = this.recipes.get(this.recipeAt);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (recipe != null) {
|
2016-11-10 23:37:40 +01:00
|
|
|
this.setupRecipe(gui, recipe, startX, startY);
|
|
|
|
}
|
2016-11-11 18:55:32 +01:00
|
|
|
|
|
|
|
this.recipeAt++;
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.recipeAt >= this.recipes.size()) {
|
2016-11-11 18:55:32 +01:00
|
|
|
this.recipeAt = 0;
|
|
|
|
}
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void initGui(GuiBookletBase gui, int startX, int startY) {
|
2016-11-11 18:55:32 +01:00
|
|
|
super.initGui(gui, startX, startY);
|
|
|
|
this.findRecipe(gui, startX, startY);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public void getItemStacksForPage(List<ItemStack> list) {
|
2016-11-11 18:55:32 +01:00
|
|
|
super.getItemStacksForPage(list);
|
2016-11-10 23:37:40 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (!this.recipes.isEmpty()) {
|
|
|
|
for (IRecipe recipe : this.recipes) {
|
|
|
|
if (recipe != null) {
|
2016-11-10 23:37:40 +01:00
|
|
|
ItemStack output = recipe.getRecipeOutput();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(output)) {
|
2016-11-10 23:37:40 +01:00
|
|
|
ItemStack copy = output.copy();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (this.isWildcard) {
|
2016-11-10 23:37:40 +01:00
|
|
|
copy.setItemDamage(Util.WILDCARD);
|
|
|
|
}
|
2016-11-11 18:55:32 +01:00
|
|
|
list.add(copy);
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
private void setupRecipe(GuiBookletBase gui, IRecipe recipe, int startX, int startY) {
|
2017-06-17 00:48:49 +02:00
|
|
|
Ingredient[] ings = new Ingredient[9];
|
2016-11-10 23:37:40 +01:00
|
|
|
int width = 3;
|
|
|
|
int height = 3;
|
2019-02-27 19:53:05 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
if (recipe instanceof BlankRecipe) {
|
|
|
|
this.recipeTypeLocKey = "tooltip." + ActuallyAdditions.MODID + ".disabled";
|
|
|
|
gui.addOrModifyItemRenderer(recipe.getRecipeOutput(), startX + 100, startY + 25, 1F, false);
|
2017-08-02 04:20:47 +02:00
|
|
|
return;
|
2019-05-02 09:10:29 +02:00
|
|
|
} else if (recipe instanceof ShapedRecipes) {
|
|
|
|
ShapedRecipes shaped = (ShapedRecipes) recipe;
|
2016-11-10 23:37:40 +01:00
|
|
|
width = shaped.recipeWidth;
|
|
|
|
height = shaped.recipeHeight;
|
2017-06-17 00:48:49 +02:00
|
|
|
ings = shaped.recipeItems.toArray(new Ingredient[shaped.recipeItems.size()]);
|
2019-05-02 09:10:29 +02:00
|
|
|
this.recipeTypeLocKey = "booklet." + ActuallyAdditions.MODID + ".shapedRecipe";
|
|
|
|
} else if (recipe instanceof ShapelessRecipes) {
|
|
|
|
ShapelessRecipes shapeless = (ShapelessRecipes) recipe;
|
|
|
|
for (int i = 0; i < shapeless.recipeItems.size(); i++) {
|
2017-06-17 00:48:49 +02:00
|
|
|
ings[i] = shapeless.recipeItems.get(i);
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
this.recipeTypeLocKey = "booklet." + ActuallyAdditions.MODID + ".shapelessRecipe";
|
|
|
|
} else if (recipe instanceof ShapedOreRecipe) {
|
|
|
|
ShapedOreRecipe shaped = (ShapedOreRecipe) recipe;
|
|
|
|
try {
|
2019-05-02 09:08:15 +02:00
|
|
|
width = RefHelp.getPrivateValue(ShapedOreRecipe.class, shaped, 4);
|
|
|
|
height = RefHelp.getPrivateValue(ShapedOreRecipe.class, shaped, 5);
|
2019-05-02 09:10:29 +02:00
|
|
|
} catch (Exception e) {
|
2018-05-10 11:38:58 +02:00
|
|
|
ActuallyAdditions.LOGGER.error("Something went wrong trying to get the Crafting Recipe in the booklet to display!", e);
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int i = 0; i < shaped.getIngredients().size(); i++) {
|
2017-06-29 18:30:02 +02:00
|
|
|
ings[i] = shaped.getIngredients().get(i);
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
this.recipeTypeLocKey = "booklet." + ActuallyAdditions.MODID + ".shapedOreRecipe";
|
|
|
|
} else if (recipe instanceof ShapelessOreRecipe) {
|
|
|
|
ShapelessOreRecipe shapeless = (ShapelessOreRecipe) recipe;
|
|
|
|
for (int i = 0; i < shapeless.getIngredients().size(); i++) {
|
2017-06-29 18:30:02 +02:00
|
|
|
ings[i] = shapeless.getIngredients().get(i);
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
this.recipeTypeLocKey = "booklet." + ActuallyAdditions.MODID + ".shapelessOreRecipe";
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
Ingredient ing = ings[y * width + x];
|
|
|
|
if (ing != null) {
|
2017-06-29 18:30:02 +02:00
|
|
|
ItemStack[] stacks = ing.getMatchingStacks();
|
2019-05-02 09:10:29 +02:00
|
|
|
if (stacks != null && stacks.length > 0) {
|
2017-06-17 00:48:49 +02:00
|
|
|
ItemStack stack = stacks[0];
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(stack)) {
|
2017-06-17 00:48:49 +02:00
|
|
|
ItemStack copy = stack.copy();
|
2018-06-23 01:39:30 +02:00
|
|
|
copy.setCount(1);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (copy.getItemDamage() == Util.WILDCARD) {
|
2017-06-17 00:48:49 +02:00
|
|
|
copy.setItemDamage(0);
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
gui.addOrModifyItemRenderer(copy, startX + 6 + x * 18, startY + 7 + y * 18, 1F, true);
|
2017-06-17 00:48:49 +02:00
|
|
|
}
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
gui.addOrModifyItemRenderer(recipe.getRecipeOutput(), startX + 100, startY + 25, 1F, false);
|
2016-11-10 23:37:40 +01:00
|
|
|
}
|
2016-11-10 21:07:15 +01:00
|
|
|
}
|