示例#1
0
 @SuppressWarnings("unchecked")
 public <T extends Object> T getCustomData(Class<? extends T> clazz) {
   if (currentGame == null)
     throw new NullPointerException("Cannot supply custom data for player who is not in a game.");
   if (customData == null) {
     try {
       customData =
           clazz.getConstructor(currentGame.getClass(), UUID.class).newInstance(currentGame, uuid);
     } catch (Exception e) {
       throw new IllegalArgumentException(
           "Custom data class "
               + clazz.getName()
               + " lacks required constructor ("
               + currentGame.getClass().getSimpleName()
               + ", UUID)",
           e);
     }
   } else if (clazz.isInstance(customData)) {
     //
   } else {
     throw new ClassCastException(
         "Bad custom data class. Expected: "
             + clazz.getName()
             + ". Found: "
             + customData.getClass().getName());
   }
   return (T) customData; // unchecked, kind of
 }
示例#2
0
 public void setCurrentGame(Game game) {
   // Remove player from previos game, if any.
   final Game old = getCurrentGame();
   if (old != null) old.removePlayer(this);
   // Add them to the new game and save it in their info.
   if (game == null) {
     currentGame = null;
   } else {
     currentGame = game;
     game.addPlayer(this);
   }
   hasJoinedBefore = false;
   customData = null;
 }
示例#3
0
 public boolean addPlayer(Game game, UUID uuid) {
   if (playerManager.getCurrentGame(uuid) != null) {
     getLogger()
         .warning(
             "Tried to add player "
                 + uuid
                 + " to game "
                 + game.getUuid()
                 + " but they were already in game "
                 + playerManager.getCurrentGame(uuid).getUuid());
     return false;
   } else {
     playerManager.setCurrentGame(uuid, game);
     return true;
   }
 }