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

180 lines
7.5 KiB
Java
Raw Normal View History

2015-08-29 23:01:13 +02: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://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2015-11-02 20:55:19 +01:00
* © 2015 Ellpeck
2015-08-29 23:01:13 +02:00
*/
package ellpeck.actuallyadditions.booklet.page;
import cpw.mods.fml.relauncher.ReflectionHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2015-08-29 23:01:13 +02:00
import ellpeck.actuallyadditions.booklet.GuiBooklet;
import ellpeck.actuallyadditions.proxy.ClientProxy;
2015-08-29 23:01:13 +02:00
import ellpeck.actuallyadditions.util.ModUtil;
import ellpeck.actuallyadditions.util.StringUtil;
import ellpeck.actuallyadditions.util.Util;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.ShapedRecipes;
import net.minecraft.item.crafting.ShapelessRecipes;
import net.minecraft.util.EnumChatFormatting;
2015-08-29 23:01:13 +02:00
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import java.util.ArrayList;
2015-10-03 22:09:53 +02:00
2015-08-29 23:01:13 +02:00
public class PageCrafting extends BookletPage{
private final IRecipe[] recipes;
private int recipePos;
2015-08-29 23:01:13 +02:00
public PageCrafting(int id, ArrayList<IRecipe> recipes){
this(id, recipes.toArray(new IRecipe[recipes.size()]));
}
2015-10-03 10:19:40 +02:00
public PageCrafting(int id, IRecipe... recipes){
super(id);
this.recipes = recipes;
2015-10-30 21:42:14 +01:00
this.addToPagesWithItemStackData();
2015-08-29 23:01:13 +02:00
}
@Override
@SideOnly(Side.CLIENT)
2015-10-04 13:21:07 +02:00
public void renderPre(GuiBooklet gui, int mouseX, int mouseY, int ticksElapsed, boolean mousePressed){
if(this.recipes[this.recipePos] != null){
gui.mc.getTextureManager().bindTexture(ClientProxy.bulletForMyValentine ? GuiBooklet.resLocValentine : GuiBooklet.resLoc);
2015-08-29 23:01:13 +02:00
gui.drawTexturedModalRect(gui.guiLeft+27, gui.guiTop+20, 146, 20, 99, 60);
}
}
@SuppressWarnings("unchecked")
@Override
@SideOnly(Side.CLIENT)
2015-10-04 13:21:07 +02:00
public void render(GuiBooklet gui, int mouseX, int mouseY, int ticksElapsed, boolean mousePressed){
IRecipe recipe = this.recipes[this.recipePos];
if(recipe == null){
gui.mc.fontRenderer.drawSplitString(EnumChatFormatting.DARK_RED+StringUtil.localize("booklet."+ModUtil.MOD_ID_LOWER+".recipeDisabled"), gui.guiLeft+14, gui.guiTop+15, 115, 0);
2015-08-29 23:01:13 +02:00
}
2015-08-31 10:45:05 +02:00
else{
String strg = StringUtil.localize("booklet."+ModUtil.MOD_ID_LOWER+"."+(recipe instanceof ShapedRecipes ? "shapedRecipe" : (recipe instanceof ShapelessRecipes ? "shapelessRecipe" : (recipe instanceof ShapelessOreRecipe ? "shapelessOreRecipe" : "shapedOreRecipe"))));
gui.mc.fontRenderer.drawString(strg, gui.guiLeft+gui.xSize/2-gui.mc.fontRenderer.getStringWidth(strg)/2, gui.guiTop+10, 0);
2015-08-31 10:45:05 +02:00
}
2015-08-29 23:01:13 +02:00
2015-08-30 04:02:28 +02:00
String text = gui.currentPage.getText();
if(text != null && !text.isEmpty()){
gui.mc.fontRenderer.drawSplitString(text, gui.guiLeft+14, gui.guiTop+90, 115, 0);
2015-08-30 04:02:28 +02:00
}
2015-08-29 23:01:13 +02:00
if(recipe != null){
2015-08-29 23:01:13 +02:00
ItemStack[] stacks = new ItemStack[9];
int width = 3;
int height = 3;
if(recipe instanceof ShapedRecipes){
ShapedRecipes shaped = (ShapedRecipes)recipe;
width = shaped.recipeWidth;
height = shaped.recipeHeight;
stacks = shaped.recipeItems;
}
else if(recipe instanceof ShapelessRecipes){
ShapelessRecipes shapeless = (ShapelessRecipes)recipe;
for(int i = 0; i < shapeless.recipeItems.size(); i++){
stacks[i] = (ItemStack)shapeless.recipeItems.get(i);
}
}
else if(recipe instanceof ShapedOreRecipe){
ShapedOreRecipe shaped = (ShapedOreRecipe)recipe;
width = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 4);
height = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 5);
for(int i = 0; i < shaped.getInput().length; i++){
Object input = shaped.getInput()[i];
if(input != null){
stacks[i] = input instanceof ItemStack ? (ItemStack)input : (((ArrayList<ItemStack>)input).isEmpty() ? null : ((ArrayList<ItemStack>)input).get(0));
2015-08-29 23:01:13 +02:00
}
}
}
else if(recipe instanceof ShapelessOreRecipe){
ShapelessOreRecipe shapeless = (ShapelessOreRecipe)recipe;
for(int i = 0; i < shapeless.getInput().size(); i++){
Object input = shapeless.getInput().get(i);
stacks[i] = input instanceof ItemStack ? (ItemStack)input : (((ArrayList<ItemStack>)input).isEmpty() ? null : ((ArrayList<ItemStack>)input).get(0));
2015-08-29 23:01:13 +02:00
}
}
int xShowOutput = gui.guiLeft+27+82;
int yShowOutput = gui.guiTop+20+22;
renderItem(gui, recipe.getRecipeOutput(), xShowOutput, yShowOutput, 1.0F);
2015-08-29 23:01:13 +02:00
for(int i = 0; i < 2; i++){
boolean tooltip = i == 1;
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
ItemStack stack = stacks[y*width+x];
if(stack != null){
stack.stackSize = 1;
2015-10-03 10:16:18 +02:00
if(stack.getItemDamage() == Util.WILDCARD){
stack.setItemDamage(0);
}
int xShow = gui.guiLeft+27+4+x*18;
int yShow = gui.guiTop+20+4+y*18;
2015-08-29 23:01:13 +02:00
if(!tooltip){
renderItem(gui, stack, xShow, yShow, 1.0F);
2015-08-29 23:01:13 +02:00
}
else{
if(mouseX >= xShow && mouseX <= xShow+16 && mouseY >= yShow && mouseY <= yShow+16){
2015-10-04 13:21:07 +02:00
this.renderTooltipAndTransfer(gui, stack, mouseX, mouseY, true, mousePressed);
2015-08-29 23:01:13 +02:00
}
}
}
}
}
}
if(mouseX >= xShowOutput && mouseX <= xShowOutput+16 && mouseY >= yShowOutput && mouseY <= yShowOutput+16){
2015-10-04 13:21:07 +02:00
this.renderTooltipAndTransfer(gui, recipe.getRecipeOutput(), mouseX, mouseY, false, mousePressed);
2015-08-29 23:01:13 +02:00
}
}
}
2015-10-03 10:19:40 +02:00
@Override
@SideOnly(Side.CLIENT)
2015-10-03 10:19:40 +02:00
public void updateScreen(int ticksElapsed){
if(ticksElapsed%15 == 0){
2015-10-03 10:19:40 +02:00
if(this.recipePos+1 >= this.recipes.length){
this.recipePos = 0;
}
else{
this.recipePos++;
}
}
}
@Override
public ItemStack[] getItemStacksForPage(){
if(this.recipes != null){
ItemStack[] stacks = new ItemStack[this.recipes.length];
for(int i = 0; i < this.recipes.length; i++){
ItemStack output = this.recipes[i].getRecipeOutput();
if(output != null){
if(!this.arePageStacksWildcard){
stacks[i] = output;
}
else{
ItemStack wildcardOutput = output.copy();
wildcardOutput.setItemDamage(Util.WILDCARD);
stacks[i] = wildcardOutput;
}
}
}
return stacks;
2015-10-03 10:19:40 +02:00
}
return null;
2015-10-03 10:19:40 +02:00
}
2015-08-29 23:01:13 +02:00
}