Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group = net.earthmc.emcapi
version = 3.1.0-SNAPSHOT
version = 4.0.0-SNAPSHOT
description = EMCAPI

org.gradle.configuration-cache=true
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/net/earthmc/emcapi/EMCAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import jakarta.servlet.http.HttpServletResponse;
import net.earthmc.emcapi.integration.Integrations;
import net.earthmc.emcapi.manager.EndpointManager;
import net.earthmc.emcapi.manager.KeyManager;
import net.earthmc.emcapi.manager.LegacyEndpointManager;
import net.earthmc.emcapi.sse.SSEManager;
import net.earthmc.emcapi.sse.listeners.ShopSSEListener;
import net.earthmc.emcapi.sse.listeners.TownySSEListener;
Expand Down Expand Up @@ -47,8 +49,10 @@ public void onEnable() {
this.pluginIntegrations = new Integrations(this);
getServer().getPluginManager().registerEvents(this.pluginIntegrations, this);

EndpointManager endpointManager = new EndpointManager(this);
endpointManager.loadEndpoints();
if (getConfig().getBoolean("behavior.load_legacy")) {
new LegacyEndpointManager(this).loadEndpoints(); // Load retired endpoints and still serve current endpoints at /v3/aurora/
}
new EndpointManager(this).loadEndpoints();

PluginCommand apiCommand = getCommand("api");
if (apiCommand == null) {
Expand All @@ -74,7 +78,7 @@ public void onEnable() {
pm.registerEvents(new ShopSSEListener(sseManager), this);
}
try {
EndpointUtils.loadApiKeys(getDataFolder().toPath());
KeyManager.loadApiKeys(getDataFolder().toPath());
} catch (IOException e) {
getSLF4JLogger().warn("IOException while loading API keys: ", e);
}
Expand All @@ -90,7 +94,7 @@ public void onDisable() {
}
sseManager.shutdown();
try {
EndpointUtils.saveApiKeys(getDataFolder().toPath());
KeyManager.saveApiKeys(getDataFolder().toPath());
} catch (IOException e) {
getSLF4JLogger().warn("IOException while saving API keys: ", e);
}
Expand Down
15 changes: 8 additions & 7 deletions src/main/java/net/earthmc/emcapi/command/ApiCommand.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.earthmc.emcapi.command;

import net.earthmc.emcapi.manager.KeyManager;
import net.earthmc.emcapi.sse.SSEManager;
import net.earthmc.emcapi.util.EndpointUtils;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -77,9 +78,9 @@ private void handleKey(Player player, String[] args) {
}

UUID playerID = player.getUniqueId();
UUID key;
String key;
if (args.length == 1) {
key = EndpointUtils.getPlayerKey(playerID);
key = KeyManager.getPlayerKey(playerID);
if (key != null) {
player.sendMessage(Component.text("Click to copy your API key.", NamedTextColor.GREEN).clickEvent(ClickEvent.copyToClipboard(key.toString())));
} else {
Expand All @@ -90,25 +91,25 @@ private void handleKey(Player player, String[] args) {
String action = args[1].toLowerCase();
switch (action) {
case "create" -> {
if (EndpointUtils.getPlayerKey(playerID) != null) {
if (KeyManager.getPlayerKey(playerID) != null) {
player.sendMessage(Component.text("You already have an API key! Use /api key to get it.", NamedTextColor.RED));
} else {
key = EndpointUtils.createApiKey(playerID);
key = KeyManager.createApiKey(playerID);
player.sendMessage(Component.text("Key created! Click to copy.", NamedTextColor.GREEN).clickEvent(ClickEvent.copyToClipboard(key.toString())));
}
}
case "delete" -> {
key = EndpointUtils.getPlayerKey(playerID);
key = KeyManager.getPlayerKey(playerID);
if (key != null) {
SSEManager.deleteKey(key);
EndpointUtils.deletePlayerKey(playerID);
KeyManager.deletePlayerKey(playerID);
player.sendMessage(Component.text("Successfully deleted your API key", NamedTextColor.GREEN));
} else {
player.sendMessage(Component.text("You do not have an API key.", NamedTextColor.RED));
}
}
case "copy" -> {
key = EndpointUtils.getPlayerKey(playerID);
key = KeyManager.getPlayerKey(playerID);
if (key != null) {
player.sendMessage(Component.text("Click to copy your API key.", NamedTextColor.GREEN).clickEvent(ClickEvent.copyToClipboard(key.toString())));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.earthmc.emcapi.util.EndpointUtils;
import net.earthmc.emcapi.util.HttpExceptions;
import net.earthmc.emcapi.util.JSONUtil;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.regex.Matcher;
Expand All @@ -21,7 +22,7 @@ public class DiscordEndpoint extends PostEndpoint<DiscordContext> {
private static final BadRequestResponse INVALID_TYPE_TARGET = new BadRequestResponse("Your JSON query has an invalid type or target");

@Override
public DiscordContext getObjectOrNull(JsonElement element) {
public DiscordContext getObjectOrNull(JsonElement element, @Nullable String key) {
JsonObject jsonObject = JSONUtil.getJsonElementAsJsonObjectOrNull(element);
if (jsonObject == null) {
throw HttpExceptions.NOT_A_JSON_OBJECT;
Expand Down Expand Up @@ -60,7 +61,7 @@ public DiscordContext getObjectOrNull(JsonElement element) {
}

@Override
public JsonElement getJsonElement(DiscordContext context) {
public JsonElement getJsonElement(DiscordContext context, @Nullable String key) {
DiscordType type = context.getType();
String target = context.getTarget();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
import net.earthmc.emcapi.util.JSONUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;

public class LocationEndpoint extends PostEndpoint<Pair<Integer, Integer>> {

@Override
public Pair<Integer, Integer> getObjectOrNull(JsonElement element) {
public Pair<Integer, Integer> getObjectOrNull(JsonElement element, @Nullable String key) {
JsonArray jsonArray = JSONUtil.getJsonElementAsJsonArrayOrNull(element);
if (jsonArray == null) throw new BadRequestResponse("Your query contains a value that is not a JSON array");

Expand All @@ -40,7 +41,7 @@ public Pair<Integer, Integer> getObjectOrNull(JsonElement element) {
}

@Override
public JsonElement getJsonElement(Pair<Integer, Integer> pair) {
public JsonElement getJsonElement(Pair<Integer, Integer> pair, @Nullable String key) {
int x = pair.getFirst();
int z = pair.getSecond();

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/earthmc/emcapi/endpoint/NearbyEndpoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@
import net.earthmc.emcapi.util.JSONUtil;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class NearbyEndpoint extends PostEndpoint<NearbyContext> {

@Override
public NearbyContext getObjectOrNull(JsonElement element) {
public NearbyContext getObjectOrNull(JsonElement element, @Nullable String key) {
JsonObject jsonObject = JSONUtil.getJsonElementAsJsonObjectOrNull(element);
if (jsonObject == null) throw new BadRequestResponse("Your query contains a value that is not a JSON object");

Expand Down Expand Up @@ -64,7 +65,7 @@ public NearbyContext getObjectOrNull(JsonElement element) {
}

@Override
public JsonElement getJsonElement(NearbyContext context) {
public JsonElement getJsonElement(NearbyContext context, @Nullable String key) {
NearbyType targetType = context.getTargetType();
int radius = context.getRadius();
switch (targetType) {
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/net/earthmc/emcapi/endpoint/ShopEndpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package net.earthmc.emcapi.endpoint;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import io.javalin.http.BadRequestResponse;
import net.earthmc.emcapi.EMCAPI;
import net.earthmc.emcapi.integration.QuickShopIntegration;
import net.earthmc.emcapi.manager.KeyManager;
import net.earthmc.emcapi.object.endpoint.PostEndpoint;
import net.earthmc.emcapi.util.EndpointUtils;
import net.earthmc.emcapi.util.JSONUtil;
import org.jetbrains.annotations.Nullable;
import org.maxgamer.quickshop.api.shop.Shop;

import java.util.List;
import java.util.UUID;

public class ShopEndpoint extends PostEndpoint<List<Shop>> {
private final QuickShopIntegration integration;

public ShopEndpoint(EMCAPI plugin) {
this.integration = plugin.integrations().quickShopIntegration();
}

@Override
public List<Shop> getObjectOrNull(JsonElement element, @Nullable String key) {
String string = JSONUtil.getJsonElementAsStringOrNull(element);
if (string == null) throw new BadRequestResponse("Your query contains a value that is not a string");

UUID player;
try {
player = UUID.fromString(string);
} catch (IllegalArgumentException ignored) {
return null;
}

integration.throwIfDisabled();
return integration.getPlayerShops(player, key);
}

@Override
public JsonElement getJsonElement(List<Shop> object, @Nullable String key) {
JsonObject shopsObject = new JsonObject();
int counter = 1;
UUID keyOwner = KeyManager.getKeyOwner(key);
for (Shop shop : object) {
if (!shop.getOwner().equals(keyOwner)) continue;
shopsObject.add(String.valueOf(counter++), EndpointUtils.getShopObject(shop));
}

return shopsObject;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.earthmc.emcapi.endpoint;
package net.earthmc.emcapi.endpoint.legacy;

import com.google.gson.JsonObject;
import net.earthmc.emcapi.object.endpoint.GetEndpoint;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.earthmc.emcapi.endpoint;
package net.earthmc.emcapi.endpoint.legacy;

public class MudkipEndpoint {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.earthmc.emcapi.endpoint;
package net.earthmc.emcapi.endpoint.legacy;

import com.google.gson.JsonObject;
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
Expand All @@ -16,7 +16,6 @@
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class PlayerStatsEndpoint {
private final EMCAPI plugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
import net.earthmc.emcapi.object.endpoint.PostEndpoint;
import net.earthmc.emcapi.util.EndpointUtils;
import net.earthmc.emcapi.util.JSONUtil;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.UUID;

public class NationsEndpoint extends PostEndpoint<Nation> {

@Override
public Nation getObjectOrNull(JsonElement element) {
public Nation getObjectOrNull(JsonElement element, @Nullable String key) {
String string = JSONUtil.getJsonElementAsStringOrNull(element);
if (string == null) throw new BadRequestResponse("Your query contains a value that is not a string");

Expand All @@ -34,7 +35,7 @@ public Nation getObjectOrNull(JsonElement element) {
}

@Override
public JsonElement getJsonElement(Nation nation) {
public JsonElement getJsonElement(Nation nation, @Nullable String key) {
JsonObject nationObject = new JsonObject();

nationObject.addProperty("name", nation.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@
import com.palmergames.bukkit.towny.TownyEconomyHandler;
import com.palmergames.bukkit.towny.object.Resident;
import io.javalin.http.BadRequestResponse;
import net.earthmc.emcapi.manager.KeyManager;
import net.earthmc.emcapi.object.endpoint.PostEndpoint;
import net.earthmc.emcapi.util.EndpointUtils;
import net.earthmc.emcapi.util.JSONUtil;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.UUID;

public class PlayersEndpoint extends PostEndpoint<Resident> {

@Override
public Resident getObjectOrNull(JsonElement element) {
public Resident getObjectOrNull(JsonElement element, @Nullable String key) {
String string = JSONUtil.getJsonElementAsStringOrNull(element);
if (string == null) throw new BadRequestResponse("Your query contains a value that is not a string");

Expand All @@ -28,15 +30,17 @@ public Resident getObjectOrNull(JsonElement element) {
resident = TownyAPI.getInstance().getResident(string);
}

if (resident != null && EndpointUtils.playerOptedOut(resident.getUUID())) {
if (resident != null && EndpointUtils.playerOptedOut(resident.getUUID())
&& !resident.getUUID().equals(KeyManager.getKeyOwner(key))
) {
return null;
}

return resident;
}

@Override
public JsonElement getJsonElement(Resident resident) {
public JsonElement getJsonElement(Resident resident, @Nullable String key) {
JsonObject playerObject = new JsonObject();

playerObject.addProperty("name", resident.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import net.earthmc.emcapi.util.EndpointUtils;
import net.earthmc.emcapi.util.JSONUtil;
import org.bukkit.Location;
import org.jetbrains.annotations.Nullable;

import java.awt.*;
import java.util.UUID;

public class QuartersEndpoint extends PostEndpoint<Quarter> {

@Override
public Quarter getObjectOrNull(JsonElement element) {
public Quarter getObjectOrNull(JsonElement element, @Nullable String key) {
String string = JSONUtil.getJsonElementAsStringOrNull(element);
if (string == null) throw new BadRequestResponse("Your query contains a value that is not a string");

Expand All @@ -33,7 +34,7 @@ public Quarter getObjectOrNull(JsonElement element) {
}

@Override
public JsonElement getJsonElement(Quarter quarter) {
public JsonElement getJsonElement(Quarter quarter, @Nullable String key) {
JsonObject quarterObject = new JsonObject();

quarterObject.addProperty("name", quarter.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.earthmc.emcapi.object.endpoint.PostEndpoint;
import net.earthmc.emcapi.util.EndpointUtils;
import net.earthmc.emcapi.util.JSONUtil;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;

Expand All @@ -26,7 +27,7 @@ public TownsEndpoint(EMCAPI plugin) {
}

@Override
public Town getObjectOrNull(JsonElement element) {
public Town getObjectOrNull(JsonElement element, @Nullable String key) {
String string = JSONUtil.getJsonElementAsStringOrNull(element);
if (string == null) throw new BadRequestResponse("Your query contains a value that is not a string");

Expand All @@ -41,7 +42,7 @@ public Town getObjectOrNull(JsonElement element) {
}

@Override
public JsonElement getJsonElement(Town town) {
public JsonElement getJsonElement(Town town, @Nullable String key) {
JsonObject townObject = new JsonObject();

townObject.addProperty("name", town.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Integrations implements Listener {
private final QuartersIntegration quartersIntegration;
private final SuperbVoteIntegration superbVoteIntegration;
private final MysteryMasterIntegration mysteryMasterIntegration;
private final QuickShopIntegration quickShopIntegration;

public Integrations(final EMCAPI plugin) {
this.plugin = plugin;
Expand All @@ -25,6 +26,7 @@ public Integrations(final EMCAPI plugin) {
this.quartersIntegration = addIntegration(new QuartersIntegration());
this.superbVoteIntegration = addIntegration(new SuperbVoteIntegration());
this.mysteryMasterIntegration = addIntegration(new MysteryMasterIntegration());
this.quickShopIntegration = addIntegration(new QuickShopIntegration());
}

private <T extends Integration> T addIntegration(final T integration) {
Expand All @@ -50,6 +52,10 @@ public MysteryMasterIntegration mysteryMasterIntegration() {
return this.mysteryMasterIntegration;
}

public QuickShopIntegration quickShopIntegration() {
return quickShopIntegration;
}

@EventHandler
public void onPluginEnable(final PluginEnableEvent event) {
final Integration integration = integrations.get(event.getPlugin().getName());
Expand Down
Loading