Jump to content

Recommended Posts

Posted

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!

Posted
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.

Posted (edited)
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
Posted

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.

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



×
×
  • Create New...

Important Information

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