Jump to content

ClientOnly message causes problem on server start.


Raycoms

Recommended Posts

I have the message:

 

@Nullable
    @Override
    public IMessage onMessage(@NotNull SaveScanMessage message, MessageContext ctx)
    {
        File file = new File(FMLClientHandler.instance().getClient().mcDataDir, message.storeLocation);
        StructureWrapper.createScanDirectory(FMLClientHandler.instance().getClient().theWorld);

        try (OutputStream outputstream = new FileOutputStream(file))
        {
            CompressedStreamTools.writeCompressed(message.nbttagcompound, outputstream);
        }
        catch (Exception e)
        {
            LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer, LanguageHandler.format("item.scepterSteel.scanFailure"));
            return null;
        }

        LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer,
                LanguageHandler.format("item.scepterSteel.scanSuccess", message.storeLocation));

        return null;
    }

 

Which will be sent from the server to the client.

 

I registered it.

 

        getNetwork().registerMessage(SaveScanMessage.class, SaveScanMessage.class, 51, Side.CLIENT);

 

It still crashes the server on loading because he has problems with:

 

Caused by: java.lang.ClassNotFoundException: net.minecraft.client.multiplayer.WorldClient

 

Also tried it with Minecraft.getMinecraft() no success.

 

Shouldn't it work because it will only be executed on the client?

 

 

Link to comment
Share on other sites

I have the message:

 

@Nullable
    @Override
    public IMessage onMessage(@NotNull SaveScanMessage message, MessageContext ctx)
    {
        File file = new File(FMLClientHandler.instance().getClient().mcDataDir, message.storeLocation);
        StructureWrapper.createScanDirectory(FMLClientHandler.instance().getClient().theWorld);

        try (OutputStream outputstream = new FileOutputStream(file))
        {
            CompressedStreamTools.writeCompressed(message.nbttagcompound, outputstream);
        }
        catch (Exception e)
        {
            LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer, LanguageHandler.format("item.scepterSteel.scanFailure"));
            return null;
        }

        LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer,
                LanguageHandler.format("item.scepterSteel.scanSuccess", message.storeLocation));

        return null;
    }

 

Which will be sent from the server to the client.

 

I registered it.

 

        getNetwork().registerMessage(SaveScanMessage.class, SaveScanMessage.class, 51, Side.CLIENT);

 

It still crashes the server on loading because he has problems with:

 

Caused by: java.lang.ClassNotFoundException: net.minecraft.client.multiplayer.WorldClient

 

Also tried it with Minecraft.getMinecraft() no success.

 

Shouldn't it work because it will only be executed on the client?

Is your IMessage and  IMessageHandler the same class?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Sure.

 

package com.minecolonies.network.messages;

import com.minecolonies.util.LanguageHandler;
import com.minecolonies.util.StructureWrapper;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
* Send a scan outputStream to the client.
*/
public class SaveScanMessage implements IMessage, IMessageHandler<SaveScanMessage, IMessage>
{
    private NBTTagCompound nbttagcompound;
    private String         storeLocation;

    /**
     * Public standard constructor.
     */
    public SaveScanMessage()
    {
        /*
         * Intentionally left empty.
         */
    }

    /**
     * Send a scan compound to the client.
     *
     * @param nbttagcompound the stream.
     * @param storeAt string describing where to store the scan.
     */
    public SaveScanMessage(NBTTagCompound nbttagcompound, String storeAt)
    {
        this.nbttagcompound = nbttagcompound;
        this.storeLocation = storeAt;
    }

    @Override
    public void fromBytes(@NotNull ByteBuf buf)
    {
        nbttagcompound = ByteBufUtils.readTag(buf);
        storeLocation = ByteBufUtils.readUTF8String(buf);
    }

    @Override
    public void toBytes(@NotNull ByteBuf buf)
    {
        ByteBufUtils.writeTag(buf, nbttagcompound);
        ByteBufUtils.writeUTF8String(buf, storeLocation);
    }

    @Nullable
    @Override
    public IMessage onMessage(@NotNull SaveScanMessage message, MessageContext ctx)
    {
        File file = new File(FMLClientHandler.instance().getClient().mcDataDir, message.storeLocation);
        StructureWrapper.createScanDirectory(FMLClientHandler.instance().getClient().theWorld);

        try (OutputStream outputstream = new FileOutputStream(file))
        {
            CompressedStreamTools.writeCompressed(message.nbttagcompound, outputstream);
        }
        catch (Exception e)
        {
            LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer, LanguageHandler.format("item.scepterSteel.scanFailure"));
            return null;
        }

        LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer,
                LanguageHandler.format("item.scepterSteel.scanSuccess", message.storeLocation));

        return null;
    }
}

Link to comment
Share on other sites

TL;DR don't do that. Use separate classes.

Your packet handler code is trying to load client-side-only objects and because it's the same class on the server, the server tries to load those objects in order to instantiate your packet.  Ergo, it crashes.

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

So one class only for the handler and one for the message?

One class for the handler and one more class for the message.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

        getNetwork().registerMessage(SaveScanMessageHandler.class, SaveScanMessage.class, 51, Side.CLIENT);

 

package com.minecolonies.network.messages;

import com.minecolonies.util.LanguageHandler;
import com.minecolonies.util.StructureWrapper;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraftforge.fml.client.FMLClientHandler;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

/**
* Handles sendScanMessages.
*/
public class SaveScanMessageHandler implements IMessageHandler<SaveScanMessage, IMessage>
{
    /**
     * Public standard constructor.
     */
    public SaveScanMessageHandler()
    {
        /*
         * Intentionally left empty.
         */
    }

    @Nullable
    @Override
    public IMessage onMessage(@NotNull SaveScanMessage message, MessageContext ctx)
    {
        File file = new File(FMLClientHandler.instance().getClient().mcDataDir, message.storeLocation);
        StructureWrapper.createScanDirectory(FMLClientHandler.instance().getClient().theWorld);

        try (OutputStream outputstream = new FileOutputStream(file))
        {
            CompressedStreamTools.writeCompressed(message.nbttagcompound, outputstream);
        }
        catch (Exception e)
        {
            LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer, LanguageHandler.format("item.scepterSteel.scanFailure"));
            return null;
        }

        LanguageHandler.sendPlayerLocalizedMessage(FMLClientHandler.instance().getClient().thePlayer,
                LanguageHandler.format("item.scepterSteel.scanSuccess", message.storeLocation));

        return null;
    }
}

 

 

package com.minecolonies.network.messages;

import io.netty.buffer.ByteBuf;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import org.jetbrains.annotations.NotNull;

/**
* Creates a message to save scans on the clients.
*/
public class SaveScanMessage implements IMessage
{
    public NBTTagCompound nbttagcompound;
    public String         storeLocation;

    /**
     * Send a scan compound to the client.
     *
     * @param nbttagcompound the stream.
     * @param storeAt string describing where to store the scan.
     */
    public SaveScanMessage(NBTTagCompound nbttagcompound, String storeAt)
    {
        this.nbttagcompound = nbttagcompound;
        this.storeLocation = storeAt;
    }

    @Override
    public void fromBytes(@NotNull ByteBuf buf)
    {
        nbttagcompound = ByteBufUtils.readTag(buf);
        storeLocation = ByteBufUtils.readUTF8String(buf);
    }

    @Override
    public void toBytes(@NotNull ByteBuf buf)
    {
        ByteBufUtils.writeTag(buf, nbttagcompound);
        ByteBufUtils.writeUTF8String(buf, storeLocation);
    }
}

 

Still, same problem.

Link to comment
Share on other sites

You need a 0-argument constructor for SaveScanMessage.

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

Still:

 

Caused by: java.lang.RuntimeException: Attempted to load class net/minecraft/client/multiplayer/WorldClient for invalid side SERVER
at net.minecraftforge.fml.common.asm.transformers.SideTransformer.transform(SideTransformer.java:56) ~[forgeSrc-1.10.2-12.18.1.2077-PROJECT(minecolonies).jar:?]
at net.minecraftforge.fml.common.asm.ASMTransformerWrapper$TransformerWrapper.transform(ASMTransformerWrapper.java:249) ~[forgeSrc-1.10.2-12.18.1.2077-PROJECT(minecolonies).jar:?]
at net.minecraft.launchwrapper.LaunchClassLoader.runTransformers(LaunchClassLoader.java:279) ~[launchwrapper-1.12.jar:?]
at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:176) ~[launchwrapper-1.12.jar:?]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[?:1.8.0_112]
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[?:1.8.0_112]
at java.lang.Class.getDeclaredConstructors0(Native Method) ~[?:1.8.0_112]
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671) ~[?:1.8.0_112]
at java.lang.Class.getConstructor0(Class.java:3075) ~[?:1.8.0_112]
at java.lang.Class.newInstance(Class.java:412) ~[?:1.8.0_112]
at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.instantiate(SimpleNetworkWrapper.java:166) ~[forgeSrc-1.10.2-12.18.1.2077-PROJECT(minecolonies).jar:?]
at net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper.registerMessage(SimpleNetworkWrapper.java:159) ~[forgeSrc-1.10.2-12.18.1.2077-PROJECT(minecolonies).jar:?]
at com.minecolonies.MineColonies.initializeNetwork(MineColonies.java:149) ~[minecolonies_main/:?]
at com.minecolonies.MineColonies.init(MineColonies.java:96) ~[minecolonies_main/:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_112]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_112]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_112]
at net.minecraftforge.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:597) ~[forgeSrc-1.10.2-12.18.1.2077-PROJECT(minecolonies).jar:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_112]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_112]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_112]
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]
at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]
at net.minecraftforge.fml.common.LoadController.sendEventToModContainer(LoadController.java:239) ~[LoadController.class:?]
at net.minecraftforge.fml.common.LoadController.propogateStateMessage(LoadController.java:217) ~[LoadController.class:?]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_112]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_112]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_112]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_112]
at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74) ~[guava-17.0.jar:?]
at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47) ~[guava-17.0.jar:?]
at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322) ~[guava-17.0.jar:?]
at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304) ~[guava-17.0.jar:?]
at com.google.common.eventbus.EventBus.post(EventBus.java:275) ~[guava-17.0.jar:?]
at net.minecraftforge.fml.common.LoadController.distributeStateMessage(LoadController.java:142) ~[LoadController.class:?]
at net.minecraftforge.fml.common.Loader.initializeMods(Loader.java:788) ~[Loader.class:?]
... 5 more

Link to comment
Share on other sites

Register your network handler in your client proxy.

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

The server doesn't need to know it?

 

Having had to fight with COG to get the server running in Dev, I was wrong.

 

Yes, the server needs to know about the packet handler, because you need to register the networking stuff on both sides.

 

Which means...

 

You guessed it...

 

The packet handler cannot contain client-side-only code.  I just had to move a bunch of stuff to my client proxy.

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

You can call a method in your proxy class, yes.  But it must be done through MainMod.proxy.whatever() not ClientProxy.whatever().

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

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.



×
×
  • Create New...

Important Information

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