Jump to content

1.3 Client/Server Determination Utility-Code


jakj

Recommended Posts

RunningMode . IsServer ( )

Returns true if (and only if) the code is running in a dedicated server.

RunningMode . IsPrivate ( )

Returns true if (and only if) the code is running in a private singleplayer world.

RunningMode . IsLocalHost ( )

Returns true if (and only if) the code is running in a singleplayer world that has been opened to LAN.

RunningMode . IsClient ( )

Returns true if (and only if) the code is running in a client connected to a remote server (either dedicated or singleplayer-opened-to-LAN, which are equivalent).

RunningMode . IsClientSide ( <EntityPlayer> )

Returns true if (and only if) the EntityPlayer object provided is a client-side object.

RunningMode . IsServerSide ( <EntityPlayer> )

Returns true if (and only if) the EntityPlayer object provided is a server-side object.

IsClientSide and IsServerSide are useful for things like an item's use-handler (e.g., onItemUseFirst), so you can do different things depending on whether it's running in client or server code (because, on an integrated server, your onItemUseFirst is called twice, first on the client, and then (if you return <false> from the function) on the server).

RunningMode.java

package <ModPackage> ;

 

import cpw . mods . fml . common . SidedProxy ;

 

import net . minecraft . src . EntityPlayer ;

 

public class RunningMode

{

    @SidedProxy ( clientSide = "<ModPackage>.ClientRunningMode" , serverSide = "<ModPackage>.ServerRunningMode" )

    public static CommonRunningMode CommonRunningMode ;

 

    public static boolean IsServer ( )

    {

        return ( CommonRunningMode . IsServer ( ) ) ;

    }

 

    public static boolean IsPrivate ( )

    {

        return ( CommonRunningMode . IsPrivate ( ) ) ;

    }

 

    public static boolean IsLocalHost ( )

    {

        return ( CommonRunningMode . IsLocalHost ( ) ) ;

    }

 

    public static boolean IsClient ( )

    {

        return ( CommonRunningMode . IsClient ( ) ) ;

    }

 

    public static boolean IsClientSide ( EntityPlayer Player )

    {

        return ( CommonRunningMode . IsClientSide ( Player ) ) ;

    }

 

    public static boolean IsServerSide ( EntityPlayer Player )

    {

        return ( CommonRunningMode . IsServerSide ( Player ) ) ;

    }

}


 

CommonRunningMode.java


package <ModPackage> ;

 

import net . minecraft . src . EntityPlayer ;

 

public abstract class CommonRunningMode

{

    public boolean IsServer ( )

    {

        return ( false ) ;

    }

 

    public boolean IsPrivate ( )

    {

        return ( false ) ;

    }

 

    public boolean IsLocalHost ( )

    {

        return ( false ) ;

    }

 

    public boolean IsClient ( )

    {

        return ( false ) ;

    }

 

    public boolean IsClientSide ( EntityPlayer Player )

    {

        return ( false ) ;

    }

 

    public boolean IsServerSide ( EntityPlayer Player )

    {

        return ( false ) ;

    }

}


 

ServerRunningMode.java


package <ModPackage> ;

 

import net . minecraft . src . EntityPlayer ;

 

public class ServerRunningMode extends CommonRunningMode

{

    @Override

    public boolean IsServer ( )

    {

        return ( true ) ;

    }

 

    @Override

    public boolean IsServerSide ( EntityPlayer Player )

    {

        return ( true ) ;

    }

}


 

ClientRunningMode.java


package <ModPackage> ;

 

import cpw . mods . fml . client . FMLClientHandler ;

 

import net . minecraft . src . IntegratedServer ;

 

import net . minecraft . src . EntityPlayer ;

import net . minecraft . src . EntityPlayerSP ;

import net . minecraft . src . EntityPlayerMP ;

 

public class ClientRunningMode extends CommonRunningMode

{

    @Override

    public boolean IsPrivate ( )

    {

        if ( IsClient ( ) )

        {

            return ( false ) ;

        }

 

        return ( ! IntegratedServerIsPublic ( ) ) ;

    }

 

    @Override

    public boolean IsLocalHost ( )

    {

        if ( IsClient ( ) )

        {

            return ( false ) ;

        }

 

        return ( IntegratedServerIsPublic ( ) ) ;

    }

 

    @Override

    public boolean IsClient ( )

    {

        return ( ! FMLClientHandler . instance ( ) . getClient ( ) . isIntegratedServerRunning ( ) ) ;

    }

 

    private boolean IntegratedServerIsPublic ( )

    {

        return ( ( ( IntegratedServer ) FMLClientHandler . instance ( ) . getServer ( ) ) . func_71344_c ( ) ) ;

    }

 

    @Override

    public boolean IsClientSide ( EntityPlayer Player )

    {

        return ( Player instanceof EntityPlayerSP ) ;

    }

 

    @Override

    public boolean IsServerSide ( EntityPlayer Player )

    {

        return ( Player instanceof EntityPlayerMP ) ;

    }

}

Edited to nice static version not requiring an additional class-name reference.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...