Added basic item display system and crafting page

This commit is contained in:
Ellpeck 2016-11-10 23:37:40 +01:00
parent 4a739b6ab4
commit b295672585
13 changed files with 278 additions and 31 deletions

View file

@ -17,11 +17,13 @@ import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.List;
public interface IBookletPage{
ItemStack[] getItemStacksForPage();
List<ItemStack> getItemStacksForPage();
FluidStack[] getFluidStacksForPage();
List<FluidStack> getFluidStacksForPage();
IBookletChapter getChapter();
@ -42,10 +44,10 @@ public interface IBookletPage{
void actionPerformed(GuiBookletBase gui, GuiButton button);
@SideOnly(Side.CLIENT)
void initGui(GuiBookletBase gui);
void initGui(GuiBookletBase gui, int startX, int startY);
@SideOnly(Side.CLIENT)
void updateScreen(GuiBookletBase gui);
void updateScreen(GuiBookletBase gui, int startX, int startY);
@SideOnly(Side.CLIENT)
void drawScreenPre(GuiBookletBase gui, int startX, int startY, int mouseX, int mouseY, float partialTicks);

View file

@ -12,6 +12,7 @@ package de.ellpeck.actuallyadditions.api.booklet.internal;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import java.util.List;
@ -30,4 +31,6 @@ public abstract class GuiBookletBase extends GuiScreen{
public abstract int getSizeX();
public abstract int getSizeY();
public abstract void addItemRenderer(ItemStack renderedStack, int x, int y, float scale, boolean shouldTryTransfer);
}

View file

@ -44,6 +44,7 @@ import net.minecraftforge.fluids.FluidStack;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class InitBooklet{
@ -77,10 +78,10 @@ public final class InitBooklet{
}
for(IBookletPage page : chapter.getAllPages()){
ItemStack[] items = page.getItemStacksForPage();
FluidStack[] fluids = page.getFluidStacksForPage();
List<ItemStack> items = page.getItemStacksForPage();
List<FluidStack> fluids = page.getFluidStacksForPage();
if((items != null && items.length > 0) || (fluids != null && fluids.length > 0)){
if((items != null && !items.isEmpty()) || (fluids != null && !items.isEmpty())){
if(!ActuallyAdditionsAPI.BOOKLET_PAGES_WITH_ITEM_OR_FLUID_DATA.contains(page)){
ActuallyAdditionsAPI.BOOKLET_PAGES_WITH_ITEM_OR_FLUID_DATA.add(page);
infoCount++;

View file

@ -24,13 +24,13 @@ import java.io.IOException;
import java.util.List;
@SideOnly(Side.CLIENT)
public class GuiBooklet extends GuiBookletBase{
public abstract class GuiBooklet extends GuiBookletBase{
public static final int BUTTONS_PER_PAGE = 12;
public static final ResourceLocation RES_LOC_GUI = AssetUtil.getBookletGuiLocation("guiBooklet");
public static final ResourceLocation RES_LOC_GADGETS = AssetUtil.getBookletGuiLocation("guiBookletGadgets");
protected GuiScreen previousScreen;
public GuiScreen previousScreen;
protected GuiBookletBase parentPage;
protected int xSize;

View file

@ -15,6 +15,7 @@ import de.ellpeck.actuallyadditions.api.booklet.IBookletEntry;
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.mod.booklet.button.EntryButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -61,4 +62,9 @@ public class GuiEntry extends GuiBooklet{
}
}
}
@Override
public void addItemRenderer(ItemStack renderedStack, int x, int y, float scale, boolean shouldTryTransfer){
}
}

View file

@ -15,6 +15,7 @@ import de.ellpeck.actuallyadditions.api.booklet.IBookletEntry;
import de.ellpeck.actuallyadditions.mod.booklet.button.EntryButton;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
@ -59,4 +60,9 @@ public class GuiMainPage extends GuiBooklet{
super.actionPerformed(button);
}
}
@Override
public void addItemRenderer(ItemStack renderedStack, int x, int y, float scale, boolean shouldTryTransfer){
}
}

View file

@ -12,17 +12,22 @@ package de.ellpeck.actuallyadditions.mod.booklet.gui;
import de.ellpeck.actuallyadditions.api.booklet.IBookletPage;
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.mod.booklet.page.ItemDisplay;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
@SideOnly(Side.CLIENT)
public class GuiPage extends GuiBooklet{
private final IBookletPage[] pages = new IBookletPage[2];
private final List<ItemDisplay> itemDisplays = new ArrayList<ItemDisplay>();
public final IBookletPage[] pages = new IBookletPage[2];
public GuiPage(GuiScreen previousScreen, GuiBookletBase parentPage, IBookletPage page1, IBookletPage page2){
super(previousScreen, parentPage);
@ -35,6 +40,10 @@ public class GuiPage extends GuiBooklet{
public void mouseClicked(int mouseX, int mouseY, int mouseButton) throws IOException{
super.mouseClicked(mouseX, mouseY, mouseButton);
for(ItemDisplay display : this.itemDisplays){
display.onMousePress(mouseButton, mouseX, mouseY);
}
for(IBookletPage page : this.pages){
if(page != null){
page.mouseClicked(this, mouseX, mouseY, mouseButton);
@ -78,10 +87,12 @@ public class GuiPage extends GuiBooklet{
@Override
public void initGui(){
super.initGui();
this.itemDisplays.clear();
for(IBookletPage page : this.pages){
for(int i = 0; i < this.pages.length; i++){
IBookletPage page = this.pages[i];
if(page != null){
page.initGui(this);
page.initGui(this, this.guiLeft+6+i*142, this.guiTop+7);
}
}
}
@ -90,15 +101,20 @@ public class GuiPage extends GuiBooklet{
public void updateScreen(){
super.updateScreen();
for(IBookletPage page : this.pages){
for(int i = 0; i < this.pages.length; i++){
IBookletPage page = this.pages[i];
if(page != null){
page.updateScreen(this);
page.updateScreen(this, this.guiLeft+6+i*142, this.guiTop+7);
}
}
}
@Override
public void drawScreen(int mouseX, int mouseY, float partialTicks){
for(ItemDisplay display : this.itemDisplays){
display.drawPre();
}
for(int i = 0; i < this.pages.length; i++){
IBookletPage page = this.pages[i];
if(page != null){
@ -108,6 +124,10 @@ public class GuiPage extends GuiBooklet{
super.drawScreen(mouseX, mouseY, partialTicks);
for(ItemDisplay display : this.itemDisplays){
display.drawPost(mouseX, mouseY);
}
for(int i = 0; i < this.pages.length; i++){
IBookletPage page = this.pages[i];
if(page != null){
@ -115,4 +135,9 @@ public class GuiPage extends GuiBooklet{
}
}
}
@Override
public void addItemRenderer(ItemStack renderedStack, int x, int y, float scale, boolean shouldTryTransfer){
this.itemDisplays.add(new ItemDisplay(this, x, y, scale, renderedStack, shouldTryTransfer));
}
}

View file

@ -21,12 +21,14 @@ import de.ellpeck.actuallyadditions.mod.util.ItemUtil;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.item.ItemStack;
import java.util.List;
public final class BookletUtils{
public static IBookletPage findFirstPageForStack(ItemStack stack){
for(IBookletPage page : ActuallyAdditionsAPI.BOOKLET_PAGES_WITH_ITEM_OR_FLUID_DATA){
ItemStack[] stacks = page.getItemStacksForPage();
if(stacks != null && stacks.length > 0){
List<ItemStack> stacks = page.getItemStacksForPage();
if(stacks != null && !stacks.isEmpty()){
for(ItemStack pageStack : stacks){
if(ItemUtil.areItemsEqual(pageStack, stack, true)){
return page;
@ -37,12 +39,18 @@ public final class BookletUtils{
return null;
}
public static GuiBooklet createBookletGuiFromPage(GuiScreen previousScreen, IBookletPage page){
public static GuiPage createBookletGuiFromPage(GuiScreen previousScreen, IBookletPage page){
GuiMainPage mainPage = new GuiMainPage(previousScreen);
IBookletChapter chapter = page.getChapter();
GuiEntry entry = new GuiEntry(previousScreen, mainPage, chapter.getEntry(), chapter);
return createPageGui(previousScreen, entry, page);
}
public static GuiPage createPageGui(GuiScreen previousScreen, GuiBooklet parentPage, IBookletPage page){
IBookletChapter chapter = page.getChapter();
IBookletPage[] allPages = chapter.getAllPages();
int pageIndex = chapter.getPageNum(page)-1;
IBookletPage page1;
@ -57,6 +65,6 @@ public final class BookletUtils{
page2 = page;
}
return new GuiPage(previousScreen, entry, page1, page2);
return new GuiPage(previousScreen, parentPage, page1, page2);
}
}

View file

@ -44,13 +44,13 @@ public class BookletPage implements IBookletPage{
}
@Override
public ItemStack[] getItemStacksForPage(){
return this.itemsForPage.toArray(new ItemStack[this.itemsForPage.size()]);
public List<ItemStack> getItemStacksForPage(){
return this.itemsForPage;
}
@Override
public FluidStack[] getFluidStacksForPage(){
return this.fluidsForPage.toArray(new FluidStack[this.fluidsForPage.size()]);
public List<FluidStack> getFluidStacksForPage(){
return this.fluidsForPage;
}
@Override
@ -109,13 +109,13 @@ public class BookletPage implements IBookletPage{
@Override
@SideOnly(Side.CLIENT)
public void initGui(GuiBookletBase gui){
public void initGui(GuiBookletBase gui, int startX, int startY){
}
@Override
@SideOnly(Side.CLIENT)
public void updateScreen(GuiBookletBase gui){
public void updateScreen(GuiBookletBase gui, int startX, int startY){
}

View file

@ -0,0 +1,89 @@
/*
* This file ("ItemDisplay.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
*
* © 2015-2016 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.booklet.page;
import de.ellpeck.actuallyadditions.api.booklet.IBookletPage;
import de.ellpeck.actuallyadditions.mod.booklet.gui.GuiBooklet;
import de.ellpeck.actuallyadditions.mod.booklet.gui.GuiPage;
import de.ellpeck.actuallyadditions.mod.booklet.misc.BookletUtils;
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.util.StringUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.client.config.GuiUtils;
import java.util.List;
public class ItemDisplay{
private final GuiPage gui;
private final int x;
private final int y;
private final float scale;
private final ItemStack stack;
private final IBookletPage page;
public ItemDisplay(GuiPage gui, int x, int y, float scale, ItemStack stack, boolean shouldTryTransfer){
this.gui = gui;
this.x = x;
this.y = y;
this.scale = scale;
this.stack = stack;
this.page = shouldTryTransfer ? BookletUtils.findFirstPageForStack(stack) : null;
}
public void drawPre(){
AssetUtil.renderStackToGui(this.stack, this.x, this.y, this.scale);
}
public void drawPost(int mouseX, int mouseY){
if(this.isHovered(mouseX, mouseY)){
Minecraft mc = this.gui.mc;
boolean flagBefore = mc.fontRendererObj.getUnicodeFlag();
mc.fontRendererObj.setUnicodeFlag(false);
List<String> list = this.stack.getTooltip(mc.thePlayer, mc.gameSettings.advancedItemTooltips);
for(int k = 0; k < list.size(); ++k){
if(k == 0){
list.set(k, this.stack.getRarity().rarityColor+list.get(k));
}
else{
list.set(k, TextFormatting.GRAY+list.get(k));
}
}
if(this.page != null && this.page != this.gui.pages[0] && this.page != this.gui.pages[1]){
list.add(TextFormatting.GOLD+StringUtil.localize("booklet."+ModUtil.MOD_ID+".clickToSeeRecipe"));
}
GuiUtils.drawHoveringText(list, mouseX, mouseY, mc.displayWidth, mc.displayHeight, -1, mc.fontRendererObj);
mc.fontRendererObj.setUnicodeFlag(flagBefore);
}
}
public void onMousePress(int button, int mouseX, int mouseY){
if(button == 0 && this.isHovered(mouseX, mouseY)){
if(this.page != null && this.page != this.gui.pages[0] && this.page != this.gui.pages[1]){
GuiBooklet gui = BookletUtils.createPageGui(this.gui.previousScreen, this.gui, this.page);
this.gui.mc.displayGuiScreen(gui);
}
}
}
public boolean isHovered(int mouseX, int mouseY){
return mouseX >= this.x && mouseY >= this.y && mouseX < this.x+16*this.scale && mouseY < this.y+16*this.scale;
}
}

View file

@ -10,22 +10,129 @@
package de.ellpeck.actuallyadditions.mod.booklet.page;
import de.ellpeck.actuallyadditions.api.booklet.internal.GuiBookletBase;
import de.ellpeck.actuallyadditions.mod.util.ModUtil;
import de.ellpeck.actuallyadditions.mod.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.minecraftforge.fml.relauncher.ReflectionHelper;
import net.minecraftforge.oredict.ShapedOreRecipe;
import net.minecraftforge.oredict.ShapelessOreRecipe;
import java.util.Arrays;
import java.util.List;
//TODO
public class PageCrafting extends BookletPage{
private boolean isWildcard;
private final List<IRecipe> recipes;
public PageCrafting(int localizationKey, List<IRecipe> recipes){
super(localizationKey);
this.recipes = recipes;
}
public PageCrafting(int localizationKey, IRecipe... recipes){
super(localizationKey);
this(localizationKey, Arrays.asList(recipes));
}
public BookletPage setWildcard(){
this.isWildcard = true;
return this;
}
@Override
public void initGui(GuiBookletBase gui, int startX, int startY){
super.initGui(gui, startX, startY);
if(!this.recipes.isEmpty()){
IRecipe recipe = this.recipes.get(0);
if(recipe != null){
this.setupRecipe(gui, recipe, startX, startY);
}
}
}
@Override
public List<ItemStack> getItemStacksForPage(){
List<ItemStack> stacks = super.getItemStacksForPage();
if(!this.recipes.isEmpty()){
for(IRecipe recipe : this.recipes){
if(recipe != null){
ItemStack output = recipe.getRecipeOutput();
if(output != null){
ItemStack copy = output.copy();
if(this.isWildcard){
copy.setItemDamage(Util.WILDCARD);
}
stacks.add(copy);
}
}
}
}
return stacks;
}
private void setupRecipe(GuiBookletBase gui, IRecipe recipe, int startX, int startY){
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] = shapeless.recipeItems.get(i);
}
}
else if(recipe instanceof ShapedOreRecipe){
ShapedOreRecipe shaped = (ShapedOreRecipe)recipe;
try{
width = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 4);
height = ReflectionHelper.getPrivateValue(ShapedOreRecipe.class, shaped, 5);
}
catch(Exception e){
ModUtil.LOGGER.error("Something went wrong trying to get the Crafting Recipe in the booklet to display!", e);
}
for(int i = 0; i < shaped.getInput().length; i++){
Object input = shaped.getInput()[i];
if(input != null){
stacks[i] = input instanceof ItemStack ? (ItemStack)input : (((List<ItemStack>)input).isEmpty() ? null : ((List<ItemStack>)input).get(0));
}
}
}
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 : (((List<ItemStack>)input).isEmpty() ? null : ((List<ItemStack>)input).get(0));
}
}
for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
ItemStack stack = stacks[y*width+x];
if(stack != null){
ItemStack copy = stack.copy();
copy.stackSize = 1;
if(copy.getItemDamage() == Util.WILDCARD){
copy.setItemDamage(0);
}
gui.addItemRenderer(stack, startX+10+x*18, startY+10+y*18, 1F, true);
}
}
}
gui.addItemRenderer(recipe.getRecipeOutput(), startX+50, startY+50, 1F, false);
}
}

View file

@ -49,6 +49,6 @@ public class BookletRecipeCategory extends BlankRecipeCategory<BookletRecipeWrap
@Override
public void setRecipe(IRecipeLayout recipeLayout, BookletRecipeWrapper wrapper, IIngredients ingredients){
recipeLayout.getItemStacks().init(0, true, 70, -4);
recipeLayout.getItemStacks().set(0, Arrays.asList(wrapper.thePage.getItemStacksForPage()));
recipeLayout.getItemStacks().set(0, wrapper.thePage.getItemStacksForPage());
}
}

View file

@ -34,11 +34,11 @@ public class BookletRecipeWrapper extends RecipeWrapperWithButton{
@Override
public void getIngredients(IIngredients ingredients){
ingredients.setInputs(ItemStack.class, Arrays.asList(this.thePage.getItemStacksForPage()));
ingredients.setInputs(FluidStack.class, Arrays.asList(this.thePage.getFluidStacksForPage()));
ingredients.setInputs(ItemStack.class, this.thePage.getItemStacksForPage());
ingredients.setInputs(FluidStack.class, this.thePage.getFluidStacksForPage());
ingredients.setOutputs(ItemStack.class, Arrays.asList(this.thePage.getItemStacksForPage()));
ingredients.setOutputs(FluidStack.class, Arrays.asList(this.thePage.getFluidStacksForPage()));
ingredients.setOutputs(ItemStack.class, this.thePage.getItemStacksForPage());
ingredients.setOutputs(FluidStack.class, this.thePage.getFluidStacksForPage());
}
@Override