Jump to content

Setting string in tile entity from gui?


robertcars

Recommended Posts

So I am trying to set the string pass which is in my tile entity class from my gui class but its not doing so.

 

GuiKeyPad

package com.techmod.client.gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.world.IBlockAccess;

import org.lwjgl.opengl.GL11;

import com.techmod.client.container.ContainerKeyPad;
import com.techmod.client.tileentity.TileEntityKeyPad;

public class GuiKeyPad extends GuiContainer {

    @SuppressWarnings("unused")
private TileEntityKeyPad tileEntityKeyPad;
    
    private GuiTextField textfield;
    
    public static String text = "Keypad";
    private static String pass = null;
    
    public GuiKeyPad(InventoryPlayer inventoryPlayer, TileEntityKeyPad keypad)
    {
        super(new ContainerKeyPad(inventoryPlayer, keypad));
        tileEntityKeyPad = keypad;

        xSize = 176;
        ySize = 175;

    }
    
    @SuppressWarnings("unchecked")
public void initGui() {
    	
    	this.buttonList.clear();

    	//int posX = (this.width - xSize) / 2;
    	int posY = (this.height - ySize) / 2;
    	
   		//if(this.KeyPad.pass == null) {
    	if(new TileEntityKeyPad().pass == null || new TileEntityKeyPad().pass.isEmpty()) {
		this.buttonList.add(new GuiButton(0, this.width / 2 - 100 / 2, posY + 150, 100, 20, "Set Password"));
	}else{
		this.buttonList.add(new GuiButton(12, this.width / 2 - 100 / 2, posY + 150, 100, 20, "Enter"));
	}
    	this.buttonList.add(new GuiButton(1, this.width / 2 - 20 - 30 / 2, posY + 50, 20, 20, "1"));
    	this.buttonList.add(new GuiButton(2, this.width / 2 - 20 / 2, posY + 50, 20, 20, "2"));
    	this.buttonList.add(new GuiButton(3, this.width / 2 - 20 + 70 / 2, posY + 50, 20, 20, "3"));
    	
    	this.buttonList.add(new GuiButton(4, this.width / 2 - 20 - 30 / 2, posY + 75, 20, 20, "4"));
    	this.buttonList.add(new GuiButton(5, this.width / 2 - 20 / 2, posY + 75, 20, 20, "5"));
    	this.buttonList.add(new GuiButton(6, this.width / 2 - 20 + 70 / 2, posY + 75, 20, 20, "6"));
    	
    	this.buttonList.add(new GuiButton(7, this.width / 2 - 20 - 30 / 2, posY + 100, 20, 20, "7"));
    	this.buttonList.add(new GuiButton(8, this.width / 2 - 20 / 2, posY + 100, 20, 20, "8"));
    	this.buttonList.add(new GuiButton(9, this.width / 2 - 20 + 70 / 2, posY + 100, 20, 20, "9"));

    	this.buttonList.add(new GuiButton(10, this.width / 2 - 20 / 2, posY + 125, 20, 20, "0"));

    	this.buttonList.add(new GuiButton(11, this.width / 2 - 20 + 70 / 2, posY + 125, 20, 20, "<-"));

    	
    	textfield = new GuiTextField(fontRendererObj, this.width / 2 - 70 / 2, this.height / 2 - 65, 70, 20);
    	textfield.setFocused(false);
    	textfield.setMaxStringLength(10);

    }
    
    public void updateScreen() {

    	pass = textfield.getText();    	
    	
    }
    
@Override
    protected void drawGuiContainerForegroundLayer(int x, int y) {
    	fontRendererObj.drawString(text, this.width / 2 - 15, this.height / 2 - 80, 4210752);
    	
    }

    
    
    public void drawScreen(int i, int j, float f) {
    	super.drawScreen(i, j, f);
    	textfield.drawTextBox();

    }
    
    protected void actionPerformed(GuiButton guibutton) {
    	IBlockAccess world;
    	if(guibutton.id == 0) {
    		if(new TileEntityKeyPad().pass == null) {
    			new TileEntityKeyPad().pass = pass;
    			System.out.println(new TileEntityKeyPad().pass);
    		}else{

    		}
    	}
    	
    	if(guibutton.id == 12) {
    		if(new TileEntityKeyPad().pass == null) {
    		}else{
    			String np = textfield.getText();
    			if(np.equals(new TileEntityKeyPad().pass)) {
    				System.out.println("dddasd");
    			}
    		}

    	}
    	
    	if(guibutton.id == 1) {
    		pass = pass + "1";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 2) {
    		pass = pass + "2";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 3) {
    		pass = pass + "3";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 4) {
    		pass = pass + "4";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 5) {
    		pass = pass + "5";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 6) {
    		pass = pass + "6";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 7) {
    		pass = pass + "7";
    		textfield.setText(pass);
    	}
    	if(guibutton.id ==  {
    		pass = pass + "8";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 9) {
    		pass = pass + "9";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 10) {
    		pass = pass + "0";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 11) {
    		if(pass != null && pass != "") {
    			pass = pass.substring(0, pass.length() - 1);
    			textfield.setText(pass);
    		}
    	}
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float opacity, int x, int y)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.mc.getTextureManager().bindTexture(Textures.KEY_PAD);

        int xStart = (width - xSize) / 2;
        int yStart = (height - ySize) / 2;
        this.drawTexturedModalRect(xStart, yStart, 0, 0, xSize, ySize);
    }

}

 

TileEntityKeyPad

 

package com.techmod.client.tileentity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityKeyPad extends TileEntity {

public String pass;
public int pa;

    public void readFromNBT(NBTTagCompound p_145839_1_) {
        super.readFromNBT(p_145839_1_);

        pass = p_145839_1_.getString("Pass");

    }
    
    public void writeToNBT(NBTTagCompound p_145841_1_)
    {
        super.writeToNBT(p_145841_1_);
        p_145841_1_.setString("Pass", pass);
    }



}

Link to comment
Share on other sites

So I am trying to set the string pass which is in my tile entity class from my gui class but its not doing so.

 

GuiKeyPad

package com.techmod.client.gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.world.IBlockAccess;

import org.lwjgl.opengl.GL11;

import com.techmod.client.container.ContainerKeyPad;
import com.techmod.client.tileentity.TileEntityKeyPad;

public class GuiKeyPad extends GuiContainer {

    @SuppressWarnings("unused")
private TileEntityKeyPad tileEntityKeyPad;
    
    private GuiTextField textfield;
    
    public static String text = "Keypad";
    private static String pass = null;
    
    public GuiKeyPad(InventoryPlayer inventoryPlayer, TileEntityKeyPad keypad)
    {
        super(new ContainerKeyPad(inventoryPlayer, keypad));
        tileEntityKeyPad = keypad;

        xSize = 176;
        ySize = 175;

    }
    
    @SuppressWarnings("unchecked")
public void initGui() {
    	
    	this.buttonList.clear();

    	//int posX = (this.width - xSize) / 2;
    	int posY = (this.height - ySize) / 2;
    	
   		//if(this.KeyPad.pass == null) {
    	if(new TileEntityKeyPad().pass == null || new TileEntityKeyPad().pass.isEmpty()) {
		this.buttonList.add(new GuiButton(0, this.width / 2 - 100 / 2, posY + 150, 100, 20, "Set Password"));
	}else{
		this.buttonList.add(new GuiButton(12, this.width / 2 - 100 / 2, posY + 150, 100, 20, "Enter"));
	}
    	this.buttonList.add(new GuiButton(1, this.width / 2 - 20 - 30 / 2, posY + 50, 20, 20, "1"));
    	this.buttonList.add(new GuiButton(2, this.width / 2 - 20 / 2, posY + 50, 20, 20, "2"));
    	this.buttonList.add(new GuiButton(3, this.width / 2 - 20 + 70 / 2, posY + 50, 20, 20, "3"));
    	
    	this.buttonList.add(new GuiButton(4, this.width / 2 - 20 - 30 / 2, posY + 75, 20, 20, "4"));
    	this.buttonList.add(new GuiButton(5, this.width / 2 - 20 / 2, posY + 75, 20, 20, "5"));
    	this.buttonList.add(new GuiButton(6, this.width / 2 - 20 + 70 / 2, posY + 75, 20, 20, "6"));
    	
    	this.buttonList.add(new GuiButton(7, this.width / 2 - 20 - 30 / 2, posY + 100, 20, 20, "7"));
    	this.buttonList.add(new GuiButton(8, this.width / 2 - 20 / 2, posY + 100, 20, 20, "8"));
    	this.buttonList.add(new GuiButton(9, this.width / 2 - 20 + 70 / 2, posY + 100, 20, 20, "9"));

    	this.buttonList.add(new GuiButton(10, this.width / 2 - 20 / 2, posY + 125, 20, 20, "0"));

    	this.buttonList.add(new GuiButton(11, this.width / 2 - 20 + 70 / 2, posY + 125, 20, 20, "<-"));

    	
    	textfield = new GuiTextField(fontRendererObj, this.width / 2 - 70 / 2, this.height / 2 - 65, 70, 20);
    	textfield.setFocused(false);
    	textfield.setMaxStringLength(10);

    }
    
    public void updateScreen() {

    	pass = textfield.getText();    	
    	
    }
    
@Override
    protected void drawGuiContainerForegroundLayer(int x, int y) {
    	fontRendererObj.drawString(text, this.width / 2 - 15, this.height / 2 - 80, 4210752);
    	
    }

    
    
    public void drawScreen(int i, int j, float f) {
    	super.drawScreen(i, j, f);
    	textfield.drawTextBox();

    }
    
    protected void actionPerformed(GuiButton guibutton) {
    	IBlockAccess world;
    	if(guibutton.id == 0) {
    		if(new TileEntityKeyPad().pass == null) {
    			new TileEntityKeyPad().pass = pass;
    			System.out.println(new TileEntityKeyPad().pass);
    		}else{

    		}
    	}
    	
    	if(guibutton.id == 12) {
    		if(new TileEntityKeyPad().pass == null) {
    		}else{
    			String np = textfield.getText();
    			if(np.equals(new TileEntityKeyPad().pass)) {
    				System.out.println("dddasd");
    			}
    		}

    	}
    	
    	if(guibutton.id == 1) {
    		pass = pass + "1";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 2) {
    		pass = pass + "2";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 3) {
    		pass = pass + "3";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 4) {
    		pass = pass + "4";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 5) {
    		pass = pass + "5";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 6) {
    		pass = pass + "6";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 7) {
    		pass = pass + "7";
    		textfield.setText(pass);
    	}
    	if(guibutton.id ==  {
    		pass = pass + "8";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 9) {
    		pass = pass + "9";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 10) {
    		pass = pass + "0";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 11) {
    		if(pass != null && pass != "") {
    			pass = pass.substring(0, pass.length() - 1);
    			textfield.setText(pass);
    		}
    	}
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float opacity, int x, int y)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.mc.getTextureManager().bindTexture(Textures.KEY_PAD);

        int xStart = (width - xSize) / 2;
        int yStart = (height - ySize) / 2;
        this.drawTexturedModalRect(xStart, yStart, 0, 0, xSize, ySize);
    }

}

 

TileEntityKeyPad

 

package com.techmod.client.tileentity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;

public class TileEntityKeyPad extends TileEntity {

public String pass;
public int pa;

    public void readFromNBT(NBTTagCompound p_145839_1_) {
        super.readFromNBT(p_145839_1_);

        pass = p_145839_1_.getString("Pass");

    }
    
    public void writeToNBT(NBTTagCompound p_145841_1_)
    {
        super.writeToNBT(p_145841_1_);
        p_145841_1_.setString("Pass", pass);
    }



}

 

why do you use "new TileEntityKeyPad()" every time?

It would be a new instance, and using the component of the instance would be no use.

instead, you can use GuiKeyPad#tileEntityKeyPad. Replace new TileEntityKeyPad() to tileEntityKeyPad.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Are you saying to make a instance like public TileEntityKeyPad tileentitykeypad;? if so when I do that I crash when using the set password button. And the reason I use new TileEntityKeyPad() is because I was told to make the variables in the tileentity class non static to fix another problem and that was the only way I could figure out how to fix it and not crash

Link to comment
Share on other sites

Are you saying to make a instance like public TileEntityKeyPad tileentitykeypad;? if so when I do that I crash when using the set password button. And the reason I use new TileEntityKeyPad() is because I was told to make the variables in the tileentity class non static to fix another problem and that was the only way I could figure out how to fix it and not crash

You already have the tileEntityKeyPad in GuiKeyPad class, So you can use it.

Also, if the variable is not static, it will be always an initialized variable when you calls "new TileEntityKeyPad().pass".

So you should not use new TileEntityKeyPad().

Just use the tileEntityKeyPad in GuiKeyPad class for those use, I think it is the solution.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

Well I got that working but now but all the tileentitys of the same type have the same string. Say tile entity a has the password 123 the all the others of that same type have it do you know what is causing that? Btw thank you so much for your help.

Link to comment
Share on other sites

Then please post your code now. I think it would be changed a lot..

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

Link to comment
Share on other sites

GuiKeyPad.java

package com.techmod.client.gui;

import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.world.IBlockAccess;

import org.lwjgl.opengl.GL11;

import com.techmod.client.container.ContainerKeyPad;
import com.techmod.client.tileentity.TileEntityKeyPad;

public class GuiKeyPad extends GuiContainer {

public IBlockAccess world;

    @SuppressWarnings("unused")
private TileEntityKeyPad tileEntityKeyPad;
    
    private GuiTextField textfield;
    
    public static TileEntityKeyPad tileentitykeypad = new TileEntityKeyPad();

    
    public static String text = "Keypad";
    private static String pass = null;
    
    
    
    public GuiKeyPad(InventoryPlayer inventoryPlayer, TileEntityKeyPad keypad)
    {
        super(new ContainerKeyPad(inventoryPlayer, keypad));
        tileEntityKeyPad = keypad;
        
        xSize = 176;
        ySize = 175;

    }
    
    @SuppressWarnings("unchecked")
public void initGui() {
    	
    	this.buttonList.clear();

    	//int posX = (this.width - xSize) / 2;
    	int posY = (this.height - ySize) / 2;
    	
   		//if(this.KeyPad.pass == null) {
    	TileEntityKeyPad tile = (TileEntityKeyPad) tileentitykeypad.getWorldObj().getTileEntity(tileentitykeypad.xCoord, tileentitykeypad.yCoord, tileentitykeypad.zCoord);

    	if (tile != null) {
    	if(tileentitykeypad.pass == null || tileentitykeypad.pass.isEmpty()) {
			this.buttonList.add(new GuiButton(0, this.width / 2 - 100 / 2, posY + 150, 100, 20, "Set Password"));
		}else{
			this.buttonList.add(new GuiButton(12, this.width / 2 - 100 / 2, posY + 150, 100, 20, "Enter"));
		}
    	this.buttonList.add(new GuiButton(1, this.width / 2 - 20 - 30 / 2, posY + 50, 20, 20, "1"));
    	this.buttonList.add(new GuiButton(2, this.width / 2 - 20 / 2, posY + 50, 20, 20, "2"));
    	this.buttonList.add(new GuiButton(3, this.width / 2 - 20 + 70 / 2, posY + 50, 20, 20, "3"));
    	
    	this.buttonList.add(new GuiButton(4, this.width / 2 - 20 - 30 / 2, posY + 75, 20, 20, "4"));
    	this.buttonList.add(new GuiButton(5, this.width / 2 - 20 / 2, posY + 75, 20, 20, "5"));
    	this.buttonList.add(new GuiButton(6, this.width / 2 - 20 + 70 / 2, posY + 75, 20, 20, "6"));
    	
    	this.buttonList.add(new GuiButton(7, this.width / 2 - 20 - 30 / 2, posY + 100, 20, 20, "7"));
    	this.buttonList.add(new GuiButton(8, this.width / 2 - 20 / 2, posY + 100, 20, 20, "8"));
    	this.buttonList.add(new GuiButton(9, this.width / 2 - 20 + 70 / 2, posY + 100, 20, 20, "9"));

    	this.buttonList.add(new GuiButton(10, this.width / 2 - 20 / 2, posY + 125, 20, 20, "0"));

    	this.buttonList.add(new GuiButton(11, this.width / 2 - 20 + 70 / 2, posY + 125, 20, 20, "<-"));

    	
    	textfield = new GuiTextField(fontRendererObj, this.width / 2 - 70 / 2, this.height / 2 - 65, 70, 20);
    	textfield.setFocused(false);
    	textfield.setMaxStringLength(10);
    	}
    }
    
    public void updateScreen() {

    	pass = textfield.getText();    	
    	
    }
    
@Override
    protected void drawGuiContainerForegroundLayer(int x, int y) {
    	fontRendererObj.drawString(text, this.width / 2 - 15, this.height / 2 - 80, 4210752);
    	
    }

    
    
    public void drawScreen(int i, int j, float f) {
    	super.drawScreen(i, j, f);
    	textfield.drawTextBox();

    }
    
    protected void actionPerformed(GuiButton guibutton) {
    	TileEntityKeyPad tile = (TileEntityKeyPad) tileentitykeypad.getWorldObj().getTileEntity(tileentitykeypad.xCoord, tileentitykeypad.yCoord, tileentitykeypad.zCoord);

    	if(guibutton.id == 0) {
        	if (tile != null) {
    		if(tileentitykeypad.pass == null) {
    			tileentitykeypad.pass = pass;
    		}else{

    		}
        	}
    	}
    	
    	if(guibutton.id == 12) {
        	if (tile != null) {
    		if(tileentitykeypad.pass == null) {
    		}else{
    			String np = textfield.getText();
    			if(np.equals(tileentitykeypad.pass)) {
    				System.out.println("dddasd");
    			}
    		}
        	}
    	}
    	
    	if(guibutton.id == 1) {
    		pass = pass + "1";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 2) {
    		pass = pass + "2";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 3) {
    		pass = pass + "3";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 4) {
    		pass = pass + "4";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 5) {
    		pass = pass + "5";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 6) {
    		pass = pass + "6";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 7) {
    		pass = pass + "7";
    		textfield.setText(pass);
    	}
    	if(guibutton.id ==  {
    		pass = pass + "8";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 9) {
    		pass = pass + "9";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 10) {
    		pass = pass + "0";
    		textfield.setText(pass);
    	}
    	if(guibutton.id == 11) {
    		if(pass != null && pass != "") {
    			pass = pass.substring(0, pass.length() - 1);
    			textfield.setText(pass);
    		}
    	}
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float opacity, int x, int y)
    {
        GL11.glColor4f(1.0F, 1.0F, 1.0F, 1.0F);

        this.mc.getTextureManager().bindTexture(Textures.KEY_PAD);

        int xStart = (width - xSize) / 2;
        int yStart = (height - ySize) / 2;
        this.drawTexturedModalRect(xStart, yStart, 0, 0, xSize, ySize);
    }

}

 

TileEntityKeyPad.java

 

package com.techmod.client.tileentity;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

public class TileEntityKeyPad extends TileEntity {

public String pass;
public int pa;

    public void readFromNBT(NBTTagCompound p_145839_1_) {
        super.readFromNBT(p_145839_1_);

        pass = p_145839_1_.getString("Pass");

    }

    public void writeToNBT(NBTTagCompound p_145841_1_)
    {
        super.writeToNBT(p_145841_1_);    
        
        p_145841_1_.setString("Pass", pass);
    }



}

 

Link to comment
Share on other sites

Hmm.. maybe you are confused with capital letters.

Do not make and use a static variable: tileentitykeypad.

Just change all of them to: tileEntityKeyPad. It was already declared in the GuiKeyPad class, but not used.

I. Stellarium for Minecraft: Configurable Universe for Minecraft! (WIP)

II. Stellar Sky, Better Star Rendering&Sky Utility mod, had separated from Stellarium.

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.