feat: add basic Discord Integration

BREAKING CHANGE: teams.json needs manual migration
This commit is contained in:
Simon Giesel 2020-05-17 19:24:02 +02:00
parent 63603178ee
commit 72c6fa8d9c
10 changed files with 526 additions and 42 deletions

30
varo/.factorypath Normal file
View file

@ -0,0 +1,30 @@
<factorypath>
<factorypathentry kind="VARJAR" id="M2_REPO/com/destroystokyo/paper/paper-api/1.15.2-R0.1-SNAPSHOT/paper-api-1.15.2-R0.1-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/commons-lang/commons-lang/2.6/commons-lang-2.6.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/guava/guava/21.0/guava-21.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/md-5/bungeecord-chat/1.15-SNAPSHOT/bungeecord-chat-1.15-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/yaml/snakeyaml/1.25/snakeyaml-1.25.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/ow2/asm/asm/8.0.1/asm-8.0.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/ow2/asm/asm-commons/8.0.1/asm-commons-8.0.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/ow2/asm/asm-tree/8.0.1/asm-tree-8.0.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/ow2/asm/asm-analysis/8.0.1/asm-analysis-8.0.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/spigotmc/spigot/1.15.2-R0.1-SNAPSHOT/spigot-1.15.2-R0.1-SNAPSHOT.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/dv8tion/JDA/4.1.1_101/JDA-4.1.1_101.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/jetbrains/annotations/16.0.1/annotations-16.0.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/neovisionaries/nv-websocket-client/2.9/nv-websocket-client-2.9.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/squareup/okhttp3/okhttp/3.13.0/okhttp-3.13.0.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/squareup/okio/okio/1.17.2/okio-1.17.2.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/commons/commons-collections4/4.1/commons-collections4-4.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/net/sf/trove4j/trove4j/3.0.3/trove4j-3.0.3.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/fasterxml/jackson/core/jackson-databind/2.10.1/jackson-databind-2.10.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/fasterxml/jackson/core/jackson-annotations/2.10.1/jackson-annotations-2.10.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/fasterxml/jackson/core/jackson-core/2.10.1/jackson-core-2.10.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/com/googlecode/json-simple/json-simple/1.1.1/json-simple-1.1.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/junit/junit/4.10/junit-4.10.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/logging/log4j/log4j-core/2.1/log4j-core-2.1.jar" enabled="true" runInBatchMode="false"/>
<factorypathentry kind="VARJAR" id="M2_REPO/org/apache/logging/log4j/log4j-api/2.1/log4j-api-2.1.jar" enabled="true" runInBatchMode="false"/>
</factorypath>

View file

@ -1,2 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
org.eclipse.jdt.apt.aptEnabled=true
org.eclipse.jdt.apt.genSrcDir=target/generated-sources/annotations
org.eclipse.jdt.apt.genTestSrcDir=target/generated-test-sources/test-annotations

View file

@ -4,6 +4,6 @@ org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.processAnnotations=enabled
org.eclipse.jdt.core.compiler.release=disabled
org.eclipse.jdt.core.compiler.source=11

View file

@ -26,6 +26,11 @@
<id>papermc</id>
<url>https://papermc.io/repo/repository/maven-public/</url>
</repository>
<repository>
<id>jcenter</id>
<name>jcenter-bintray</name>
<url>https://jcenter.bintray.com</url>
</repository>
</repositories>
<dependencies>
<!--Paperclip API -->
@ -42,6 +47,31 @@
<version>1.15.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<!-- Java Discord API (JDA) -->
<dependency>
<groupId>net.dv8tion</groupId>
<artifactId>JDA</artifactId>
<version>4.1.1_101</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
<artifactId>opus-java</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- json.simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
@ -53,5 +83,110 @@
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<relocations>
<!-- relocated because Bukkit includes Gson and I have no control over it -->
<!-- <relocation>
<pattern>com.google</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.google</shadedPattern>
</relocation> -->
<!-- relocated due to api usage -->
<relocation>
<pattern>net.dv8tion.jda</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.jda</shadedPattern>
</relocation>
<relocation>
<pattern>com.neovisionaries.ws</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.ws</shadedPattern>
</relocation>
<relocation>
<pattern>net.kyori</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.net.kyori</shadedPattern>
</relocation>
<relocation>
<pattern>me.vankka</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.me.vankka</shadedPattern>
</relocation>
<!-- relocated due to dependency hell -->
<!-- <relocation>
<pattern>org.apache.commons</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.commons</shadedPattern>
</relocation> -->
<relocation>
<pattern>org.apache.http</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.apache.http</shadedPattern>
</relocation>
<relocation>
<pattern>org.json</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.json</shadedPattern>
</relocation>
<relocation>
<pattern>org.json.simple</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.json.simple</shadedPattern>
</relocation>
<relocation>
<pattern>okhttp3</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.okhttp3</shadedPattern>
</relocation>
<relocation>
<pattern>okio</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.okio</shadedPattern>
</relocation>
<!-- <relocation>
<pattern>gnu.trove</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.trove</shadedPattern>
</relocation> -->
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>de.cliffbreak.varo.dependencies.jackson</shadedPattern>
</relocation>
</relocations>
<minimizeJar>true</minimizeJar>
<filters>
<filter>
<artifact>commons-logging:commons-logging</artifact>
<includes>
<include>**</include>
</includes>
</filter>
<filter>
<artifact>com.fasterxml.jackson.core:jackson-databind</artifact>
<includes>
<include>**</include>
</includes>
</filter>
</filters>
</configuration>
</execution>
</executions>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
</plugin>
</plugins>
</build>
</project>

View file

@ -5,19 +5,20 @@ import java.io.IOException;
import java.util.ArrayList;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import de.cliffbreak.varo.commands.CoordsCommand;
import de.cliffbreak.varo.commands.VaroCommand;
import de.cliffbreak.varo.commands.VaroTabCompleter;
import de.cliffbreak.varo.discord.DiscordBot;
import de.cliffbreak.varo.listeners.BannedActionListener;
import de.cliffbreak.varo.listeners.BannedItemListener;
import de.cliffbreak.varo.listeners.ChatListener;
import de.cliffbreak.varo.listeners.PlayerInteractNPCListener;
import de.cliffbreak.varo.listeners.EntityRegainHealthListener;
import de.cliffbreak.varo.listeners.PlayerClientOptionsChangeListener;
import de.cliffbreak.varo.listeners.PlayerDeathListener;
import de.cliffbreak.varo.listeners.PlayerInteractNPCListener;
import de.cliffbreak.varo.listeners.PlayerJoinQuitListener;
import de.cliffbreak.varo.listeners.PlayerLoginListener;
import de.cliffbreak.varo.listeners.PlayerResourcePackStatusListener;
@ -38,6 +39,7 @@ public class Varo extends JavaPlugin {
private VaroUtils varoUtils;
private TeamManager teamManager;
private MessageUtils messageUtils;
private DiscordBot discordBot;
private boolean canCreatePortal = false;
@ -53,11 +55,13 @@ public class Varo extends JavaPlugin {
this.playerCache = new PlayerCache(this);
this.banUtils = new BanUtils(this);
this.varoUtils = new VaroUtils(this);
this.teamManager = new TeamManager(this);
this.teamManager = new TeamManager();
this.messageUtils = new MessageUtils(this);
this.config.addDefault("Varo.Start", 1590427800000f);
this.config.addDefault("Varo.Debug", false);
this.config.addDefault("Varo.Discord.Token", "insert.token.here");
this.config.addDefault("Varo.Discord.Channel", "numeric.channel.id");
this.config.addDefault("Varo.RetroHealthRegen", false);
this.config.addDefault("Varo.Bans", new ArrayList<String>());
this.config.options().copyDefaults(true);
@ -79,12 +83,19 @@ public class Varo extends JavaPlugin {
this.getCommand("coords").setExecutor(new CoordsCommand(this));
this.varoUtils.init();
this.discordBot = new DiscordBot(this);
}
@Override
public void onDisable() {
this.getLogger().info("Stopping CliffbreakVaro!");
this.playerCache.shutdown();
try {
this.discordBot.shutdown();
} catch (final NoClassDefFoundError err) {
}
}
@Override
@ -129,4 +140,8 @@ public class Varo extends JavaPlugin {
public void setCanCreatePortal(final boolean canCreatePortal) {
this.canCreatePortal = canCreatePortal;
}
public DiscordBot getDiscordBot() {
return this.discordBot;
}
}

View file

@ -0,0 +1,50 @@
package de.cliffbreak.varo.discord;
import java.util.EnumSet;
import javax.security.auth.login.LoginException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Logger;
import de.cliffbreak.varo.Varo;
import de.cliffbreak.varo.discord.filters.JDAFilter;
import de.cliffbreak.varo.discord.listeners.MessageListener;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.utils.cache.CacheFlag;
public class DiscordBot {
private final Varo plugin;
private final JDA jda;
public DiscordBot(final Varo plugin) {
this.plugin = plugin;
((Logger) LogManager.getRootLogger()).addFilter((Filter) new JDAFilter());
this.jda = getJDA();
}
public void shutdown() {
this.jda.shutdown();
}
public void broadcastMessage(final String message) {
this.jda.getTextChannelById(this.plugin.getConfig().getString("Varo.Discord.Channel")).sendMessage(message)
.queue();
}
private JDA getJDA() {
try {
return new JDABuilder().setAutoReconnect(true).setBulkDeleteSplittingEnabled(false)
.setDisabledCacheFlags(EnumSet.of(CacheFlag.ACTIVITY, CacheFlag.VOICE_STATE))
.setToken(this.plugin.getConfig().getString("Varo.Discord.Token")).setContextEnabled(false)
.addEventListeners(new MessageListener(this.plugin)).build().awaitReady();
} catch (LoginException | InterruptedException e) {
this.plugin.getLogger().warning("The Discord-Service didn't connect properly.");
return null;
}
}
}

View file

@ -0,0 +1,172 @@
package de.cliffbreak.varo.discord.filters;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Marker;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.message.Message;
import org.bukkit.Bukkit;
public class JDAFilter implements Filter {
public Result check(final Logger logger, final Level level, final String message, final Throwable throwable) {
// only listen for JDA logs
if (!logger.getName().startsWith("de.cliffbreak.varo.dependencies.jda"))
return Result.NEUTRAL;
switch (level.name()) {
case "INFO":
Bukkit.getLogger().info("[Discord:INFO] " + message);
break;
case "WARN":
Bukkit.getLogger().warning("[Discord:WARN] " + message);
break;
case "ERROR":
if (message.contains("Got an unexpected error. Please redirect following message to the"))
return Result.DENY;
Bukkit.getLogger().warning("[Discord:ERROR] " + message);
break;
default:
Bukkit.getLogger().info("[Discord] " + message);
}
// all JDA messages should be denied because we handle them ourselves
return Result.DENY;
}
@Override
public Result filter(final LogEvent logEvent) {
return check((Logger) LogManager.getLogger(logEvent.getLoggerName()), logEvent.getLevel(),
logEvent.getMessage().getFormattedMessage(), logEvent.getThrown());
}
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final String message,
final Object... parameters) {
return check(logger, level, message, null);
}
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Object message,
final Throwable throwable) {
return check(logger, level, message.toString(), throwable);
}
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Message message,
final Throwable throwable) {
return check(logger, level, message.getFormattedMessage(), throwable);
}
public void start() {
}
public void stop() {
}
public boolean isStarted() {
return true;
}
public boolean isStopped() {
return false;
}
@Override
public Result getOnMismatch() {
return Result.NEUTRAL;
}
@Override
public Result getOnMatch() {
return Result.NEUTRAL;
}
@Override
public State getState() {
// Auto-generated method stub
return null;
}
@Override
public void initialize() {
// Auto-generated method stub
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6, final Object arg7) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6, final Object arg7, final Object arg8) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6, final Object arg7, final Object arg8, final Object arg9) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6, final Object arg7, final Object arg8, final Object arg9,
final Object arg10) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6, final Object arg7, final Object arg8, final Object arg9,
final Object arg10, final Object arg11) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6, final Object arg7, final Object arg8, final Object arg9,
final Object arg10, final Object arg11, final Object arg12) {
// Auto-generated method stub
return null;
}
@Override
public Result filter(final Logger arg0, final Level arg1, final Marker arg2, final String arg3, final Object arg4,
final Object arg5, final Object arg6, final Object arg7, final Object arg8, final Object arg9,
final Object arg10, final Object arg11, final Object arg12, final Object arg13) {
// Auto-generated method stub
return null;
}
}

View file

@ -0,0 +1,45 @@
package de.cliffbreak.varo.discord.listeners;
import java.util.ArrayList;
import java.util.Arrays;
import de.cliffbreak.varo.Varo;
import net.dv8tion.jda.api.entities.EmbedType;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageChannel;
import net.dv8tion.jda.api.entities.MessageEmbed;
import net.dv8tion.jda.api.entities.MessageEmbed.Field;
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
public class MessageListener extends ListenerAdapter {
final Varo plugin;
public MessageListener(final Varo plugin) {
this.plugin = plugin;
}
@Override
public void onMessageReceived(final MessageReceivedEvent e) {
// Don't respond to Bot accounts (and myself)
if (e.getAuthor().isBot()) {
return;
}
// Only answer in configured Channels
final MessageChannel channel = e.getChannel();
if (!channel.getId().equals(this.plugin.getConfig().getString("Varo.Discord.Channel"))) {
return;
}
final Message message = e.getMessage();
switch (message.getContentRaw()) {
case "!ping":
channel.sendMessage("Pong!").queue();
break;
case "!test":
channel.sendMessage(new MessageEmbed(null, "title", "description", EmbedType.RICH, null, 0, null, null,
null, null, null, null, new ArrayList<Field>(Arrays.asList()))).queue();
break;
}
}
}

View file

@ -27,6 +27,8 @@ public class PlayerDeathListener implements Listener {
e.getEntity().setHealth(20d); // Reset health to prevent death dialog after rejoin (if unbanned)
e.getEntity().setGameMode(GameMode.SPECTATOR);
this.plugin.getBanUtils().addBan(e.getEntity().getUniqueId());
this.plugin.getDiscordBot().broadcastMessage("Der Spieler <@!"
+ this.plugin.getTeamManager().getDiscordIdByPlayer(e.getEntity()) + "> ist gestorben.");
}
}

View file

@ -15,27 +15,24 @@ import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.json.simple.parser.ParseException;
import de.cliffbreak.varo.Varo;
import de.cliffbreak.varo.uitls.JSONConfig;
import de.cliffbreak.varo.uitls.WebUtils;
public class TeamManager {
private final Varo plugin;
private final JSONConfig config;
private final Scoreboard scoreboard;
private final ArrayList<Team> teams;
private static final String TEAM_PREFIX = "~";
public TeamManager(final Varo plugin) {
this.plugin = plugin;
public TeamManager() {
this.config = new JSONConfig(new File("plugins/CliffbreakVaro", "teams.json"), getDefaults());
this.config.save();
this.scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
this.teams = new ArrayList<Team>();
for (Team team : this.scoreboard.getTeams()) {
for (final Team team : this.scoreboard.getTeams()) {
team.unregister();
}
@ -43,49 +40,56 @@ public class TeamManager {
}
private void init() {
final JSONArray jsonTeams = config.getArray("Teams");
final JSONArray jsonTeams = config.getArray("teams");
int teamKey = -1;
for (final Object jsonRawTeam : jsonTeams) {
teamKey++;
final JSONObject jsonTeam = (JSONObject) jsonRawTeam;
final Team team, afkTeam;
if (jsonTeam.get("Name") == null) {
if (jsonTeam.get("name") == null) {
continue;
}
if (this.scoreboard.getTeam((String) jsonTeam.get("Name")) == null) {
team = this.scoreboard.registerNewTeam((String) jsonTeam.get("Name"));
if (this.scoreboard.getTeam((String) jsonTeam.get("name")) == null) {
team = this.scoreboard.registerNewTeam((String) jsonTeam.get("name"));
} else {
team = this.scoreboard.getTeam((String) jsonTeam.get("Name"));
team = this.scoreboard.getTeam((String) jsonTeam.get("name"));
}
if (this.scoreboard.getTeam(TEAM_PREFIX + jsonTeam.get("Name")) == null) {
afkTeam = this.scoreboard.registerNewTeam(TEAM_PREFIX + jsonTeam.get("Name"));
if (this.scoreboard.getTeam(TEAM_PREFIX + jsonTeam.get("name")) == null) {
afkTeam = this.scoreboard.registerNewTeam(TEAM_PREFIX + jsonTeam.get("name"));
} else {
afkTeam = this.scoreboard.getTeam(TEAM_PREFIX + jsonTeam.get("Name"));
afkTeam = this.scoreboard.getTeam(TEAM_PREFIX + jsonTeam.get("name"));
}
this.teams.add(team);
this.teams.add(afkTeam);
team.setPrefix(ChatColor.valueOf((String) jsonTeam.get("Color")) + "§l[" + jsonTeam.get("Prefix") + "] ");
team.setPrefix(ChatColor.valueOf((String) jsonTeam.get("color")) + "§l[" + jsonTeam.get("prefix") + "] ");
afkTeam.setPrefix("§8§l[AFK] ");
team.setColor(ChatColor.WHITE);
afkTeam.setColor(ChatColor.valueOf((String) jsonTeam.get("Color")));
afkTeam.setColor(ChatColor.valueOf((String) jsonTeam.get("color")));
for (final String entry : team.getEntries()) {
team.removeEntry(entry);
}
for (final String entry : afkTeam.getEntries()) {
team.removeEntry(entry);
}
for (final Object uuid : (JSONArray) jsonTeam.get("Players")) {
String playerName = "";
int playerKey = -1;
for (final Object rawPlayer : (JSONArray) jsonTeam.get("players")) {
playerKey++;
final JSONObject player = (JSONObject) rawPlayer;
try {
final String result = WebUtils
.GET("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
.GET("https://sessionserver.mojang.com/session/minecraft/profile/" + player.get("uuid"));
if (result.isEmpty()) {
continue;
}
final JSONObject profile = (JSONObject) JSONValue.parseWithException(result);
team.addEntry((String) profile.get("name"));
playerName = (String) profile.get("name");
config.updatePlayerName(teamKey, playerKey, playerName);
} catch (IOException | ParseException e) {
this.plugin.getLogger().info(
"§c§lFehler:§r§c beim Verbinden mit der Mojang-API ist ein Fehler aufgetreten. Eventuell sind die Mojang Server down.");
playerName = (String) player.get("name");
}
team.addEntry(playerName);
}
}
}
@ -99,6 +103,20 @@ public class TeamManager {
return null;
}
public String getDiscordIdByPlayer(final Player player) {
final JSONArray jsonTeams = config.getArray("teams");
for (final Object jsonRawTeam : jsonTeams) {
final JSONObject jsonTeam = (JSONObject) jsonRawTeam;
for (final Object rawPlayer : ((JSONArray) jsonTeam.get("players"))) {
final JSONObject jsonPlayer = (JSONObject) rawPlayer;
if (jsonPlayer.get("uuid").equals(player.getUniqueId().toString().replace("-", ""))) {
return jsonPlayer.get("discord").toString();
}
}
}
return "";
}
public void setAFK(final String player) {
final Team oldTeam = getTeamByPlayer(player);
for (final Team team : this.teams) {
@ -109,44 +127,59 @@ public class TeamManager {
}
public void setTeam(final Player player) {
final JSONArray jsonTeams = config.getArray("Teams");
final JSONArray jsonTeams = config.getArray("teams");
for (final Object jsonRawTeam : jsonTeams) {
final JSONObject jsonTeam = (JSONObject) jsonRawTeam;
if (((JSONArray) jsonTeam.get("Players")).contains(player.getUniqueId().toString().replace("-", ""))) {
for (final Object rawPlayer : ((JSONArray) jsonTeam.get("players"))) {
final JSONObject jsonPlayer = (JSONObject) rawPlayer;
if (jsonPlayer.get("uuid").equals(player.getUniqueId().toString().replace("-", ""))) {
for (final Team team : this.teams) {
if (team.getName().equals(jsonTeam.get("Name").toString())) {
if (team.getName().equals(jsonTeam.get("name").toString())) {
team.addEntry(player.getName());
}
}
}
}
}
}
@SuppressWarnings("unchecked")
private HashMap<String, Object> getDefaults() {
final HashMap<String, Object> defaults = new HashMap<String, Object>();
final JSONObject team1 = new JSONObject();
team1.put("Name", "Demo-Team");
team1.put("Color", ChatColor.RED);
team1.put("Prefix", "DMO");
team1.put("name", "Demo-Team");
team1.put("color", ChatColor.RED);
team1.put("prefix", "DMO");
final JSONArray team1Players = new JSONArray();
team1Players.add("a6019ccffe694bf780bcc3f3afa265dd");
team1Players.add("069a79f444e94726a5befca90e38aaf5");
team1.put("Players", team1Players);
final JSONObject player = new JSONObject();
player.put("name", "AragurLP");
player.put("uuid", "a6019ccffe694bf780bcc3f3afa265dd");
player.put("discord", "99820967022329856");
team1Players.add(player);
final JSONObject player2 = new JSONObject();
player2.put("name", "Notch");
player2.put("uuid", "069a79f444e94726a5befca90e38aaf5");
player2.put("discord", "");
team1Players.add(player2);
team1.put("players", team1Players);
final JSONObject team2 = new JSONObject();
team2.put("Name", "Demo-Team 2");
team2.put("Color", ChatColor.GREEN);
team2.put("Prefix", "DM2");
team2.put("name", "Demo-Team 2");
team2.put("color", ChatColor.GREEN);
team2.put("prefix", "DM2");
final JSONArray team2Players = new JSONArray();
team2Players.add("dff082fcd52d4ad2b2d8d8202aea53ec");
team2.put("Players", team2Players);
final JSONObject player3 = new JSONObject();
player3.put("name", "AragurTV");
player3.put("uuid", "dff082fcd52d4ad2b2d8d8202aea53ec");
player3.put("discord", "550794603754815520");
team2Players.add(player3);
team2.put("players", team2Players);
final JSONArray teams = new JSONArray();
teams.add(team1);
teams.add(team2);
defaults.put("Teams", teams);
defaults.put("teams", teams);
return defaults;
}
}