Search in sources :

Example 1 with ChestData

use of com.bgsoftware.wildchests.api.objects.data.ChestData in project WildChests by BG-Software-LLC.

the class InventoryListener method handleUpgrade.

private void handleUpgrade(Player player, boolean confirm) {
    try {
        Chest chest = WChest.viewers.get(player.getUniqueId());
        ChestData chestData = chest.getData();
        InventoryData inventoryData = buyNewPage.get(player.getUniqueId());
        int pageIndex = chest.getPagesAmount() - 1;
        if (!chestData.getPagesData().containsKey(pageIndex + 2)) {
            Locale.EXPAND_FAILED.send(player);
        } else {
            if (confirm) {
                if (plugin.getProviders().withdrawPlayer(player, inventoryData.getPrice())) {
                    Locale.EXPAND_PURCHASED.send(player);
                    chest.setPage(++pageIndex, chestData.getDefaultSize(), inventoryData.getTitle());
                } else {
                    Locale.EXPAND_FAILED.send(player);
                }
            } else {
                Locale.EXPAND_FAILED.send(player);
            }
        }
        final int PAGE = pageIndex;
        Executor.sync(() -> chest.openPage(player, PAGE));
    } catch (Exception ex) {
        Locale.EXPAND_FAILED_CHEST_BROKEN.send(player);
    }
    buyNewPage.remove(player.getUniqueId());
}
Also used : WChest(com.bgsoftware.wildchests.objects.chests.WChest) Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) InventoryData(com.bgsoftware.wildchests.api.objects.data.InventoryData)

Example 2 with ChestData

use of com.bgsoftware.wildchests.api.objects.data.ChestData in project WildChests by BG-Software-LLC.

the class CommandGive method perform.

@Override
public void perform(WildChestsPlugin plugin, CommandSender sender, String[] args) {
    Player target = Bukkit.getPlayer(args[1]);
    if (target == null) {
        Locale.INVALID_PLAYER.send(sender, args[1]);
        return;
    }
    ChestData chestData = plugin.getChestsManager().getChestData(args[2]);
    if (chestData == null) {
        Locale.INVALID_CHEST.send(sender, args[2]);
        return;
    }
    ItemStack chestItem = chestData.getItemStack();
    if (args.length == 4) {
        try {
            chestItem.setAmount(Integer.valueOf(args[3]));
        } catch (IllegalArgumentException ex) {
            Locale.INVALID_AMOUNT.send(sender);
            return;
        }
    }
    ItemUtils.addItem(chestItem, target.getInventory(), target.getLocation());
    Locale.CHEST_GIVE_PLAYER.send(sender, target.getName(), chestItem.getAmount(), chestData.getName(), chestItem.getItemMeta().getDisplayName());
    Locale.CHEST_RECIEVE.send(target, chestItem.getAmount(), chestData.getName(), sender.getName(), chestItem.getItemMeta().getDisplayName());
}
Also used : Player(org.bukkit.entity.Player) ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with ChestData

use of com.bgsoftware.wildchests.api.objects.data.ChestData in project WildChests by BG-Software-LLC.

the class WChest method onInteract.

@Override
public boolean onInteract(InventoryClickEvent event) {
    if (event.getSlotType() != InventoryType.SlotType.OUTSIDE)
        return false;
    ChestData chestData = getData();
    int index = getPageIndex(event.getWhoClicked().getOpenInventory().getTopInventory());
    if (event.getClick() == ClickType.LEFT) {
        // Making sure he's not in the first page
        if (index != 0) {
            // movingBetweenPages.add(event.getWhoClicked().getUniqueId());
            openPage((Player) event.getWhoClicked(), index - 1);
        // movingBetweenPages.remove(event.getWhoClicked().getUniqueId());
        }
    } else if (event.getClick() == ClickType.RIGHT) {
        // Making sure it's not the last page
        if (index + 1 < getPagesAmount()) {
            // movingBetweenPages.add(event.getWhoClicked().getUniqueId());
            openPage((Player) event.getWhoClicked(), index + 1);
        } else // Making sure next page is purchasble
        if (chestData.getPagesData().containsKey(++index + 1)) {
            InventoryData inventoryData = chestData.getPagesData().get(index + 1);
            InventoryListener.buyNewPage.put(event.getWhoClicked().getUniqueId(), inventoryData);
            if (plugin.getSettings().confirmGUI) {
                event.getWhoClicked().openInventory(guiConfirm);
            } else {
                Locale.EXPAND_CHEST.send(event.getWhoClicked(), inventoryData.getPrice());
                event.getWhoClicked().closeInventory();
            }
        }
    }
    return true;
}
Also used : ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) Player(org.bukkit.entity.Player) InventoryData(com.bgsoftware.wildchests.api.objects.data.InventoryData)

Example 4 with ChestData

use of com.bgsoftware.wildchests.api.objects.data.ChestData in project WildChests by BG-Software-LLC.

the class DataHandler method loadOldDatabase.

private void loadOldDatabase() {
    File dataFolder = new File(plugin.getDataFolder(), "data");
    if (!dataFolder.exists())
        return;
    YamlConfiguration cfg;
    for (File chestFile : dataFolder.listFiles()) {
        try {
            cfg = YamlConfiguration.loadConfiguration(chestFile);
            UUID placer = UUID.fromString(cfg.getString("placer"));
            Location location = LocationUtils.fromString(chestFile.getName().replace(".yml", ""));
            ChestData chestData = plugin.getChestsManager().getChestData(cfg.getString("data"));
            WChest chest = (WChest) plugin.getChestsManager().addChest(placer, location, chestData);
            chest.loadFromFile(cfg);
            chestFile.delete();
        } catch (Exception ex) {
            WildChestsPlugin.log("Looks like the file " + chestFile.getName() + " is corrupted. Creating a backup file...");
            File backupFile = new File(plugin.getDataFolder(), "data-backup/" + chestFile.getName());
            copyFiles(chestFile, backupFile);
            ex.printStackTrace();
        }
    }
    if (dataFolder.listFiles().length == 0)
        dataFolder.delete();
}
Also used : ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) UUID(java.util.UUID) WChest(com.bgsoftware.wildchests.objects.chests.WChest) File(java.io.File) SQLException(java.sql.SQLException) IOException(java.io.IOException) Location(org.bukkit.Location)

Example 5 with ChestData

use of com.bgsoftware.wildchests.api.objects.data.ChestData in project WildChests by BG-Software-LLC.

the class DataHandler method loadResultSet.

private void loadResultSet(ResultSet resultSet, String tableName, List<Chest> updateContentsChests) throws SQLException {
    while (resultSet.next()) {
        UUID placer = UUID.fromString(resultSet.getString("placer"));
        String stringLocation = resultSet.getString("location");
        String errorMessage = null;
        try {
            if (Bukkit.getWorld(stringLocation.split(", ")[0]) == null) {
                errorMessage = "Null world.";
            } else {
                Location location = LocationUtils.fromString(stringLocation);
                ChestData chestData = plugin.getChestsManager().getChestData(resultSet.getString("chest_data"));
                WChest chest = plugin.getChestsManager().loadChest(placer, location, chestData);
                if (chest instanceof StorageChest) {
                    String item = resultSet.getString("item");
                    String amount = resultSet.getString("amount");
                    String maxAmount = resultSet.getString("max_amount");
                    ((WStorageChest) chest).loadFromData(item, amount, maxAmount);
                } else {
                    String serialized = resultSet.getString("inventories");
                    if (chest instanceof LinkedChest) {
                        String linkedChest = resultSet.getString("linked_chest");
                        ((WLinkedChest) chest).loadFromData(serialized, linkedChest);
                    } else {
                        ((WRegularChest) chest).loadFromData(serialized);
                    }
                    if (serialized.toCharArray()[0] != '*')
                        updateContentsChests.add(chest);
                }
            }
        } catch (Exception ex) {
            errorMessage = ex.getMessage();
        }
        if (errorMessage != null) {
            WildChestsPlugin.log("Couldn't load the location " + stringLocation);
            WildChestsPlugin.log(errorMessage);
            if (errorMessage.contains("Null") && plugin.getSettings().invalidWorldDelete) {
                SQLHelper.executeUpdate("DELETE FROM " + tableName + " WHERE location = '" + stringLocation + "';");
                WildChestsPlugin.log("Deleted spawner (" + stringLocation + ") from database.");
            }
        }
    }
}
Also used : ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) WLinkedChest(com.bgsoftware.wildchests.objects.chests.WLinkedChest) UUID(java.util.UUID) WChest(com.bgsoftware.wildchests.objects.chests.WChest) LinkedChest(com.bgsoftware.wildchests.api.objects.chests.LinkedChest) WLinkedChest(com.bgsoftware.wildchests.objects.chests.WLinkedChest) WRegularChest(com.bgsoftware.wildchests.objects.chests.WRegularChest) WStorageChest(com.bgsoftware.wildchests.objects.chests.WStorageChest) SQLException(java.sql.SQLException) IOException(java.io.IOException) Location(org.bukkit.Location) StorageChest(com.bgsoftware.wildchests.api.objects.chests.StorageChest) WStorageChest(com.bgsoftware.wildchests.objects.chests.WStorageChest)

Aggregations

ChestData (com.bgsoftware.wildchests.api.objects.data.ChestData)12 Chest (com.bgsoftware.wildchests.api.objects.chests.Chest)5 WChest (com.bgsoftware.wildchests.objects.chests.WChest)4 WChestData (com.bgsoftware.wildchests.objects.data.WChestData)3 UUID (java.util.UUID)3 Location (org.bukkit.Location)3 Player (org.bukkit.entity.Player)3 EventHandler (org.bukkit.event.EventHandler)3 LinkedChest (com.bgsoftware.wildchests.api.objects.chests.LinkedChest)2 StorageChest (com.bgsoftware.wildchests.api.objects.chests.StorageChest)2 InventoryData (com.bgsoftware.wildchests.api.objects.data.InventoryData)2 WLinkedChest (com.bgsoftware.wildchests.objects.chests.WLinkedChest)2 WRegularChest (com.bgsoftware.wildchests.objects.chests.WRegularChest)2 WStorageChest (com.bgsoftware.wildchests.objects.chests.WStorageChest)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 Material (org.bukkit.Material)2 Block (org.bukkit.block.Block)2 ItemStack (org.bukkit.inventory.ItemStack)2