Jump to content

Setting String in TileEntity from GuiContainer


jaswon5791

Recommended Posts

The situation is as follows: I've created a furnace-esque block called a Write Drive, and in the GUI I have a GuiTextField, and I want to set a string in the TileEntity object to the contents of this GuiTextField. Here is the relevant code:

 

GuiWriteDrive.java:

 


public class GuiWriteDrive extends GuiContainer {

public static final ResourceLocation bground = new ResourceLocation(ModInfo.ID.toLowerCase(),"textures/gui/furnace.png");

private GuiTextField path;

public TileEntityWriteDrive writeDrive;
private TileEntityWriteDrive tile;

public GuiWriteDrive(InventoryPlayer invPlayer, TileEntityWriteDrive entity) {
	super(new ContainerWriteDrive(invPlayer, entity));
	this.writeDrive = entity;
	this.xSize = 176;
	this.ySize = 166;
}

public void drawGuiContainerForegroundLayer(int par1, int par2) {
	String name = this.writeDrive.hasCustomInventoryName() ? this.writeDrive.getInventoryName() : I18n.format(this.writeDrive.getInventoryName(), new Object[0]);
	this.fontRendererObj.drawString(name, (this.xSize - this.fontRendererObj.getStringWidth(name))/2, 6, 4210752);
}

@Override
protected void drawGuiContainerBackgroundLayer(float f, int j, int i) {
	GL11.glColor4f(1F, 1F, 1F, 1F);
	Minecraft.getMinecraft().getTextureManager().bindTexture(bground);
	drawTexturedModalRect(guiLeft, guiTop, 0, 0, xSize, ySize);
	if (writeDrive.isCooking())
	{
		int j1 = writeDrive.getCookTimeRemainingScaled(24);
		drawTexturedModalRect(guiLeft + 78, guiTop + 39, 176, 14, j1 , 17);
	}
	if (writeDrive.isBurning()) {
		int j2 = writeDrive.getBurnTimeRemainingScaled(35);
		drawTexturedModalRect(guiLeft + 27, guiTop + 69, 176, 0, j2 , 7);
	}
}

@Override
    public void drawScreen(int par1, int par2, float par3)
    {
        super.drawScreen(par1, par2, par3);
        this.path.drawTextBox();
    }

public void initGui()
    {
    	super.initGui();
    	this.path = new GuiTextField(this.fontRendererObj,  guiLeft + 6, guiTop + 40, 67, 13);
    	this.path.setTextColor(-1);
    	this.path.setDisabledTextColour(-1);
        this.path.setEnableBackgroundDrawing(true);
        this.path.setMaxStringLength(40);
        this.path.setFocused(true);
    }

public void keyTyped(char c, int i){
	this.path.textboxKeyTyped(c, i);
	TileEntityWriteDrive tile = (TileEntityWriteDrive) writeDrive.getWorldObj().getTileEntity(writeDrive.xCoord, writeDrive.yCoord, writeDrive.zCoord);
	if (tile != null) {
    		tile.path = this.path.getText();
    	}
        if(!( i == Keyboard.KEY_E  &&  this.path.isFocused())) super.keyTyped(c, i);
}
protected void mouseClicked(int par1, int par2, int par3)
    {
        super.mouseClicked(par1, par2, par3);
        this.path.mouseClicked(par1, par2, par3);
    }

public void updateScreen()
    {
        this.path.updateCursorCounter();
    }

}

 

TileEntityWriteDrive.java:

 


public class TileEntityWriteDrive extends TileEntity implements IInventory {

private String local;

private ItemStack[] slots;

public int burnTime;
public int currentItemBurnTime;
public int cookTime;

public String path;

public TileEntityWriteDrive() {
	slots = new ItemStack[3];
	setGuiDisplayName("Write Drive");
	burnTime = 0;
	currentItemBurnTime = 0;
	cookTime = 0;
	path = "";
}

@Override
public void writeToNBT(NBTTagCompound compound) {
	super.writeToNBT(compound);
	NBTTagList list = new NBTTagList();
	for(int i = 0; i < getSizeInventory(); i++) {
		ItemStack itemstack = getStackInSlot(i);
		if(itemstack != null) {
			NBTTagCompound item = new NBTTagCompound();
			item.setByte("Slot", (byte) i);
			itemstack.writeToNBT(item);
			list.appendTag(item);
		}
	}
	compound.setTag("ItemsWriteDrive", list);
	compound.setString("path", path);
}

@Override
public void readFromNBT(NBTTagCompound compound) {
	super.readFromNBT(compound);
	path = compound.getString("path");
	NBTTagList list = compound.getTagList("ItemsWriteDrive", compound.getId());
	for(int i = 0; i < list.tagCount(); i++) {
		NBTTagCompound item = list.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if (slot >= 0 && slot < getSizeInventory()) {
			setInventorySlotContents(slot, ItemStack.loadItemStackFromNBT(item));
		}
	}
}
public boolean canWrite() {
	System.out.println("path = " + path);
	if (path != "testing") {
		return false;
	}
	if (slots[0] == null || slots[1] == null)
	 {
		 return false;
	 }
	 ItemStack itemstack = DriveRecipes.getSmeltingResult(Item.getIdFromItem(slots[0].getItem()), Item.getIdFromItem(slots[1].getItem()));
	 if (itemstack == null)
	 {
		 return false;
	 }
	 if (slots[2] == null)
	 {
		 return true;
	 }
	 if (!slots[2].isItemEqual(itemstack))
	 {
		 return false;
	 }
	 if (slots[2].stackSize < getInventoryStackLimit() && slots[2].stackSize < slots[2].getMaxStackSize())
	 {
		 return true;
	 }
	 else
	 {
		 return slots[2].stackSize < itemstack.getMaxStackSize();
	 }
}

public void writeToDisc()
{
	 if (!canWrite())
	 {
		 return;
	 }
	 ItemStack itemstack = DriveRecipes.getSmeltingResult(Item.getIdFromItem(slots[0].getItem()), Item.getIdFromItem(slots[1].getItem()));
	 if (slots[2] == null)
	 {
		 slots[2] = itemstack.copy();
	 }
	 else if (Item.getIdFromItem(slots[2].getItem()) == Item.getIdFromItem(itemstack.getItem()))
	 {
		 slots[2].stackSize++;
	 }
	 for (int i = 0; i < 2; i++)
	 {
		 if (slots[i].stackSize <= 0)
		 {
			 slots[i] = new ItemStack(slots[i].getItem().setFull3D());
		 }
		 else
		 {
			 slots[i].stackSize--;
		 }
		 if (slots[i].stackSize <= 0)
		 {
			 slots[i] = null;
		 }
	 }
}
public void updateEntity()
{
	 boolean flag = burnTime > 0;
	 boolean flag1 = false;
	 if (burnTime > 0)
	 {
		 burnTime--;
	 }
	 if (!worldObj.isRemote)
	 {
		 if (burnTime == 0 && canWrite())
		 {
			 currentItemBurnTime = burnTime = getItemBurnTime(slots[1]);
			 if (burnTime > 0)
			 {
				 flag1 = true;
				 if (slots[1] != null)
				 {
					 if (slots[1].stackSize == 0)
					 {
						 slots[1] = new ItemStack(slots[1].getItem().setFull3D());
					 }
					 else
					 {
						 slots[1].stackSize--;
					 }
					 if (slots[1].stackSize == 0)
					 {
						 slots[1] = null;
					 }
				 }
			 }
		 }
		 if (isBurning() && canWrite())
		 {
			 cookTime++;
			 if (cookTime == 200)
			 {
				 cookTime = 0;
				 writeToDisc();
				 flag1 = true;
			 }
		 }
		 else
		 {
			 cookTime = 0;
		 }
		 if (flag != (burnTime > 0))
		 {
			 flag1 = true;
		 }
	 }
	 if (flag1)
	 {
		 markDirty();
	 }
}
}


 

What's supposed to happen is that every time a key is pressed within the Gui, tile.path should be set to the contents of the textfield, but that's not actually working. keyTyped() is firing every time I press a key, getText() correctly returns the contents of the textfield, tile does not return null, but for some reason the sysout I have in the first line of the canWrite() method keeps showing "path = " i.e. path wasn't set by the Gui. Can someone lead me in the right direction? I'm pretty sure it's either something really wrong with my code or a typo, but I can't find anything on the internet.

Link to comment
Share on other sites

I'm assuming you're talking about this line (and a few others):

 

Rot.net.sendToServer(new BaseBuilderPacket("2;"+te.xCoord+","+te.yCoord+","+te.zCoord));

 

I found the BaseBuilderPacket class, as well as the TextPacket class, and the method call makes it clear what you're doing. However, I couldn't find the actual code for Rot.net.sendToServer, could you please elaborate on this part?

 

edit:

 

I actually just found that Rot.net refers to a SimpleNetworkWrapper object within your main mod file. I'll look into this, thank you for the resources!

Link to comment
Share on other sites

@Override

protected void actionPerformed(GuiButton button)

{

 

I then check to see if the button ID is my send list button, if so. Send a packet to the server Rot.net.sendToServer(new BaseBuilderPacket("0;"+te.xCoord+","+te.yCoord+","+te.zCoord+";"+list[listIndex++]));

 

now of course you don't need all of what I sent and how I did it, you can use ints for the tile entity coords I kinda derped when making the packet (used to web based stuff and turning everything into a string and decomplining it at the other end.) but the idea of GUI to server to tileEntity is there

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.