Advertisement
Guest User

Untitled

a guest
Oct 10th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. public class RedisStuff implements Listener{
  2.  
  3. private static ConcurrentHashMap<Long, Integer> map = new ConcurrentHashMap<>();
  4.  
  5. public static void register(){
  6. try{
  7. Bukkit.getScheduler().runTaskLater(NetAPI.plugin, new Runnable() {
  8. @Override
  9. public void run() {
  10. if(Bukkit.getPluginManager().getPlugin("General") != null)
  11. RedisBukkit.a().registerChannel("distributor_bridge_netapi");
  12. }
  13. }, 2);
  14. }catch(Exception ex){
  15. ex.printStackTrace();
  16. }
  17. }
  18.  
  19. @EventHandler
  20. public void onPubSub(AsyncRedisMessageReceivedEvent e){
  21. if(e.getChannel().equalsIgnoreCase("distributor_bridge_netapi")){
  22. String[] data = e.getMessage().split("%split%");
  23. long session = Long.valueOf(data[0]);
  24. int id = Integer.parseInt(data[1]);
  25. map.put(session, id);
  26. }
  27. }
  28.  
  29. public static int loadGameMode(String gametype, boolean ignoreincalculation){
  30. long session = new Random().nextLong();
  31. RedisBukkit.a().sendMessage("distributor_bridge_netapi", gametype+"%split%"+ignoreincalculation+"%split%"+session);
  32. int timeout = 2000;
  33. long start = System.currentTimeMillis();
  34. while(System.currentTimeMillis()-start < timeout){
  35. if(map.containsKey(session)){
  36. int id = map.get(session);
  37. map.remove(session);
  38. return id;
  39. }
  40. try {
  41. Thread.sleep(2);
  42. } catch (InterruptedException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. return -2;
  46. }
  47. }
  48. return -2;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement