Advertisement
Guest User

Untitled

a guest
Feb 13th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.10 KB | None | 0 0
  1. package l2f.gameserver.taskmanager;
  2.  
  3. import l2f.gameserver.Config;
  4. import l2f.gameserver.ThreadPoolManager;
  5. import l2f.gameserver.model.GameObjectsStorage;
  6. import l2f.gameserver.model.Player;
  7. import org.apache.commons.lang3.ArrayUtils;
  8.  
  9. import java.util.HashMap;
  10. import java.util.Map;
  11.  
  12. public class ExtremeColoring implements Runnable
  13. {
  14.     private final Map<Integer, Long> lastUpdates;
  15.  
  16.     private ExtremeColoring()
  17.     {
  18.         lastUpdates = new HashMap<>();
  19.     }
  20.  
  21.     public static void startThread()
  22.     {
  23.         ThreadPoolManager.getInstance().scheduleAtFixedRate(new ExtremeColoring(), 200L, 200L);
  24.     }
  25.  
  26.     @Override
  27.     public void run()
  28.     {
  29.         long currentTime = System.currentTimeMillis();
  30.  
  31.         for(Player player : GameObjectsStorage.getAllPlayers())
  32.         {
  33.             int[] playerColorsGroup = getMyColors(player);
  34.             int myCurrentColor = player.getAppearence().getNameColor();
  35.             if(canColorBeUpdated(player, playerColorsGroup, myCurrentColor, currentTime))
  36.             {
  37.                 int newColor = getNextRecordValue(playerColorsGroup, myCurrentColor);
  38.                 updateColor(player, newColor);
  39.                 lastUpdates.put(player.getObjectId(), currentTime);
  40.             }
  41.         }
  42.     }
  43.  
  44.     private static int[] getMyColors(Player player)
  45.     {
  46.         int lastI = 0;
  47.         for (int i : Config.PLAYER_COLORS.keySet())
  48.         {
  49.             if (player.getPvpKills() >= i)
  50.             {
  51.                 lastI = i;
  52.             }
  53.         }
  54.         return Config.PLAYER_COLORS.get(lastI);
  55.     }
  56.  
  57.     private static int getNextRecordValue(int[] array, int currentValue)//I think Rnd.get(array) would be more fun
  58.     {
  59.         for(int i = 0;i < array.length - 1;i ++)
  60.             if(array[i] == currentValue)
  61.                 return array[i + 1];
  62.         return array[0];
  63.     }
  64.  
  65.     private boolean canColorBeUpdated(Player player, int[] playerColorsGroup, int myCurrentColor, long currentTime)
  66.     {
  67.         if(playerColorsGroup == null)
  68.             return false;
  69.  
  70.         if(!ArrayUtils.contains(playerColorsGroup, myCurrentColor))
  71.             return true;
  72.  
  73.         return lastUpdates.getOrDefault(player.getObjectId(), 0L) + Config.PLAYER_COLORS_TIME < currentTime;
  74.     }
  75.  
  76.     private static void updateColor(Player player, int newColor)
  77.     {
  78.         player.getAppearance().setNameColor(newColor);
  79.         player.broadcastUserInfo();
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement