Пример #1
0
  /**
   * Looks for an existing instance of <code>Player</code> for the given name, and creates one if an
   * instance doesn't already exist. This also takes care of registering the player.
   *
   * @param name the account name of the user
   * @return a reference to the <code>Player</code> with the given name
   */
  public static Player getInstance(String name) {
    DataManager dataManager = AppContext.getDataManager();

    // try to lookup the existing Player
    Player player = null;
    try {
      player = (Player) dataManager.getBinding(NAME_PREFIX + name);
    } catch (NameNotBoundException e) {
      player = new Player(name);
      dataManager.setBinding(NAME_PREFIX + name, player);
    }

    return player;
  }
 /**
  * Find or create the player object for the given session, and mark the player as logged in on
  * that session.
  *
  * @param session which session to find or create a player for
  * @return a player for the given session
  */
 public static SwordWorldPlayer loggedIn(ClientSession session) {
   String playerBinding = PLAYER_BIND_PREFIX + session.getName();
   // try to find player object, if non existent then create
   DataManager dataMgr = AppContext.getDataManager();
   SwordWorldPlayer player;
   try {
     player = (SwordWorldPlayer) dataMgr.getBinding(playerBinding);
   } catch (NameNotBoundException ex) {
     // this is a new player
     player = new SwordWorldPlayer(playerBinding);
     logger.log(Level.INFO, "New player created: {0}", player);
     dataMgr.setBinding(playerBinding, player);
   }
   player.setSession(session);
   return player;
 }