ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/booklet/page/PageCrafting.java

198 lines
7.5 KiB
Java
Raw Normal View History

2016-11-10 21:07:15 +01:00
/*
* This file ("PageCrafting.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
2016-11-10 21:07:15 +01:00
*/
package de.ellpeck.actuallyadditions.mod.booklet.page;
2019-05-02 09:08:15 +02:00
import java.util.Arrays;
import java.util.List;
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
2018-05-10 11:38:58 +02:00
import de.ellpeck.actuallyadditions.mod.ActuallyAdditions;
2016-11-11 16:37:45 +01:00
import de.ellpeck.actuallyadditions.mod.booklet.gui.GuiBooklet;
2019-05-02 09:08:15 +02:00
import de.ellpeck.actuallyadditions.mod.util.RefHelp;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
2016-11-11 16:37:45 +01:00
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import de.ellpeck.actuallyadditions.mod.util.Util;
import de.ellpeck.actuallyadditions.mod.util.crafting.BlankRecipe;
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;
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;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
2016-11-10 21:07:15 +01:00
public class PageCrafting extends BookletPage{
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;
private boolean isWildcard;
2016-11-22 22:54:35 +01:00
public PageCrafting(int localizationKey, int priority, List<IRecipe> recipes){
super(localizationKey, priority);
this.recipes = recipes;
2016-11-10 21:07:15 +01:00
}
2016-11-22 22:54:35 +01:00
public PageCrafting(int localizationKey, List<IRecipe> recipes){
this(localizationKey, 0, recipes);
}
2016-11-10 21:07:15 +01:00
public PageCrafting(int localizationKey, IRecipe... recipes){
2016-11-22 22:54:35 +01:00
this(localizationKey, 0, recipes);
}
public PageCrafting(int localizationKey, int priority, IRecipe... recipes){
this(localizationKey, priority, Arrays.asList(recipes));
2016-11-10 21:07:15 +01:00
}
public BookletPage setWildcard(){
this.isWildcard = true;
2016-11-10 21:07:15 +01:00
return this;
}
2016-11-11 16:37:45 +01:00
@Override
@SideOnly(Side.CLIENT)
public void drawScreenPre(GuiBookletBase gui, int startX, int startY, int mouseX, int mouseY, float partialTicks){
super.drawScreenPre(gui, startX, startY, mouseX, mouseY, partialTicks);
gui.mc.getTextureManager().bindTexture(GuiBooklet.RES_LOC_GADGETS);
GuiUtils.drawTexturedModalRect(startX+5, startY+6, 20, 0, 116, 54, 0);
gui.renderScaledAsciiString("("+StringUtil.localize(this.recipeTypeLocKey)+")", startX+6, startY+65, 0, false, gui.getMediumFontSize());
2016-11-11 16:37:45 +01:00
PageTextOnly.renderTextToPage(gui, this, startX+6, startY+80);
}
@Override
2016-11-11 18:55:32 +01:00
@SideOnly(Side.CLIENT)
public void updateScreen(GuiBookletBase gui, int startX, int startY, int pageTimer){
super.updateScreen(gui, startX, startY, pageTimer);
2016-11-11 18:55:32 +01:00
if(pageTimer%20 == 0){
this.findRecipe(gui, startX, startY);
}
}
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);
if(recipe != null){
this.setupRecipe(gui, recipe, startX, startY);
}
2016-11-11 18:55:32 +01:00
this.recipeAt++;
if(this.recipeAt >= this.recipes.size()){
this.recipeAt = 0;
}
}
}
@Override
2016-11-11 18:55:32 +01:00
public void initGui(GuiBookletBase gui, int startX, int startY){
super.initGui(gui, startX, startY);
this.findRecipe(gui, startX, startY);
}
@Override
public void getItemStacksForPage(List<ItemStack> list){
super.getItemStacksForPage(list);
if(!this.recipes.isEmpty()){
for(IRecipe recipe : this.recipes){
if(recipe != null){
ItemStack output = recipe.getRecipeOutput();
if(StackUtil.isValid(output)){
ItemStack copy = output.copy();
if(this.isWildcard){
copy.setItemDamage(Util.WILDCARD);
}
2016-11-11 18:55:32 +01:00
list.add(copy);
}
}
}
}
}
private void setupRecipe(GuiBookletBase gui, IRecipe recipe, int startX, int startY){
2017-06-17 00:48:49 +02:00
Ingredient[] ings = new Ingredient[9];
int width = 3;
int height = 3;
2019-02-27 19:53:05 +01:00
if(recipe instanceof BlankRecipe){
2018-05-10 11:38:58 +02:00
this.recipeTypeLocKey = "tooltip."+ActuallyAdditions.MODID+".disabled";
gui.addOrModifyItemRenderer(recipe.getRecipeOutput(), startX+100, startY+25, 1F, false);
return;
}
else if(recipe instanceof ShapedRecipes){
ShapedRecipes shaped = (ShapedRecipes)recipe;
width = shaped.recipeWidth;
height = shaped.recipeHeight;
2017-06-17 00:48:49 +02:00
ings = shaped.recipeItems.toArray(new Ingredient[shaped.recipeItems.size()]);
2018-05-10 11:38:58 +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);
}
2018-05-10 11:38:58 +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);
}
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);
}
for(int i = 0; i < shaped.getIngredients().size(); i++){
ings[i] = shaped.getIngredients().get(i);
}
2018-05-10 11:38:58 +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++){
ings[i] = shapeless.getIngredients().get(i);
}
2018-05-10 11:38:58 +02:00
this.recipeTypeLocKey = "booklet."+ActuallyAdditions.MODID+".shapelessOreRecipe";
}
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
2017-06-17 00:48:49 +02:00
Ingredient ing = ings[y*width+x];
if(ing != null){
ItemStack[] stacks = ing.getMatchingStacks();
2017-06-17 00:48:49 +02:00
if(stacks != null && stacks.length > 0){
ItemStack stack = stacks[0];
if(StackUtil.isValid(stack)){
ItemStack copy = stack.copy();
copy.setCount(1);
2017-06-17 00:48:49 +02:00
if(copy.getItemDamage() == Util.WILDCARD){
copy.setItemDamage(0);
}
gui.addOrModifyItemRenderer(copy, startX+6+x*18, startY+7+y*18, 1F, true);
}
}
}
}
}
2016-11-11 18:55:32 +01:00
gui.addOrModifyItemRenderer(recipe.getRecipeOutput(), startX+100, startY+25, 1F, false);
}
2016-11-10 21:07:15 +01:00
}