UUIDUpdateAsyncTask.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package com.gmail.nossr50.runnables.database;
  2. import com.gmail.nossr50.database.DatabaseManager;
  3. import com.gmail.nossr50.datatypes.database.UpgradeType;
  4. import com.gmail.nossr50.mcMMO;
  5. import com.gmail.nossr50.util.Misc;
  6. import com.gmail.nossr50.util.uuid.UUIDFetcher;
  7. import org.bukkit.scheduler.BukkitRunnable;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. import java.util.UUID;
  12. import java.util.logging.Level;
  13. public class UUIDUpdateAsyncTask extends BukkitRunnable {
  14. private mcMMO plugin;
  15. private static final int MAX_LOOKUP = Math.max(HiddenMainConfig.getInstance().getUUIDConvertAmount(), 100);
  16. private static final int RATE_LIMIT = HiddenMainConfig.getInstance().getMojangRateLimit();
  17. private static final long LIMIT_PERIOD = HiddenMainConfig.getInstance().getMojangLimitPeriod();
  18. private static final int BATCH_SIZE = MAX_LOOKUP * 3;
  19. private List<String> userNames;
  20. private int size;
  21. private int checkedUsers;
  22. private long startMillis;
  23. public UUIDUpdateAsyncTask(mcMMO plugin, List<String> userNames) {
  24. this.plugin = plugin;
  25. this.userNames = userNames;
  26. this.checkedUsers = 0;
  27. this.startMillis = System.currentTimeMillis();
  28. }
  29. @Override
  30. public void run() {
  31. size = userNames.size();
  32. plugin.getLogger().info("Starting to check and update UUIDs, total amount of users: " + size);
  33. List<String> userNamesSection;
  34. Map<String, UUID> fetchedUUIDs = new HashMap<String, UUID>();
  35. while (size != 0) {
  36. if (checkedUsers + 100 > RATE_LIMIT) {
  37. try {
  38. Thread.sleep(LIMIT_PERIOD);
  39. } catch (InterruptedException e) {
  40. e.printStackTrace();
  41. return;
  42. }
  43. startMillis = System.currentTimeMillis();
  44. checkedUsers = 0;
  45. }
  46. if (size > MAX_LOOKUP) {
  47. userNamesSection = userNames.subList(size - MAX_LOOKUP, size);
  48. size -= MAX_LOOKUP;
  49. }
  50. else {
  51. userNamesSection = userNames.subList(0, size);
  52. size = 0;
  53. }
  54. try {
  55. fetchedUUIDs.putAll(new UUIDFetcher(userNamesSection).call());
  56. }
  57. catch (Exception e) {
  58. // Handle 429
  59. if (e.getMessage().contains("429")) {
  60. size += userNamesSection.size();
  61. try {
  62. Thread.sleep(LIMIT_PERIOD);
  63. } catch (InterruptedException ex) {
  64. e.printStackTrace();
  65. return;
  66. }
  67. continue;
  68. }
  69. plugin.getLogger().log(Level.SEVERE, "Unable to fetch UUIDs!", e);
  70. return;
  71. }
  72. checkedUsers += userNamesSection.size();
  73. userNamesSection.clear();
  74. size = userNames.size();
  75. Misc.printProgress(checkedUsers, DatabaseManager.progressInterval, startMillis);
  76. if (fetchedUUIDs.size() >= BATCH_SIZE) {
  77. mcMMO.getDatabaseManager().saveUserUUIDs(fetchedUUIDs);
  78. fetchedUUIDs = new HashMap<String, UUID>();
  79. }
  80. }
  81. if (fetchedUUIDs.size() == 0 || mcMMO.getDatabaseManager().saveUserUUIDs(fetchedUUIDs)) {
  82. mcMMO.getUpgradeManager().setUpgradeCompleted(UpgradeType.ADD_UUIDS);
  83. plugin.getLogger().info("UUID upgrade completed!");
  84. }
  85. }
  86. }