Search in sources :

Example 6 with Chest

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

the class BlockListener method onChestPlace.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChestPlace(BlockPlaceEvent e) {
    if (e.getBlockPlaced().getType() != Material.CHEST && e.getBlockPlaced().getType() != Material.TRAPPED_CHEST)
        return;
    boolean hasNearbyChest = false;
    for (BlockFace blockFace : blockFaces) {
        Block block = e.getBlockPlaced().getRelative(blockFace);
        Material blockMaterial = block.getType();
        if (blockMaterial == Material.CHEST || blockMaterial == Material.TRAPPED_CHEST) {
            hasNearbyChest = true;
            if (plugin.getChestsManager().getChest(block.getLocation()) != null) {
                e.setCancelled(true);
                return;
            }
        }
    }
    ChestData chestData = plugin.getChestsManager().getChestData(e.getItemInHand());
    if (chestData == null)
        return;
    if (hasNearbyChest) {
        e.setCancelled(true);
        return;
    }
    Chest chest = plugin.getChestsManager().addChest(e.getPlayer().getUniqueId(), e.getBlockPlaced().getLocation(), chestData);
    plugin.getProviders().notifyChestPlaceListeners(chest);
    // chest.onPlace(e);
    Locale.CHEST_PLACED.send(e.getPlayer(), chestData.getName(), e.getItemInHand().getItemMeta().getDisplayName());
}
Also used : Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) BlockFace(org.bukkit.block.BlockFace) Block(org.bukkit.block.Block) Material(org.bukkit.Material) EventHandler(org.bukkit.event.EventHandler)

Example 7 with Chest

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

the class BlockListener method onChestBreak.

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onChestBreak(BlockBreakEvent e) {
    Chest chest = plugin.getChestsManager().getChest(e.getBlock().getLocation());
    if (chest == null)
        return;
    e.setCancelled(true);
    if (e.getPlayer().getGameMode() != GameMode.CREATIVE) {
        ChestData chestData = chest.getData();
        ItemUtils.dropOrCollect(e.getPlayer(), chestData.getItemStack(), chestData.isAutoCollect(), chest.getLocation());
    }
    chest.onBreak(e);
    plugin.getProviders().notifyChestBreakListeners(e.getPlayer(), chest);
    chest.remove();
    e.getBlock().setType(Material.AIR);
}
Also used : Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) EventHandler(org.bukkit.event.EventHandler)

Example 8 with Chest

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

the class WildChestsPlugin method onDisable.

@Override
public void onDisable() {
    Bukkit.getScheduler().cancelTasks(this);
    // Closing all inventories & closing chests
    for (Chest chest : chestsManager.getChests()) {
        boolean needClose = false;
        for (Inventory inventory : chest.getPages()) {
            List<HumanEntity> viewers = new ArrayList<>(inventory.getViewers());
            for (HumanEntity humanEntity : viewers) {
                humanEntity.closeInventory();
                needClose = true;
            }
        }
        if (needClose)
            nmsAdapter.playChestAction(chest.getLocation(), false);
    }
    for (Player player : Bukkit.getOnlinePlayers()) player.closeInventory();
    int loadedChunks = 0;
    for (World world : Bukkit.getWorlds()) {
        for (Chunk chunk : world.getLoadedChunks()) {
            dataHandler.saveDatabase(chunk, false);
            loadedChunks++;
        }
    }
    log("Chunks to save: " + loadedChunks);
    log("Terminating executor...");
    Executor.stop();
    log("Terminating database...");
    SQLHelper.close();
}
Also used : Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) Player(org.bukkit.entity.Player) HumanEntity(org.bukkit.entity.HumanEntity) ArrayList(java.util.ArrayList) World(org.bukkit.World) Chunk(org.bukkit.Chunk) Inventory(org.bukkit.inventory.Inventory) NMSInventory(com.bgsoftware.wildchests.nms.NMSInventory)

Example 9 with Chest

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

the class ChestsHandler method loadChest.

public WChest loadChest(UUID placer, Location location, ChestData chestData) {
    WChest chest;
    switch(chestData.getChestType()) {
        case CHEST:
            chest = new WRegularChest(placer, location, chestData);
            break;
        case LINKED_CHEST:
            chest = new WLinkedChest(placer, location, chestData);
            break;
        case STORAGE_UNIT:
            chest = new WStorageChest(placer, location, chestData);
            break;
        default:
            throw new IllegalArgumentException("Invalid chest at " + location);
    }
    chests.put(location, chest);
    chestsByChunks.computeIfAbsent(ChunkPosition.of(location), s -> Sets.newConcurrentHashSet()).add(chest);
    return chest;
}
Also used : ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) RegularChest(com.bgsoftware.wildchests.api.objects.chests.RegularChest) HashMap(java.util.HashMap) ChunkPosition(com.bgsoftware.wildchests.utils.ChunkPosition) ArrayList(java.util.ArrayList) LinkedChest(com.bgsoftware.wildchests.api.objects.chests.LinkedChest) Location(org.bukkit.Location) WChestData(com.bgsoftware.wildchests.objects.data.WChestData) Executor(com.bgsoftware.wildchests.utils.Executor) Map(java.util.Map) WRegularChest(com.bgsoftware.wildchests.objects.chests.WRegularChest) Chunk(org.bukkit.Chunk) LocationUtils(com.bgsoftware.wildchests.utils.LocationUtils) Material(org.bukkit.Material) Bukkit(org.bukkit.Bukkit) ChestsManager(com.bgsoftware.wildchests.api.handlers.ChestsManager) StorageChest(com.bgsoftware.wildchests.api.objects.chests.StorageChest) Set(java.util.Set) UUID(java.util.UUID) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) WChest(com.bgsoftware.wildchests.objects.chests.WChest) Sets(com.google.common.collect.Sets) ItemStack(org.bukkit.inventory.ItemStack) WildChestsPlugin(com.bgsoftware.wildchests.WildChestsPlugin) List(java.util.List) WLinkedChest(com.bgsoftware.wildchests.objects.chests.WLinkedChest) WStorageChest(com.bgsoftware.wildchests.objects.chests.WStorageChest) Collections(java.util.Collections) Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) WLinkedChest(com.bgsoftware.wildchests.objects.chests.WLinkedChest) WChest(com.bgsoftware.wildchests.objects.chests.WChest) WRegularChest(com.bgsoftware.wildchests.objects.chests.WRegularChest) WStorageChest(com.bgsoftware.wildchests.objects.chests.WStorageChest)

Example 10 with Chest

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

the class DataHandler method saveDatabase.

public void saveDatabase(Chunk chunk, boolean async) {
    List<Chest> chestList = chunk == null ? plugin.getChestsManager().getChests() : plugin.getChestsManager().getChests(chunk);
    List<Chest> regularModifiedChests = chestList.stream().filter(chest -> chest.getChestType() == ChestType.CHEST).collect(Collectors.toList());
    List<Chest> storageModifiedChests = chestList.stream().filter(chest -> chest.getChestType() == ChestType.STORAGE_UNIT).collect(Collectors.toList());
    List<Chest> linkedChestsModifiedChests = chestList.stream().filter(chest -> chest.getChestType() == ChestType.LINKED_CHEST).collect(Collectors.toList());
    if (!regularModifiedChests.isEmpty()) {
        StatementHolder chestsUpdateHolder = Query.REGULAR_CHEST_UPDATE_INVENTORIES.getStatementHolder(null);
        chestsUpdateHolder.prepareBatch();
        regularModifiedChests.forEach(chest -> ((DatabaseObject) chest).setUpdateStatement(chestsUpdateHolder).addBatch());
        chestsUpdateHolder.execute(async);
    }
    if (!storageModifiedChests.isEmpty()) {
        StatementHolder chestsUpdateHolder = Query.STORAGE_UNIT_UPDATE_ITEM.getStatementHolder(null);
        chestsUpdateHolder.prepareBatch();
        storageModifiedChests.forEach(chest -> ((DatabaseObject) chest).setUpdateStatement(chestsUpdateHolder).addBatch());
        chestsUpdateHolder.execute(async);
    }
    if (!linkedChestsModifiedChests.isEmpty()) {
        StatementHolder chestsUpdateHolder = Query.LINKED_CHEST_UPDATE_INVENTORIES.getStatementHolder(null);
        chestsUpdateHolder.prepareBatch();
        linkedChestsModifiedChests.forEach(chest -> ((DatabaseObject) chest).setUpdateStatement(chestsUpdateHolder).addBatch());
        chestsUpdateHolder.execute(async);
    }
}
Also used : ChestData(com.bgsoftware.wildchests.api.objects.data.ChestData) Query(com.bgsoftware.wildchests.database.Query) ArrayList(java.util.ArrayList) StatementHolder(com.bgsoftware.wildchests.database.StatementHolder) SQLException(java.sql.SQLException) LinkedChest(com.bgsoftware.wildchests.api.objects.chests.LinkedChest) Location(org.bukkit.Location) World(org.bukkit.World) ResultSet(java.sql.ResultSet) Executor(com.bgsoftware.wildchests.utils.Executor) WRegularChest(com.bgsoftware.wildchests.objects.chests.WRegularChest) Chunk(org.bukkit.Chunk) ChestType(com.bgsoftware.wildchests.api.objects.ChestType) LocationUtils(com.bgsoftware.wildchests.utils.LocationUtils) SQLHelper(com.bgsoftware.wildchests.database.SQLHelper) Bukkit(org.bukkit.Bukkit) StorageChest(com.bgsoftware.wildchests.api.objects.chests.StorageChest) DatabaseObject(com.bgsoftware.wildchests.database.DatabaseObject) BufferedWriter(java.io.BufferedWriter) FileWriter(java.io.FileWriter) IOException(java.io.IOException) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) WChest(com.bgsoftware.wildchests.objects.chests.WChest) File(java.io.File) WildChestsPlugin(com.bgsoftware.wildchests.WildChestsPlugin) List(java.util.List) WLinkedChest(com.bgsoftware.wildchests.objects.chests.WLinkedChest) WStorageChest(com.bgsoftware.wildchests.objects.chests.WStorageChest) ChunksListener(com.bgsoftware.wildchests.listeners.ChunksListener) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Chest(com.bgsoftware.wildchests.api.objects.chests.Chest) 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) DatabaseObject(com.bgsoftware.wildchests.database.DatabaseObject) StatementHolder(com.bgsoftware.wildchests.database.StatementHolder)

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