Search in sources :

Example 1 with WRegularChest

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

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

Aggregations

LinkedChest (com.bgsoftware.wildchests.api.objects.chests.LinkedChest)2 StorageChest (com.bgsoftware.wildchests.api.objects.chests.StorageChest)2 ChestData (com.bgsoftware.wildchests.api.objects.data.ChestData)2 WChest (com.bgsoftware.wildchests.objects.chests.WChest)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 UUID (java.util.UUID)2 Location (org.bukkit.Location)2 WildChestsPlugin (com.bgsoftware.wildchests.WildChestsPlugin)1 ChestsManager (com.bgsoftware.wildchests.api.handlers.ChestsManager)1 Chest (com.bgsoftware.wildchests.api.objects.chests.Chest)1 RegularChest (com.bgsoftware.wildchests.api.objects.chests.RegularChest)1 WChestData (com.bgsoftware.wildchests.objects.data.WChestData)1 ChunkPosition (com.bgsoftware.wildchests.utils.ChunkPosition)1 Executor (com.bgsoftware.wildchests.utils.Executor)1 LocationUtils (com.bgsoftware.wildchests.utils.LocationUtils)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1 IOException (java.io.IOException)1