Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

GUI Button Packet handling


bigbaddevil6
 Share

Recommended Posts

Well, I thought I understood it and after looking for quite a while at other Open Source Code. I have come to the realization, I have hardly any clue what I'm doing with this. I have sent a packet I've gotten that far, but this one is stumping me cause I need to get the TileEntity, buttonId and handle it. I think it comes down to it, I'm handling getting the TileEntity completely wrong and most likely going to be told so. Hopefully you can look past my stupidity, and give me a few pointers on the proper way to do this. Once I get it the first time I'm usually golden. It's just getting it that first time....

 

The idea is simple click the button in the machine's GUI and the machine will be disabled and the texture will change to show. Here is what I got so far.

 

MessageButtonPressed

 

 

package com.bigbaddevil7.rustic.network.Message;

import com.bigbaddevil7.rustic.test.TileEntityMachine;
import com.bigbaddevil7.rustic.utility.LogHelper;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.tileentity.TileEntity;

/**
* Created by bigbaddevil7 on 11/5/2014.
*/
public class MessageButtonPressed implements IMessage{

    public byte id;
    public int x,y,z;

    static TileEntityMachine machine;
    public MessageButtonPressed(){

    }

    public MessageButtonPressed(TileEntityMachine tileEntityMachine, byte id){
        LogHelper.info("Entered Constructor");
        this.id = id;
        this.x = tileEntityMachine.xCoord;
        this.y = tileEntityMachine.yCoord;
        this.z = tileEntityMachine.zCoord;
    }

    @Override
    public void fromBytes(ByteBuf buf) {
        LogHelper.info("Reading Bytes");
        this.id = buf.readByte();
        this.x = buf.readInt();
        this.y = buf.readInt();
        this.z = buf.readInt();
        LogHelper.info("Done Reading Bytes");
    }

    @Override
    public void toBytes(ByteBuf buf) {
        LogHelper.info("Writing Bytes");
        buf.writeByte(id);
        buf.writeInt(x);
        buf.writeInt(y);
        buf.writeInt(z);
        LogHelper.info("Done Writing bytes");
    }

    public static class Handler implements IMessageHandler<MessageButtonPressed, IMessage>{


        @Override
        public IMessage onMessage(MessageButtonPressed message, MessageContext ctx) {
            LogHelper.info("Got Request");
            TileEntity tileEntity = FMLClientHandler.instance().getClient().theWorld.getTileEntity(message.x, message.y, message.z);
            if(tileEntity instanceof TileEntityMachine){
                ((TileEntityMachine)tileEntity).recieveButtonEvent(message.id);
                LogHelper.info("Not Machine");
            }
            LogHelper.info("Recieved Packet" + message.id);
            return null;
        }
    }

}

 

 

 

The actionPerformed() that is in the GUI class for the machine to send the packet

 

 

    @Override
    protected void actionPerformed(GuiButton button) {
        Rustic.network.sendToServer(new MessageButtonPressed(machine, (byte)button.id));
        LogHelper.info("Sent Packet");
    }

 

 

 

recieveButtonEvent() Handles the actual disabling of the machine

 

 

public void recieveButtonEvent(byte buttonId){
        switch (buttonId){
            case 0:
            int meta = worldObj.getBlockMetadata(xCoord,yCoord,zCoord);//gets the metadata

            int type = meta / 2;//uses the groups of two

            int disabled = meta % 2 == 0 ? 1: 0;//uses the groups of two using % to see if it is disabled

            int newMeta = type * 2 + disabled;

            worldObj.setBlockMetadataWithNotify(xCoord,yCoord,zCoord, newMeta, 3);//updates the block with the new metadata
                break;
        }
    }

 

 

 

It's not that I crash or anything just nothing happens, the packet gets sent, but the onMessage() never seems to be fired.

 

Link to comment
Share on other sites

a) Why are you sending the TE coordinates? The server knows what Gui the player is looking at.

b) Why are you using the client world in a message received on the server? You need to use the world you can get from the MessageContext.

 

What I saw used in other source code.

 

 

Alright I made changes. Now I get a crash. The Id from I can tell is 0, but It throws an OutOfBounds exception.

 

 

 

[12:06:46] [server thread/ERROR] [FML]: FMLIndexedMessageCodec exception caught
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(1) + length(4) exceeds writerIndex(1): SlicedByteBuf(ridx: 1, widx: 1, cap: 1/1, unwrapped: UnpooledHeapByteBuf(ridx: 1, widx: 2, cap: 2/2))
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) [DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) [DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) [EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(1) + length(4) exceeds writerIndex(1): SlicedByteBuf(ridx: 1, widx: 1, cap: 1/1, unwrapped: UnpooledHeapByteBuf(ridx: 1, widx: 2, cap: 2/2))
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1160) ~[AbstractByteBuf.class:?]
at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:611) ~[AbstractByteBuf.class:?]
at com.bigbaddevil7.rustic.network.Message.MessageButtonPressed.fromBytes(MessageButtonPressed.java:37) ~[MessageButtonPressed.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:17) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:7) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:77) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more
[12:06:46] [server thread/ERROR] [FML]: There was a critical exception handling a packet on channel Rustic
io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(1) + length(4) exceeds writerIndex(1): SlicedByteBuf(ridx: 1, widx: 1, cap: 1/1, unwrapped: UnpooledHeapByteBuf(ridx: 1, widx: 2, cap: 2/2))
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:99) ~[MessageToMessageDecoder.class:?]
at io.netty.handler.codec.MessageToMessageCodec.channelRead(MessageToMessageCodec.java:111) ~[MessageToMessageCodec.class:?]
at io.netty.channel.DefaultChannelHandlerContext.invokeChannelRead(DefaultChannelHandlerContext.java:337) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelHandlerContext.fireChannelRead(DefaultChannelHandlerContext.java:323) ~[DefaultChannelHandlerContext.class:?]
at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:785) ~[DefaultChannelPipeline.class:?]
at io.netty.channel.embedded.EmbeddedChannel.writeInbound(EmbeddedChannel.java:169) ~[EmbeddedChannel.class:?]
at cpw.mods.fml.common.network.internal.FMLProxyPacket.processPacket(FMLProxyPacket.java:86) [FMLProxyPacket.class:?]
at net.minecraft.network.NetworkManager.processReceivedPackets(NetworkManager.java:241) [NetworkManager.class:?]
at net.minecraft.network.NetworkSystem.networkTick(NetworkSystem.java:182) [NetworkSystem.class:?]
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:726) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:614) [MinecraftServer.class:?]
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) [integratedServer.class:?]
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:485) [MinecraftServer.class:?]
at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:752) [MinecraftServer$2.class:?]
Caused by: java.lang.IndexOutOfBoundsException: readerIndex(1) + length(4) exceeds writerIndex(1): SlicedByteBuf(ridx: 1, widx: 1, cap: 1/1, unwrapped: UnpooledHeapByteBuf(ridx: 1, widx: 2, cap: 2/2))
at io.netty.buffer.AbstractByteBuf.checkReadableBytes(AbstractByteBuf.java:1160) ~[AbstractByteBuf.class:?]
at io.netty.buffer.AbstractByteBuf.readInt(AbstractByteBuf.java:611) ~[AbstractByteBuf.class:?]
at com.bigbaddevil7.rustic.network.Message.MessageButtonPressed.fromBytes(MessageButtonPressed.java:37) ~[MessageButtonPressed.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:17) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.simpleimpl.SimpleIndexedCodec.decodeInto(SimpleIndexedCodec.java:7) ~[simpleIndexedCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:77) ~[FMLIndexedMessageToMessageCodec.class:?]
at cpw.mods.fml.common.network.FMLIndexedMessageToMessageCodec.decode(FMLIndexedMessageToMessageCodec.java:17) ~[FMLIndexedMessageToMessageCodec.class:?]
at io.netty.handler.codec.MessageToMessageCodec$2.decode(MessageToMessageCodec.java:81) ~[MessageToMessageCodec$2.class:?]
at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:89) ~[MessageToMessageDecoder.class:?]
... 13 more

 

 

 

Here is the changes I did to the MessageButtonPressed(). Is this what you were looking for.

 

 

package com.bigbaddevil7.rustic.network.Message;

import com.bigbaddevil7.rustic.client.interfaces.ContainerMachine;
import com.bigbaddevil7.rustic.test.TileEntityMachine;
import com.bigbaddevil7.rustic.utility.LogHelper;
import cpw.mods.fml.common.network.simpleimpl.IMessage;
import cpw.mods.fml.common.network.simpleimpl.IMessageHandler;
import cpw.mods.fml.common.network.simpleimpl.MessageContext;
import io.netty.buffer.ByteBuf;
import net.minecraft.inventory.Container;


/**
* Created by bigbaddevil7 on 11/5/2014.
*/
public class MessageButtonPressed implements IMessage{

    public byte id;
    public int x,y,z;

    static TileEntityMachine machine;
    public MessageButtonPressed(){

    }

    public MessageButtonPressed(byte id){
        LogHelper.info("Entered Constructor");
        this.id = id;

    }

    @Override
    public void fromBytes(ByteBuf buf) {
        LogHelper.info("Reading Bytes");
        LogHelper.info(id);
        this.id = buf.readByte();
        this.x = buf.readInt();
        this.y = buf.readInt();
        this.z = buf.readInt();
        LogHelper.info("Done Reading Bytes");
    }

    @Override
    public void toBytes(ByteBuf buf) {
        LogHelper.info("Writing Bytes");
        buf.writeByte(id);
        LogHelper.info(id);
        LogHelper.info("Done Writing bytes");
    }

    public static class Handler implements IMessageHandler<MessageButtonPressed, IMessage>{


        @Override
        public IMessage onMessage(MessageButtonPressed message, MessageContext ctx) {
            LogHelper.info("Got Request");
            Container container = ctx.getServerHandler().playerEntity.openContainer;
            if(container instanceof ContainerMachine){
                TileEntityMachine machine = ((ContainerMachine)container).getMachine();
                machine.recieveButtonEvent(message.id);
                LogHelper.info("Not Machine");
            }
            LogHelper.info("Recieved Packet" + message.id);
            return null;
        }
    }

}

 

 

Link to comment
Share on other sites

Oh ok. I thought the client would have to get the cords back from the server to update the proper GUI. Since the cords doesn't need to get sent, that means they don't need to be read either? The onMessage() still never gets called. I'm gonna be glad once I figure this one out.... I'm sorry for taking up your time with what seems like a simple thing. Means a lot to have people help. 

Link to comment
Share on other sites

Ahh. I had it registered for Side.CLIENT. IT WORKS!!!! OH FOR THE DEAR LOVE OF THE ALL MIGHTY, MIGHTINESS..... OF MIGHT. Queue the "IT LIVES" from Dr. Frankenstein. This is a revolutionary moment here for me. I've been putting this off for a very long time. Now I have an idea of how to use a very powerful tool. Now that it's all in place. This is a hell of a lot simpler than 1.6 it seems. I guess the name fits. Now that I got this down, I move to one step closer to eventually understanding this modding enough to start doing my steam power system. Thank you so much diesieben you have been a great help over the times.

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
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.

 Share



×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.