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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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