use of eelfloat.replcraft.exceptions.ApiError in project replcraft by LeeFlemingRepl.
the class SetBlock method execute.
@Override
public void execute(Client client, WsMessageContext ctx, JSONObject request, JSONObject response) throws ApiError {
try {
String blockDataString = request.getString("blockData");
ApiUtil.validateBlockData(blockDataString);
Inventory source = !request.isNull("source_x") ? ApiUtil.getContainer(ApiUtil.getBlock(client, request, "source_x", "source_y", "source_z"), "source") : null;
Inventory destination = !request.isNull("target_x") ? ApiUtil.getContainer(ApiUtil.getBlock(client, request, "target_x", "target_y", "target_z"), "destination") : null;
BlockData blockData = ReplCraft.plugin.getServer().createBlockData(blockDataString);
Material material = ApiUtil.remapBlockMaterialToItemMaterial(blockData.getMaterial());
if (material != Material.AIR && !ReplCraft.plugin.creative_mode) {
ItemStack stack = null;
if (source != null) {
int i = source.first(material);
if (i != -1)
stack = source.getItem(i);
} else {
stack = client.getStructure().findMaterial(material);
}
if (stack == null) {
String message = "No " + material + " available in any attached chests.";
throw new ApiError("invalid operation", message);
}
stack.setAmount(stack.getAmount() - 1);
}
Block target = ApiUtil.getBlock(client, request);
ApiUtil.checkProtectionPlugins(client.getStructure().minecraft_uuid, target.getLocation());
if (ReplCraft.plugin.block_protection) {
// Simulate breaking the block to see if GriefPrevention et al. would deny it
OfflinePlayer offlinePlayer = client.getStructure().getPlayer();
if (!(offlinePlayer instanceof Player)) {
throw ApiError.OFFLINE;
}
BlockBreakEvent evt = new BlockBreakEvent(target, (Player) offlinePlayer);
Bukkit.getPluginManager().callEvent(evt);
if (evt.isCancelled()) {
throw new ApiError("bad request", "block break event was cancelled by another plugin");
}
}
Location location = target.getLocation();
Collection<ItemStack> drops = target.getDrops();
BlockState state = target.getState();
if (state instanceof Container) {
for (ItemStack stack : ((Container) state).getInventory().getContents()) {
if (stack == null)
continue;
drops.add(stack.clone());
stack.setAmount(0);
}
}
target.setBlockData(blockData);
if (ReplCraft.plugin.core_protect) {
String player = client.getStructure().getPlayer().getName();
ReplCraft.plugin.coreProtect.logPlacement(player + " [API]", target.getLocation(), material, blockData);
}
for (ItemStack drop : drops) {
ItemStack leftover = destination != null ? destination.addItem(drop).values().stream().findFirst().orElse(null) : client.getStructure().deposit(drop);
if (leftover != null)
target.getWorld().dropItemNaturally(location, leftover);
}
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
throw new ApiError("bad request", ex.toString());
}
}
use of eelfloat.replcraft.exceptions.ApiError in project replcraft by LeeFlemingRepl.
the class StructureUtil method verifyToken.
/**
* Verifies a token and the associated structure
* @param json_web_token the jwt to verify
* @throws InvalidStructure if the structure is invalid or missing
* @return a validated structure
*/
public static Structure verifyToken(String json_web_token) throws InvalidStructure {
try {
Claims body = Jwts.parserBuilder().setSigningKey(ReplCraft.plugin.key).build().parseClaimsJws(json_web_token).getBody();
String worldName = body.get("world", String.class);
World world = ReplCraft.plugin.getServer().getWorld(worldName);
if (world == null)
throw new InvalidStructure("Invalid world");
int x = body.get("x", Integer.class);
int y = body.get("y", Integer.class);
int z = body.get("z", Integer.class);
String username = body.get("username", String.class);
UUID uuid = UUID.fromString(body.get("uuid", String.class));
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(uuid);
String permission = String.format("replcraft.auth.%s", body.get("permission", String.class));
if (!ReplCraft.plugin.permissionProvider.hasPermission(offlinePlayer, world, permission)) {
String issue = String.format("This token was issued when you held the `%s` permission, but you no longer have it.", permission);
throw new ApiError("unauthenticated", issue);
}
return verifySign(world.getBlockAt(x, y, z), uuid, username::equals);
} catch (JwtException ex) {
throw new InvalidStructure("Token is invalid: " + ex.getMessage(), ex);
} catch (ApiError e) {
throw new InvalidStructure("Token is invalid: " + e.message);
}
}
use of eelfloat.replcraft.exceptions.ApiError in project replcraft by LeeFlemingRepl.
the class Pay method execute.
@Override
public void execute(Client client, WsMessageContext ctx, JSONObject request, JSONObject response) throws InvalidStructure, ApiError {
if (ReplCraft.plugin.economy == null) {
throw new ApiError("bad request", "This command requires Vault to be installed on the server.");
}
String world = client.getStructure().sign.getWorld().getName();
OfflinePlayer sender = client.getStructure().getPlayer();
OfflinePlayer target = ApiUtil.getTargetPlayer(client, request);
double amount = request.getDouble("amount");
EconomyResponse econResponse = ReplCraft.plugin.economy.withdrawPlayer(sender, world, amount);
if (!econResponse.transactionSuccess()) {
throw new ApiError("invalid operation", "transaction failed: " + econResponse.errorMessage);
}
ReplCraft.plugin.economy.depositPlayer(target, world, amount);
}
Aggregations