Jump to content

Gui to Edit Tile Entity Data


Reika

Recommended Posts

I am aware this has been asked before, but no thread I have found has had a solution that worked for me.

What I need to do is simple and probably will look excessively newbie-like to those of you experienced with the 1.3+ integrated server.

 

I have a GUI with several custom buttons, and I want clicking these buttons to be able to update the state of variables inside the Tile Entity. I started off with old 1.2.5-style code, and got the "client side effect but no server update" glitch; switching to using the packet handlers in the forge tutorials now leaves the changes not happening at all.

 

The relevant sections of my code:

Tile Entity:

    @Override
    public Packet getDescriptionPacket()
    {
     NBTTagCompound var1 = new NBTTagCompound();
     this.writeToNBT(var1);
     return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 2, var1);
    }
            
    @Override
    public void onDataPacket(INetworkManager netManager, Packet132TileEntityData packet)
    {
     readFromNBT(packet.customParam1);
    }
    
@Override
public void handlePacketData(INetworkManager network, Packet250CustomPayload packet, EntityPlayer player, ByteArrayDataInput dataStream)
{
	try {
		this.mode = dataStream.readInt();
	} catch (Exception e)
	{
		System.out.println("[RotaryCraft] Error while handling tile entity packet.");
		e.printStackTrace();
	}
}

 

Gui:

    public GuiBorer(InventoryPlayer par1InventoryPlayer, TileEntityBorer par2TileEntityBorer, EntityPlayer par5player)
    {
        super(new ContainerBorer(par1InventoryPlayer, par2TileEntityBorer));
        borer = par2TileEntityBorer;
    	this.ySize = 215;
    	dropstatus = "Drops On";
    	player = par5player;
    	drops = borer.drops;
    	mode = borer.mode;
    }
    
    @Override
    public void initGui() {
        int j = (width - xSize) / 2;
        int k = (height - ySize) / 2;
            this.controlList.add(new GuiButton(0, j+8, -1+k+32, 72, 20, "1x1 Square"));
            this.controlList.add(new GuiButton(1, j+8, -1+k+52, 72, 20, "1x2 Hallway"));
            this.controlList.add(new GuiButton(2, j+8, -1+k+72, 72, 20, "2x2 Square"));
            this.controlList.add(new GuiButton(3, j+8, -1+k+92, 72, 20, "3x3 Square"));
            this.controlList.add(new GuiButton(4, j+8, -1+k+112, 72, 20, "5x5 Circle"));
            this.controlList.add(new GuiButton(5, j+8, -1+k+132, 72, 20, "7x4 Archway"));
            this.controlList.add(new GuiButton(6, j+8, -1+k+152, 72, 20, "4x4 Tunnel"));
            
            if (drops)
            	this.controlList.add(new GuiButton(7, j+95, -1+k+32, 72, 20, "Drops On"));
            else
            	this.controlList.add(new GuiButton(7, j+95, -1+k+32, 72, 20, "Drops Off"));
    }
    
    public void toggleDrops() {
    	if (drops) {
    		dropstatus = "Drops Off";
    		drops = false;
    	}
    	else {
    		dropstatus = "Drops On";
    		drops = true;
    	}
    	
    	this.sendPacket(1);
    }
    
    public void updateMode(int mode) {
    	borer.mode = (byte)mode;
    }
    
    /**
     * Returns true if this GUI should pause the game when it is displayed in single-player
     */
    public boolean doesGuiPauseGame()
    {
        return true;
    }

    @Override
    public void actionPerformed(GuiButton button) {
            if (button.id == 7) {
            	this.toggleDrops();
            }
            if (button.id < 7) {
            	//this.updateMode(button.id);
            	mode = button.id;
            }
            this.sendPacket(2);
            this.refreshScreen();
            
    }
    
    public void sendPacket(int a) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(;
        DataOutputStream outputStream = new DataOutputStream(bos);
        try {
        	if (a == 1)
        		outputStream.writeBoolean(drops);
        	if (a == 2)
        		outputStream.writeInt(mode);
        } catch (Exception ex) {
                ex.printStackTrace();
        }
       
        Packet250CustomPayload packet = new Packet250CustomPayload();
        packet.channel = "GenericRandom";
        packet.data = bos.toByteArray();
        packet.length = bos.size();
       
        Side side = FMLCommonHandler.instance().getEffectiveSide();
        if (side == Side.SERVER) {
                // We are on the server side.
                EntityPlayerMP player2 = (EntityPlayerMP) player;
        } else if (side == Side.CLIENT) {
                // We are on the client side.
                EntityClientPlayerMP player2 = (EntityClientPlayerMP) player;
                this.mc.getSendQueue().addToSendQueue(packet);
        } else {
                // We are on the Bukkit server.
        }
    }

 

Container:

public class ContainerBorer extends Container
{
    private TileEntityBorer borer;

    public ContainerBorer(InventoryPlayer par1InventoryPlayer, TileEntityBorer par2TileEntityBorer)
    {
        borer = par2TileEntityBorer;
        int posX = borer.xCoord;
        int posY = borer.yCoord;
        int posZ = borer.zCoord;
    }

    public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    {
        return borer.isUseableByPlayer(par1EntityPlayer);
    }
}

 

Packet Handler:

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload;
import cpw.mods.fml.common.network.IPacketHandler;
import cpw.mods.fml.common.network.Player;


public class ClientPacketHandler implements IPacketHandler {

    @Override
    public void onPacketData(INetworkManager manager, Packet250CustomPayload payload, Player player) {
        DataInputStream data = new DataInputStream(new ByteArrayInputStream(payload.data));
    }
}

 

Excerpt from main mod class:

@Mod( modid = "RotaryCraft", name="RotaryCraft", version="beta v0.1")
@NetworkMod(clientSideRequired=true, serverSideRequired=false,
channels={"GenericRandom"}, packetHandler = PacketHandler.class)

Link to comment
Share on other sites

Everything but block was in the OP, but here is my block code:

public BlockBorer(int blockID, int textureID) {
	super(blockID, textureID, Material.iron);
	setHardness(4F);
	setResistance(10F);
	setLightValue(0F);
	setStepSound(soundMetalFootstep);
	this.requiresSelfNotify[this.blockID] = true;	
	this.blockIndexInTexture = 57;
	setCreativeTab(mod_RotaryCraft.tabRotary);
}

public void addCreativeItems(ArrayList list)
{
        list.add(new ItemStack(this));
}

    /**
     * Returns the TileEntity used by this block.
     */
    public TileEntity createNewTileEntity(World world)
    {
        return new TileEntityBorer();
    }

    public int idDropped(int par1, Random par2Random, int par3)
    {
        return this.blockID;
    }
    
    public int damageDropped(int par1)
    {
        return 0;
    }
    
    public int quantityDropped(Random par1Random)
    {
        return 1;
    }
    
    /**
     * Called upon block activation (left or right click on the block.). The three integers represent x,y,z of the
     * block.
     */
    public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer ep, int par6, float par7, float par8, float par9)
    {
        if (world.isRemote)
        {
            return true;
        }

        TileEntityBorer borer = (TileEntityBorer)world.getBlockTileEntity(x, y, z);

        if (borer != null)
        {
        	ep.openGui(mod_RotaryCraft.instance, 9, world, x, y, z);
        	//ModLoader.openGUI(ep, new GuiBorer());
        }

        return true;
    }
public void onBlockPlacedBy(World par1World, int par2, int par3, int par4, EntityLiving par5EntityLiving)		//Directional code
    {
        int i = MathHelper.floor_double((double)((par5EntityLiving.rotationYaw * 4F) / 360F) + 0.5D);
        while (i > 3)
        	i -= 4;
        while (i < 0)
        	i += 4;

        switch (i) {
        case 0:
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 2);
        break;
        case 1:
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 1);
        break;
        case 2:
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 3);
        break; 
        case 3:
            par1World.setBlockMetadataWithNotify(par2, par3, par4, 0);
        break;
        }            
       this.metadata = par1World.getBlockMetadata(par2, par3, par4);
    }
@Override
public String getTextureFile(){
	return "/Reika/RotaryCraft/textures.png"; //return the block texture where the block texture is saved in
}

Link to comment
Share on other sites

Your problem is the packet handler class.

first of all you named you class ClientPacketHandler but in your networkMod annotation you put PacketHandler.class.

second you didnt put any code in your packet handler to actually handle the packet.

the packet reaches the server but the server doesnt know what to do with it so ignores it.

there is an excellent packet hadling tutorial on the forge wiki.

http://www.minecraftforge.net/wiki/Packet_Handling

i would advice to follow this tutorial and if you got any questions left ask them  ;)

Link to comment
Share on other sites

Your problem is the packet handler class.

first of all you named you class ClientPacketHandler but in your networkMod annotation you put PacketHandler.class.

second you didnt put any code in your packet handler to actually handle the packet.

the packet reaches the server but the server doesnt know what to do with it so ignores it.

there is an excellent packet hadling tutorial on the forge wiki.

http://www.minecraftforge.net/wiki/Packet_Handling

i would advice to follow this tutorial and if you got any questions left ask them  ;)

It works now, but given that I was working from that tutorial - my PacketHandler is a copy-and-paste of its code - I question its use if it caused this much headache.

Of course, in hindsight one remembers one of the opening lines of the tutorial, which says the tutorial will not actually show how to use the packets usefully...

 

 

Now, I am left with one other, smaller, glitch. My button toggles the state of a variable in the Tile Entity, and should update the GUI to match (switch a variable inside the Gui Class which controls a string inside a button constructor). The Tile Entity updates and behaves correctly, but the GUI must be closed and reopened, manually, to change...why? (Even more interesting: before I set it to set the GUI class variable to be equal to the one in the Tile Entity, it would only update on world reload (save/reenter).)

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.



×
×
  • Create New...

Important Information

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