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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • For crash 1, set max-tick-time to -1 in your server.properties Crash 2 shows a conflict or incompatibility between LuckPerms and the mod boh-0.0.6.1-forge-1.20.1_2.jar
    • Add the crash-report or latest.log (logs-folder) with sites like https://mclo.gs/ and paste the link to it here  
    • so my minecraft crashes when opening my world, i played without any troubles for about 5 days and today it started tweaking.. pls help me
    • Hi guys! I am having some issues with the server crashing over and over and I was hoping to get some guidance.  Thanks in advance! Crash 1: java.lang.Error: ServerHangWatchdog detected that a single server tick took 60.00 seconds (should be max 0.05)     at net.minecraft.server.dedicated.ServerWatchdog.run(ServerWatchdog.java:43) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:classloading}     at java.lang.Thread.run(Thread.java:840) ~[?:?] { Crash 2: java.lang.IllegalStateException: Capability missing for eeb7f026-34b4-42f5-9164-e7736461df83     at me.lucko.luckperms.forge.capabilities.UserCapabilityImpl.lambda$get$0(UserCapabilityImpl.java:66) ~[?:?] {re:classloading,re:classloading,re:classloading}     at net.minecraftforge.common.util.LazyOptional.orElseThrow(LazyOptional.java:261) ~[forge-1.20.1-47.3.10-universal.jar%23222!/:?] {re:mixin,re:classloading}     at me.lucko.luckperms.forge.capabilities.UserCapabilityImpl.get(UserCapabilityImpl.java:66) ~[?:?] {re:classloading,re:classloading,re:classloading}     at me.lucko.luckperms.forge.util.BrigadierInjector$InjectedPermissionRequirement.test(BrigadierInjector.java:143) ~[?:?] {}     at me.lucko.luckperms.forge.util.BrigadierInjector$InjectedPermissionRequirement.test(BrigadierInjector.java:129) ~[?:?] {}     at com.mojang.brigadier.tree.CommandNode.canUse(CommandNode.java:65) ~[brigadier-1.1.8.jar%2376!/:?] {}     at com.mojang.brigadier.CommandDispatcher.parseNodes(CommandDispatcher.java:359) ~[brigadier-1.1.8.jar%2376!/:?] {}     at com.mojang.brigadier.CommandDispatcher.parse(CommandDispatcher.java:349) ~[brigadier-1.1.8.jar%2376!/:?] {}     at com.mojang.brigadier.CommandDispatcher.parse(CommandDispatcher.java:317) ~[brigadier-1.1.8.jar%2376!/:?] {}     at net.minecraft.commands.Commands.m_230957_(Commands.java:237) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:classloading}     at net.mcreator.boh.procedures.TeleportbenProcedure.lambda$execute$2(TeleportbenProcedure.java:65) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading}     at net.mcreator.boh.BohMod.lambda$tick$2(BohMod.java:96) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading}     at java.util.ArrayList.forEach(ArrayList.java:1511) ~[?:?] {re:mixin}     at net.mcreator.boh.BohMod.tick(BohMod.java:96) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading}     at net.mcreator.boh.__BohMod_tick_ServerTickEvent.invoke(.dynamic) ~[boh-0.0.6.1-forge-1.20.1_2.jar%23165!/:?] {re:classloading,pl:eventbus:B}     at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:73) ~[eventbus-6.0.5.jar%2352!/:?] {}     at net.minecraftforge.eventbus.EventBus.post(EventBus.java:315) ~[eventbus-6.0.5.jar%2352!/:?] {}     at net.minecraftforge.eventbus.EventBus.post(EventBus.java:296) ~[eventbus-6.0.5.jar%2352!/:?] {}     at net.minecraftforge.event.ForgeEventFactory.onPostServerTick(ForgeEventFactory.java:950) ~[forge-1.20.1-47.3.10-universal.jar%23222!/:?] {re:classloading}     at net.minecraft.server.MinecraftServer.m_5705_(MinecraftServer.java:835) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,pl:mixin:A}     at net.minecraft.server.MinecraftServer.m_130011_(MinecraftServer.java:661) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,pl:mixin:A}     at net.minecraft.server.MinecraftServer.m_206580_(MinecraftServer.java:251) ~[server-1.20.1-20230612.114412-srg.jar%23217!/:?] {re:mixin,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,re:classloading,pl:accesstransformer:B,xf:fml:xaerominimap:xaero_minecraftserver,pl:mixin:A}     at java.lang.Thread.run(Thread.java:840) ~[?:?] {}
    • 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!
  • Topics

×
×
  • Create New...

Important Information

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