Skip to content
Open
Show file tree
Hide file tree
Changes from 17 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
14 changes: 14 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

plugins {
alias(libs.plugins.conventions.java)
alias(libs.plugins.shadow)
}

repositories {
Expand Down Expand Up @@ -40,9 +41,22 @@ dependencies {
compileOnly(libs.quickshop) {
exclude("*")
}
implementation(libs.hikaricp) {
exclude(group = "org.slf4j", module = "slf4j-api")
}
}

tasks {
assemble {
dependsOn(shadowJar)
}

shadowJar {
archiveClassifier.set("")

relocate("com.zaxxer.hikari", "net.earthmc.emcapi.libs.hikari")
}

processResources {
val shortCommitId = providers.exec { commandLine("git", "rev-parse", "--short", "HEAD") }.standardOutput.asText.get().trim()
val commitId = providers.exec { commandLine("git", "rev-parse", "HEAD") }.standardOutput.asText.get().trim()
Expand Down
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
8 changes: 7 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ quarters = "e9ed8a133a"
towny = "0.102.0.0"
mysterymaster-api = "1.0.0"
superbvote = "0.6.0"
conventions = "1.0.8"
quickshop = "5.1.2.5-SNAPSHOT"
hikaricp = "7.0.2"

# plugins
conventions = "1.0.8"
shadow = "9.3.0"

[libraries]
discordsrv = { group = "com.discordsrv", name = "discordsrv", version.ref = "discordsrv" }
Expand All @@ -18,6 +22,8 @@ paper = { group = "io.papermc.paper", name = "paper-api", version.ref = "paper"
mysterymaster-api = { group = "net.earthmc.mysterymaster", name = "mysterymaster-api", version.ref = "mysterymaster-api" }
superbvote = { group = "net.earthmc.superbvote", name = "SuperbVote", version.ref = "superbvote" }
quickshop = { group = "org.maxgamer", name = "QuickShop", version.ref = "quickshop" }
hikaricp = { group = "com.zaxxer", name = "HikariCP", version.ref = "hikaricp" }

[plugins]
conventions-java = { id = "net.earthmc.conventions.java", version.ref = "conventions" }
shadow = { id = "com.gradleup.shadow", version.ref = "shadow" }
111 changes: 80 additions & 31 deletions src/main/java/net/earthmc/emcapi/EMCAPI.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package net.earthmc.emcapi;

import com.google.gson.Gson;
import com.zaxxer.hikari.HikariConfig;
import io.javalin.Javalin;
import io.javalin.json.JsonMapper;
import io.javalin.util.JavalinLogger;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import net.earthmc.emcapi.database.APIDatabase;
import net.earthmc.emcapi.database.DatabaseSchema;
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.manager.OptOut;
import net.earthmc.emcapi.sse.SSEManager;
import net.earthmc.emcapi.sse.listeners.ShopSSEListener;
import net.earthmc.emcapi.sse.listeners.TownySSEListener;
import net.earthmc.emcapi.util.EndpointUtils;
import net.earthmc.emcapi.command.ApiCommand;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.eclipse.jetty.server.Connector;
Expand All @@ -21,15 +28,21 @@
import org.eclipse.jetty.servlet.ErrorPageErrorHandler;
import org.eclipse.jetty.webapp.WebAppContext;
import org.jetbrains.annotations.NotNull;
import org.jspecify.annotations.NonNull;

import java.io.IOException;
import java.lang.reflect.Type;
import java.sql.Connection;
import java.sql.SQLException;

public final class EMCAPI extends JavaPlugin {

public static EMCAPI instance;
private Javalin javalin;
private Integrations pluginIntegrations;
private SSEManager sseManager;
private final APIDatabase database = new APIDatabase();
private final OptOut optOut = new OptOut(this);

@Override
public void onLoad() {
Expand All @@ -42,27 +55,20 @@ public void onEnable() {
instance = this;

loadConfig();
loadDatabase();
initialiseJavalin();

this.pluginIntegrations = new Integrations(this);
getServer().getPluginManager().registerEvents(this.pluginIntegrations, this);

EndpointManager endpointManager = new EndpointManager(this);
endpointManager.loadEndpoints();

PluginCommand apiCommand = getCommand("api");
if (apiCommand == null) {
getLogger().warning("API command not found.");
} else {
ApiCommand cmd = new ApiCommand();
apiCommand.setExecutor(cmd);
apiCommand.setTabCompleter(cmd);
}
try {
EndpointUtils.loadOptOut(getDataFolder().toPath());
} catch (IOException e) {
getLogger().warning("IOException while loading opted-out players: " + e);
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();

this.getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS, event -> event.registrar().register(ApiCommand.create(this)));

optOut.loadOptOut();

sseManager = new SSEManager(this);
sseManager.loadSSE();
Expand All @@ -73,27 +79,19 @@ public void onEnable() {
if (pm.isPluginEnabled("QuickShop")) {
pm.registerEvents(new ShopSSEListener(sseManager), this);
}

try {
EndpointUtils.loadApiKeys(getDataFolder().toPath());
} catch (IOException e) {
getSLF4JLogger().warn("IOException while loading API keys: ", e);
KeyManager.loadApiKeys(this);
} catch (SQLException e) {
getSLF4JLogger().warn("exception while loading API keys: ", e);
}
}

@Override
public void onDisable() {
javalin.stop();
try {
EndpointUtils.saveOptOut(getDataFolder().toPath());
} catch (IOException e) {
getLogger().warning("IOException while saving opted-out players: " + e);
}
sseManager.shutdown();
try {
EndpointUtils.saveApiKeys(getDataFolder().toPath());
} catch (IOException e) {
getSLF4JLogger().warn("IOException while saving API keys: ", e);
}
javalin.stop();
database.close();
}

private void initialiseJavalin() {
Expand All @@ -108,6 +106,19 @@ private void initialiseJavalin() {

server.setHandler(context);
});

final Gson gson = new Gson();
config.jsonMapper(new JsonMapper() {
@Override
public @NonNull String toJsonString(@NonNull Object obj, @NonNull Type type) {
return gson.toJson(obj, type);
}

@Override
public @NonNull <T> T fromJsonString(@NonNull String json, @NonNull Type targetType) {
return gson.fromJson(json, targetType);
}
});
});

javalin.start(getConfig().getString("networking.host"), getConfig().getInt("networking.port"));
Expand All @@ -118,6 +129,36 @@ private void loadConfig() {
reloadConfig();
}

private void loadDatabase() {
final HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:mysql://" + getConfig().getString("database.host") + ":" + getConfig().getString("database.port") + "/" + getConfig().getString("database.name") + getConfig().getString("database.flags"));
final String username = getConfig().getString("database.username");
final String password = getConfig().getString("database.password");

config.setUsername(username);
config.setPassword(password);

config.setMaximumPoolSize(Math.min(1, getConfig().getInt("database.max-pool-size", 1)));
config.setMinimumIdle(Math.min(0, getConfig().getInt("database.min-pool-size", 0)));
config.setPoolName("EMCAPI");

try {
database.start(config);

try (final Connection connection = database.getConnection()) {
DatabaseSchema.createTables(connection);
} catch (SQLException e) {
getSLF4JLogger().warn("Failed to create default tables", e);
}
} catch (SQLException e) {
if (!"root".equals(username) || !"".equals(password)) {
getSLF4JLogger().warn("Failed to start datasource", e);
}
} catch (ReflectiveOperationException e) {
getSLF4JLogger().warn("Failed to find embedded sql driver", e);
}
}

private void disableServerVersionHeader(final Server server) {
for (Connector conn : server.getConnectors()) {
conn.getConnectionFactories().stream()
Expand Down Expand Up @@ -151,4 +192,12 @@ public String getURLPath() {
String version = getConfig().getString("networking.api_version", "3");
return "v" + version + "/" + getConfig().getString("networking.url_path");
}

public APIDatabase getDatabase() {
return database;
}

public OptOut getOptOut() {
return optOut;
}
}
Loading
Loading