Skip to content

Commit a35cb57

Browse files
authored
Merge pull request #8 from Event-Alerts/api-keys
API keys support
2 parents c16e9a1 + 89773e5 commit a35cb57

11 files changed

Lines changed: 318 additions & 22 deletions

File tree

build.gradle.kts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ sourceSets.main { blossom.javaSources {
3535
} }
3636

3737
// Dependencies
38-
repository(Repository.TRIUMPH_SNAPSHOTS)
38+
repository(Repository.TRIUMPH_SNAPSHOTS, Repository.PLACEHOLDER_API)
3939
dependencies {
4040
implementationRelocate("dev.triumphteam:triumph-gui-paper:4.0.0-SNAPSHOT", "dev.triumphteam")
4141

@@ -49,6 +49,8 @@ dependencies {
4949
compileOnly("net.fellbaum:jemoji:$jEmojiVersion") { // Downloaded on runtime
5050
relocate("net.fellbaum")
5151
}
52+
53+
compileOnly("me.clip:placeholderapi:2.12.2")
5254
}
5355

5456
// Unknown relocations

src/main/java/gg/eventalerts/eventalertsintegration/EventAlertsIntegration.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ public String getUserAgent() {
9393
return getName() + "/" + getDescription().getVersion() + " (MC/" + AnnoyingPlugin.MINECRAFT_VERSION + ")";
9494
}
9595

96+
@NotNull
97+
public Map<String, String> getSocketHeaders() {
98+
final Map<String, String> headers = new HashMap<>();
99+
headers.put("User-Agent", getUserAgent());
100+
if (config.apiKeys.playerApiKey != null) headers.put("X-Player-Key", config.apiKeys.playerApiKey);
101+
if (config.apiKeys.serverApiKey != null) headers.put("X-Server-Key", config.apiKeys.serverApiKey);
102+
return headers;
103+
}
104+
96105
public void runOnMainThread(@NotNull Runnable runnable) {
97106
Bukkit.getScheduler().runTask(this, runnable);
98107
}

src/main/java/gg/eventalerts/eventalertsintegration/config/ConfigYml.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,17 @@
1717

1818

1919
public class ConfigYml extends AnnoyingResource {
20+
@NotNull public static final String PATH_API_KEYS = "api-keys";
21+
@NotNull public static final String PATH_DISCORD_MESSAGE_SYNCING = "discord-message-syncing";
2022
@NotNull public static final String PATH_LINKING = "linking";
2123
@NotNull public static final String PATH_CROSS_BAN = "cross-ban";
2224
@NotNull public static final String PATH_EVENT_MESSAGES = "event-messages";
2325
@NotNull public static final String PATH_ADVANCED = "advanced";
2426

2527
@NotNull private final EventAlertsIntegration eaPlugin;
2628

29+
@NotNull public final ApiKeys apiKeys;
30+
@NotNull public final DiscordMessageSyncing discordMessageSyncing;
2731
@NotNull public final Linking linking;
2832
@NotNull public final CrossBan crossBan;
2933
@NotNull public final EventMessages eventMessages;
@@ -32,6 +36,9 @@ public class ConfigYml extends AnnoyingResource {
3236
public ConfigYml(@NotNull EventAlertsIntegration plugin) {
3337
super(plugin, "config.yml");
3438
eaPlugin = plugin;
39+
40+
apiKeys = new ApiKeys();
41+
discordMessageSyncing = new DiscordMessageSyncing();
3542
linking = new Linking();
3643
crossBan = new CrossBan();
3744
eventMessages = new EventMessages();
@@ -55,6 +62,29 @@ private <T> boolean toggleSetItem(@NotNull String path, @NotNull Set<T> set, @No
5562
return newStatus;
5663
}
5764

65+
public class ApiKeys {
66+
@NotNull public static final String PATH_PLAYER_API_KEY = PATH_API_KEYS + ".player";
67+
@NotNull public static final String PATH_SERVER_API_KEY = PATH_API_KEYS + ".server";
68+
69+
@Nullable public String playerApiKey = getString(PATH_PLAYER_API_KEY);
70+
@Nullable public String serverApiKey = getString(PATH_SERVER_API_KEY);
71+
72+
public ApiKeys() {
73+
if (playerApiKey != null && !playerApiKey.startsWith("EA.Player.1.")) playerApiKey = null;
74+
if (serverApiKey != null && !serverApiKey.startsWith("EA.PartnerServer.1.")) serverApiKey = null;
75+
}
76+
}
77+
78+
public class DiscordMessageSyncing {
79+
@NotNull public static final String PATH_ENABLED = PATH_DISCORD_MESSAGE_SYNCING + ".enabled";
80+
@NotNull public static final String DISCORDSRV_INTEGRATION = PATH_DISCORD_MESSAGE_SYNCING + ".discordsrv-integration";
81+
@NotNull public static final String PATH_FORMAT = PATH_DISCORD_MESSAGE_SYNCING + ".format";
82+
83+
public boolean enabled = getBoolean(PATH_ENABLED, true);
84+
public boolean discordSRVIntegration = getBoolean(DISCORDSRV_INTEGRATION, true);
85+
@NotNull public String format = getString(PATH_FORMAT, "<dark_aqua>\uD83C\uDF89 [<event_title>] <aqua>[<author_name>] <content_stripped>");
86+
}
87+
5888
public class Linking {
5989
@NotNull public static final String PATH_REQUIRE_LINK = PATH_LINKING + ".require-link";
6090
@NotNull public static final String PATH_CHECK_ON_JOIN = PATH_LINKING + ".check-on-join";

src/main/java/gg/eventalerts/eventalertsintegration/objects/Event.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,71 @@
1717

1818

1919
public class Event extends EAObject {
20+
// BUILDER/CUSTOM
21+
@NotNull public final ObjectId id;
22+
@NotNull public final String type;
23+
public final long channel;
24+
@Nullable public final Long message;
25+
@Nullable public final Long controlPanel;
2026
public final boolean custom;
21-
@NotNull public final String host;
22-
@Nullable public final ObjectId server;
27+
@NotNull public final Date created;
2328
@Nullable public final String title;
29+
public final long host;
2430
@Nullable public final String description;
31+
@Nullable public final Set<Long> roles;
32+
@Nullable public final Set<String> rolesNamed;
33+
@Nullable public final ObjectId server;
34+
@Nullable public final CachedMedia media;
2535
// BUILDER
2636
@Nullable public final String ip;
2737
@Nullable public final String platform;
2838
@Nullable public final String version;
2939
@Nullable public final String prize;
40+
@Nullable public final Integer maxPlayers;
3041
/**
3142
* The time the event starts
3243
*/
3344
@Nullable public final Date time;
34-
// WEBSOCKET
35-
@NotNull public final Set<String> rolesNamed;
45+
@Nullable public final Set<Long> subscribers;
3646

3747
public Event(@NotNull JsonObject json) {
3848
super(json);
49+
id = new ObjectId(json.get("id").getAsString());
50+
type = json.get("type").getAsString();
51+
channel = json.get("channel").getAsLong();
52+
message = json.has("message") ? json.get("message").getAsLong() : null;
53+
controlPanel = json.has("controlPanel") ? json.get("controlPanel").getAsLong() : null;
3954
custom = json.get("custom").getAsBoolean();
40-
server = json.has("server") ? new ObjectId(json.get("server").getAsString()) : null;
41-
host = json.get("host").getAsString();
55+
created = new Date(json.get("created").getAsLong());
4256
title = json.has("title") ? json.get("title").getAsString() : null;
57+
host = json.get("host").getAsLong();
4358
description = json.has("description") ? json.get("description").getAsString() : null;
59+
if (json.has("roles")) {
60+
roles = new HashSet<>();
61+
for (final JsonElement element : json.getAsJsonArray("roles")) roles.add(element.getAsLong());
62+
} else {
63+
roles = null;
64+
}
65+
if (json.has("rolesNamed")) {
66+
rolesNamed = new HashSet<>();
67+
for (final JsonElement element : json.getAsJsonArray("rolesNamed")) rolesNamed.add(element.getAsString());
68+
} else {
69+
rolesNamed = null;
70+
}
71+
server = json.has("server") ? new ObjectId(json.get("server").getAsString()) : null;
72+
media = json.has("media") ? new CachedMedia(json.getAsJsonObject("media")) : null;
4473
ip = json.has("ip") ? json.get("ip").getAsString() : null;
4574
platform = json.has("platform") ? json.get("platform").getAsString() : null;
4675
version = json.has("version") ? json.get("version").getAsString() : null;
4776
prize = json.has("prize") ? json.get("prize").getAsString() : null;
77+
maxPlayers = json.has("maxPlayers") ? json.get("maxPlayers").getAsInt() : null;
4878
time = json.has("time") ? new Date(json.get("time").getAsLong()) : null;
49-
50-
// roles
51-
rolesNamed = new HashSet<>();
52-
if (json.has("rolesNamed")) for (final JsonElement element : json.getAsJsonArray("rolesNamed")) rolesNamed.add(element.getAsString());
79+
if (json.has("subscribers")) {
80+
subscribers = new HashSet<>();
81+
for (final JsonElement element : json.getAsJsonArray("subscribers")) subscribers.add(element.getAsLong());
82+
} else {
83+
subscribers = null;
84+
}
5385
}
5486

5587
@Nullable
@@ -59,11 +91,20 @@ public Long getTimeUntil() {
5991

6092
@NotNull
6193
public Set<PingRole> getPingRoles() {
94+
if (rolesNamed == null) return Set.of();
6295
final Set<PingRole> pingRoles = new HashSet<>();
6396
for (final String role : rolesNamed) {
6497
final PingRole pingRole = EventAlertsIntegration.getEnum(PingRole.class, role);
6598
if (pingRole != null) pingRoles.add(pingRole);
6699
}
67100
return pingRoles;
68101
}
102+
103+
public static class CachedMedia {
104+
@NotNull public final String name;
105+
106+
public CachedMedia(@NotNull JsonObject json) {
107+
this.name = json.get("name").getAsString();
108+
}
109+
}
69110
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package gg.eventalerts.eventalertsintegration.objects;
2+
3+
import com.google.gson.JsonObject;
4+
5+
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
import xyz.srnyx.annoyingapi.libs.javautilities.MiscUtility;
9+
10+
11+
public class EventThreadMessage extends EAObject {
12+
@NotNull private static final String PROP_MESSAGE_ID = "messageId";
13+
@NotNull private static final String PROP_EVENT = "event";
14+
@NotNull private static final String PROP_CHANNEL = "channel";
15+
@NotNull private static final String PROP_AUTHOR = "author";
16+
@NotNull private static final String PROP_CONTENT = "content";
17+
18+
@NotNull public final String messageId;
19+
@NotNull public final Event event;
20+
@NotNull public final Channel channel;
21+
@NotNull public final Author author;
22+
@NotNull public final Content content;
23+
24+
public EventThreadMessage(@NotNull JsonObject json) {
25+
super(json);
26+
this.messageId = json.get(PROP_MESSAGE_ID).getAsString();
27+
this.event = new Event(json.getAsJsonObject(PROP_EVENT));
28+
this.channel = new Channel(json.getAsJsonObject(PROP_CHANNEL));
29+
this.author = new Author(json.getAsJsonObject(PROP_AUTHOR));
30+
this.content = new Content(json.getAsJsonObject(PROP_CONTENT));
31+
}
32+
33+
public static class Channel {
34+
@NotNull private static final String PROP_ID = "id";
35+
@NotNull private static final String PROP_NAME = "name";
36+
37+
@NotNull public final String id;
38+
@NotNull public final String name;
39+
40+
public Channel(@NotNull JsonObject json) {
41+
this.id = json.get(PROP_ID).getAsString();
42+
this.name = json.get(PROP_NAME).getAsString();
43+
}
44+
}
45+
46+
public static class Author {
47+
@NotNull private static final String PROP_ID = "id";
48+
@NotNull private static final String PROP_NAME = "name";
49+
@NotNull private static final String PROP_EFFECTIVE_NAME = "effectiveName";
50+
@NotNull private static final String PROP_PLAYER = "player";
51+
52+
@NotNull public final String id;
53+
@NotNull public final String name;
54+
@NotNull public final String effectiveName;
55+
@Nullable public final EAPlayer player;
56+
57+
public Author(@NotNull JsonObject json) {
58+
this.id = json.get(PROP_ID).getAsString();
59+
this.name = json.get(PROP_NAME).getAsString();
60+
this.effectiveName = json.get(PROP_EFFECTIVE_NAME).getAsString();
61+
this.player = MiscUtility.handleException(() -> new EAPlayer(json.getAsJsonObject(PROP_PLAYER))).orElse(null);
62+
}
63+
}
64+
65+
public static class Content {
66+
@NotNull private static final String PROP_RAW = "raw";
67+
@NotNull private static final String PROP_DISPLAY = "display";
68+
@NotNull private static final String PROP_STRIPPED = "stripped";
69+
70+
@NotNull public final String raw;
71+
@NotNull public final String display;
72+
@NotNull public final String stripped;
73+
74+
public Content(@NotNull JsonObject json) {
75+
this.raw = json.get(PROP_RAW).getAsString();
76+
this.display = json.get(PROP_DISPLAY).getAsString();
77+
this.stripped = json.get(PROP_STRIPPED).getAsString();
78+
}
79+
}
80+
}

src/main/java/gg/eventalerts/eventalertsintegration/socket/SocketClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import xyz.srnyx.annoyingapi.AnnoyingPlugin;
2020

2121
import java.net.URI;
22-
import java.util.Map;
2322
import java.util.logging.Level;
2423

2524

@@ -33,7 +32,7 @@ public abstract class SocketClient<T extends EAObject> extends WebSocketClient {
3332
@Nullable public Runnable toRunOnStop;
3433

3534
public SocketClient(@NotNull EventAlertsIntegration plugin, @NotNull SocketEndpoint endpoint, @NotNull Class<T> objectClass) {
36-
super(URI.create(plugin.getSocketHost() + endpoint.name().toLowerCase()), Map.of("User-Agent", plugin.getUserAgent()));
35+
super(URI.create(plugin.getSocketHost() + endpoint.name().toLowerCase()), plugin.getSocketHeaders());
3736
this.plugin = plugin;
3837
this.endpoint = endpoint;
3938
this.objectClass = objectClass;
@@ -65,15 +64,15 @@ public void retryConnection(@NotNull String reason, @Nullable Integer retryDelay
6564
close(1001, "Retrying connection");
6665

6766
// Schedule retry
68-
if (plugin.config.advanced.websockets.logs) AnnoyingPlugin.log(Level.INFO, "We will try to reconnect to " + endpoint + " in " + finalRetryDelay + " minutes");
67+
if (plugin.config.advanced.websockets.logs) AnnoyingPlugin.log(Level.INFO, "We will try to reconnect to " + endpoint + " in " + finalRetryDelay + " minutes due to: " + reason);
6968
retryTask = new BukkitRunnable() {
7069
@Override
7170
public void run() {
7271
if (plugin.config.advanced.websockets.logs) AnnoyingPlugin.log(Level.INFO, "Retrying websocket connection for " + endpoint + " with reason: " + reason);
7372
retryTask = null;
7473
reconnect();
7574
}
76-
}.runTaskLaterAsynchronously(plugin, finalRetryDelay * 1200);
75+
}.runTaskLaterAsynchronously(plugin, finalRetryDelay * 1200L);
7776
}
7877

7978
@Override

src/main/java/gg/eventalerts/eventalertsintegration/socket/SocketEndpoint.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package gg.eventalerts.eventalertsintegration.socket;
22

33
import gg.eventalerts.eventalertsintegration.EventAlertsIntegration;
4-
import gg.eventalerts.eventalertsintegration.socket.clients.CrossBanClient;
5-
import gg.eventalerts.eventalertsintegration.socket.clients.EventPostedClient;
6-
import gg.eventalerts.eventalertsintegration.socket.clients.FamousEventPostedClient;
7-
import gg.eventalerts.eventalertsintegration.socket.clients.LinkClient;
4+
import gg.eventalerts.eventalertsintegration.socket.clients.*;
85

96
import org.jetbrains.annotations.NotNull;
107
import org.jetbrains.annotations.Nullable;
@@ -19,7 +16,8 @@ public enum SocketEndpoint {
1916
EVENT_POSTED(EventPostedClient.class),
2017
FAMOUS_EVENT_POSTED(FamousEventPostedClient.class),
2118
CROSS_BAN(CrossBanClient.class),
22-
LINK(LinkClient.class);
19+
LINK(LinkClient.class),
20+
EVENT_CHAT(EventChatClient.class);
2321

2422
@Nullable public final Constructor<? extends SocketClient<?>> clientConstructor;
2523

0 commit comments

Comments
 (0)