|
1 | 1 | package net.earthmc.emcapi.integration; |
2 | 2 |
|
| 3 | +import com.palmergames.bukkit.towny.TownyAPI; |
| 4 | +import com.palmergames.bukkit.towny.object.Resident; |
| 5 | +import github.scarsz.discordsrv.DiscordSRV; |
| 6 | +import github.scarsz.discordsrv.api.ListenerPriority; |
| 7 | +import github.scarsz.discordsrv.api.Subscribe; |
| 8 | +import github.scarsz.discordsrv.api.events.AccountLinkedEvent; |
| 9 | +import github.scarsz.discordsrv.api.events.AccountUnlinkedEvent; |
| 10 | +import org.bukkit.OfflinePlayer; |
| 11 | + |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Set; |
| 14 | +import java.util.UUID; |
| 15 | +import java.util.concurrent.ConcurrentHashMap; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.stream.Collectors; |
| 18 | + |
3 | 19 | public class DiscordIntegration extends Integration { |
| 20 | + private static final Map<UUID, String> UUID_TO_DISCORD = new ConcurrentHashMap<>(); |
| 21 | + private static final Map<String, UUID> DISCORD_TO_UUID = new ConcurrentHashMap<>(); |
| 22 | + |
4 | 23 | public DiscordIntegration() { |
5 | 24 | super("DiscordSRV"); |
6 | 25 | } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void register() { |
| 29 | + super.register(); |
| 30 | + if (isEnabled()) { |
| 31 | + plugin.getServer().getAsyncScheduler().runDelayed(plugin, t -> { |
| 32 | + Set<UUID> uuids = TownyAPI.getInstance().getResidents().stream().filter(res -> !res.isNPC()).map(Resident::getUUID).collect(Collectors.toSet()); |
| 33 | + |
| 34 | + Map<UUID, String> result = DiscordSRV.getPlugin().getAccountLinkManager().getManyDiscordIds(uuids); |
| 35 | + UUID_TO_DISCORD.putAll(result); |
| 36 | + |
| 37 | + for (Map.Entry<UUID, String> entry : result.entrySet()) { |
| 38 | + DISCORD_TO_UUID.put(entry.getValue(), entry.getKey()); |
| 39 | + } |
| 40 | + }, 30, TimeUnit.SECONDS); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + public UUID getUUID(String discordId) { |
| 45 | + return DISCORD_TO_UUID.get(discordId); |
| 46 | + } |
| 47 | + |
| 48 | + public String getDiscord(UUID uuid) { |
| 49 | + return UUID_TO_DISCORD.get(uuid); |
| 50 | + } |
| 51 | + |
| 52 | + @Subscribe(priority = ListenerPriority.MONITOR) |
| 53 | + public void onDiscordLink(AccountLinkedEvent event) { |
| 54 | + OfflinePlayer player = event.getPlayer(); |
| 55 | + String discordId = event.getUser().getId(); |
| 56 | + |
| 57 | + UUID_TO_DISCORD.put(player.getUniqueId(), discordId); |
| 58 | + DISCORD_TO_UUID.put(discordId, player.getUniqueId()); |
| 59 | + } |
| 60 | + |
| 61 | + @Subscribe(priority = ListenerPriority.MONITOR) |
| 62 | + public void onDiscordUnlink(AccountUnlinkedEvent event) { |
| 63 | + OfflinePlayer player = event.getPlayer(); |
| 64 | + String discordId = event.getDiscordId(); |
| 65 | + |
| 66 | + UUID_TO_DISCORD.remove(player.getUniqueId()); |
| 67 | + DISCORD_TO_UUID.remove(discordId); |
| 68 | + } |
7 | 69 | } |
0 commit comments