Jump to content

[1.7.10] about @SideOnly(Side.SERVER)


kauan99

Recommended Posts

I have the following code (simplified to improve readability):

 

This is my implementation of IMessage.

 

public class ClientToServerMsg implements IMessage
{
    private String key;
    private int x;
    private int y;
    private int z;
    
    public ClientToServerMsg()
    { }

    public ClientToServerMsg(String key, byte firstIndex, byte lastIndex, int x, int y, int z)
    {
        this.key = key;
        this.x = x;
        this.y = y;
        this.z = z;
    }
    
    @Override
    public void fromBytes(ByteBuf buf)
    {
        this.key = ByteBufUtils.readUTF8String(buf);
        this.x = buf.readInt();
        this.y = buf.readInt();
        this.z = buf.readInt();
    }

    @Override
    public void toBytes(ByteBuf buf)
    {
        ByteBufUtils.writeUTF8String(buf, key);
        buf.writeInt(this.x);
        buf.writeInt(this.y);
        buf.writeInt(this.z);
    }

    public static class Handler implements IMessageHandler<ClientToServerMsg, ServerResponse>
    {
        @Override
        public ServerResponse onMessage(ClientToServerMsg message, MessageContext ctx) 
        {
        	if(ctx.side == Side.SERVER)//note here that this code only runs if side == SERVER
        	{
        		boolean needsReply = false;
        		
        		EntityPlayerMP player = ctx.getServerHandler().playerEntity;
        		TileEntity te = player.worldObj.getTileEntity(message.x, message.y, message.z);
        		if(te instanceof MyTileEntity)
        		{
        			MyTileEntity myTile = (MyTileEntity)te;
        			if(myTile.setKey(message.key))//this line throws the exception NoSuchMethodError
        				needsReply = true;
        		}
        		if(needsReply)
        		{
        			return new ServerResponse(message.key, message.x, message.y, message.z);
        		}
        	}
        	return null;
        }
    }
}

 

this is how I register the messages:

 

channel = NetworkRegistry.INSTANCE.newSimpleChannel("mymodchannel");
channel.registerMessage(ClientToServerMsg.Handler.class, ClientToServerMsg.class, 0, Side.SERVER);
channel.registerMessage(ServerResponse.Handler.class, ServerResponse.class, 1, Side.CLIENT);

 

and this is the TileEntity:

 

public class MyTileEntity extends TileEntity
{
@SideOnly(Side.SERVER)
public boolean setKey(String key)
{
	//method body
}
}

 

I annotated 

MyTileEntity#setKey

with

@SideOnly(Side.SERVER)

because I wanted this exact exception thrown in case I tried to call this method from the client side. Why doesn't it work?

 

If I change

setKey

to this:

 

public boolean setKey(String key)
{
if(!this.worldObj.isRemote)
{
	//then inside this method, the moment  I try to use a class marked with @SideOnly(Side.SERVER)
	//I get the error java.lang.NoClassDefFoundError
	//java.lang.RuntimeException: Attempted to load class MyServerOnlyClass for invalid side CLIENT
}
}

 

This leads me to believe @SideOnly(Side.SERVER) doesn't behave the way I expected. How does that annotation work? I can't mark stuff as server only at all?

WIP mods: easyautomation, easyenergy, easyelectronics, easymoney, easytrasportation, easysecurity, easymultiverse, easyfactions, easymagick, easyalchemy, easyseasons

Link to comment
Share on other sites

There is no point in using SideOnly(Server).  Don't use it

Somebody came up with a use case for it a few weeks ago. If I recall correctly, he wanted to produce a mod that would exclude some secret sauce from the client version jar file that he planned to distribute to WAN players (it referred to a DB engine installed on his server, something his players were unlikely to own).

 

He used SideOnly(Server) to test that such a stripped client would still run correctly on his own physical server and client. It was temporary but still helpful during development/test cycles because it saved him from having to manually add and delete packages each time around.

The debugger is a powerful and necessary tool in any IDE, so learn how to use it. You'll be able to tell us more and get better help here if you investigate your runtime problems in the debugger before posting.

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



×
×
  • Create New...

Important Information

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