From 27c2492599631c4e2475dc0f2dad51f49842d60d Mon Sep 17 00:00:00 2001 From: Vankka Date: Fri, 5 Oct 2018 22:30:57 +0300 Subject: [PATCH] DiscordSRV plugin data by Vankka (#743) --- Plan/src/main/resources/plugin.yml | 3 +- PlanPluginBridge/pom.xml | 10 ++ .../djrapitops/pluginbridge/plan/Bridge.java | 2 + .../plan/discordsrv/DiscordSRVData.java | 124 ++++++++++++++++++ .../plan/discordsrv/DiscordSRVHook.java | 26 ++++ 5 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVData.java create mode 100644 PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVHook.java diff --git a/Plan/src/main/resources/plugin.yml b/Plan/src/main/resources/plugin.yml index d66bbf18c..436213abd 100644 --- a/Plan/src/main/resources/plugin.yml +++ b/Plan/src/main/resources/plugin.yml @@ -21,6 +21,7 @@ softdepend: - RedProtect - AdvancedBan - LuckPerms +- DiscordSRV commands: plan: @@ -89,7 +90,7 @@ permissions: default: op plan.webmanage: description: Manage the webusers, delete, list, check - default: op; + default: op plan.basic: children: diff --git a/PlanPluginBridge/pom.xml b/PlanPluginBridge/pom.xml index d61b0fe23..329b230d8 100644 --- a/PlanPluginBridge/pom.xml +++ b/PlanPluginBridge/pom.xml @@ -85,6 +85,10 @@ nucleus-repo http://repo.drnaylor.co.uk/artifactory/list/minecraft + + discordsrv-repo + https://ci.scarsz.me/plugin/repository/everything/ + @@ -179,6 +183,12 @@ 4.3 provided + + github.scarsz.discordsrv + DiscordSRV + 1.16.4 + provided + diff --git a/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/Bridge.java b/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/Bridge.java index fc835bacd..4ac01895d 100644 --- a/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/Bridge.java +++ b/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/Bridge.java @@ -10,6 +10,7 @@ import com.djrapitops.pluginbridge.plan.advancedban.AdvancedBanHook; import com.djrapitops.pluginbridge.plan.askyblock.ASkyBlockHook; import com.djrapitops.pluginbridge.plan.banmanager.BanManagerHook; import com.djrapitops.pluginbridge.plan.buycraft.BuyCraftHook; +import com.djrapitops.pluginbridge.plan.discordsrv.DiscordSRVHook; import com.djrapitops.pluginbridge.plan.essentials.EssentialsHook; import com.djrapitops.pluginbridge.plan.factions.FactionsHook; import com.djrapitops.pluginbridge.plan.griefprevention.GriefPreventionHook; @@ -100,6 +101,7 @@ public class Bridge { new ASkyBlockHook(h), new BanManagerHook(h), new BuyCraftHook(h), + new DiscordSRVHook(h), new EssentialsHook(h), new FactionsHook(h), new GriefPreventionHook(h), diff --git a/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVData.java b/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVData.java new file mode 100644 index 000000000..0223833ca --- /dev/null +++ b/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVData.java @@ -0,0 +1,124 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package com.djrapitops.pluginbridge.plan.discordsrv; + +import com.djrapitops.plan.data.element.AnalysisContainer; +import com.djrapitops.plan.data.element.InspectContainer; +import com.djrapitops.plan.data.plugin.ContainerSize; +import com.djrapitops.plan.data.plugin.PluginData; +import com.djrapitops.plan.utilities.FormatUtils; +import com.djrapitops.plan.utilities.html.icon.Color; +import com.djrapitops.plan.utilities.html.icon.Family; +import com.djrapitops.plan.utilities.html.icon.Icon; +import github.scarsz.discordsrv.DiscordSRV; +import github.scarsz.discordsrv.dependencies.jda.core.entities.Member; +import github.scarsz.discordsrv.dependencies.jda.core.entities.Role; +import github.scarsz.discordsrv.dependencies.jda.core.entities.User; +import github.scarsz.discordsrv.util.DiscordUtil; +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; +import org.apache.commons.text.TextStringBuilder; + +/** + * PluginData for DiscordSRV plugin. + * + * @author Vankka + */ +public class DiscordSRVData extends PluginData { + public DiscordSRVData() { + super(ContainerSize.THIRD, "DiscordSRV"); + setPluginIcon(Icon.called("discord").of(Family.BRAND).build()); + } + + @Override + public InspectContainer getPlayerData(UUID uuid, InspectContainer inspectContainer) { + if (!DiscordSRV.isReady) { + return inspectContainer; + } + + String userId = DiscordSRV.getPlugin().getAccountLinkManager().getDiscordId(uuid); + User user = userId != null ? DiscordUtil.getUserById(userId) : null; + + if (user == null) { + return inspectContainer; + } + + inspectContainer.addValue( + getWithIcon("Username", Icon.called("user").of(Family.SOLID).of(Color.CYAN)), + "@" + user.getName() + "#" + user.getDiscriminator() + ); + inspectContainer.addValue( + getWithIcon("Account creation date", Icon.called("plus").of(Family.SOLID).of(Color.BLUE)), + FormatUtils.formatTimeStampYear(user.getCreationTime().toEpochSecond() * 1000L) + ); + + Member member = DiscordSRV.getPlugin().getMainGuild().getMember(user); + + if (member != null) { + addMemberData(member, inspectContainer); + } + + return inspectContainer; + } + + private void addMemberData(Member member, InspectContainer inspectContainer) { + String nickname = member.getNickname(); + + inspectContainer.addValue( + getWithIcon("Nickname", Icon.called("user-ninja").of(Family.SOLID).of(Color.ORANGE)), + nickname != null ? nickname : "None" + ); + inspectContainer.addValue( + getWithIcon("Join Date", Icon.called("plus").of(Family.SOLID).of(Color.GREEN)), + FormatUtils.formatTimeStampYear(member.getJoinDate().toEpochSecond() * 1000L) + ); + + List roles = member.getRoles().stream().map(Role::getName).collect(Collectors.toList()); // Ordered list of role names + if (!roles.isEmpty()) { + inspectContainer.addValue( + getWithIcon("Roles", Icon.called("user-circle").of(Family.SOLID).of(Color.RED)), + new TextStringBuilder().appendWithSeparators(roles, ", ").build() + ); + } + } + + @Override + public AnalysisContainer getServerData(Collection uuids, AnalysisContainer analysisContainer) { + if (!DiscordSRV.isReady) { + return analysisContainer; + } + + int accountsLinked = DiscordSRV.getPlugin().getAccountLinkManager().getLinkedAccounts().size(); + int guildUsers = DiscordSRV.getPlugin().getMainGuild().getMembers().size(); + + analysisContainer.addValue( + getWithIcon("Accounts linked", Icon.called("link").of(Family.SOLID).of(Color.CYAN)), + accountsLinked + ); + analysisContainer.addValue( + getWithIcon("Users in main guild", Icon.called("users").of(Family.SOLID).of(Color.GREEN)), + guildUsers + ); + analysisContainer.addValue( + getWithIcon("Accounts linked / Total players", Icon.called("percentage").of(Family.SOLID).of(Color.TEAL)), + calculatePercentage(accountsLinked, uuids.size()) + "%" + ); + analysisContainer.addValue( + getWithIcon("Accounts linked / Users in main guild", Icon.called("percentage").of(Family.SOLID).of(Color.LIGHT_GREEN)), + calculatePercentage(accountsLinked, guildUsers) + "%" + ); + + return analysisContainer; + } + + private double calculatePercentage(int input1, int input2) { + if (input1 == 0 || input2 == 0) + return 0D; + + return Math.round((double) input1 / input2 * 10000D) / 100D; // 2 decimals + } +} diff --git a/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVHook.java b/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVHook.java new file mode 100644 index 000000000..0c951b7c6 --- /dev/null +++ b/PlanPluginBridge/src/main/java/com/djrapitops/pluginbridge/plan/discordsrv/DiscordSRVHook.java @@ -0,0 +1,26 @@ +/* + * Licence is provided in the jar as license.yml also here: + * https://github.com/Rsl1122/Plan-PlayerAnalytics/blob/master/Plan/src/main/resources/license.yml + */ +package com.djrapitops.pluginbridge.plan.discordsrv; + +import com.djrapitops.plan.data.plugin.HookHandler; +import com.djrapitops.pluginbridge.plan.Hook; + +/** + * Hook for DiscordSRV plugin. + * + * @author Vankka + */ +public class DiscordSRVHook extends Hook { + public DiscordSRVHook(HookHandler hookHandler) { + super("github.scarsz.discordsrv.DiscordSRV", hookHandler); + } + + @Override + public void hook() throws NoClassDefFoundError { + if (enabled) { + addPluginDataSource(new DiscordSRVData()); + } + } +}