Defining Command Handlers

From Multiverse

Jump to: navigation, search


Contents

Overview

Command handlers are Python functions defined on the server that the user invokes by entering a command preceded by the slash character "/". For this reasons, they are sometimes referred to as "slash commands". Although the command is handled on the client, it is defined in server code.

Contrast these to client commands, which are defined purely in client scripts.

Define a slash command in mv-home/config/world/extensions_proxy.py by:

  1. Defining a class for the command with a method that performs the actions of the command.
  2. Registering the command with proxyPlugin.registerCommand().

Define command class

A client command class must implement the Java interface multiverse.server.plugins.ProxyPlugin.CommandParser, and must have a parse(self, cmdEvent) method that does whatever you want the command to do.

For example, the following command class defines command handler for the "/who" command that displays the number of players currently logged in to the server:

class WhoCommand (ProxyPlugin.CommandParser):
   def parse(self, cmdEvent):
       global proxyPlugin;
       playerOid = cmdEvent.getObjectOid()
       playerNames = proxyPlugin.getPlayerNames()
       Log.debug("/who: oid=" + str(playerOid) + ": returning "+str(playerNames.size())+" players")
       response = "players\n------------\n"
       for name in playerNames:
           response = response + name + "\n"
       response = response + str(playerNames.size()) + " players\n"
       WorldManagerClient.sendObjChatMsg(playerOid, 0, response)
       return None

Looping animation command

If you have an animation such as a dance animation that you want to loop, that is, restart once it finishes, do the following:

  • Use ClientAnimations.py that has variables and methods for dancing. See Client Animation System. To loop other kinds of animations, follow this example.
  • Add a command such as the following /dance command to your world's extensions_proxy.py file (see multiverse/config/sampleworld/extensions_proxy.py for example). This will toggle the dancestate instead of just playing a single animation. ClientAnimations.py will handle the rest.
class DanceCommand (ProxyPlugin.CommandParser):
    def parse(self, cmdEvent):
        playerOid = cmdEvent.getObjectOid()
        Log.debug("/dance: oid=" + str(playerOid))
        WorldManagerClient.setObjectProperty(playerOid, 
                                             "dancestate", 
                                             Boolean(not WorldManagerClient.getObjectProperty(playerOid,
                                             "dancestate") ) )
...
proxyPlugin.registerCommand("/dance", DanceCommand())

Register command

After defining the command class, register it by calling proxyPlugin.registerCommand(), with the command and the class as parameters: For example:

proxyPlugin.registerCommand("/who", WhoCommand())

For more information, see registerCommand().

Calling a command handler from Client code

To call a server-side command handler from Client code, use the following:

ClientAPI.Network.SendCommMessage("/" + command_string)

where OID is the relevant object ID and command_string is the slash command command string.

Built-in command handlers

The multiverse/config/common/proxy.py script defines a set of "built-in" command handlers (slash commands), as described in the following table.

Slash Command Description Definition
/ability abilityName Add the specified ability to the PC. AbilityCommand
/addParticle attachName effectName [velocity] [size] Attach the specified particle effect to the specified attachment point. AddParticleEffectCommand
/conclude Conclude the specified quest. ConcludeCommand
/createitem TemplateName Creates an item based on a template name and puts it in your inventory. CreateItemCommand
/dirlight x y z Changes the direction of the light for all logged in players. The floating-point arguments (x, y, and z) is a vector that is the direction the light is pointing. Uses a grey color. DirLightCommand
/debugoid Oid Need description DebugOidCommand
/dumpstacks Displays all thread stacks. DumpStacksCommand
/find TemplateName Displays the OID of specified item in your inventory. FindCommand
/findweapon Displays the OID of item equipped in your primary weapon slot. FindWeaponCommand
/generate Need description GenerateCommand
/goto MarkerName Move PC to the specified named marker location. GotoCommand
/lootall Loots all items from a targeted corpse. LootCommand
/nomove Need description} NoMoveCommand
/parm paramName paramValue Assigns the value paramValue to the client parameter paramName. See Client Parameters. ParmCommand
/persist OID Persists specified object to the database. Also saves all associated subobjects. Use the client /info command to determine object IDs. PersistCommand
/playercount Displays the number of players logged in. PlayerCountCommand
/release Return player to starting state. Reanimates a dead PC. ReleaseCommand
/remove Oid Removes specified item from your inventory RemoveCommand
/resetquests Resets quest states for the player to their starting state. ResetQuestCommand
/setloc x y z Set player location to specified coordinates SetLocCommand
/setmesh meshname Sets the player's model to the specified mesh. Visible on next login. SetMeshCommand
/stuck Move avatar to marker named "stuck" or (0,0,0) if not defined. StuckCommand
/sys Generates a system chat message that goes to all players on the server. SysCommand
/test command Tests the specified command, with special case for "persistKey" command to persist data keyed to a word. TestCommand
/threadstats Display thread statistics for debugging. ThreadStatsCommand
/who Displays list of all users logged into same proxy server. WhoCommand

Get and Set Property Commands

Define these test commands to set and get properties of player subobjects. Add them to your server scripts in multiverse/config/common/proxy.py test how a property affects the combat system.

Use the commands from within the client as follows:

  • SetProperty: /setproperty NamespaceString PropertyName Value
  • GetProperty: /getproperty NamespaceString PropertyName

For a complete lsit of predefined namespace strings, see Multiverse Object Architecture (Predefined namespaces).

SetpropertyCommand

class SetpropertyCommand (ProxyPlugin.CommandParser):
    def parse(self, cmdEvent):
        cmd = cmdEvent.getCommand()
        playerOid = cmdEvent.getObjectOid()
        targetOid = cmdEvent.getTarget()     
        splitCmd = cmd.split(" ")
        namespaceString = str(splitCmd[1])
        namespace = Namespace.intern(namespaceString)
        property = str(splitCmd[2])
        value = int(splitCmd[3])
        Log.debug("SetpropertyCommand: property " + property + ", value " + str(value))
        EnginePlugin.setObjectProperty(playerOid, namespace, property, value);
        WorldManagerClient.sendObjChatMsg(playerOid, 1, "set property: for Namespace " + str(namespace + 
                   " and " + property + str(value))
        return None

GetpropertyCommand

 
class GetpropertyCommand (ProxyPlugin.CommandParser):
    def parse(self, cmdEvent):
        cmd = cmdEvent.getCommand()
        playerOid = cmdEvent.getObjectOid()
        targetOid = cmdEvent.getTarget()
        splitCmd = cmd.split(" ")
        namespaceString = str(splitCmd[1])
        namespace = Namespace.intern(namespaceString)
        property = str(splitCmd[2])
        value = WorldManagerClient.getObjectProperty(playerOid, property);
        Log.debug("GetpropertyCommand: property " + property + ", value " + str(value))
        WorldManagerClient.sendObjChatMsg(playerOid, 1, str(value))
        return None

Register commands

Be sure to register the commands with these lines:

proxyPlugin.registerCommand("/setproperty ", SetpropertyCommand ())
proxyPlugin.registerCommand("/getproperty ", GetpropertyCommand ())
Personal tools