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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello there! I am trying to make custom dimensions for a modpack I am making in an older minecraft version, 1.16.5. I like that version and it has a few other mods that have not been updated that I would still like to use. Anyway, I am having a terrible time with getting my dimension to work and have tried using code from other peoples projects to at least figure out what I'm supposed to be doing but it has not been as helpful as I would have liked. If anyone could help that would be greatly appreciated! Here is my github with all the code as I am using it: https://github.com/BladeColdsteel/InvigoratedDimensionsMod I have also included the last log, https://pastebin.com/zX9vsDSq, I had when I tried to load up a world, let me know if there is anything else I should send though, thank you!
    • Whether you are a fan of Hypixel Bedwars, SkyWars and PvP gamemodes like that, well you would enjoy this server! We have a very fun and unique style of PvP that a lot of our players really enjoy and we want to bring this server to more players like you! Yes you reading this post haha. Introducing, the Minezone Network, home of SUPER CRAFT BLOCKS. We've been working on this server for over 4 years now. Here is what we have to offer: SUPER CRAFT BLOCKS: This has 3 different gamemodes you can play, Classic, Duels and Frenzy. Each mode offers over 60 kits to choose from, along with a total of over 60 maps, allowing for various different playstyles on each map. There are also random powerups that spawn on the map which can include Health Pots, Bazookas, Nukes, Extra Lives and way way more! There is also double jump in this gamemode as well, which makes PvP a lot more fun & unique. You only need a minimum of 2 players to start any mode! Classic: Choose a kit, 5 lives for each player, fight it out and claim the #1 spot! Look out for lightning as they can spawn powerups to really give you an advantage in the game! Duels: Fight against another random player or one of your friends and see who is the best! Frenzy: Your kit is randomly selected for you, each life you will have a different kit. You can fight with up to 100 players in this mode and lets see who will be the best out of that 100! All the other stuff from Classic/Duels apply to this mode as well like powerups. We have 2 ranks on this server too, VIP and CAPTAIN which has a bunch of different perks for SCB and other things like Cosmetics and more.   SERVER IP: If this server has caught your interest in any way, please consider joining and you will NOT regret it! Bring some of your friends online for an even better experience and join in on the fun at: IP: minezone.club Hope to see you online!   SERVER TRAILER: https://www.youtube.com/watch?v=0phpMgu1mH0
    • The mod give new blocks  
    • I will a Mode for 1.21 in this Mod give new block, items and dimensions   
  • Topics

×
×
  • Create New...

Important Information

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