Jump to content

TileEntity and GUI problems [1.7.2]


kris91268

Recommended Posts

Hello,

 

I have a gravity lift block, which repels you upwards from a certain set height, however I am having problems with that height not saving.

 

When the GUI is closed, it calls a method directly in the tile entity class to set the height for which the gravity lift would propel you upwards. It does work, but does not save.

 

I know you must use packets for this, but I have not been able to find a tutorial that teaches what I need - all of the packet tutorials are not for 1.7.2.

 

I have posted a topic on the Forge Forums a while back about the same issue, but that was for MC 1.6, so it is outdated.

 

Any help with ways of solving this would be much appreciated! Plus any links to tutorials that have exactly what I need.

 

Code:

TileEntityGravityLift.java

 

package kris91268.lbd.Tileentity;

import kris91268.lbd.ModLBD;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;

/**
* 
* @author Arbiter
*
*/
public class TileEntityGravityLift extends TileEntity
{
public float height;

public void readFromNBT(NBTTagCompound par1)
{
	super.readFromNBT(par1);
	height = par1.getFloat("Height");
}
public void writeToNBT(NBTTagCompound par1)
{
	super.writeToNBT(par1);
	par1.setFloat("Height", height);
}
public void setHeight(float height)
{
	this.height = height;
	this.markDirty();
	worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
	worldObj.notifyBlockChange(xCoord, yCoord, zCoord, this.getBlockType());
}
}

 

 

GuiGravityLift.java

 

package kris91268.lbd;

import org.lwjgl.input.Keyboard;
import kris91268.lbd.Packet.PacketUpdateHeight;
import kris91268.lbd.Tileentity.TileEntityGravityLift;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.GuiTextField;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.network.NetHandlerPlayClient;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.network.play.client.C12PacketUpdateSign;
import net.minecraft.network.play.client.C17PacketCustomPayload;
import net.minecraft.util.ResourceLocation;

/**
* 
* @author Arbiter
*
*/
public class GuiGravityLift extends GuiContainer
{
private static final ResourceLocation texture = new ResourceLocation("lbd:textures/gui/gravLift.png");
public TileEntityGravityLift tileEntity;
private GuiButton doneButton, decByPointZeroOne, decByPointOne, incByPointOne, incByOnePointZero;
private GuiTextField number;
private float liftHeight;

public GuiGravityLift(TileEntityGravityLift tileEntity)
{
	super(new ContainerGravityLift());
	this.tileEntity = tileEntity;
}
@Override
public void drawGuiContainerForegroundLayer(int par1, int par2)
{
	this.fontRendererObj.drawString("Gravity Lift", this.xSize / 2 - 
			this.fontRendererObj.getStringWidth("Gravity Lift") / 2, 6, 4210752);
}
@Override
public void drawGuiContainerBackgroundLayer(float par1, int par2, int par3)
{
	this.mc.getTextureManager().bindTexture(texture);
}
@Override
public void initGui()
{
	this.buttonList.add(this.doneButton = new GuiButton(0, this.width / 2 - 25, this.height / 4 + 35, 50, 20, "Done"));
	this.buttonList.add(this.decByPointZeroOne = new GuiButton(1, this.width / 2 - 75, this.height / 4 + 0, 40, 20, "-0.1"));
	this.buttonList.add(this.decByPointOne = new GuiButton(2, this.width / 2 - 75, this.height / 4 + 25, 40, 20, "-1.0"));
	this.buttonList.add(this.incByPointOne = new GuiButton(3, this.width / 2 + 25, this.height / 4 + 0, 40, 20, "+0.1"));
	this.buttonList.add(this.incByOnePointZero = new GuiButton(4, this.width / 2 + 25, this.height / 4 + 25, 40, 20, "+1.0"));
	this.number = new GuiTextField(this.fontRendererObj, this.width / 2 - 20, this.height / 4 + 1, 40, 20);
	this.number.setVisible(true);
	this.number.setEnabled(true);
	this.number.setText(Float.toString(liftHeight));
}
@Override
public void onGuiClosed()
{
	Keyboard.enableRepeatEvents(false);
	this.tileEntity.setHeight(liftHeight);
}
@Override
protected void actionPerformed(GuiButton button)
{
	switch (button.id)
	{
	case 0:
		this.mc.displayGuiScreen((GuiScreen)null);
		break;
	case 1:
		liftHeight -= 0.1f;
		break;
	case 2:
		liftHeight -= 1.0f;
		break;
	case 3:
		liftHeight += 0.1f;
		break;
	case 4:
		liftHeight += 1.0f;
		break;
	}
}
}
class ContainerGravityLift extends Container
{
@Override
public boolean canInteractWith(EntityPlayer var1) 
{
	return true;
}	
}

 

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.