Skip to content

Commit

Permalink
Merge branch 'master' of github.com:phybros/servertap
Browse files Browse the repository at this point in the history
  • Loading branch information
phybros committed May 19, 2020
2 parents 4c712a8 + 14b5e54 commit 76223ac
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
7 changes: 5 additions & 2 deletions src/main/java/io/servertap/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ public class Constants {

public static final String API_V1 = "v1";

//Economy Related Messages
// Economy Related Messages
public static final String VAULT_MISSING = "Vault not found. Related functionality disabled";
public static final String VAULT_MISSING_PAY_PARAMS = "Missing uuid and/or amount";
public static final String VAULT_GREATER_THAN_ZERO = "You must use a value greater than zero";

//Player Related Messages
// Player Related Messages
public static final String PLAYER_MISSING_PARAMS = "Missing uuid and/or world";
public static final String PLAYER_UUID_MISSING = "Player UUID is required";
public static final String PLAYER_INV_PARSE_FAIL = "A problem occured when attempting to parse the user file";
public static final String PLAYER_NOT_FOUND = "Player cannot be found";

// World messages
public static final String WORLD_NOT_FOUND = "World cannot be found";
}
2 changes: 1 addition & 1 deletion src/main/java/io/servertap/PluginEntrypoint.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void onEnable() {
get("players", PlayerApi::playersGet);
get("players/all", PlayerApi::offlinePlayersGet);
get("players/:uuid", PlayerApi::playerGet);
get("players/:uuid/:world/inventory", PlayerApi::getPlayerInv);
get("players/:playerUuid/:worldUuid/inventory", PlayerApi::getPlayerInv);

// Whitelist routes
get("whitelist", ServerApi::whitelistGet);
Expand Down
99 changes: 59 additions & 40 deletions src/main/java/io/servertap/api/v1/PlayerApi.java
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
package io.servertap.api.v1;

import io.javalin.http.BadRequestResponse;
import io.javalin.http.Context;
import de.tr7zw.nbtapi.NBTFile;
import de.tr7zw.nbtapi.NBTListCompound;
import io.javalin.http.*;
import io.javalin.plugin.openapi.annotations.*;
import io.javalin.http.InternalServerErrorResponse;
import io.javalin.http.NotFoundResponse;
import io.servertap.Constants;
import io.servertap.PluginEntrypoint;
import io.servertap.api.v1.models.ItemStack;
import io.servertap.api.v1.models.Player;

import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.InventoryHolder;

import de.tr7zw.nbtapi.NBTEntity;
import de.tr7zw.nbtapi.NBTFile;
import de.tr7zw.nbtapi.NBTListCompound;
import de.tr7zw.nbtapi.data.PlayerData;
import de.tr7zw.nbtapi.plugin.NBTAPI;
import org.bukkit.World;

import java.io.File;
import java.nio.file.Paths;
Expand All @@ -28,12 +20,12 @@
public class PlayerApi {

@OpenApi(
path = "/v1/players",
summary = "Gets all currently online players",
tags = {"Player"},
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = Player.class, isArray = true))
}
path = "/v1/players",
summary = "Gets all currently online players",
tags = {"Player"},
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = Player.class, isArray = true))
}
)
public static void playersGet(Context ctx) {
ArrayList<Player> players = new ArrayList<>();
Expand Down Expand Up @@ -64,16 +56,16 @@ public static void playersGet(Context ctx) {
}

@OpenApi(
path = "/v1/players/:uuid",
method = HttpMethod.GET,
summary = "Gets a specific online player by their UUID",
tags = {"Player"},
pathParams = {
@OpenApiParam(name = "uuid", description = "UUID of the player")
},
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = Player.class))
}
path = "/v1/players/:uuid",
method = HttpMethod.GET,
summary = "Gets a specific online player by their UUID",
tags = {"Player"},
pathParams = {
@OpenApiParam(name = "uuid", description = "UUID of the player")
},
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = Player.class))
}
)
public static void playerGet(Context ctx) {
Player p = new Player();
Expand Down Expand Up @@ -109,12 +101,12 @@ public static void playerGet(Context ctx) {
}

@OpenApi(
path = "/v1/players/all",
summary = "Gets all players that have ever joined the server ",
tags = {"Player"},
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = io.servertap.api.v1.models.OfflinePlayer.class, isArray = true))
}
path = "/v1/players/all",
summary = "Gets all players that have ever joined the server ",
tags = {"Player"},
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = io.servertap.api.v1.models.OfflinePlayer.class, isArray = true))
}
)
public static void offlinePlayersGet(Context ctx) {

Expand Down Expand Up @@ -143,13 +135,25 @@ public static void offlinePlayersGet(Context ctx) {
ctx.json(players);
}

@OpenApi(
path = "/v1/players/:playerUuid/:worldUuid/inventory",
method = HttpMethod.GET,
summary = "Gets a specific online player's Inventory in the specified world",
tags = {"Player"},
pathParams = {
@OpenApiParam(name = "playerUuid", description = "UUID of the player"),
@OpenApiParam(name = "worldUuid", description = "UUID of the world")
},
responses = {
@OpenApiResponse(status = "200", content = @OpenApiContent(from = io.servertap.api.v1.models.ItemStack.class, isArray = true))
}
)
public static void getPlayerInv(Context ctx) {
if (ctx.pathParam("uuid") == null || ctx.pathParam("world") == null) {
// TODO: Move to Constants
if (ctx.pathParam("playerUuid").isEmpty() || ctx.pathParam("worldUuid").isEmpty()) {
throw new InternalServerErrorResponse(Constants.PLAYER_MISSING_PARAMS);
}
ArrayList<ItemStack> inv = new ArrayList<ItemStack>();
org.bukkit.entity.Player player = Bukkit.getPlayer(UUID.fromString(ctx.pathParam("uuid")));
org.bukkit.entity.Player player = Bukkit.getPlayer(UUID.fromString(ctx.pathParam("playerUuid")));
if (player != null) {
player.updateInventory();
Integer location = -1;
Expand All @@ -167,9 +171,20 @@ public static void getPlayerInv(Context ctx) {
ctx.json(inv);
} else {
try {
String playerDatPath = Paths.get(new File("./").getAbsolutePath(), ctx.formParam("world"), "playerdata", ctx.formParam("uuid") + ".dat").toString();
File playerfile = new File(playerDatPath);
if(!playerfile.exists()){
World bukWorld = Bukkit.getWorld(UUID.fromString(ctx.pathParam("worldUuid")));

if (bukWorld == null) {
throw new BadRequestResponse(Constants.WORLD_NOT_FOUND);
}

String dataPath = String.format(
"%s/%s/playerdata/%s.dat",
new File("./").getAbsolutePath(),
bukWorld.getName(),
ctx.pathParam("playerUuid")
);
File playerfile = new File(Paths.get(dataPath).toString());
if (!playerfile.exists()) {
throw new InternalServerErrorResponse(Constants.PLAYER_NOT_FOUND);
}
NBTFile playerFile = new NBTFile(playerfile);
Expand All @@ -183,6 +198,10 @@ public static void getPlayerInv(Context ctx) {
}

ctx.json(inv);

} catch (HttpResponseException e) {
// Pass any javalin exceptions up the chain
throw e;
} catch (Exception e) {
Bukkit.getLogger().warning(e.getMessage());
throw new InternalServerErrorResponse(Constants.PLAYER_INV_PARSE_FAIL);
Expand Down

0 comments on commit 76223ac

Please sign in to comment.