Jump to content

ServerMessageHandler Run on Client?


OuiOuiCroissant

Recommended Posts

Hello!

I followed SimpleImpl as closely as I could, but my ServerMessageHandler is being run on the client side!

 

It is printing out "Remote = true" after I add this to it:

System.out.println("Remote = " + Minecraft.getMinecraft().world.isRemote);

 

Here is everything:

 

Registering my ServerMessageHandler:

Spoiler

package com.ani.s;

import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.Mod.Instance;
import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side;

import com.ani.s.packets.ClientMessage;
import com.ani.s.packets.ClientMessageHandler;
import com.ani.s.packets.ServerMessage;
import com.ani.s.packets.ServerMessageHandler;
import com.ani.s.proxies.CommonProxy;

@Mod(modid = SiegeVariables.MODID, name = SiegeVariables.NAME, version = SiegeVariables.VERSION)
public class SiegeMod {

    @SidedProxy(serverSide = SiegeVariables.SERVER_SIDE, clientSide = SiegeVariables.CLIENT_SIDE)
    public static CommonProxy proxy;
    @Instance(SiegeVariables.MODID)
    public static SiegeMod instance;

    public static int handler_id;

    public static final SimpleNetworkWrapper MESSAGE_HANDLER_INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(SiegeVariables.MODID);

    public static final SiegeModTab CREATIVETAB = new SiegeModTab();

    @EventHandler
    public void preInit(FMLPreInitializationEvent event) {
        SiegeModEntityRegistry.init();
        SiegeModRecipes.init();
        proxy.registerModels();
        proxy.register();
        MESSAGE_HANDLER_INSTANCE.registerMessage(ClientMessageHandler.class, ClientMessage.class, handler_id++, Side.CLIENT);
        MESSAGE_HANDLER_INSTANCE.registerMessage(ServerMessageHandler.class, ServerMessage.class, handler_id++, Side.SERVER);
    }

    @EventHandler
    public void init(FMLInitializationEvent event) {
    }

    @EventHandler
    public void postInit(FMLPostInitializationEvent event) {
    }
}
 

My ServerMessage:

Spoiler

package com.ani.s.packets;

import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;

public class ServerMessage implements IMessage {
    public int peasant_id;
    public int stance;
    public ServerMessage() {
    }

    public ServerMessage(int p_peasant_id) {
        peasant_id = p_peasant_id;
    }

    public void toBytes(ByteBuf buf) {
        buf.writeInt(peasant_id);
        buf.writeInt(stance);
    }

    public void fromBytes(ByteBuf buf) {
        peasant_id = buf.readInt();
        stance = buf.readInt();
    }
}

And my ServerMessageHandler (ignore all the "!= null"s, I've had... issues):

Spoiler

package com.ani.s.packets;

import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
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 com.ani.s.entity.EntityCitizen;

public class ServerMessageHandler implements IMessageHandler<ServerMessage, IMessage> {

    public IMessage onMessage(ServerMessage message, MessageContext ctx) {
        if (message != null) {
            System.out.println("Remote = " + Minecraft.getMinecraft().world.isRemote);
            if (Minecraft.getMinecraft().world != null && Minecraft.getMinecraft().world.getEntityByID(message.peasant_id) != null) {
                Entity entity = Minecraft.getMinecraft().world.getEntityByID(message.peasant_id);
                if (entity != null && entity instanceof EntityCitizen) {
                    EntityCitizen citizen = (EntityCitizen) entity;
                    citizen.cycleStance(message.stance);
                }
            }
        }
        return null;
    }
}

Packet is sent from WorkerGUI Class (WorkerGUI is opened when interacting with EntityCitizen) in #mouseReleased()

Spoiler

package com.ani.s.inventory;

import net.minecraft.client.audio.SoundHandler;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.EntityList;
import net.minecraft.inventory.IInventory;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import com.ani.s.SiegeMod;
import com.ani.s.SiegeVariables;
import com.ani.s.entity.EntityCitizen;
import com.ani.s.packets.ServerMessage;

@SideOnly(Side.CLIENT)
public class WorkerGUI extends GuiContainer {

    public final ResourceLocation TEXTURE = new ResourceLocation(SiegeVariables.MODID + ":textures/gui/worker_gui.png");

    private EntityCitizen owner;
    private final int SIZE_X = 176;
    private final int SIZE_Y = 186;
    private int button_id;

    public WorkerGUI(final EntityCitizen ent, IInventory playerInventory) {
        super(new WorkerContainer(ent, ent.getWorkerInventory(), playerInventory));
        owner = ent;
    }

    @Override
    protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
        GlStateManager.color(1.0F, 1.0F, 1.0F);
        this.mc.getTextureManager().bindTexture(TEXTURE);
        this.drawModalRectWithCustomSizedTexture(this.guiLeft, this.guiTop, 0, 0, SIZE_X, SIZE_Y, 256, 256);
        int pos = this.guiLeft + 30;

        GuiButton butt = new GuiButton(++button_id, this.guiLeft + 10, this.guiTop + 18, 156, 20, owner.getStringFromStance(owner.client_message.stance)) {
            @Override
            public void mouseReleased(int mouseX, int mouseY) {
                if (mousePressed(mc, mouseX, mouseY)) {
                    ServerMessage server_message = new ServerMessage(owner.getEntityId());
                    server_message.stance = owner.stance + 1;
                    SiegeMod.MESSAGE_HANDLER_INSTANCE.sendToServer(server_message);

                }
            }

            @Override
            public void playPressSound(SoundHandler soundHandlerIn) {
            }
        };
        this.addButton(butt);

        drawCenteredString(fontRenderer, EntityList.getEntityString(owner), pos, this.guiTop + 5, 0xFFFFFF);

        drawCenteredString(fontRenderer, owner.client_message.king_name, pos + ((SIZE_X / 3)), this.guiTop + 5, 0xFFFFFF);

        drawCenteredString(fontRenderer, "H: " + owner.client_message.saturation, pos + ((SIZE_X / 3) * 2), this.guiTop + 5, 0xFFFFFF);
    }
}

Thanks!

Link to comment
Share on other sites

6 minutes ago, OuiOuiCroissant said:

Minecraft.getMinecraft().world.isRemote);

This will always be true. Minecraft, the class, does not exist on the server.

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

10 minutes ago, Draco18s said:

This will always be true. Minecraft, the class, does not exist on the server.

Ahhh thank you. What's the easiest way to call a specific entity using an entity ID on the server?

I'm using this. ctx is the MessageContext parameter in #onMessage().

ctx.getServerHandler().player.world.getEntityByID(message.peasant_id);

Edited by OuiOuiCroissant
Link to comment
Share on other sites

MessageContext has everything you need and want.

 

Additionally, you have ignored the GIANT WARNING on the documentation page about networking:

Quote

Warning

As of Minecraft 1.8 packets are by default handled on the network thread.

That means that your IMessageHandler can not interact with most game objects directly. Minecraft provides a convenient way to make your code execute on the main thread instead using IThreadListener.addScheduledTask.

The way to obtain an IThreadListener is using either the Minecraft instance (client side) or a WorldServer instance (server side). The code above shows an example of this by getting a WorldServer instance from an EntityPlayerMP.

 

  • Like 1

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi I have an issue with a server it solved deleting the ice and fire but my friends and me really want to use it does any of these mods are causing the issue due to incompatibility  Security craft Aquaculture Architectury Artifacts Astikorcarts Baubles BetterCaves yungs BetterStrongholds yungs BetterMineshafts yungs Bountiful Byg  Callablehorses Carry on Chococraft Citadel Clumps Collective Comforts Corailrecycler Craftable horse armor Comes alive Curios forge Decocraft Dungeonsarise  Open blocks elevator Expandability Farlanders Goblin traders Hwyla  Ice and fire Infernal expansion Jei Jump over fences Mantle  Macaws bridges Macaws doors Macaws fences Macaws furniture  Macaws fencesbyg Morpheus Mouse tweaks Mowzies mobs Obfuscate Paraglider Patchouli  Pick up notifier Random village names Spartan shields Small ships Sophisticated backpacks Spartan weaponry Storage drawers straw golem Structure gel Twilight forest  Tinkers construct Two players one horse  Valhesia core  Valhesia structures  Way stones  Vehicle mod  Wall jump Wings Wolves with armor Xptome  Xaeros minimap  Yungsapi  
    • { "type": "magic_overhaul:rune_inscribing", "base": { "item": "//base item" }, "template": { "item": "//template item" }, "output": { "count": 1, "item": "//output item" } } ^ Ok, where is your recipe JSON placed in your file structure? What's it called? And paste the actual JSON you're testing with, because it needs to be valid.
    • I'm trying to run Forge 1.12.2 on Minecraft  so I can use certain mods, but this error popped up. I've seen multiple errors but I just can't figure this one out. Unable to locate the Java runtime. Error details: 0x00000000 - system: The operation completed successfully. Filename on disk:  Path: . Exists: Directory
    • https://mclo.gs/b422RFc
    • Плагин и Приват
  • Topics

×
×
  • Create New...

Important Information

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