Skip to content

Commit

Permalink
Add additional endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Pequla committed Feb 13, 2023
1 parent 7d7df44 commit dd5cfda
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# LittleWeb
Spigot plugin that exposes a REST API for developers to consume

Requires plugin [LittleLink](https://github.com/Pequla/LittleLink/releases/latest)
> Requires plugin [LittleLink](https://github.com/Pequla/LittleLink/releases/latest)
## Configuration

Expand Down Expand Up @@ -217,6 +217,12 @@ Sample response:

> When there are no players online the `list` will be returned as an empty array

There are also additional endpoints for targeting specific fields:

- GET `/api/status/players` (Returns only the `players` object)
- GET `/api/status/plugins` (Returns only the `plugins` array)
- GET `/api/status/world` (Returns only the `world` object)

### GET `/api/user`

> The `uuid` query parameter is mandatory. So the example url should be `/api/user?uuid=69c154a4-0a6a-4578-b169-c4be379e1e8e`
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.pequla.link</groupId>
<groupId>com.pequla</groupId>
<artifactId>little-web</artifactId>
<version>1.3</version>
<version>1.4</version>
<packaging>jar</packaging>

<name>LittleWeb</name>
Expand Down Expand Up @@ -79,7 +79,7 @@
<dependency>
<groupId>com.pequla</groupId>
<artifactId>little-link</artifactId>
<version>1.7</version>
<version>1.8</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
83 changes: 47 additions & 36 deletions src/main/java/com/pequla/link/LittleWeb.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pequla.link.model.*;
import com.pequla.link.service.DataService;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginManager;
Expand Down Expand Up @@ -52,41 +51,17 @@ public void onEnable() {
.name(p.getName())
.build()).collect(Collectors.toList()))));

Spark.get("/api/status", (request, response) -> {
HashSet<PlayerData> list = new HashSet<>();
getServer().getOnlinePlayers().forEach(player -> list.add(PlayerData.builder()
.id(player.getUniqueId().toString())
.name(player.getName())
.build())
);
List<PluginData> plugins = Arrays.stream(manager.getPlugins())
.map(p -> {
PluginDescriptionFile description = p.getDescription();
return PluginData.builder()
.name(p.getName())
.website(p.getDescription().getWebsite())
.description(description.getDescription())
.version(description.getVersion())
.authors(description.getAuthors())
.build();
}).collect(Collectors.toList());

World world = getServer().getWorlds().get(0);
return mapper.writeValueAsString(ServerStatus.builder()
.players(PlayerStatus.builder()
.online(list.size())
.max(getServer().getMaxPlayers())
.list(list)
.build())
.plugins(plugins)
.world(WorldData.builder()
.seed(String.valueOf(world.getSeed()))
.time(world.getTime())
.type(getServer().getWorldType())
.build())
.version(getServer().getVersion())
.build());
});
Spark.get("/api/status", (request, response) ->
mapper.writeValueAsString(ServerStatus.builder()
.players(getPlayerStatus())
.plugins(getPluginData(manager))
.world(getWorldData())
.version(getServer().getVersion())
.build()));

Spark.get("/api/status/players", (request, response) -> mapper.writeValueAsString(getPlayerStatus()));
Spark.get("/api/status/plugins", (request, response) -> mapper.writeValueAsString(getPluginData(manager)));
Spark.get("/api/status/world", (request, response) -> mapper.writeValueAsString(getWorldData()));

Spark.get("/api/user", (request, response) -> {
String uuid = request.queryParams("uuid");
Expand Down Expand Up @@ -128,6 +103,42 @@ public void onDisable() {
Spark.stop();
}

public PlayerStatus getPlayerStatus() {
HashSet<PlayerData> list = new HashSet<>();
getServer().getOnlinePlayers().forEach(player -> list.add(PlayerData.builder()
.id(player.getUniqueId().toString())
.name(player.getName())
.build())
);
return PlayerStatus.builder()
.online(list.size())
.max(getServer().getMaxPlayers())
.list(list)
.build();
}

public List<PluginData> getPluginData(PluginManager manager) {
return Arrays.stream(manager.getPlugins()).map(p -> {
PluginDescriptionFile description = p.getDescription();
return PluginData.builder()
.name(p.getName())
.website(p.getDescription().getWebsite())
.description(description.getDescription())
.version(description.getVersion())
.authors(description.getAuthors())
.build();
}).collect(Collectors.toList());
}

public WorldData getWorldData() {
World world = getServer().getWorlds().get(0);
return WorldData.builder()
.seed(String.valueOf(world.getSeed()))
.time(world.getTime())
.type(getServer().getWorldType())
.build();
}

public static String generateError(String error) throws JsonProcessingException {
ObjectMapper mapper = DataService.getInstance().getMapper();
return mapper.writeValueAsString(WebError.builder()
Expand Down

0 comments on commit dd5cfda

Please sign in to comment.