Search in sources :

Example 11 with VirtualCrate

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

the class DepositKey method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    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 plr = (Player) commandSource;
    if (!plr.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        commandSource.sendMessage(Text.of("You must be holding an item to deposit a key."));
        return CommandResult.empty();
    }
    ItemStack key = plr.getItemInHand(HandTypes.MAIN_HAND).get();
    if (HuskyCrates.instance.crateUtilities.vcFromKey(key) == null) {
        commandSource.sendMessage(Text.of(TextColors.RED, "Not a valid key."));
        return CommandResult.empty();
    }
    VirtualCrate virtualCrate = HuskyCrates.instance.crateUtilities.vcFromKey(plr.getItemInHand(HandTypes.MAIN_HAND).get());
    int keyCount = key.getQuantity();
    plr.setItemInHand(HandTypes.MAIN_HAND, null);
    virtualCrate.giveVirtualKeys(plr, keyCount);
    // commandSource.sendMessage(Text.of(TextColors.GREEN,"Successfully deposited " + keyCount + " ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName),TextColors.GREEN," Key(s)."));
    commandSource.sendMessage(TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.getLangData().formatter(virtualCrate.getLangData().depositSuccess, null, plr, virtualCrate, null, null, keyCount)));
    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)

Example 12 with VirtualCrate

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

the class Key method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    if (commandContext.getOne("type").isPresent()) {
        String type = commandContext.<String>getOne("type").get();
        Optional<Player> player = commandContext.getOne("player");
        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 + ". Please check your config."));
            return CommandResult.empty();
        }
        if (!player.isPresent()) {
            commandSource.sendMessage(Text.of("You need to be in game or specify a player for this command to work."));
            return CommandResult.empty();
        }
        ItemStack keyItemStack = virtualCrate.getCrateKey(quantity);
        InventoryTransactionResult.Type mainInventory = player.get().getInventory().offer(keyItemStack.copy()).getType();
        if (!mainInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
            InventoryTransactionResult.Type enderInventory = player.get().getEnderChestInventory().offer(keyItemStack.copy()).getType();
            if (!enderInventory.equals(InventoryTransactionResult.Type.SUCCESS)) {
                commandSource.sendMessage(Text.of("Couldn't give key to " + player.get().getName() + " because of a full inventory and enderchest"));
                HuskyCrates.instance.logger.info("Couldn't give key to " + player.get().getName() + " because of a full inventory and enderchest");
            } else {
                if (player.isPresent()) {
                    player.get().sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " key(s), but some have been placed in your Ender Chest."));
                } else {
                    commandSource.sendMessage(Text.of("You have been given 1 or more ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), " key(s), but some have been placed in your Ender Chest."));
                }
            }
        }
    } else {
        commandSource.sendMessage(Text.of("Usage: /crate key <id> [player] [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) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult)

Example 13 with VirtualCrate

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

the class KeyBal method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    Optional<User> user = commandContext.getOne("player");
    if (!user.isPresent()) {
        commandSource.sendMessage(Text.of("You need to be in game or specify a player for this command to work."));
        return CommandResult.empty();
    }
    if (commandSource.hasPermission("huskycrates.keybal.others") && user.get() != commandSource) {
        commandSource.sendMessage(Text.of(TextColors.GREEN, user.get().getName() + "'s Key Balance"));
    } else {
        commandSource.sendMessage(Text.of(TextColors.GREEN, "Your Key Balance"));
    }
    boolean atleastOne = false;
    for (String vcid : HuskyCrates.instance.crateUtilities.getCrateTypes()) {
        VirtualCrate vc = HuskyCrates.instance.crateUtilities.getVirtualCrate(vcid);
        int keys = vc.getVirtualKeyBalance(user.get());
        if (keys > 0) {
            atleastOne = true;
            commandSource.sendMessage(Text.of("  ", TextSerializers.FORMATTING_CODE.deserialize(vc.displayName), ": " + keys + " (id: " + vc.id + ") "));
        }
    }
    if (!atleastOne) {
        if (commandSource.hasPermission("huskycrates.keybal.others") && user.get() != commandSource) {
            commandSource.sendMessage(Text.of(TextColors.GRAY, TextStyles.ITALIC, user.get().getName() + " currently has no virtual keys."));
        } else {
            commandSource.sendMessage(Text.of(TextColors.GRAY, TextStyles.ITALIC, "You currently have no virtual keys."));
        }
    }
    return CommandResult.success();
}
Also used : VirtualCrate(com.codehusky.huskycrates.crate.VirtualCrate) User(org.spongepowered.api.entity.living.player.User)

Example 14 with VirtualCrate

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

the class DBReader method saveHuskyData.

public static void saveHuskyData() throws SQLException {
    /*
            CRATELOCATIONS (ID INTEGER NOT NULL AUTO_INCREMENT, X DOUBLE, Y DOUBLE, Z DOUBLE, worldID INTEGER, isEntityCrate BOOLEAN, crateID CHARACTER
            VALIDKEYS (keyUUID CHARACTER, crateID CHARACTER, amount INTEGER
            KEYBALANCES (userUUID CHARACTER, crateID CHARACTER, amount INTEGER
            WORLDINFO (ID INTEGER NOT NULL AUTO_INCREMENT,uuid CHARACTER, name CHARACTER
        */
    connectDB();
    // crate positions
    HashMap<UUID, Integer> worldsInserted = new HashMap<>();
    ArrayList<Location<World>> locationSurvey = new ArrayList<>();
    locationSurvey.addAll(HuskyCrates.instance.crateUtilities.physicalCrates.keySet());
    locationSurvey.addAll(HuskyCrates.instance.crateUtilities.brokenCrates);
    for (Location<World> location : locationSurvey) {
        if (!worldsInserted.keySet().contains(location.getExtent().getUniqueId())) {
            PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM WORLDINFO WHERE name = ? OR uuid = ?");
            statement.setString(1, location.getExtent().getName());
            statement.setString(2, location.getExtent().getUniqueId().toString());
            ResultSet results = statement.executeQuery();
            boolean exists = results.next();
            if (exists) {
                worldsInserted.put(location.getExtent().getUniqueId(), results.getInt(1));
            } else {
                dbConnection.prepareStatement("INSERT INTO WORLDINFO(uuid,name) VALUES('" + location.getExtent().getUniqueId().toString() + "','" + location.getExtent().getName() + "')").executeUpdate();
                PreparedStatement fetchPrim = dbConnection.prepareStatement("SELECT * FROM WORLDINFO WHERE name = ? OR uuid = ?");
                fetchPrim.setString(1, location.getExtent().getName());
                fetchPrim.setString(2, location.getExtent().getUniqueId().toString());
                ResultSet primResults = fetchPrim.executeQuery();
                primResults.next();
                worldsInserted.put(location.getExtent().getUniqueId(), primResults.getInt(1));
            }
        }
        if (!HuskyCrates.instance.crateUtilities.brokenCrates.contains(location)) {
            PhysicalCrate crate = HuskyCrates.instance.crateUtilities.physicalCrates.get(location);
            PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM CRATELOCATIONS WHERE worldID = ? AND X = ? AND Y = ? AND Z = ? AND isEntityCrate = ?");
            statement.setInt(1, worldsInserted.get(location.getExtent().getUniqueId()));
            statement.setDouble(2, location.getX());
            statement.setDouble(3, location.getY());
            statement.setDouble(4, location.getZ());
            statement.setBoolean(5, crate.isEntity);
            ResultSet results = statement.executeQuery();
            boolean exists = results.next();
            if (!exists) {
                dbConnection.prepareStatement("INSERT INTO CRATELOCATIONS(X,Y,Z,worldID,isEntityCrate,crateID) VALUES(" + location.getX() + "," + location.getY() + "," + location.getZ() + "," + worldsInserted.get(location.getExtent().getUniqueId()) + "," + crate.isEntity + ",'" + crate.vc.id + "')").executeUpdate();
            }
        }
    }
    for (Location<World> broken : HuskyCrates.instance.crateUtilities.brokenCrates) {
        PreparedStatement delState = dbConnection.prepareStatement("DELETE FROM CRATELOCATIONS WHERE worldID = ? AND X = ? AND Y = ? AND Z = ?");
        delState.setInt(1, worldsInserted.get(broken.getExtent().getUniqueId()));
        delState.setDouble(2, broken.getX());
        delState.setDouble(3, broken.getY());
        delState.setDouble(4, broken.getZ());
        delState.executeUpdate();
    }
    HuskyCrates.instance.crateUtilities.brokenCrates.clear();
    for (VirtualCrate crate : HuskyCrates.instance.crateUtilities.crateTypes.values()) {
        for (String keyUUID : crate.pendingKeys.keySet()) {
            int amount = crate.pendingKeys.get(keyUUID);
            PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM VALIDKEYS WHERE keyUUID = ?");
            statement.setString(1, keyUUID);
            ResultSet results = statement.executeQuery();
            boolean exists = results.next();
            if (exists) {
                PreparedStatement uState = dbConnection.prepareStatement("UPDATE VALIDKEYS SET amount = ? WHERE keyUUID = ?");
                uState.setInt(1, amount);
                uState.setString(2, keyUUID);
                uState.executeUpdate();
            } else {
                dbConnection.prepareStatement("INSERT INTO VALIDKEYS(keyUUID,crateID,amount) VALUES('" + keyUUID + "','" + crate.id + "'," + amount + ")").executeUpdate();
            }
        // System.out.println(g);
        }
        for (UUID uuid : crate.lastUsed.keySet()) {
            PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM LASTUSED WHERE crateID = ? AND userUUID = ?");
            statement.setString(1, crate.id);
            statement.setString(2, uuid.toString());
            ResultSet results = statement.executeQuery();
            long epoch = crate.lastUsed.get(uuid).atZone(ZoneId.systemDefault()).toEpochSecond();
            if (results.next()) {
                // exists
                PreparedStatement crState = dbConnection.prepareStatement("UPDATE LASTUSED SET lastUsed = ? WHERE userUUID = ? AND crateID = ?");
                crState.setString(2, uuid.toString());
                crState.setString(3, crate.id);
                crState.setLong(1, epoch);
                crState.executeUpdate();
            } else {
                PreparedStatement crState = dbConnection.prepareStatement("INSERT INTO LASTUSED(userUUID,crateID,lastUsed) VALUES(?,?,?)");
                crState.setString(1, uuid.toString());
                crState.setString(2, crate.id);
                crState.setLong(3, epoch);
                crState.executeUpdate();
            }
        }
    }
    for (String vcID : HuskyCrates.instance.crateUtilities.getCrateTypes()) {
        VirtualCrate vc = HuskyCrates.instance.crateUtilities.getVirtualCrate(vcID);
        String crateID = vc.id;
        for (String uuid : vc.virtualBalances.keySet()) {
            int amount = vc.virtualBalances.get(uuid);
            PreparedStatement statement = dbConnection.prepareStatement("SELECT * FROM KEYBALANCES WHERE userUUID = ? AND crateID = ?");
            statement.setString(1, uuid);
            statement.setString(2, crateID);
            ResultSet results = statement.executeQuery();
            boolean exists = results.next();
            if (exists) {
                PreparedStatement uState = dbConnection.prepareStatement("UPDATE KEYBALANCES SET amount = ? WHERE userUUID = ? AND crateID = ?");
                uState.setInt(1, amount);
                uState.setString(2, uuid);
                uState.setString(3, crateID);
                uState.executeUpdate();
            } else {
                dbConnection.prepareStatement("INSERT INTO KEYBALANCES(userUUID,crateID,amount) VALUES('" + uuid + "','" + crateID + "'," + amount + ")").executeUpdate();
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) PhysicalCrate(com.codehusky.huskycrates.crate.PhysicalCrate) World(org.spongepowered.api.world.World) VirtualCrate(com.codehusky.huskycrates.crate.VirtualCrate) UUID(java.util.UUID) Location(org.spongepowered.api.world.Location)

Example 15 with VirtualCrate

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

the class VirtualKeyAll method execute.

@Override
public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
    if (commandContext.getOne("type").isPresent()) {
        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 + ". Please check your config."));
            return CommandResult.empty();
        }
        commandSource.sendMessage(Text.of("Gave everyone " + quantity + " vkeys."));
        virtualCrate.givePlayersVirtualKeys(Sponge.getServer().getOnlinePlayers(), quantity);
        for (Player e : Sponge.getServer().getOnlinePlayers()) {
            if (commandSource != e) {
                e.sendMessage(Text.of(TextColors.GREEN, "You received " + quantity + " virtual keys for a ", TextSerializers.FORMATTING_CODE.deserialize(virtualCrate.displayName), "."));
            }
        }
    } else {
        commandSource.sendMessage(Text.of("Usage: /crate vkeyall <id> [count]"));
    }
    return CommandResult.success();
}
Also used : VirtualCrate(com.codehusky.huskycrates.crate.VirtualCrate) Player(org.spongepowered.api.entity.living.player.Player)

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