Jump to content

[1.8.9/Solved] PluginChannel is only working on first login.


Recommended Posts

Posted

Hey guys,

I am sirati97 and i am still a forge noob. (I am more that type of guy that forks NMS or Spigot xD)

So anyway i am trying to communicate with my minecraft server over two plugin channels. I got them registered and receiving works fine. But somehow there are two things that doesnt work:

On the first channel i am just sending back the packet when i receive it. Its just a check if the client has my mod installed.

That's my code:

    public static class Handler implements IMessageHandler<InitMessage, IMessage> {
        private SimpleNetworkWrapper network;

        public Handler(SimpleNetworkWrapper network) {
            this.network = network;
        }

        @Override
        public IMessage onMessage(final InitMessage message, final MessageContext ctx) {
            System.out.println("received init");
            return message;
        }
    }

It always receives the packet but it the answer arrives the server in only 1 of 10 tries. Because i dont make anything different i think it has something to do with multi-threading inside the client. Maybe the clients only allows you to send packets pack at the server, if he completely logged in. But that should be the case as the server sends the init packet as soon as the player logged in. Anyway i was annoyed of this and just tried it like this to be sure that the server receives the packet.

So i made this rather stupid code:

    public static class Handler implements IMessageHandler<InitMessage, IMessage> {
        private SimpleNetworkWrapper network;

        public Handler(SimpleNetworkWrapper network) {
            this.network = network;
        }

        @Override
        public IMessage onMessage(final InitMessage message, final MessageContext ctx) {
            System.out.println("received init");
            network.sendToServer(new InitMessage());
            Thread t = new Thread() {
                @Override
                public void run() {
                    for (int i=0;i<20;i++) {
                        try {
                            Thread.sleep(150);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        network.sendToServer(new InitMessage());
                    }
                }
            };
            t.start();
            return message;
        }
    }

Now the server gets spammed with init packets. And it works. But only on the first log in. When you leave the server and log in again it doesnt work anymore. There is no error, but my server doesnt receive any pluginchannelpackets. My first guess was that i need to register the packet on every login so i did that but it fails, because it is already registered. So i searched for an unregister method but as there is no method like this i assume that you just need to register it once. SO my registering code looks like this:

 

    @EventHandler
    public void init(FMLInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(this);
        SimpleNetworkWrapper network = NetworkRegistry.INSTANCE.newSimpleChannel("{name removed}|INIT");
        InitMessage.Handler handler = new InitMessage.Handler(network);
        network.registerMessage(handler, InitMessage.class, 0, Side.CLIENT);
    }

 

Anyway i know that it is not the servers fault because i monitor every incoming packet.

 

Thanks in advance, sirati97

You can contact me here: [email protected]

(this is not an email)

Posted

Its just a check if the client has my mod installed.

 

There are other, better ways of doing this.  For one, if your mod is required client and server (which 90% of most mods will be) then Forge does this automatically for you and disallows the connection if the mod is missing.

 

If its only optional, then you can get access to the list of mods the client connection has, because see above.  Normally the mod list can't be considered reliable (as someone can make a mod that doesn't report itself to the server) but as you're not doing something asinine like that, then your mod will show up in said list.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

My server doesnt require a custom client. The client is completely optional and i dont want to rely on forge. I only use forge as a base for the client mod because i want to be compertible with other mods.

 

Its just a check if the client has my mod installed.

After that i am going to communicate over another pluginchannel. When the clients send pack the packet, the server is going to open a connection to the client that is tunnelt through the pluginchannel. So i need functioning plugin channels.

You can contact me here: [email protected]

(this is not an email)

Posted

Pretty sure it is because the message you register

network.registerMessage(handler, InitMessage.class, 0, Side.CLIENT);

can only send messages from the server to the client.

 

You either need to make it a bidirectional packet, which I think you just register twice, once with side.client and once with side.server, or make a second message that you register with side.server and use that one to send information from the client to the server.

Current Project: Armerger 

Planned mods: Light Drafter  | Ore Swords

Looking for help getting a mod off the ground? Coding  | Textures

Posted

But that would not explain why they work at the first login on a server with the client.

Anyway i am no longer using the SimpleNetworkWrapper. I implemented my own PluginChannel handler. Its code is based on the SimpleNetworkWrapper, but it doesnt converts the FMLProxyPacket to an IMessage, because i dont need it anyway. I just need the byte[] payload. My Channel works fine, but it still only works on the first login. Maybe this is actually a forge bug?! If you are interested here is the code:

package de.sirati97.oilmod.forge;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import net.minecraft.network.INetHandler;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.fml.common.network.FMLEmbeddedChannel;
import net.minecraftforge.fml.common.network.FMLOutboundHandler;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import net.minecraftforge.fml.relauncher.Side;

import java.util.EnumMap;

/**
* Created by sirati97 on 16.03.2016.
*/
public abstract class PluginChannel {
    private String name;
    private EnumMap<Side, FMLEmbeddedChannel> channels;
    private PluginChannelInboundHandler handler = new PluginChannelInboundHandler(this);

    public PluginChannel(String name) {
        this.name = name;
        channels = NetworkRegistry.INSTANCE.newChannel(name, handler);
    }

    public void sendToServer(byte[] out) {
        FMLProxyPacket message = encode(out);
        channels.get(Side.CLIENT).attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.TOSERVER);
        channels.get(Side.CLIENT).writeAndFlush(message).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
    }

    public String getName() {
        return name;
    }

    public abstract byte[] onMessage(byte[] in, INetHandler netHandler);

    public FMLProxyPacket encode(byte[] out) {
        PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
        buffer.writeBytes(out);
        FMLProxyPacket proxy = new FMLProxyPacket(buffer, getName());
        return proxy;
    }

    public byte[] decode(FMLProxyPacket msg) {
        ByteBuf buf = msg.payload().copy();
        byte[] stream = new byte[buf.array().length-buf.arrayOffset()];
        buf.readBytes(stream);
        return stream;
    }
}

 

and the imbound handler:

package de.sirati97.oilmod.forge;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import net.minecraft.network.INetHandler;
import net.minecraftforge.fml.common.FMLLog;
import net.minecraftforge.fml.common.network.FMLOutboundHandler;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
import org.apache.logging.log4j.Level;

/**
* Created by sirati97 on 16.03.2016.
*/

@ChannelHandler.Sharable
public class PluginChannelInboundHandler extends SimpleChannelInboundHandler<FMLProxyPacket> {
    private final PluginChannel pluginChannel;

    public PluginChannelInboundHandler(PluginChannel pluginChannel) {
        this.pluginChannel = pluginChannel;
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FMLProxyPacket msg) throws Exception {
        INetHandler iNetHandler = ctx.channel().attr(NetworkRegistry.NET_HANDLER).get();
        byte[] out = pluginChannel.onMessage(pluginChannel.decode(msg), iNetHandler);
        if (out != null) {
            FMLProxyPacket result = pluginChannel.encode(out);
            ctx.channel().attr(FMLOutboundHandler.FML_MESSAGETARGET).set(FMLOutboundHandler.OutboundTarget.REPLY);
            ctx.writeAndFlush(result).addListener(ChannelFutureListener.FIRE_EXCEPTION_ON_FAILURE);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        FMLLog.log(Level.ERROR, cause, "PluginChannelInboundHandler exception");
        super.exceptionCaught(ctx, cause);
    }


}

 

 

Pretty sure it is because the message you register

network.registerMessage(handler, InitMessage.class, 0, Side.CLIENT);

can only send messages from the server to the client.

 

You either need to make it a bidirectional packet, which I think you just register twice, once with side.client and once with side.server, or make a second message that you register with side.server and use that one to send information from the client to the server.

That should not be an issue because NetworkRegistry.INSTANCE.newChannel(String name, ChannelHandler... handlers) puts them in there map on both sides:

    public EnumMap<Side,FMLEmbeddedChannel> newChannel(String name, ChannelHandler... handlers)
    {
        if (channels.get(Side.CLIENT).containsKey(name) || channels.get(Side.SERVER).containsKey(name) || name.startsWith("MC|") || name.startsWith("\u0001") || name.startsWith("FML"))
        {
            throw new RuntimeException("That channel is already registered");
        }
        EnumMap<Side,FMLEmbeddedChannel> result = Maps.newEnumMap(Side.class);

        for (Side side : Side.values())
        {
            FMLEmbeddedChannel channel = new FMLEmbeddedChannel(name, side, handlers);
            channels.get(side).put(name,channel);
            result.put(side, channel);
        }
        return result;
    }

You can contact me here: [email protected]

(this is not an email)

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

    • Without Network protocol fix mod, I get kicked with a Network Protocol error when on LAN. Also, both of these issues are caused by a Null Pointer Exception/Screen cannot be null in a "Client Bound Player Combat Kill Packet".
    • You need a new "items" folder at  resources/assets/yourmodid/ there you add for every item model a .json file with the exact item/block name and fill it like this if it's an item: { "model": { "type": "minecraft:model", "model": "yourmodid:item/youritem" } } and so if its a block: { "model": { "type": "minecraft:model", "model": "yourmodid:block/youritem" } } There is also a generator for it you can do namy crazy things with it which replaces the previous hard coded Item Properties implementaion method (Bow pulling animation for example). https://misode.github.io/assets/item/
    • Hello! I'm playing a modpack (custom made) with some friends, and we have the server running on BisectHosting. We encountered a bug with an entity from The Box Of Horrors mod, that would crash the game whenever someone nearby it would log in. We tried to fix it by: 1) Editing the player.dat files to change the location of the affected players (something I had done successfully previously) 2) Updating the version of the mod we had (0.0.8.2) to the latest alpha version (0.0.8.3 However, after doing both of those, none of us are able to join the server, and we get the following errors: Server side: https://pastebin.com/J5sc3VQN Client side: Internal Server Error (that's basically all I've gotten) Please help! I've tried restoring the player data to how it was before I made the changes (Bisect allows you to restore deleted files) and deleting all of my player data files and I still get the same error. Deleting Box Of Horrors causes the error: Failed to load datapacks, cannot continue with server load.
    • Get $100 Off temu Coupon Code { ald123454 } | for new users + 30% Discount for all users You can get a $100 Off temu Coupon code using the code { ald123454 }. This temu $100 Off code is specifically for new customers and can be redeemed to receive a $100 discount on your purchase. Our exclusive temu Coupon code offers a flat $100 Off your purchase, plus an additional 30% discount. As a new temu customer, you can slash prices by up to 30% as a new temu customer using code { ald123454 }. Existing users can enjoy $100 Off their next haul with this code. But that’s not all! With our temu coupon code get up to 90% discount on select items and clearance sales. Whether you’re a new customer or an existing shopper, our temu codes provide extra discounts tailored just for you. Save up to 30% with these current temu Coupons { ald123454 } for February 2025. The latest temu coupon codes at here. Free temu codes $100 Off — { ald123454 } temu Coupon $100 Off — { ald123454 } temu Coupon $100 Off — { ald123454 } temu Memorial Day Sale 75% off — { ald123454 } temu Coupon code today — { ald123454 } temu free gift code — { ald123454 } Without inviting friends or family member  Coupon code for Canada - $100 Off— { ald123454 } temu Coupon code Australia - $100 Off— { ald123454 } temu Coupon code New Zealand - $100 Off — { ald123454 } temu Coupon code Japan -$100 Off — { ald123454 } temu Coupon code Mexico - $100 Off — { ald123454 } temu Coupon code Chile - $100 Off — { ald123454 } temu Coupon code Peru - $100 Off — { ald123454 } temu Coupon code Colombia - $100 Off — { ald123454 } temu Coupon code Malaysia - $100 Off — { ald123454 } temu Coupon code the Philippines - $100 Off — { ald123454 } temu Coupon code South Korea - $100 Off — { ald123454 } Redeem Free temu Coupon Code { ald123454 }for first time user Get $100 discount on your temu order with the promo code "acr804084". You can get a discount by clicking on the item to purchase and entering this temu Coupon code $100 Off "{ ald123454 }". temu Coupon Code { ald123454 }: Get Up To $100 Off In June 2025 Are you looking for the best temu Coupon codes to get amazing discounts? Our temu Coupons are perfect for getting those extra savings you crave. We regularly test our coupon codes for temu to ensure they work flawlessly, giving you a guaranteed discount every time. temu New User Coupon { ald123454 }: Up To 75% OFF For First-Time Users Our temu first-time user coupon codes are designed just for new customers, offering the biggest discounts and the best deals currently available on temu. To maximize your savings, download the temu Coupon For $100 Off { ald123454 }: Get A Flat $100 Discount On Order Value Get ready to save big with our incredible temu Coupon code for $100 Off! Our amazing temu $100 Off coupon code will give you a flat $100 discount on your order value, making your shopping experience even more rewarding. temu Coupon Code For $100 Off { ald123454 } For Both New And Existing Customers Our incredible temu Coupon code for $100 Off is here to help you save big on your purchases. Whether you’re a new user or an existing customer, our $100 Off code for temu will give you an additional discount! temu Coupon Bundle { ald123454 }: Flat $100 Off + Up To 30% Discount Get ready for an unbelievable deal with our temu Coupon bundle for 2025! Our temu Coupon bundles will give you a flat $100 discount and an additional $100 Off on top of it. Free temu Coupons { ald123454 }: Unlock Unlimited Savings! Get ready to unlock a world of savings with our free temu Coupons! We’ve got you covered with a wide range of temu Coupon code options that will help you maximize your shopping experience. $100 Off temu Coupons, Promo Codes + 25% Cash Back { ald123454 } Redeem temu Coupon Code { ald123454 }
    • Get $100 Off temu Coupon Code { ald123454 } | for new users + 30% Discount for all users You can get a $100 Off temu Coupon code using the code { ald123454 }. This temu $100 Off code is specifically for new customers and can be redeemed to receive a $100 discount on your purchase. Our exclusive temu Coupon code offers a flat $100 Off your purchase, plus an additional 30% discount. As a new temu customer, you can slash prices by up to 30% as a new temu customer using code { ald123454 }. Existing users can enjoy $100 Off their next haul with this code. But that’s not all! With our temu coupon code get up to 90% discount on select items and clearance sales. Whether you’re a new customer or an existing shopper, our temu codes provide extra discounts tailored just for you. Save up to 30% with these current temu Coupons { ald123454 } for February 2025. The latest temu coupon codes at here. Free temu codes $100 Off — { ald123454 } temu Coupon $100 Off — { ald123454 } temu Coupon $100 Off — { ald123454 } temu Memorial Day Sale 75% off — { ald123454 } temu Coupon code today — { ald123454 } temu free gift code — { ald123454 } Without inviting friends or family member  Coupon code for Canada - $100 Off— { ald123454 } temu Coupon code Australia - $100 Off— { ald123454 } temu Coupon code New Zealand - $100 Off — { ald123454 } temu Coupon code Japan -$100 Off — { ald123454 } temu Coupon code Mexico - $100 Off — { ald123454 } temu Coupon code Chile - $100 Off — { ald123454 } temu Coupon code Peru - $100 Off — { ald123454 } temu Coupon code Colombia - $100 Off — { ald123454 } temu Coupon code Malaysia - $100 Off — { ald123454 } temu Coupon code the Philippines - $100 Off — { ald123454 } temu Coupon code South Korea - $100 Off — { ald123454 } Redeem Free temu Coupon Code { ald123454 }for first time user Get $100 discount on your temu order with the promo code "acr804084". You can get a discount by clicking on the item to purchase and entering this temu Coupon code $100 Off "{ ald123454 }". temu Coupon Code { ald123454 }: Get Up To $100 Off In June 2025 Are you looking for the best temu Coupon codes to get amazing discounts? Our temu Coupons are perfect for getting those extra savings you crave. We regularly test our coupon codes for temu to ensure they work flawlessly, giving you a guaranteed discount every time. temu New User Coupon { ald123454 }: Up To 75% OFF For First-Time Users Our temu first-time user coupon codes are designed just for new customers, offering the biggest discounts and the best deals currently available on temu. To maximize your savings, download the temu Coupon For $100 Off { ald123454 }: Get A Flat $100 Discount On Order Value Get ready to save big with our incredible temu Coupon code for $100 Off! Our amazing temu $100 Off coupon code will give you a flat $100 discount on your order value, making your shopping experience even more rewarding. temu Coupon Code For $100 Off { ald123454 } For Both New And Existing Customers Our incredible temu Coupon code for $100 Off is here to help you save big on your purchases. Whether you’re a new user or an existing customer, our $100 Off code for temu will give you an additional discount! temu Coupon Bundle { ald123454 }: Flat $100 Off + Up To 30% Discount Get ready for an unbelievable deal with our temu Coupon bundle for 2025! Our temu Coupon bundles will give you a flat $100 discount and an additional $100 Off on top of it. Free temu Coupons { ald123454 }: Unlock Unlimited Savings! Get ready to unlock a world of savings with our free temu Coupons! We’ve got you covered with a wide range of temu Coupon code options that will help you maximize your shopping experience. $100 Off temu Coupons, Promo Codes + 25% Cash Back { ald123454 } Redeem temu Coupon Code { ald123454 }
  • Topics

×
×
  • Create New...

Important Information

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