Search in sources :

Example 16 with VirtualCrate

use of com.codehusky.huskycrates.crate.VirtualCrate in project HuskyCrates-Sponge by codeHusky.

the class WithdrawKey method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    if (commandContext.getOne("type").isPresent()) {
        if (!(commandSource instanceof Player)) {
            commandSource.sendMessage(Text.of("You need to be in game or specify a player for this command to work."));
            return CommandResult.empty();
        }
        Player player = (Player) commandSource;
        String type = commandContext.<String>getOne("type").get();
        VirtualCrate virtualCrate = HuskyCrates.instance.getCrateUtilities().getVirtualCrate(type);
        int quantity = commandContext.getOne("quantity").isPresent() ? commandContext.<Integer>getOne("quantity").get() : 1;
        if (virtualCrate == null) {
            commandSource.sendMessage(Text.of("Invalid crate id: " + type + ". Try using tab auto completion."));
            return CommandResult.empty();
        }
        int balance = virtualCrate.getVirtualKeyBalance(player);
        if (balance >= quantity && quantity > 0) {
            ItemStack key = virtualCrate.getCrateKey(quantity);
            virtualCrate.takeVirtualKeys(player, quantity);
            player.getInventory().offer(key);
            commandSource.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.getLangData().formatter(virtualCrate.getLangData().withdrawSuccess, null, player, virtualCrate, null, null, quantity)));
        } else {
            if (quantity <= 0) {
                player.sendMessage(Text.of(TextColors.RED, "Positive integer amounts only."));
            } else {
                commandSource.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.getLangData().formatter(virtualCrate.getLangData().withdrawInsufficient, null, player, virtualCrate, null, null, quantity)));
            }
        }
    } else {
        commandSource.sendMessage(Text.of("Usage: /crate withdraw <id> [count]"));
    }
    return CommandResult.success();
}
Also used : VirtualCrate(com.codehusky.huskycrates.crate.VirtualCrate) Player(org.spongepowered.api.entity.living.player.Player) ItemStack(org.spongepowered.api.item.inventory.ItemStack)

Aggregations

VirtualCrate (com.codehusky.huskycrates.crate.VirtualCrate)16 Player (org.spongepowered.api.entity.living.player.Player)11 ItemStack (org.spongepowered.api.item.inventory.ItemStack)8 PhysicalCrate (com.codehusky.huskycrates.crate.PhysicalCrate)4 InventoryTransactionResult (org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)4 World (org.spongepowered.api.world.World)4 HashMap (java.util.HashMap)3 Listener (org.spongepowered.api.event.Listener)3 Location (org.spongepowered.api.world.Location)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 UUID (java.util.UUID)2 User (org.spongepowered.api.entity.living.player.User)2 IOException (java.io.IOException)1 CommentedConfigurationNode (ninja.leaping.configurate.commented.CommentedConfigurationNode)1 JSONObject (org.json.JSONObject)1 CommandSource (org.spongepowered.api.command.CommandSource)1 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)1 Task (org.spongepowered.api.scheduler.Task)1 Text (org.spongepowered.api.text.Text)1