Jump to content

[1.12]Opening a Gui referencing to another Guis' object


ArmamentHaki

Recommended Posts

Hello, it's me again with another Problem on my GUI.
The Custom Buttons I made get a Card Attribute to save the Card that they are related to.
I wanted to use this in order to construct a new GUI, which is opened when the Player clicks a Card Button.
Then I would use this Card as a Parameter in the new GUI, in order to create a Card-Info-File from the data stored in the buttons' Card Attribute.
The Problem is that when I open the new Gui, the old one gets closed and all the objects I was referencing seem to then equal null, which is why I get a NullPointerException when I try using the cards' data...
Is there a good way to do this? I tried cloning the Card but that wouldn't work either. Then I tried to find out which Card I have to create by comparing the buttons' Card to the Cards the Player owns, and then using the one
which is found in the playerCards Array. I will just Show you my GUI-Code, although it is a bit messy, it should still be possible to understand it.
 

package armamenthaki.duelmonsters.gui;

import java.util.ArrayList;
import java.util.Collection;

import armamenthaki.duelmonsters.DuelMonsters;
import armamenthaki.duelmonsters.duel.Card;
import armamenthaki.duelmonsters.duel.cards.CardSpeedWarrior;
import armamenthaki.duelmonsters.individual.capabilities.DuelDataProvider;
import armamenthaki.duelmonsters.util.Log;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.event.RenderGameOverlayEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class GuiDeckEditor extends GuiScreen
{
	//the cards I want to display in the boxes
    private ArrayList<Card> deck = new ArrayList<Card>();
	private ArrayList<Card> displayedCards = new ArrayList<Card>();
	//the players cards which out of which the the displayedCards resolves
	private ArrayList<Card> playerCards = new ArrayList<Card>();
	
	//the background, fixed to the screen width and height
	private static final ResourceLocation debase = new ResourceLocation("duelmonsters", "textures/textures/gui/guidebase.png");
	private ButtonCard clickedButton;
	private int allCardButtons;
	private EntityPlayer theplayer;
	public ResourceLocation guiresource;
	private ScaledResolution res;
	private int scroll;
	private int scroll2;
	private boolean scrollingUp;
	private boolean scrollingDown;
	private int buttonHeight;
	
    //controls up and down for both boxes
	private ButtonControl cardUp;
	private ButtonControl cardDown;
	private ButtonControl deckUp;
	private ButtonControl deckDown;
	
	public GuiDeckEditor(EntityPlayer player)
	{
		
		theplayer = player;
		if(!player.world.isRemote)
		{
			//capabilities don't seem to be working as the gui is cient side
			Log.logString("player");
			playerCards = player.getCapability(DuelDataProvider.DUELDAT_CAP, null).getPlayerCards();
			playerCards.add(new CardSpeedWarrior());
		}
		//just some cards to add to the screen for testing purposes
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		displayedCards.add(new CardSpeedWarrior());
		
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());
		deck.add(new CardSpeedWarrior());


		this.mc = Minecraft.getMinecraft();
		res = new ScaledResolution(this.mc);
		


	}
	/* basic Gui methods*/
	@Override
    public void drawScreen(int mouseX, int mouseY, float partialTicks)
	{

		initializeStuff(mouseX, mouseY);
		drawBackAndForeground();
	}

	@Override
    public void initGui()
    {
		initDisplayedCards();
		initDeckCards();
		initControls();
    }
	
	/*this happens in drawScreen*/
	public void drawBackAndForeground()
	{
		GlStateManager.pushMatrix();
		float x = (float)width/512;
		float y = (float)height/288;
		int a = res.getScaleFactor();
		mc.renderEngine.bindTexture(debase);
		GlStateManager.scale(x, y, 1.0);
		GlStateManager.translate(0F, 0F, 2F);
		this.drawModalRectWithCustomSizedTexture(0, 0, 0, 0, 512, 288, 512, 288);
		GlStateManager.popMatrix();	
	}
	
	public void initializeStuff(int mouseX, int mouseY)
	{
		int i = this.buttonList.size()-1;
		for(; i>=0; i--)
		{
			if(this.buttonList.get(i) instanceof ButtonCard)
			{
				this.buttonList.remove(i);
			}
		}
		this.initDisplayedCards();
		this.initDeckCards();
		if(cardUp.dragging)++scroll;
		if(cardDown.dragging)--scroll;
		if(deckUp.dragging)++scroll2;
		if(deckDown.dragging)--scroll2;
		this.updateControlResources(mouseX, mouseY);
		cardUp.drawButton(mc);
		cardDown.drawButton(mc);
		deckUp.drawButton(mc);
		deckDown.drawButton(mc);
		for(GuiButton button : this.buttonList)
		{
			if(button instanceof ButtonCard)
			{
				((ButtonCard) button).drawScreen(mc);
			}
		}
	}
	
	/*responds to user input*/
	@Override
	public void actionPerformed(GuiButton button)
	{	
		if(button.id <= allCardButtons && cardUp.hoverState != 1 && cardDown.hoverState != 1)
		{
			for(Card card : playerCards)
			{
				//this tests if the resourcePath of any players' card equals the buttons' resource, which would mean that they would be the same cards.
				//however, the capabilities don't work..
				if(card.getResource().getResourcePath()==(((ButtonCard) button).getResource().getResourcePath()))
				{
					mc.displayGuiScreen(new GuiCardDisplay(this, card));
					Log.logString("okokok");
				}
			}
		}
		if(button.id == allCardButtons+1)
		{
			cardUp.dragging = true;
		}
		if(button.id == allCardButtons+2)
		{
			cardDown.dragging = true;
		}
		if(button.id == allCardButtons+3)
		{
			deckUp.dragging = true;
		}
		if(button.id == allCardButtons+4)
		{
			deckDown.dragging = true;
		}
		
		
	}
	/*get and set*/
	public int getWidth()
	{
		return this.width;
	}
	public int getHeight()
	{
		return this.height;
	}
	
	/*init for all buttons*/
	public void initDisplayedCards()
	{
		int line = 0;
		int column = 0;
		int id = 0;
		for(Card itCard : displayedCards) 
		{
			ResourceLocation resource = itCard.getResource();
			int i = width/6 + (column* width/17);
			int j = (height/6+height/30) + (line* width/13)+this.scroll;
			float trX = (float)width/6 +(float)(column* width/17);
			float trY = (float)(height/6+height/30) +(float)(line* width/13)+(float)this.scroll;
			float sf = (float)width/256F /20F;
			int rF = res.getScaleFactor();
			float fWidth = sf * 256F;
			float fHeight = sf * 373F;
			int bWidth = (int)fWidth;
			int bHeight = (int)fHeight;
			this.buttonHeight = bHeight;
	        column++;
	        if(column == 5)
	        {
	        	line++;
				column = 0;
	        }
			GuiButton button = new ButtonCard(id, sf, i, j, "", itCard, trX, trY, bWidth, bHeight, this);
			this.buttonList.add(button);
			id++;
		}
		allCardButtons = id;
	}
	
	
	public void initDeckCards()
	{
		int line = 0;
		int column = 0;
		int id = allCardButtons;
		for(Card itCard : deck) 
		{
			ResourceLocation resource = itCard.getResource();
			float trX = (float)width-(float)width/2.45F +(float)(column* width/17);
			float trY = (float)height/3.1F +(float)(line* width/13)+(float)this.scroll2;
			int i = (int)trX;
			int j = (int)trY;
			float sf = (float)width/256F /20F;
			int rF = res.getScaleFactor();
			float fWidth = sf * 256F;
			float fHeight = sf * 373F;
			int bWidth = (int)fWidth;
			int bHeight = (int)fHeight;
			this.buttonHeight = bHeight;
	        column++;
	        if(column == 5)
	        {
	        	line++;
				column = 0;
	        }
			GuiButton button = new ButtonCard(id, sf, i, j, "", itCard, trX, trY, bWidth, bHeight, this);
			this.buttonList.add(button);
			id++;
		}
		allCardButtons = id;
	}
	public void initControls()
	{
		initButtonUp();
		initButtonDown();
		initDeckUp();
		initDeckDown();
	}
	
	//init controls
	public void initButtonUp()
	{
		ResourceLocation resource = new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonUp.png");
		float sF = (float)width/33F/20;
		int posX = width/4 + width/27;
		int posY = height/8;
		float fWidth =  sF * 33F;
		float fHeight = sF * 19F;
		int bWidth = (int)fWidth;
		int bHeight = (int)fHeight;
		ButtonControl cardUp = new ButtonControl(allCardButtons+1, posX, posY, bWidth, bHeight, "", resource);
		cardUp.setScale(sF);
		cardUp.setTextureScale(33, 19);
		cardUp.setTr((float)posX, (float)posY);
		this.cardUp = cardUp;
		this.buttonList.add(this.cardUp);
	}
	
	public void initButtonDown()
	{
		ResourceLocation resource = new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonDown.png");
		float sF = (float)width/33F/20;
		int posX = width/4 + width/27;
		int posY = height-height/16;
		float fWidth =  sF * 33F;
		float fHeight = sF * 19F;
		int bWidth = (int)fWidth;
		int bHeight = (int)fHeight;
		ButtonControl cardDown = new ButtonControl(allCardButtons+2, posX, posY, bWidth, bHeight, "", resource);
		cardDown.setScale(sF);
		cardDown.setTextureScale(33, 19);
		cardDown.setTr((float)posX, (float)posY);
		this.cardDown = cardDown;
		this.buttonList.add(this.cardDown);
	}
	
	public void initDeckUp()
	{
		ResourceLocation resource = new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonUp.png");
		float sF = (float)width/33F/20;
		int posX = width-width/4;
		int posY = height/4;
		float fWidth =  sF * 33F;
		float fHeight = sF * 19F;
		int bWidth = (int)fWidth;
		int bHeight = (int)fHeight;
		ButtonControl cardUp = new ButtonControl(allCardButtons+3, posX, posY, bWidth, bHeight, "", resource);
		cardUp.setScale(sF);
		cardUp.setTextureScale(33, 19);
		cardUp.setTr((float)posX, (float)posY);
		this.deckUp = cardUp;
		this.buttonList.add(this.deckUp);
	}
	
	public void initDeckDown()
	{
		ResourceLocation resource = new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonDown.png");
		float sF = (float)width/33F/20;
		int posX = width-width/4;
		int posY = height-height/16;
		float fWidth =  sF * 33F;
		float fHeight = sF * 19F;
		int bWidth = (int)fWidth;
		int bHeight = (int)fHeight;
		ButtonControl cardDown = new ButtonControl(allCardButtons+4, posX, posY, bWidth, bHeight, "", resource);
		cardDown.setScale(sF);
		cardDown.setTextureScale(33, 19);
		cardDown.setTr((float)posX, (float)posY);
		this.deckDown = cardDown;
		this.buttonList.add(this.deckDown);
	}
	
	public void updateControlResources(int mouseX, int mouseY)
	{
		cardUp.setMouse(mouseX, mouseY);
		cardDown.setMouse(mouseX, mouseY);
		deckUp.setMouse(mouseX, mouseY);
		deckDown.setMouse(mouseX, mouseY);
		if(cardUp.hoverState == 1)cardUp.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonup2.png"));
		else cardUp.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonup.png"));
		if(cardDown.hoverState == 1)cardDown.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttondown2.png"));
		else cardDown.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttondown.png"));
		if(deckUp.hoverState == 1)deckUp.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonup2.png"));
		else deckUp.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttonup.png"));
		if(deckDown.hoverState == 1)deckDown.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttondown2.png"));
		else deckDown.setResource(new ResourceLocation(DuelMonsters.MODID, "textures/textures/gui/buttondown.png"));
	}
	
}

And this is the Gui I am opening:

package armamenthaki.duelmonsters.gui;

import java.util.ArrayList;

import armamenthaki.duelmonsters.duel.Card;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;

public class GuiCardDisplay extends GuiScreen
{
	
	private ResourceLocation resource;
	private GuiScreen guiIn;
	private Card card;
	
	public GuiCardDisplay(GuiScreen gui, Card card)
	{
		this.card = card;
		this.resource = card.getResource();
		this.guiIn = gui;
	}
	
	
	public GuiCardDisplay(GuiScreen gui, ArrayList<Card> cards, int id)
	{
		this.card = cards.get(id);
		this.resource = card.getResource();
		this.guiIn = gui;
	}
	@Override
	public void drawScreen(int mouseX, int mouseY, float partialTicks)
	{
		mc.renderEngine.bindTexture(resource);
		GlStateManager.translate((float)this.width/4, (float)this.height/3, 1.0);
		float sf = (float)this.width/256F/4F;
		GlStateManager.scale(sf, sf, 1.0F);
		this.drawModalRectWithCustomSizedTexture(0, 0, 0, 0, 256, 373, 256, 373);
	}
	
	@Override
	public void initGui()
	{
		GuiButton button = new GuiButton(0, 0, 0, this.width, this.height, "");
		this.buttonList.add(button);
	}
	
	public void actionPerformed(GuiButton button)
	{
		if(button.id == 0)mc.displayGuiScreen(this.guiIn);
	}
}


I hope it isn't to hard to understand, thank you for your help.
 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.