Search in sources :

Example 1 with Chest

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

the class InventoryListener method onChestClose.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChestClose(InventoryCloseEvent e) {
    Chest chest = WChest.viewers.get(e.getPlayer().getUniqueId());
    // Making sure it's still a valid chest
    if (chest == null) {
        WChest.viewers.remove(e.getPlayer().getUniqueId());
        return;
    }
    chest.onClose(e);
}
Also used : WChest(com.bgsoftware.wildchests.objects.chests.WChest) Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) EventHandler(org.bukkit.event.EventHandler)

Example 2 with Chest

use of com.bgsoftware.wildchests.api.objects.chests.Chest 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 3 with Chest

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

the class CommandLink method perform.

@Override
public void perform(WildChestsPlugin plugin, CommandSender sender, String[] args) {
    if (!(sender instanceof Player)) {
        sender.sendMessage(ChatColor.RED + "Only players can use this command.");
        return;
    }
    Player player = (Player) sender;
    Block targetBlock = player.getTargetBlock((Set<Material>) null, 5);
    if (targetBlock == null || targetBlock.getType() != Material.CHEST) {
        Locale.INVALID_BLOCK_CHEST.send(player);
        return;
    }
    LinkedChestInteractEvent linkedChestInteractEvent = new LinkedChestInteractEvent(player, targetBlock);
    Bukkit.getPluginManager().callEvent(linkedChestInteractEvent);
    if (linkedChestInteractEvent.isCancelled()) {
        Locale.NOT_LINKED_CHEST.send(player);
        return;
    }
    Chest chest = plugin.getChestsManager().getChest(targetBlock.getLocation());
    if (!(chest instanceof LinkedChest)) {
        Locale.NOT_LINKED_CHEST.send(player);
        return;
    }
    LinkedChest linkedChest = (LinkedChest) chest;
    if (players.containsKey(player.getUniqueId())) {
        LinkedChest originalChest = plugin.getChestsManager().getLinkedChest(players.get(player.getUniqueId()));
        players.remove(player.getUniqueId());
        if (originalChest == null || originalChest.getLocation().equals(linkedChest.getLocation()) || originalChest.equals(linkedChest.getLinkedChest())) {
            Locale.NOT_LINKED_CHEST.send(player);
            return;
        }
        List<ItemStack> toMove = new ArrayList<>();
        for (int page = 0; page < originalChest.getPagesAmount(); page++) {
            Inventory inventory = originalChest.getPage(page);
            for (ItemStack itemStack : inventory.getContents()) {
                if (itemStack != null && itemStack.getType() != Material.AIR) {
                    toMove.add(itemStack);
                }
            }
            inventory.clear();
        }
        originalChest.linkIntoChest(linkedChest);
        Locale.LINKED_SUCCEED.send(player, LocationUtils.toString(originalChest.getLocation()));
        if (!toMove.isEmpty()) {
            linkedChest.addItems(toMove.toArray(new ItemStack[] {}));
            Locale.LEFTOVERS_ITEMS_WARNING.send(player);
        }
        return;
    }
    players.put(player.getUniqueId(), linkedChest.getLocation());
    Executor.async(() -> players.remove(player.getUniqueId()), 6000L);
    Locale.SELECT_ANOTHER_CHEST.send(player);
}
Also used : LinkedChest(com.bgsoftware.wildchests.api.objects.chests.LinkedChest) Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) Player(org.bukkit.entity.Player) LinkedChestInteractEvent(com.bgsoftware.wildchests.utils.LinkedChestInteractEvent) ArrayList(java.util.ArrayList) Block(org.bukkit.block.Block) Material(org.bukkit.Material) LinkedChest(com.bgsoftware.wildchests.api.objects.chests.LinkedChest) ItemStack(org.bukkit.inventory.ItemStack) Inventory(org.bukkit.inventory.Inventory)

Example 4 with Chest

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

the class DataHandler method loadDatabase.

private void loadDatabase() {
    // Creating default tables
    SQLHelper.executeUpdate("CREATE TABLE IF NOT EXISTS chests (location VARCHAR PRIMARY KEY, placer VARCHAR, chest_data VARCHAR, inventories VARCHAR);");
    SQLHelper.executeUpdate("CREATE TABLE IF NOT EXISTS linked_chests (location VARCHAR PRIMARY KEY, placer VARCHAR, chest_data VARCHAR, inventories VARCHAR, linked_chest VARCHAR);");
    SQLHelper.executeUpdate("CREATE TABLE IF NOT EXISTS storage_units (location VARCHAR PRIMARY KEY, placer VARCHAR, chest_data VARCHAR, item VARCHAR, amount VARCHAR, max_amount VARCHAR);");
    SQLHelper.executeUpdate("CREATE TABLE IF NOT EXISTS offline_payment (uuid VARCHAR PRIMARY KEY, payment VARCHAR);");
    List<Chest> updateContentsChests = new ArrayList<>();
    // Loading all tables
    SQLHelper.executeQuery("SELECT * FROM chests;", resultSet -> loadResultSet(resultSet, "chests", updateContentsChests));
    SQLHelper.executeQuery("SELECT * FROM linked_chests;", resultSet -> loadResultSet(resultSet, "linked_chests", updateContentsChests));
    SQLHelper.executeQuery("SELECT * FROM storage_units;", resultSet -> loadResultSet(resultSet, "storage_units", updateContentsChests));
    updateContentsChests.forEach(chest -> ((WChest) chest).executeUpdateStatement(false));
    Executor.sync(() -> {
        for (World world : Bukkit.getWorlds()) {
            for (Chunk chunk : world.getLoadedChunks()) ChunksListener.handleChunkLoad(plugin, chunk);
        }
    }, 1L);
}
Also used : LinkedChest(com.bgsoftware.wildchests.api.objects.chests.LinkedChest) WRegularChest(com.bgsoftware.wildchests.objects.chests.WRegularChest) StorageChest(com.bgsoftware.wildchests.api.objects.chests.StorageChest) WChest(com.bgsoftware.wildchests.objects.chests.WChest) WLinkedChest(com.bgsoftware.wildchests.objects.chests.WLinkedChest) WStorageChest(com.bgsoftware.wildchests.objects.chests.WStorageChest) Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) ArrayList(java.util.ArrayList) World(org.bukkit.World) Chunk(org.bukkit.Chunk)

Example 5 with Chest

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

the class BlockListener method onChestExplode.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChestExplode(EntityExplodeEvent e) {
    List<Block> blockList = new ArrayList<>(e.blockList());
    Player sourcePlayer = null;
    if (e.getEntity() instanceof TNTPrimed && ((TNTPrimed) e.getEntity()).getSource() instanceof Player) {
        sourcePlayer = (Player) ((TNTPrimed) e.getEntity()).getSource();
    }
    for (Block block : blockList) {
        Chest chest = plugin.getChestsManager().getChest(block.getLocation());
        if (chest == null)
            continue;
        e.blockList().remove(block);
        if (plugin.getSettings().explodeDropChance > 0 && (plugin.getSettings().explodeDropChance == 100 || ThreadLocalRandom.current().nextInt(101) <= plugin.getSettings().explodeDropChance)) {
            ChestData chestData = chest.getData();
            ItemUtils.dropOrCollect(null, chestData.getItemStack(), false, chest.getLocation());
        }
        chest.onBreak(new BlockBreakEvent(block, sourcePlayer));
        plugin.getProviders().notifyChestBreakListeners(sourcePlayer, chest);
        chest.remove();
        block.setType(Material.AIR);
    }
}
Also used : Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) Player(org.bukkit.entity.Player) ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) ArrayList(java.util.ArrayList) TNTPrimed(org.bukkit.entity.TNTPrimed) Block(org.bukkit.block.Block) BlockBreakEvent(org.bukkit.event.block.BlockBreakEvent) EventHandler(org.bukkit.event.EventHandler)

Aggregations

Chest (com.bgsoftware.wildchests.api.objects.chests.Chest)12 WChest (com.bgsoftware.wildchests.objects.chests.WChest)7 ChestData (com.bgsoftware.wildchests.api.objects.data.ChestData)6 ArrayList (java.util.ArrayList)6 EventHandler (org.bukkit.event.EventHandler)6 LinkedChest (com.bgsoftware.wildchests.api.objects.chests.LinkedChest)4 Chunk (org.bukkit.Chunk)4 StorageChest (com.bgsoftware.wildchests.api.objects.chests.StorageChest)3 WLinkedChest (com.bgsoftware.wildchests.objects.chests.WLinkedChest)3 WRegularChest (com.bgsoftware.wildchests.objects.chests.WRegularChest)3 WStorageChest (com.bgsoftware.wildchests.objects.chests.WStorageChest)3 Material (org.bukkit.Material)3 World (org.bukkit.World)3 Block (org.bukkit.block.Block)3 Player (org.bukkit.entity.Player)3 Inventory (org.bukkit.inventory.Inventory)3 WildChestsPlugin (com.bgsoftware.wildchests.WildChestsPlugin)2 Executor (com.bgsoftware.wildchests.utils.Executor)2 LinkedChestInteractEvent (com.bgsoftware.wildchests.utils.LinkedChestInteractEvent)2 LocationUtils (com.bgsoftware.wildchests.utils.LocationUtils)2