Jump to content

[1.7.10] How to make a modular IMessage?


Ermans

Recommended Posts

As you know, the IMessage lets you to create your own packet to exchange data with your tiles, but if you have a lot of tiles which basically do the same things except for one thing, you have to create (at least this is what I am doing) an IMessage for each of them.

How can I make the IMessage like the NBT tags? I mean when you deal with NBT you just override the two methods (calling the super) and use the NBTTagCompound object to write/get the things you want.

How I achieve the same result with IMessage? Or Where can I find some code that have already implemented such things?

I don't even know if I need to keep using IMessage interface.  :-\

 

 

 

 

Link to comment
Share on other sites

create a super class like "MyMessageForAllMyTiles", write all the data that EVERY of ur tiles have ( let it implement IMessage ofc)

after that you can inherit from that class and have much less effort.

or you make one big message that supports all your tileentites and send one number for the "id" of each tileentity for switch case issues

Link to comment
Share on other sites

This has nothing to do with modding, but rather code design. Well... some may argue.

Anyway - to make it look nice you need some kind of abstraction.

 

If all TEs are doing similar thing, you can make some AbstractBaseTe. Make some common methods that return data or even better - make methods that encode data onto buffer of your packet. That way you could make one packet that accepts AbstractBaseTE and calls some encode(). Then when you receive packet you can read some ID (type) of TE and read it back.

 

There is literally infinite ways to design your code and if you don't know now - give it time, read other ppl code (open sources). You will probably rewrite big parts of your mod some day when you realize there is better way to do it.

 

Finally - yes, you will always use IMessage and MessageHandler.

 

INSTA EDIT

Well, damn me! Failender was faster :D

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Thanks both for introducing me to all these possibilities.

This is what I've done to resolve my problem:

 

On the super class of all my tiles I decided to add this two methods:

public void fromBytes(ByteBuf buf) {

}

public void toBytes(ByteBuf buf) {

}

So my tiles can override them.

 

And on the IMessage class:

public class MessageTilev2 implements IMessage{

    private MyTile tile;
    private int x;
    private int y;
    private int z;

    public MessageTilev2() {
    }

    public MessageTilev2(MyTile tile) {
        this.tile = tile;
        this.x = tile.xCoord;
        this.y = tile.yCoord;
        this.z = tile.zCoord;
    }


    @Override
    public void fromBytes(ByteBuf buf) {
        this.x = buf.readInt();
        this.y = buf.readInt();
        this.z = buf.readInt();

        TileEntity entity = Minecraft.getMinecraft().thePlayer.worldObj.getTileEntity(this.x, this.y, this.z);
        if (entity instanceof MyTile){
            ((MyTile) entity).fromBytes(buf);
        }
    }

    @Override
    public void toBytes(ByteBuf buf) {
        buf.writeInt(this.x);
        buf.writeInt(this.y);
        buf.writeInt(this.z);

        tile.toBytes(buf);
    }

    public static class Handler implements IMessageHandler<MessageTilev2, IMessage> {
        @Override
        public IMessage onMessage(MessageTilev2 message, MessageContext ctx) {
            return null;
        }
    }
}

 

Last question not closely related to this topic... Is it good to send a message every tick when a tile receive energy (to sync the server energy storage with the client)?

Link to comment
Share on other sites

If you only need to display the energy value in a GUI, and you have a Container associated with your GUI, use the Container to syncronize the energy value to the client. This will only make it syncronize if the GUI is open. Look at the ContainerFurnace class, specifically the

addCraftingToCrafters

,

detectAndSendChanges

and

updateProgressBar

methods.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

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.