Jump to content

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


sirati97

Recommended Posts

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)

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.



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • ChemsXpress (https://chemsxpress.com): ChemsXpress is a platform specializing in the distribution of research chemicals and related products. It offers a wide range of substances for scientific and laboratory use, catering to researchers and professionals. BubatzLand (https://bubatzland.com): BubatzLand focuses on providing high-quality cannabis products. The site offers a variety of strains, edibles, and related accessories, aiming to serve the needs of cannabis enthusiasts and medicinal users. HalluShroom (https://hallushroom.com): HalluShroom specializes in the sale of psychedelic mushrooms and related products. It caters to individuals interested in exploring the benefits and experiences associated with psilocybin and other psychedelic substances. HighAsch (https://highasch.com): HighAsch offers a diverse range of cannabis products, including flowers, concentrates, and edibles. The site aims to provide premium-quality products to both recreational and medicinal users. KetaminHaus (https://ketaminhaus.com): KetaminHaus provides ketamine and related products for research and therapeutic use. The platform focuses on ensuring the availability of high-quality, safe, and effective ketamine for various applications. KokainLabor (https://kokainlabor.com): KokainLabor offers cocaine and other related substances. The site targets individuals seeking high-purity products for personal or research purposes, emphasizing quality and safety. MDPHPWelt (https://mdphpwelt.com): MDPHPWelt specializes in the distribution of MDPHP and other similar research chemicals. It aims to supply researchers with high-quality substances for scientific and experimental use. NembutaLabor (https://nembutalabor.com): NembutaLabor provides Nembutal (pentobarbital) and related products. The site focuses on supplying these substances for research and veterinary purposes, ensuring high standards of quality and safety. NembutalQuelle (https://nembutalquelle.com): NembutalQuelle is dedicated to offering Nembutal and similar barbiturates. It caters to researchers and professionals who require these substances for scientific, medical, or veterinary applications. JBSaves Ltda Brazil (https://jbsavesltdabrazil.com): JBSaves Ltda Brazil is involved in the food and agricultural industry, providing a variety of products and services related to food processing, distribution, and agricultural practices. Bello Alimentos Ltda (https://belloalimentosltda.com): Bello Alimentos Ltda offers a wide range of food products. The site focuses on high-quality, nutritious, and safe food items, catering to consumers and businesses in the food industry. São Salvador Alimentos (https://saosalvadoralimentoss.com): São Salvador Alimentos specializes in the production and distribution of food products. The site provides a variety of options aimed at meeting the dietary needs and preferences of diverse consumers. WietWereld (https://wietwereld.com): WietWereld is a platform dedicated to the cannabis community. It offers information, products, and resources related to cannabis cultivation, consumption, and culture. 14. BeuhParadise (https://beuhparadis.com): BeuhParadise offers a range of cannabis products, including flowers, edibles, and concentrates. The site aims to provide high-quality cannabis products to recreational and medicinal users, emphasizing purity and potency. 15. WietWereld (https://wietwereld.com): WietWereld is a comprehensive resource for cannabis enthusiasts. It provides products, cultivation guides, and community forums for those interested in cannabis culture and cultivation. GrasOase (https://grasoase.com): GrasOase specializes in cannabis products and accessories. The site offers a variety of strains, seeds, and related items for cannabis users, focusing on quality and customer satisfaction. https://chemsxpress.com/ https://bubatzland.com/ https://hallushroom.com/ https://highasch.com/ https://ketaminhaus.com/ https://kokainlabor.com/ https://mdphpwelt.com/ https://nembutalabor.com/ https://nembutalquelle.com/ https://jbsavesltdabrazil.com/ https://belloalimentosltda.com/ https://saosalvadoralimentoss.com/ https://wietwereld.com https://beuhparadis.com https://wietwereld.com https://grasoase.com/
    • For Example I have an Main Mod that adds new Content to Minecraft. But I'm using custom Classes etc. and I want it so that is an api integrated in the Main Mod but can be download seperately like an Module. Is this possible? If you need more Information just comment what you need to know ^^
    • Delete the config of this mod (config folder) and test it again   If there is no change, try other builds  
    • Add the full crash-report or latest.log (logs-folder) with sites like https://paste.ee/ and paste the link to it here
  • Topics

×
×
  • Create New...

Important Information

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