Change QueryAPI to PingAPI and add PluginAPI (#4)

typo

Fix null fields

Signed-off-by: Etzelia <etzelia@hotmail.com>

Change QueryAPI to PingAPI and add PluginAPI

Signed-off-by: Etzelia <etzelia@hotmail.com>

Reviewed-on: https://git.etztech.xyz/Minecraft/ServerAPI/pulls/4
Reviewed-by: ZeroHD <joey@ahines.net>
post
Etzelia 2020-08-12 02:43:42 +02:00
parent 6f094c6dd4
commit 055672468d
12 changed files with 127 additions and 28 deletions

View File

@ -3,7 +3,7 @@
<groupId>xyz.etztech</groupId> <groupId>xyz.etztech</groupId>
<artifactId>ServerAPI</artifactId> <artifactId>ServerAPI</artifactId>
<!-- Version is used in plugin.yml --> <!-- Version is used in plugin.yml -->
<version>0.0.1</version> <version>0.0.2</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<!-- Plugin Information --> <!-- Plugin Information -->

View File

@ -3,9 +3,9 @@ package xyz.etztech.serverapi;
import org.bukkit.BanEntry; import org.bukkit.BanEntry;
import org.bukkit.BanList; import org.bukkit.BanList;
import org.bukkit.OfflinePlayer;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import xyz.etztech.serverapi.commands.MainCommand; import xyz.etztech.serverapi.commands.MainCommand;
import xyz.etztech.serverapi.tps.TPS; import xyz.etztech.serverapi.tps.TPS;
@ -13,10 +13,7 @@ import xyz.etztech.serverapi.web.IProvider;
import xyz.etztech.serverapi.web.Web; import xyz.etztech.serverapi.web.Web;
import xyz.etztech.serverapi.web.api.*; import xyz.etztech.serverapi.web.api.*;
import java.util.ArrayList; import java.util.*;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger; import java.util.logging.Logger;
public class ServerAPI extends JavaPlugin implements IProvider { public class ServerAPI extends JavaPlugin implements IProvider {
@ -111,8 +108,17 @@ public class ServerAPI extends JavaPlugin implements IProvider {
} }
@Override @Override
public QueryAPI query() { public PingAPI ping() {
return QueryAPI.fromMinecraft(getServer()); return PingAPI.fromMinecraft(getServer());
}
@Override
public List<PluginAPI> plugins() {
List<PluginAPI> plugins = new ArrayList<>();
for (Plugin plugin : getServer().getPluginManager().getPlugins()) {
plugins.add(PluginAPI.fromMinecraft(plugin));
}
return plugins;
} }
} }

View File

@ -21,9 +21,14 @@ public class GraphQL implements QueryGraphql {
return provider.players().toArray(new PlayerAPI[0]); return provider.players().toArray(new PlayerAPI[0]);
} }
@GraphQLName("query") @GraphQLName("ping")
public QueryAPI getQuery() { public PingAPI getPing() {
return provider.query(); return provider.ping();
}
@GraphQLName("plugins")
public PluginAPI[] getPlugins() {
return provider.plugins().toArray(new PluginAPI[0]);
} }
@GraphQLName("world") @GraphQLName("world")

View File

@ -8,10 +8,11 @@ import java.util.Set;
public interface IProvider { public interface IProvider {
Set<BanAPI> bans(); Set<BanAPI> bans();
Set<PlayerAPI> players(); Set<PlayerAPI> players();
QueryAPI query(); PingAPI ping();
TPSAPI TPS(); TPSAPI TPS();
List<WorldAPI> worlds(); List<WorldAPI> worlds();
WorldAPI world(String name); WorldAPI world(String name);
List<PluginAPI> plugins();
void log(String message); void log(String message);
} }

View File

@ -0,0 +1,22 @@
# Adding a new endpoint
1. Create a new `<endpoint>API` class in [api](api).
* Make sure to correctly add the appropriate JSON and GraphQL annotations.
Look at other classes for examples.
* **NOTE:** If anything returned could be null, make sure to instead provide an appropriate zero-value property,
otherwise GraphQL will choke.
See [PluginAPI::new](api/PluginAPI.java) for an example.
2. Add a method to return the needed data to [IProvider](IProvider.java).
3. Add a new REST endpoint to the [Web::start](Web.java) method.
4. Add a new GraphQL method to [GraphQL](GraphQL.java)
* Make sure to correctly add the appropriate GraphQL annotation.
5. Modify both [ServerAPI](../ServerAPI.java)
and [MockProvider](../../../../../../test/java/xyz/etztech/serverapi/MockProvider.java)
to fulfill the [IProvider](IProvider.java) interface.
# Testing
[ServerRunner](../../../../../../test/java/xyz/etztech/serverapi/ServerRunner.java) should start up the API using
[MockProvider](../../../../../../test/java/xyz/etztech/serverapi/MockProvider.java).
If possible, a real test on a running Minecraft server would ideal!

View File

@ -36,9 +36,12 @@ public class Web {
app.before(this::access); app.before(this::access);
} }
// REST Endpoints
// For GraphQL endpoints, see GraphQL.java
app.get("/bans", this::bans); app.get("/bans", this::bans);
app.get("/players", this::players); app.get("/players", this::players);
app.get("/query", this::query); app.get("/plugins", this::plugins);
app.get("/ping", this::ping);
app.get("/tps", this::tps); app.get("/tps", this::tps);
app.get("/worlds", this::worlds); app.get("/worlds", this::worlds);
app.get("/worlds/:name", this::world); app.get("/worlds/:name", this::world);
@ -107,11 +110,15 @@ public class Web {
ctx.json(provider.players()); ctx.json(provider.players());
} }
public void plugins(Context ctx) {
ctx.json(provider.plugins());
}
public void bans(Context ctx) { public void bans(Context ctx) {
ctx.json(provider.bans()); ctx.json(provider.bans());
} }
public void query(Context ctx) { public void ping(Context ctx) {
ctx.json(provider.query()); ctx.json(provider.ping());
} }
} }

View File

@ -1,12 +1,9 @@
package xyz.etztech.serverapi.web.api; package xyz.etztech.serverapi.web.api;
import com.expediagroup.graphql.annotations.GraphQLDescription; import com.expediagroup.graphql.annotations.GraphQLDescription;
import com.expediagroup.graphql.annotations.GraphQLDirective;
import com.expediagroup.graphql.annotations.GraphQLName; import com.expediagroup.graphql.annotations.GraphQLName;
import org.bukkit.BanEntry; import org.bukkit.BanEntry;
import java.util.Date;
@GraphQLName("Ban") @GraphQLName("Ban")
@GraphQLDescription("Ban GraphQL") @GraphQLDescription("Ban GraphQL")
public class BanAPI { public class BanAPI {

View File

@ -5,9 +5,9 @@ import com.expediagroup.graphql.annotations.GraphQLName;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import org.bukkit.Server; import org.bukkit.Server;
@GraphQLName("MCQuery") @GraphQLName("Ping")
@GraphQLDescription("MCQuery GraphQL") @GraphQLDescription("Ping GraphQL")
public class QueryAPI { public class PingAPI {
private final String type; private final String type;
private final String version; private final String version;
private final String motd; private final String motd;
@ -16,7 +16,7 @@ public class QueryAPI {
@JsonProperty("max_players") @JsonProperty("max_players")
private final int maxPlayers; private final int maxPlayers;
public QueryAPI(String type, String version, String motd, int currentPlayers, int maxPlayers) { public PingAPI(String type, String version, String motd, int currentPlayers, int maxPlayers) {
this.type = type; this.type = type;
this.version = version; this.version = version;
this.motd = motd; this.motd = motd;
@ -49,8 +49,8 @@ public class QueryAPI {
return maxPlayers; return maxPlayers;
} }
public static QueryAPI fromMinecraft(Server server) { public static PingAPI fromMinecraft(Server server) {
return new QueryAPI( return new PingAPI(
server.getName(), server.getName(),
server.getBukkitVersion().split("-")[0], // 1.x.x-R0.1-SNAPSHOT server.getBukkitVersion().split("-")[0], // 1.x.x-R0.1-SNAPSHOT
server.getMotd(), server.getMotd(),

View File

@ -0,0 +1,54 @@
package xyz.etztech.serverapi.web.api;
import com.expediagroup.graphql.annotations.GraphQLDescription;
import com.expediagroup.graphql.annotations.GraphQLName;
import org.bukkit.plugin.Plugin;
import java.util.ArrayList;
import java.util.List;
@GraphQLName("Plugin")
@GraphQLDescription("Plugin GraphQL")
public class PluginAPI {
private final String name;
private final String version;
private final List<String> authors;
private final String website;
public PluginAPI(String name, String version, List<String> authors, String website) {
this.name = name;
this.version = version;
this.authors = authors != null ? authors : new ArrayList<>();
this.website = website != null ? website : "";
}
@GraphQLName("name")
public String getName() {
return name;
}
@GraphQLName("version")
public String getVersion() {
return version;
}
@GraphQLName("authors")
public List<String> getAuthors() {
return authors;
}
@GraphQLName("website")
public String getWebsite() {
return website;
}
public static PluginAPI fromMinecraft(Plugin plugin) {
return new PluginAPI(
plugin.getName(),
plugin.getDescription().getVersion(),
plugin.getDescription().getAuthors(),
plugin.getDescription().getWebsite()
);
}
}

View File

@ -3,7 +3,6 @@ package xyz.etztech.serverapi.web.api;
import com.expediagroup.graphql.annotations.GraphQLDescription; import com.expediagroup.graphql.annotations.GraphQLDescription;
import com.expediagroup.graphql.annotations.GraphQLName; import com.expediagroup.graphql.annotations.GraphQLName;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import org.bukkit.World; import org.bukkit.World;
@GraphQLName("World") @GraphQLName("World")

View File

@ -56,8 +56,17 @@ public class MockProvider implements IProvider {
} }
@Override @Override
public QueryAPI query() { public PingAPI ping() {
return new QueryAPI("Mock", "0.0.1", "Hello, world!", 0, 100); return new PingAPI("Mock", "0.0.1", "Hello, world!", 0, 100);
}
@Override
public List<PluginAPI> plugins() {
return Arrays.asList(
new PluginAPI("ServerAPI", "0.0.1", Collections.singletonList("Etzelia"), "https://git.etztech.xyz"),
new PluginAPI("dynmap", "0.1.0", null, "https://www.spigotmc.org/resources/dynmap.274/"),
new PluginAPI("CoreProtect", "1.0.0", Collections.singletonList("Intelli"), null)
);
} }
@Override @Override

View File

@ -6,7 +6,6 @@ public class ServerRunner {
public static void main(String[] args) { public static void main(String[] args) {
Web web = new Web(new MockProvider()); Web web = new Web(new MockProvider());
web.start(8080, ""); web.start(8080, "");
} }
} }