Search in sources :

Example 11 with ApiError

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());
    }
}
Also used : Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) Material(org.bukkit.Material) Container(org.bukkit.block.Container) BlockState(org.bukkit.block.BlockState) Block(org.bukkit.block.Block) OfflinePlayer(org.bukkit.OfflinePlayer) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) ApiError(eelfloat.replcraft.exceptions.ApiError) BlockData(org.bukkit.block.data.BlockData) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory) Location(org.bukkit.Location)

Example 12 with ApiError

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);
    }
}
Also used : InvalidStructure(eelfloat.replcraft.exceptions.InvalidStructure) Claims(io.jsonwebtoken.Claims) JwtException(io.jsonwebtoken.JwtException) ApiError(eelfloat.replcraft.exceptions.ApiError)

Example 13 with ApiError

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);
}
Also used : OfflinePlayer(org.bukkit.OfflinePlayer) EconomyResponse(net.milkbowl.vault.economy.EconomyResponse) ApiError(eelfloat.replcraft.exceptions.ApiError)

Aggregations

ApiError (eelfloat.replcraft.exceptions.ApiError)13 Block (org.bukkit.block.Block)5 JSONObject (org.json.JSONObject)5 BlockState (org.bukkit.block.BlockState)4 OfflinePlayer (org.bukkit.OfflinePlayer)3 Player (org.bukkit.entity.Player)3 ItemStack (org.bukkit.inventory.ItemStack)3 JSONArray (org.json.JSONArray)3 InvalidStructure (eelfloat.replcraft.exceptions.InvalidStructure)2 Location (org.bukkit.Location)2 Container (org.bukkit.block.Container)2 Sign (org.bukkit.block.Sign)2 Inventory (org.bukkit.inventory.Inventory)2 JSONException (org.json.JSONException)2 ApiUtil.getBlock (eelfloat.replcraft.util.ApiUtil.getBlock)1 Claims (io.jsonwebtoken.Claims)1 JwtException (io.jsonwebtoken.JwtException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 EconomyResponse (net.milkbowl.vault.economy.EconomyResponse)1