Search in sources :

Example 1 with LinkedChest

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

use of com.bgsoftware.wildchests.api.objects.chests.LinkedChest 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)

Example 3 with LinkedChest

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

the class WLinkedChest method loadFromData.

public void loadFromData(String serialized, String linkedChest) {
    super.loadFromData(serialized);
    if (!linkedChest.isEmpty()) {
        Location linkedChestLocation = LocationUtils.fromString(linkedChest);
        Executor.sync(() -> {
            LinkedChest sourceChest = plugin.getChestsManager().getLinkedChest(linkedChestLocation);
            if (sourceChest != null) {
                if (((WLinkedChest) sourceChest).linkedChestsContainer == null)
                    ((WLinkedChest) sourceChest).linkedChestsContainer = new LinkedChestsContainer(sourceChest);
                this.linkedChestsContainer = ((WLinkedChest) sourceChest).linkedChestsContainer;
                this.linkedChestsContainer.linkChest(this);
            }
        }, 1L);
    }
}
Also used : LinkedChestsContainer(com.bgsoftware.wildchests.objects.containers.LinkedChestsContainer) LinkedChest(com.bgsoftware.wildchests.api.objects.chests.LinkedChest) Location(org.bukkit.Location)

Aggregations

LinkedChest (com.bgsoftware.wildchests.api.objects.chests.LinkedChest)3 Location (org.bukkit.Location)2 Chest (com.bgsoftware.wildchests.api.objects.chests.Chest)1 StorageChest (com.bgsoftware.wildchests.api.objects.chests.StorageChest)1 ChestData (com.bgsoftware.wildchests.api.objects.data.ChestData)1 WChest (com.bgsoftware.wildchests.objects.chests.WChest)1 WLinkedChest (com.bgsoftware.wildchests.objects.chests.WLinkedChest)1 WRegularChest (com.bgsoftware.wildchests.objects.chests.WRegularChest)1 WStorageChest (com.bgsoftware.wildchests.objects.chests.WStorageChest)1 LinkedChestsContainer (com.bgsoftware.wildchests.objects.containers.LinkedChestsContainer)1 LinkedChestInteractEvent (com.bgsoftware.wildchests.utils.LinkedChestInteractEvent)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 Material (org.bukkit.Material)1 Block (org.bukkit.block.Block)1 Player (org.bukkit.entity.Player)1 Inventory (org.bukkit.inventory.Inventory)1 ItemStack (org.bukkit.inventory.ItemStack)1