From 30f329a32df74770182df1b95af05be70c5c1659 Mon Sep 17 00:00:00 2001 From: Etzelia Date: Fri, 7 Aug 2020 16:38:17 -0500 Subject: [PATCH] Add normal time to Worlds and use full Ban Entries (+graphql) Signed-off-by: Etzelia --- .../java/xyz/etztech/serverapi/ServerAPI.java | 14 ++-- .../xyz/etztech/serverapi/web/GraphQL.java | 10 +-- .../xyz/etztech/serverapi/web/IProvider.java | 7 +- .../xyz/etztech/serverapi/web/api/BanAPI.java | 65 +++++++++++++++++++ .../etztech/serverapi/web/api/WorldAPI.java | 16 ++++- .../xyz/etztech/serverapi/MockProvider.java | 22 +++---- 6 files changed, 105 insertions(+), 29 deletions(-) create mode 100644 src/main/java/xyz/etztech/serverapi/web/api/BanAPI.java diff --git a/src/main/java/xyz/etztech/serverapi/ServerAPI.java b/src/main/java/xyz/etztech/serverapi/ServerAPI.java index 8317ea9..4d871e2 100644 --- a/src/main/java/xyz/etztech/serverapi/ServerAPI.java +++ b/src/main/java/xyz/etztech/serverapi/ServerAPI.java @@ -1,6 +1,8 @@ package xyz.etztech.serverapi; +import org.bukkit.BanEntry; +import org.bukkit.BanList; import org.bukkit.OfflinePlayer; import org.bukkit.World; import org.bukkit.entity.Player; @@ -78,7 +80,7 @@ public class ServerAPI extends JavaPlugin implements IProvider { return WorldAPI.fromMinecraft(world); } } - return new WorldAPI("unknown", 0, WorldAPI.WEATHER_UNKNOWN); + return new WorldAPI("unknown", 0, 0, WorldAPI.WEATHER_UNKNOWN); } @Override @@ -100,12 +102,12 @@ public class ServerAPI extends JavaPlugin implements IProvider { } @Override - public Set bans() { - Set players = new HashSet<>(); - for (OfflinePlayer player : getServer().getBannedPlayers()) { - players.add(PlayerAPI.fromMinecraft(player)); + public Set bans() { + Set bans = new HashSet<>(); + for (BanEntry entry : getServer().getBanList(BanList.Type.NAME).getBanEntries()) { + bans.add(BanAPI.fromMinecraft(entry)); } - return players; + return bans; } @Override diff --git a/src/main/java/xyz/etztech/serverapi/web/GraphQL.java b/src/main/java/xyz/etztech/serverapi/web/GraphQL.java index 87a25b5..42f5ca7 100644 --- a/src/main/java/xyz/etztech/serverapi/web/GraphQL.java +++ b/src/main/java/xyz/etztech/serverapi/web/GraphQL.java @@ -2,10 +2,7 @@ package xyz.etztech.serverapi.web; import com.expediagroup.graphql.annotations.GraphQLName; import io.javalin.plugin.graphql.graphql.QueryGraphql; -import xyz.etztech.serverapi.web.api.PlayerAPI; -import xyz.etztech.serverapi.web.api.QueryAPI; -import xyz.etztech.serverapi.web.api.TPSAPI; -import xyz.etztech.serverapi.web.api.WorldAPI; +import xyz.etztech.serverapi.web.api.*; public class GraphQL implements QueryGraphql { private final IProvider provider; @@ -14,6 +11,11 @@ public class GraphQL implements QueryGraphql { this.provider = provider; } + @GraphQLName("bans") + public BanAPI[] getBans() { + return provider.bans().toArray(new BanAPI[0]); + } + @GraphQLName("players") public PlayerAPI[] getPlayers() { return provider.players().toArray(new PlayerAPI[0]); diff --git a/src/main/java/xyz/etztech/serverapi/web/IProvider.java b/src/main/java/xyz/etztech/serverapi/web/IProvider.java index 1dbbcc0..cc353d1 100644 --- a/src/main/java/xyz/etztech/serverapi/web/IProvider.java +++ b/src/main/java/xyz/etztech/serverapi/web/IProvider.java @@ -1,15 +1,12 @@ package xyz.etztech.serverapi.web; -import xyz.etztech.serverapi.web.api.PlayerAPI; -import xyz.etztech.serverapi.web.api.QueryAPI; -import xyz.etztech.serverapi.web.api.TPSAPI; -import xyz.etztech.serverapi.web.api.WorldAPI; +import xyz.etztech.serverapi.web.api.*; import java.util.List; import java.util.Set; public interface IProvider { - Set bans(); + Set bans(); Set players(); QueryAPI query(); TPSAPI TPS(); diff --git a/src/main/java/xyz/etztech/serverapi/web/api/BanAPI.java b/src/main/java/xyz/etztech/serverapi/web/api/BanAPI.java new file mode 100644 index 0000000..c1736c3 --- /dev/null +++ b/src/main/java/xyz/etztech/serverapi/web/api/BanAPI.java @@ -0,0 +1,65 @@ +package xyz.etztech.serverapi.web.api; + +import com.expediagroup.graphql.annotations.GraphQLDescription; +import com.expediagroup.graphql.annotations.GraphQLDirective; +import com.expediagroup.graphql.annotations.GraphQLName; +import org.bukkit.BanEntry; + +import java.util.Date; + +@GraphQLName("Ban") +@GraphQLDescription("Ban GraphQL") +public class BanAPI { + private final String target; + private final String source; + private final String reason; + private final long created; + private final long expiration; + + public BanAPI(String target, String source, String reason, long created, long expiration) { + this.target = target; + this.source = source; + this.reason = reason; + this.created = created; + this.expiration = expiration; + } + + @GraphQLName("target") + public String getTarget() { + return target; + } + + @GraphQLName("source") + public String getSource() { + return source; + } + + @GraphQLName("reason") + public String getReason() { + return reason; + } + + @GraphQLName("created") + public long getCreated() { + return created; + } + + @GraphQLName("expiration") + public long getExpiration() { + return expiration; + } + + public static BanAPI fromMinecraft(BanEntry entry) { + long expiration = -1; + if (entry.getExpiration() != null) { + expiration = entry.getExpiration().getTime(); + } + return new BanAPI( + entry.getTarget(), + entry.getSource(), + entry.getReason(), + entry.getCreated().getTime(), + expiration + ); + } +} diff --git a/src/main/java/xyz/etztech/serverapi/web/api/WorldAPI.java b/src/main/java/xyz/etztech/serverapi/web/api/WorldAPI.java index 46dd5bf..813315c 100644 --- a/src/main/java/xyz/etztech/serverapi/web/api/WorldAPI.java +++ b/src/main/java/xyz/etztech/serverapi/web/api/WorldAPI.java @@ -2,6 +2,8 @@ package xyz.etztech.serverapi.web.api; import com.expediagroup.graphql.annotations.GraphQLDescription; import com.expediagroup.graphql.annotations.GraphQLName; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonNaming; import org.bukkit.World; @GraphQLName("World") @@ -14,11 +16,14 @@ public class WorldAPI { private final String name; private final long time; + @JsonProperty("full_time") + private final long fullTime; private final int weather; - public WorldAPI(String name, long time, int weather) { + public WorldAPI(String name, long time, long fullTime, int weather) { this.name = name; this.time = time; + this.fullTime = fullTime; this.weather = weather; } @@ -28,10 +33,17 @@ public class WorldAPI { } @GraphQLName("time") + @GraphQLDescription("Hours * 1000") public long getTime() { return time; } + @GraphQLName("full_time") + @GraphQLDescription("Hours * 1000") + public long getFullTime() { + return fullTime; + } + @GraphQLName("weather") public int getWeather() { return weather; @@ -45,6 +57,6 @@ public class WorldAPI { state = WEATHER_THUNDER; } } - return new WorldAPI(world.getName(), world.getFullTime(), state); + return new WorldAPI(world.getName(), world.getTime(), world.getFullTime(), state); } } diff --git a/src/test/java/xyz/etztech/serverapi/MockProvider.java b/src/test/java/xyz/etztech/serverapi/MockProvider.java index 3cac058..5051acc 100644 --- a/src/test/java/xyz/etztech/serverapi/MockProvider.java +++ b/src/test/java/xyz/etztech/serverapi/MockProvider.java @@ -1,10 +1,7 @@ package xyz.etztech.serverapi; import xyz.etztech.serverapi.web.IProvider; -import xyz.etztech.serverapi.web.api.PlayerAPI; -import xyz.etztech.serverapi.web.api.QueryAPI; -import xyz.etztech.serverapi.web.api.TPSAPI; -import xyz.etztech.serverapi.web.api.WorldAPI; +import xyz.etztech.serverapi.web.api.*; import java.util.*; @@ -13,9 +10,9 @@ public class MockProvider implements IProvider { public MockProvider() { worlds = Arrays.asList( - new WorldAPI("overworld", 1000, WorldAPI.WEATHER_CLEAR), - new WorldAPI("nether", 1500, WorldAPI.WEATHER_STORM), - new WorldAPI("end", 2000, WorldAPI.WEATHER_THUNDER) + new WorldAPI("overworld", 1000, 10000, WorldAPI.WEATHER_CLEAR), + new WorldAPI("nether", 1500, 15000, WorldAPI.WEATHER_STORM), + new WorldAPI("end", 2000, 20000, WorldAPI.WEATHER_THUNDER) ); } @@ -31,7 +28,7 @@ public class MockProvider implements IProvider { return mock; } } - return new WorldAPI("unknown", 0, WorldAPI.WEATHER_CLEAR); + return new WorldAPI("unknown", 0, 0, WorldAPI.WEATHER_CLEAR); } @Override @@ -40,11 +37,12 @@ public class MockProvider implements IProvider { } @Override - public Set bans() { + public Set bans() { + long now = new Date().getTime(); return new HashSet<>(Arrays.asList( - new PlayerAPI("Badzelia", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171"), - new PlayerAPI("LessThanZeroSD", "fe7e8413-2570-4588-9203-2b69ff188bc3"), - new PlayerAPI("Vakbuttzel", "7afbf663-2bf0-49ef-915f-22e81b298d17") + new BanAPI("Etzelia", "Console", "Reasons", now, now), + new BanAPI("Zero", "Notch", "lol lmao", now, now), + new BanAPI("Vak", "Server", "brr", now, now) )); }