|
| 1 | +package org.mvplugins.multiverse.inventories.profile; |
| 2 | + |
| 3 | +import com.dumptruckman.minecraft.util.Logging; |
| 4 | +import com.google.common.base.Strings; |
| 5 | +import net.minidev.json.JSONObject; |
| 6 | +import net.minidev.json.JSONValue; |
| 7 | +import net.minidev.json.parser.JSONParser; |
| 8 | +import org.jvnet.hk2.annotations.Service; |
| 9 | +import org.mvplugins.multiverse.external.jakarta.inject.Inject; |
| 10 | +import org.mvplugins.multiverse.external.jakarta.inject.Provider; |
| 11 | +import org.mvplugins.multiverse.external.jetbrains.annotations.NotNull; |
| 12 | +import org.mvplugins.multiverse.external.vavr.control.Option; |
| 13 | +import org.mvplugins.multiverse.inventories.MultiverseInventories; |
| 14 | +import org.mvplugins.multiverse.inventories.profile.key.GlobalProfileKey; |
| 15 | + |
| 16 | +import java.io.File; |
| 17 | +import java.io.FileReader; |
| 18 | +import java.io.FileWriter; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.UUID; |
| 22 | +import java.util.concurrent.CompletableFuture; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | + |
| 25 | +@Service |
| 26 | +public final class PlayerNamesMapper { |
| 27 | + |
| 28 | + private static final String FILENAME = "playernames.json"; |
| 29 | + |
| 30 | + private final MultiverseInventories inventories; |
| 31 | + private final Provider<ProfileDataSource> profileDataSourceProvider; |
| 32 | + private final Provider<ProfileCacheManager> profileCacheManagerProvider; |
| 33 | + |
| 34 | + private final File playerNamesFile; |
| 35 | + private final Map<String, GlobalProfileKey> playerNamesMap; |
| 36 | + private final Map<UUID, GlobalProfileKey> playerUUIDMap; |
| 37 | + |
| 38 | + private Map<String, Object> playerNamesJson; |
| 39 | + |
| 40 | + @Inject |
| 41 | + PlayerNamesMapper( |
| 42 | + @NotNull MultiverseInventories inventories, |
| 43 | + @NotNull Provider<ProfileDataSource> profileDataSourceProvider, |
| 44 | + @NotNull Provider<ProfileCacheManager> profileCacheManagerProvider |
| 45 | + ) { |
| 46 | + this.inventories = inventories; |
| 47 | + this.profileDataSourceProvider = profileDataSourceProvider; |
| 48 | + this.profileCacheManagerProvider = profileCacheManagerProvider; |
| 49 | + |
| 50 | + this.playerNamesFile = new File(inventories.getDataFolder(), FILENAME); |
| 51 | + this.playerNamesMap = new ConcurrentHashMap<>(); |
| 52 | + this.playerUUIDMap = new ConcurrentHashMap<>(); |
| 53 | + } |
| 54 | + |
| 55 | + public void loadMap() { |
| 56 | + Logging.config("Loading player names map..."); |
| 57 | + playerNamesMap.clear(); |
| 58 | + playerUUIDMap.clear(); |
| 59 | + if (playerNamesFile.exists()) { |
| 60 | + loadFromPlayerNamesFile(); |
| 61 | + } else { |
| 62 | + buildPlayerNamesMap(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private void loadFromPlayerNamesFile() { |
| 67 | + try (FileReader fileReader = new FileReader(playerNamesFile)) { |
| 68 | + playerNamesJson = new ConcurrentHashMap<>((JSONObject) new JSONParser(JSONParser.DEFAULT_PERMISSIVE_MODE).parse(fileReader)); |
| 69 | + if (playerNamesJson.isEmpty()) { |
| 70 | + buildPlayerNamesMap(); |
| 71 | + return; |
| 72 | + } |
| 73 | + playerNamesJson.forEach((String uuid, Object name) -> { |
| 74 | + UUID playerUUID = UUID.fromString(uuid); |
| 75 | + String playerName = String.valueOf(name); |
| 76 | + GlobalProfileKey globalProfileKey = GlobalProfileKey.create(playerUUID, playerName); |
| 77 | + playerNamesMap.put(playerName, globalProfileKey); |
| 78 | + playerUUIDMap.put(playerUUID, globalProfileKey); |
| 79 | + }); |
| 80 | + } catch (Exception e) { |
| 81 | + e.printStackTrace(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void buildPlayerNamesMap() { |
| 86 | + Logging.info("Generating player names map... This may take a while."); |
| 87 | + playerNamesJson = new ConcurrentHashMap<>(); |
| 88 | + |
| 89 | + ProfileDataSource profileDataSource = profileDataSourceProvider.get(); |
| 90 | + CompletableFuture[] futures = profileDataSource.listGlobalProfileUUIDs().stream() |
| 91 | + .map(uuid -> profileDataSource.getGlobalProfile(GlobalProfileKey.create(uuid)) |
| 92 | + .thenAccept(globalProfile -> setPlayerName(uuid, globalProfile.getLastKnownName()))) |
| 93 | + .toArray(CompletableFuture[]::new); |
| 94 | + CompletableFuture.allOf(futures).thenRun(this::savePlayerNames).join(); |
| 95 | + Logging.info("Generated player names map."); |
| 96 | + } |
| 97 | + |
| 98 | + boolean setPlayerName(UUID uuid, String name) { |
| 99 | + Logging.finer("Setting player name mapping for %s to %s", uuid, name); |
| 100 | + if (Strings.isNullOrEmpty(name)) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + if (getKey(name).filter(g -> g.getPlayerUUID().equals(uuid)).isDefined()) { |
| 104 | + return false; |
| 105 | + } |
| 106 | + GlobalProfileKey globalProfileKey = GlobalProfileKey.create(uuid, name); |
| 107 | + playerNamesJson.put(uuid.toString(), name); |
| 108 | + playerNamesMap.put(name, globalProfileKey); |
| 109 | + playerUUIDMap.put(uuid, globalProfileKey); |
| 110 | + return true; |
| 111 | + } |
| 112 | + |
| 113 | + void savePlayerNames() { |
| 114 | + try (FileWriter fileWriter = new FileWriter(playerNamesFile)) { |
| 115 | + fileWriter.write(JSONValue.toJSONString(playerNamesJson)); |
| 116 | + } catch (Exception e) { |
| 117 | + e.printStackTrace(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + File getFile() { |
| 122 | + return playerNamesFile; |
| 123 | + } |
| 124 | + |
| 125 | + public Option<GlobalProfileKey> getKey(String playerName) { |
| 126 | + return Option.of(playerNamesMap.get(playerName)); |
| 127 | + } |
| 128 | + |
| 129 | + public Option<GlobalProfileKey> getKey(UUID playerUUID) { |
| 130 | + return Option.of(playerUUIDMap.get(playerUUID)); |
| 131 | + } |
| 132 | + |
| 133 | + public List<GlobalProfileKey> getKeys() { |
| 134 | + return playerNamesMap.values().stream().toList(); |
| 135 | + } |
| 136 | +} |
0 commit comments