Jump to content

Recommended Posts

Posted

I don't understand where I should register my messages. I want to send one message to server and then send another back to client in onMessage(). It modifies player player's ExtendedProperties(it saves on server, but in client it being registered every connect and don't sync with server), but my hanlder on client side doesn't work. On server side all works.

 

Request message:

public class CustomPlayerAttrsRequestMessage implements IMessage {

    public CustomPlayerAttrsRequestMessage() {}

 

    @Override

    public void fromBytes(ByteBuf buf) {

    }

 

    @Override

    public void toBytes(ByteBuf buf) {

    }

}

 

 

 

Here's server handler:

 

public class CustomPlayerAttrsRequestHandler implements IMessageHandler<CustomPlayerAttrsRequestMessage, IMessage> {

    @Override

    public IMessage onMessage(CustomPlayerAttrsRequestMessage message, MessageContext ctx) {

        TESItems.networkWrapper.sendTo(new CustomPlayerAttrsMessage(ctx.getServerHandler().playerEntity), ctx.getServerHandler().playerEntity);

        System.out.println("Sending attrs packet to + " + ctx.getServerHandler().playerEntity.getDisplayName());

        return null;

    }

}

 

 

 

data message:

 

public class CustomPlayerAttrsMessage implements IMessage {

    public float strength, speed, luck, agility;

    public float blades, archery, blunt, mining;

    public NBTTagCompound tag = new NBTTagCompound();

 

    public CustomPlayerAttrsMessage(EntityPlayer player) {

        System.out.println("Getting attrs on player " + player.getDisplayName());

        CustomPlayer p = CustomPlayer.get(player);

        p.saveNBTData(tag);

    }

 

    @Override

    public void fromBytes(ByteBuf buf) {

        strength = buf.getFloat(0);

        speed = buf.getFloat(1);

        luck = buf.getFloat(2);

        agility = buf.getFloat(3);

        blades = buf.getFloat(4);

        archery = buf.getFloat(5);

        blunt = buf.getFloat(6);

        mining = buf.getFloat(7);

    }

 

    @Override

    public void toBytes(ByteBuf buf) {

        buf.setFloat(0, tag.getFloat("strength"));

        buf.setFloat(1, tag.getFloat("speed"));

        buf.setFloat(2, tag.getFloat("luck"));

        buf.setFloat(3, tag.getFloat("agility"));

        buf.setFloat(4, tag.getFloat("blades"));

        buf.setFloat(5, tag.getFloat("archery"));

        buf.setFloat(6, tag.getFloat("blunt"));

        buf.setFloat(7, tag.getFloat("mining"));

    }

}

 

 

 

Client handler(it don't write log, so it doesn't work)

public class CustomPlayerAttrsHandler implements IMessageHandler<CustomPlayerAttrsMessage, IMessage> {

    @Override

    public IMessage onMessage(CustomPlayerAttrsMessage msg, MessageContext ctx) {

        System.out.println("Got attrs package from server");

        CustomPlayer player = CustomPlayer.get(Minecraft.getMinecraft().thePlayer);

        player.strength = msg.strength;

        player.agility = msg.agility;

        player.speed = msg.speed;

        player.luck = msg.luck;

        player.blades = msg.blades;

        player.blunt = msg.blunt;

        player.archery = msg.archery;

        player.mining = msg.mining;

        return null;

    }

}

 

 

 

Registering:

@EventHandler

    public void Init(FMLInitializationEvent event) {

        networkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel("customPlayerChannel");

        networkWrapper.registerMessage(CustomPlayerOpenInventoryHandler.class, CustomPlayerOpenInventoryMessage.class, 0, Side.SERVER);

 

@EventHandler

    @SideOnly(Side.CLIENT)

    public void Load(FMLInitializationEvent event) {

        networkWrapper.registerMessage(CustomPlayerAttrsHandler.class, CustomPlayerAttrsMessage.class, 1, Side.CLIENT);

 

I tried to move client handler registration to Init(), but this throws exception on server launch due to missing EntityClientPlayerMP class.

Posted

 

@EventHandler

    public void PreInit(FMLPreInitializationEvent event) {

        networkWrapper = NetworkRegistry.INSTANCE.newSimpleChannel("customPlayerChannel");

        networkWrapper.registerMessage(CustomPlayerOpenInventoryHandler.class, CustomPlayerOpenInventoryMessage.class, 0, Side.SERVER);

        networkWrapper.registerMessage(CustomPlayerAttrsHandler.class, CustomPlayerAttrsMessage.class, 1, Side.CLIENT);

    }

 

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.