|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectit.gotoandplay.smartfoxserver.extensions.ExtensionHelper
public class ExtensionHelper
The ExtensionHelper class provides the main access to the server side API in Java. It is recommended to always keep a reference to the class in your Extension.
A good practice is to create such reference in the init() method of your extension:
private ExtensionHelper helper;
public void init()
{
helper = ExtensionHelper.instance();
}
| Field Summary | |
|---|---|
static int |
MOD_MESSAGE_TO_ROOM
|
static int |
MOD_MESSAGE_TO_USER
|
static int |
MOD_MESSAGE_TO_ZONE
|
| Method Summary | |
|---|---|
void |
addBuddy(java.lang.String buddyName,
User user)
Add a buddy to a user buddy list Based on the BuddyList configuration this may immediately add the buddy to the user list or send a buddy request that the target user must respond to. |
void |
addModerator(java.lang.String zoneName,
java.lang.String modName,
java.lang.String modPass)
Add a new moderator to a Zone |
void |
banOfflineUser(java.lang.String userName)
Banishes an offline user by name |
void |
banUser(User user,
int delay,
java.lang.String farewellMessage,
int banType)
Disconnects and bans a user. |
User |
canLogin(java.lang.String nick,
java.lang.String pass,
java.nio.channels.SocketChannel ch,
java.lang.String zone)
Checks if a connected client can log in the requested Zone. |
User |
canLogin(java.lang.String nick,
java.lang.String pass,
java.nio.channels.SocketChannel ch,
java.lang.String zone,
boolean forceLogin)
Checks if a connected client can log in the requested Zone. |
void |
clearBuddyList(User user)
Deprecated. |
User |
createNPC(java.lang.String userName,
java.lang.String ipAddress,
int port,
java.lang.String zoneName)
Creates an NPC (Non-Player Character) or BOT User that can be used for automated tasks, game AI etc... |
Room |
createRoom(Zone z,
java.util.Map params,
User creator,
boolean sendUpdate,
boolean broadcastEvent)
Create a new dynamic room. |
Room |
createRoom(Zone z,
java.util.Map params,
User creator,
java.util.HashMap roomVars,
User varsOwner,
boolean setOwnership,
boolean sendUpdate,
boolean broadcastEvent)
Create a new dynamic room. |
static void |
destroy()
|
boolean |
destroyRoom(Zone zone,
int roomId)
Destroys a server room. |
void |
disconnectUser(User u)
Disconnects a client from the server. |
void |
dispatchPrivateMessage(java.lang.String msg,
Room r,
User sender,
User recipient)
Dispatch a private message. |
void |
dispatchPublicMessage(java.lang.String msg,
Room r,
User u)
Dispatch a public message. |
java.lang.String |
getGuestName()
Return a guest name in the format of "guest_nn" where nn is an autoincrement long value |
java.lang.String |
getSecretKey(java.nio.channels.SocketChannel channel)
Return the unique secret key for this socket channel For further info about this method and its usage check the "Secure Login Tutorial" found at chapter 8.9 of the documentation. |
User |
getUserByChannel(java.nio.channels.SocketChannel sc)
Return a User object from its socket channel |
User |
getUserById(int id)
Return a User object from its numeric id |
Zone |
getZone(java.lang.String zoneName)
Return a Zone object from its name |
static ExtensionHelper |
instance()
Get an instance of the ExtensionHelper class |
boolean |
joinRoom(User usr,
int currRoom,
int newRoom,
boolean leaveRoom,
java.lang.String pword,
boolean isSpectator,
boolean broadcast)
Join a room from a server side extension |
void |
kickUser(User user,
int delay,
java.lang.String farewellMessage)
Kicks a user allowing to specify a delay before the task is executed and an admin message. |
void |
leaveRoom(User u,
int roomId,
boolean broadcastEvent)
Force a user to leave a room. |
void |
logoutUser(int id,
boolean fireClientEvt,
boolean fireInternalEvt)
Logout a user from the current Zone |
void |
logoutUser(User user,
boolean fireClientEvt,
boolean fireInternalEvt)
Logout a user from the current Zone |
void |
rebootServer()
Performs a soft reboot of the server, just like hitting the "Restart" button in the Admin interface. |
void |
removeBanishment(java.lang.String param,
int mode)
Removes a banned user from the ban-list, allowing him/her to be able to login again |
void |
removeBuddy(java.lang.String buddyName,
User user)
Removes a buddy from a user buddy list. |
void |
removeModerator(java.lang.String zoneName,
java.lang.String modName)
Removes a moderator user from a Zone |
void |
requestAddBuddyPermission(User sender,
java.lang.String targetUserName,
java.lang.String optionalMessage)
Requests permission for adding the target user in the sender buddy list. |
void |
sendBuddyListUpdate(User user)
Send an update about the specified User to all clients that have him/her in their buddy list. |
void |
sendCustomRoomList(java.util.List<Room> roomList,
User user)
Allows to send a custom room list to the client. |
void |
sendGenericMessage(java.lang.String message,
java.nio.channels.SocketChannel sender,
java.util.LinkedList channelList)
Send a generic text or XML message. |
void |
sendModeratorMessage(java.lang.String message,
User sender,
int type,
int id)
Send a moderator message to a single user, a room or an entire Zone. |
void |
sendRoomList(java.nio.channels.SocketChannel sc)
Send a room list in default format (XML) This will fire a onRoomListUpdate event on the client. |
void |
setBuddyBlockStatus(User user,
java.lang.String buddyName,
boolean isBlocked)
Set the isBlocked flag of a buddy in the User's buddy list. |
void |
setBuddyVariables(User user,
java.util.Map<java.lang.String,java.lang.String> variables)
Set the buddy variables of the specified user. |
void |
setRoomVariable(Room r,
User u,
java.lang.String vn,
java.lang.String vt,
java.lang.String vv,
boolean pr,
boolean ps,
boolean setOwnership,
boolean broadcastAll)
Deprecated. |
void |
setRoomVariables(Room r,
User u,
java.util.HashMap vars,
boolean setOwnership,
boolean broadcastAll)
Set one or more Room Variable The vars parameter requires a Map of RoomVariables where the key is the variable name and the value is the RoomVariable object. |
void |
setUserVariables(User u,
java.util.HashMap vars,
boolean broadcastAll)
Set one or more User Variables. |
void |
switchPlayer(User u,
int roomId,
boolean brodcastEvent)
Switch a player into a spectator, if possible (game rooms only) Fires an onPlayerSwitched event on the client side. |
void |
switchSpectator(User u,
int roomId,
boolean brodcastEvent)
Switch a spectator into a player, if possible (game rooms only) For more details about switching spectators to players you should check the SmartFoxTris game example. |
| Methods inherited from class java.lang.Object |
|---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int MOD_MESSAGE_TO_USER
public static final int MOD_MESSAGE_TO_ROOM
public static final int MOD_MESSAGE_TO_ZONE
| Method Detail |
|---|
public static ExtensionHelper instance()
public static void destroy()
public User canLogin(java.lang.String nick,
java.lang.String pass,
java.nio.channels.SocketChannel ch,
java.lang.String zone)
throws it.gotoandplay.smartfoxserver.exceptions.LoginException
nick - the nickname you want to use to log inpass - an optional passwordch - the SocketChannel of the connected clientzone - the zone name
it.gotoandplay.smartfoxserver.exceptions.LoginException - if there are problems with the login
public User canLogin(java.lang.String nick,
java.lang.String pass,
java.nio.channels.SocketChannel ch,
java.lang.String zone,
boolean forceLogin)
throws it.gotoandplay.smartfoxserver.exceptions.LoginException
nick - the nickname you want to use to log inpass - an optional passwordch - the SocketChannel of the connected clientzone - the zone nameforceLogin - attempts to drop a previous existing connection and logs in again
it.gotoandplay.smartfoxserver.exceptions.LoginException - if there are problems with the login
public void dispatchPublicMessage(java.lang.String msg,
Room r,
User u)
msg - the messager - the target roomu - the sender userInternalEventHandler
public void dispatchPrivateMessage(java.lang.String msg,
Room r,
User sender,
User recipient)
msg - the messager - the room of the sendersender - the sender userrecipient - the recipient userInternalEventHandlerpublic java.lang.String getGuestName()
public java.lang.String getSecretKey(java.nio.channels.SocketChannel channel)
channel - the user socket channel
public User getUserByChannel(java.nio.channels.SocketChannel sc)
sc - a valid socket channel
public User getUserById(int id)
id - a valid user id
public void logoutUser(int id,
boolean fireClientEvt,
boolean fireInternalEvt)
id - the user id
public void logoutUser(User user,
boolean fireClientEvt,
boolean fireInternalEvt)
user - the User object
public void sendGenericMessage(java.lang.String message,
java.nio.channels.SocketChannel sender,
java.util.LinkedList channelList)
message - string or xml messagesender - the sender channel (can be null)channelList - a list of recipient channels
public Room createRoom(Zone z,
java.util.Map params,
User creator,
boolean sendUpdate,
boolean broadcastEvent)
throws it.gotoandplay.smartfoxserver.exceptions.ExtensionHelperException
Create a new dynamic room.
The params parameter can contain the following keys:
| name | the room name | |
| pwd | the room password (optional) | |
| maxU | max number of users | |
| maxS | max number of spectators (optional for game rooms) | |
| isGame | a boolean flag. If true the room is a game room | |
| isLimbo | a boolean flag. If true the room is in "Limbo mode" | |
| uCount | a boolean flag. If true the room will receive the uCount update messages. | |
| xtName | The name of the extension that will be attached to the room | |
| xtClass | The name of the class (Java) or script (Actionscript)
to load as extension. The file extension should only be specified for Actionscript files (.as) |
For all the details about Rooms please refer also to the Application Architecture article found in Section A (white-papers) of the documentation
z - the Zone where the room will be createdparams - a Map of params: name, pwd, isGame etc...creator - the "owner" of the room. null = Server owned.
A Server owned Room will be ignored by the default room management system and will be never removed, unless you it manually from your code.sendUpdate - Update the clients about the new room created (true is recommended, otherwise clients will be out of synch with their Room Lists)broadcastEvent - Broadcast the event internally.
it.gotoandplay.smartfoxserver.exceptions.ExtensionHelperException
public Room createRoom(Zone z,
java.util.Map params,
User creator,
java.util.HashMap roomVars,
User varsOwner,
boolean setOwnership,
boolean sendUpdate,
boolean broadcastEvent)
throws it.gotoandplay.smartfoxserver.exceptions.ExtensionHelperException
Create a new dynamic room.
The params parameter can contain the following keys:
| name | the room name | |
| pwd | the room password (optional) | |
| maxU | max number of users | |
| maxS | max number of spectators (optional for game rooms) | |
| isGame | a boolean flag. If true the room is a game room | |
| isLimbo | a boolean flag. If true the room is in "Limbo mode" | |
| uCount | a boolean flag. If true the room will receive the uCount update messages. | |
| xtName | The name of the extension that will be attached to the room | |
| xtClass | The name of the class (Java) or script (Actionscript)
to load as extension. The file extension should only be specified for Actionscript files (.as) |
For all the details about Rooms please refer also to the Application Architecture article found in Section A (white-papers) of the documentation
z - the Zone where the room will be createdparams - a Map of params: name,pwd,maxU,maxS,isGamecreator - the "owner" of the room. null = Server ownedroomVars - an HashMap of variables to set on the newly created room (null if not needed)varsOwner - the owner of the variablessetOwnership - true = all modified/created variables will be owned by the creator User, false = ownership is not changedsendUpdate - Update the clients about the new room created (true is recommended, otherwise clients will be out of synch with their Room Lists)broadcastEvent - Broadcast the event internally.
it.gotoandplay.smartfoxserver.exceptions.ExtensionHelperException
public boolean joinRoom(User usr,
int currRoom,
int newRoom,
boolean leaveRoom,
java.lang.String pword,
boolean isSpectator,
boolean broadcast)
throws it.gotoandplay.smartfoxserver.exceptions.ExtensionHelperException
usr - the User objectcurrRoom - the id of the current room where the user is innewRoom - the id of the room that the user wants to joinleaveRoom - if true the user will leave the current room once he successfully joined the new onepword - an optional password for the roomisSpectator - if true the user is joined as spectator (for game rooms only)broadcast - if true the onJoinRoom event is fired back to all clients (recommended)
it.gotoandplay.smartfoxserver.exceptions.ExtensionHelperException
public void sendModeratorMessage(java.lang.String message,
User sender,
int type,
int id)
message - the text messagesender - the sender usertype - the type of message (ExtensionHelper.MOD_MESSAGE_TO_ constants)id - the id of the recipient user or room.
In case of a Zone message it will be ignored and the message will be sent to the current Zone.public void sendRoomList(java.nio.channels.SocketChannel sc)
There is a specific "sequence" to keep in mind when the user is logging in:
You can see this in action by checking the PixelGame example found at chapter 8.10 of the docs.
The Java extension source can be found under the {path-to-your-sfs-folder}/Server/javaExtensions/ folder under the it.gotoandplay.extensions.examples package
sc - The SocketChannel that will receive the list
public void sendCustomRoomList(java.util.List<Room> roomList,
User user)
Allows to send a custom room list to the client. This allows filtering rooms and sending a partial view of the full list of rooms in one Zone.
IMPORTANT NOTE: please make sure to understand that the client will not know about certain Rooms if you filter them out. This could lead to unexpected client side behaviors if you attempt to join o
roomList - user -
public void setRoomVariable(Room r,
User u,
java.lang.String vn,
java.lang.String vt,
java.lang.String vv,
boolean pr,
boolean ps,
boolean setOwnership,
boolean broadcastAll)
r - the Roomu - the Uservn - the var namevt - the var typevv - the var valuepr - private flagps - persistent flagsetOwnership - setOwnership flag (will set the variable owner to the User who sent the request)broadcastAll - if true the onRoomVariablesUpdate() event will be fired (reccomended)
public void setRoomVariables(Room r,
User u,
java.util.HashMap vars,
boolean setOwnership,
boolean broadcastAll)
For all the details about Rooms and RoomVariables please refer also to the Application Architecture article found in Section A (white-papers) of the documentation
Also check Chapter 5.11 of the docs for a complete example of usage of Room Variables
r - the target room, where variables should be setu - the user, owner of the variablesvars - a java.util.HashMap of RoomVariablessetOwnership - setOwnership flag (will set the variable owner to the User who sent the request)broadcastAll - if true the onRoomVariablesUpdate() event will be fired (recommended)RoomVariable
public void setUserVariables(User u,
java.util.HashMap vars,
boolean broadcastAll)
Also check Chapter 5.6 of the docs for a complete example of the usage of User Variables
vars - a map of UserVariablesu - the User objectbroadcastAll - if true the onUserVariablesUpdate() event will be fired (reccomended)UserVariablepublic Zone getZone(java.lang.String zoneName)
zoneName - the name of the zone
public void leaveRoom(User u,
int roomId,
boolean broadcastEvent)
For more details about the multi-room usage refer to Chapter 5.10 of the docs.
u - the userroomId - the id of the room to leavebroadcastEvent - if true, the response event is fired (reccomended)
public void switchSpectator(User u,
int roomId,
boolean brodcastEvent)
u - the userroomId - the target room idbrodcastEvent - if true, the response event is fired (reccomended)
public void switchPlayer(User u,
int roomId,
boolean brodcastEvent)
u - the userroomId - the target room idbrodcastEvent - if true, the response event is fired (reccomended)public void disconnectUser(User u)
u - the user to kick
public boolean destroyRoom(Zone zone,
int roomId)
zone - the zoneroomId - the roomId
public void addBuddy(java.lang.String buddyName,
User user)
If mutualAddBuddy is activated both users will be added to their respective buddy lists.
You can learn more about the Buddy List system by checking chapters 5.12, 6.10, and 8.16 of the docs.
buddyName - the name of the buddy to adduser - the user whose buddy list should be updated
public void removeBuddy(java.lang.String buddyName,
User user)
You can learn more about the Buddy List system by checking chapters 5.12, 6.10, and 8.16 of the docs.
buddyName - the name of the buddy to removeuser - the user whose buddy list should be updated
public void requestAddBuddyPermission(User sender,
java.lang.String targetUserName,
java.lang.String optionalMessage)
Requests permission for adding the target user in the sender buddy list. This is already done by default if the mode of the buddy list is set to ADVANCED. In most of the cases you wouldn't need to call this method in your extension.
It could be used to implement an off-line permission request system, where the system stores the requests for off-line users in a database and sends the requests as soon as the target user logs in.
You can learn more about the Buddy List 2.0 system by checking chapters 6.10, 8.16 of the docs.
sender - the requestertargetUserName - the name of the target useroptionalMessage - an optional text message for the request. Use null if you don't need it.public void clearBuddyList(User user)
user - the user whose buddy list should be cleared.
public void setBuddyVariables(User user,
java.util.Map<java.lang.String,java.lang.String> variables)
Set the buddy variables of the specified user.
The variables Map contains all the variables to set, where the key is the name of the variable and the value represents the variable value.
Since version 1.6.0 you can set off-line buddy variables by using a '$' sign as the first character of
the variable name.
Example:
Mapvariables = new HashMap (); variables.put("$country", "Italy"); // an off-line buddy variable variables.put("invisible", "false"); // a regular buddy variable helper.setBuddyVariables( theUser, variables );
You can learn more about the Buddy List 2.0 system by checking chapters 6.10, 8.16 of the docs.
user - the Uservariables - a map containing the variables to setpublic void sendBuddyListUpdate(User user)
You can learn more about the Buddy List 2.0 system by checking chapters 6.10, 8.16 of the docs.
user - A User
public void setBuddyBlockStatus(User user,
java.lang.String buddyName,
boolean isBlocked)
You can learn more about the Buddy List 2.0 system by checking chapters 6.10, 8.16 of the docs.
user - the owner of the buddy listbuddyName - the name of the buddy to block / unblockisBlocked - the status (true == blocked)
public void kickUser(User user,
int delay,
java.lang.String farewellMessage)
user - the userdelay - an amount of seconds before the actual task is executedfarewellMessage - an admin message that is sent immediately to the user
public void banUser(User user,
int delay,
java.lang.String farewellMessage,
int banType)
user - the userdelay - an amount of seconds to wait before disconnecting the userfarewellMessage - an admin message that is sent immediatelybanType - you can ban by Ip address or Name.BanTypespublic void banOfflineUser(java.lang.String userName)
userName - the user name
public void removeBanishment(java.lang.String param,
int mode)
param - can be a user name or an ip address based on the 2nd argumentmode - the ban mode (by ip or by name)BanTypes
public User createNPC(java.lang.String userName,
java.lang.String ipAddress,
int port,
java.lang.String zoneName)
Creates an NPC (Non-Player Character) or BOT User that can be used for automated tasks, game AI etc... It will be able to send and receive messages and it will be seen from client side as a real user.
NPCs need to be connected to a physical socket end in order to "trick" the server into thinking that it is a real connected client. This is done behind the scenes by running a connection to the localhost, so if you take a look at the Admin Tool you will notice that NPC connections all come from 127.0.0.1
NPCs are subject to the MaxIdleUserTime policy as any other connected users, so you will need to start a thread or add a Scheduled task (recommended) that invokes the updateMessageTime() method on each non-player character to avoid disconnections.
userName - name of the NPCipAddress - ip address of the socket endport - TCP port numberzoneName - the zone in which the NPC should be logged in
public void addModerator(java.lang.String zoneName,
java.lang.String modName,
java.lang.String modPass)
zoneName - name of the zonemodName - name of the user that will act as ModeratormodPass - password for the moderator
public void removeModerator(java.lang.String zoneName,
java.lang.String modName)
zoneName - name of the ZonemodName - name of the moderator userpublic void rebootServer()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||