Search in sources :

Example 1 with StorageAccess

use of mc.dragons.core.storage.StorageAccess in project DragonsOnline by UniverseCraft.

the class QuestLoader method registerNew.

public Quest registerNew(String name, String questName, int lvMin) {
    lazyLoadAll();
    LOGGER.trace("Registering new quest " + name);
    Document data = new Document("_id", UUID.randomUUID()).append("name", name).append("questName", questName).append("lvMin", lvMin).append("steps", new ArrayList<>()).append("locked", false);
    StorageAccess storageAccess = storageManager.getNewStorageAccess(GameObjectType.QUEST, data);
    Quest quest = new Quest(storageManager, storageAccess);
    masterRegistry.getRegisteredObjects().add(quest);
    return quest;
}
Also used : ArrayList(java.util.ArrayList) Document(org.bson.Document) StorageAccess(mc.dragons.core.storage.StorageAccess)

Example 2 with StorageAccess

use of mc.dragons.core.storage.StorageAccess in project DragonsOnline by UniverseCraft.

the class NPCLoader method loadObject.

public NPC loadObject(UUID uuid, UUID cid) {
    if (uuidToNpc.containsKey(uuid)) {
        LOGGER.debug("Prevented spawn of duplicate NPC " + uuid);
        return uuidToNpc.get(uuid);
    }
    LOGGER.verbose(cid, "loading NPC with uuid " + uuid);
    for (GameObject gameObject : masterRegistry.getRegisteredObjects(new GameObjectType[] { GameObjectType.NPC })) {
        NPC npc = (NPC) gameObject;
        if (npc.getUUID().equals(uuid)) {
            LOGGER.verbose("found in local cache (" + npc + "), returning");
            return npc;
        }
    }
    StorageAccess storageAccess = storageManager.getStorageAccess(GameObjectType.NPC, uuid);
    if (storageAccess == null) {
        LOGGER.warning(cid, "could not load NPC from database: returned null storage access");
        return null;
    }
    LOGGER.verbose(cid, "loaded storage access (" + storageAccess + "), constructing downstream");
    return loadObject(storageAccess);
}
Also used : GameObject(mc.dragons.core.gameobject.GameObject) LocalStorageAccess(mc.dragons.core.storage.local.LocalStorageAccess) StorageAccess(mc.dragons.core.storage.StorageAccess)

Example 3 with StorageAccess

use of mc.dragons.core.storage.StorageAccess in project DragonsOnline by UniverseCraft.

the class NPCLoader method loadAllPermanent.

/**
 * Load all persistent NPCs.
 *
 * <p>This is done as asynchronously as possible.
 *
 * @param force Whether to load even if they have already been loaded.
 */
public void loadAllPermanent(boolean force) {
    if (allPermanentLoaded && !force) {
        return;
    }
    LOGGER.debug("Loading all persistent NPCs...");
    allPermanentLoaded = true;
    masterRegistry.removeFromRegistry(GameObjectType.NPC);
    List<BukkitRunnable> asyncSpawnerRunnables = new ArrayList<>();
    storageManager.getAllStorageAccess(GameObjectType.NPC, new Document("$or", Arrays.<NPCType>asList(NPCType.values()).stream().filter(type -> (type.isPersistent() && type.isLoadedImmediately())).map(type -> new Document("npcType", type.toString())).collect(Collectors.toList()))).stream().forEach(storageAccess -> {
        LOGGER.verbose("Loading permanent NPC: " + storageAccess.getIdentifier());
        NPC npc = loadObject(storageAccess, asyncSpawnerRunnables);
        LOGGER.verbose("- Loaded permanent NPC: " + npc.getIdentifier() + " of class " + npc.getNPCClass().getClassName());
        masterRegistry.getRegisteredObjects().add(npc);
    });
    async(() -> {
        long start = System.currentTimeMillis();
        int batchSize = 5;
        int max = (int) Math.ceil((double) asyncSpawnerRunnables.size() / batchSize);
        LOGGER.info("We have " + asyncSpawnerRunnables.size() + " persistent NPCs to spawn");
        for (int i = 1; i <= max; i++) {
            final int fi = i;
            sync(() -> {
                long batchStart = System.currentTimeMillis();
                LOGGER.verbose("==SPAWNING BATCH #" + fi + "==");
                for (int j = (fi - 1) * batchSize; j < Math.min(asyncSpawnerRunnables.size(), fi * batchSize); j++) {
                    LOGGER.verbose("===Spawning #" + j);
                    asyncSpawnerRunnables.get(j).run();
                }
                long now = System.currentTimeMillis();
                long duration = now - batchStart;
                LOGGER.verbose("===Finished batch #" + fi + " in " + duration + "ms");
                if (duration > 1000) {
                    LOGGER.warning("Spawn of batch #" + fi + " took " + duration + "ms (batch size: " + batchSize + ")");
                }
                if (fi == max) {
                    LOGGER.info("Spawning of persistent NPCs complete (took " + (now - start) + "ms)");
                }
            }, i * 3);
        }
    }, 1);
    LOGGER.info("Initial entity count: " + Dragons.getInstance().getEntities().size());
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) HashMap(java.util.HashMap) FixedMetadataValue(org.bukkit.metadata.FixedMetadataValue) ArrayList(java.util.ArrayList) GameObjectType(mc.dragons.core.gameobject.GameObjectType) Location(org.bukkit.Location) World(org.bukkit.World) Map(java.util.Map) GameObjectRegistry(mc.dragons.core.gameobject.GameObjectRegistry) StorageUtil(mc.dragons.core.storage.StorageUtil) NPCType(mc.dragons.core.gameobject.npc.NPC.NPCType) LocalStorageAccess(mc.dragons.core.storage.local.LocalStorageAccess) DragonsLogger(mc.dragons.core.logging.DragonsLogger) LocalStorageManager(mc.dragons.core.storage.local.LocalStorageManager) Entity(org.bukkit.entity.Entity) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) Singleton(mc.dragons.core.util.singletons.Singleton) UUID(java.util.UUID) EntityType(org.bukkit.entity.EntityType) Collectors(java.util.stream.Collectors) Dragons(mc.dragons.core.Dragons) StorageAccess(mc.dragons.core.storage.StorageAccess) List(java.util.List) GameObjectLoader(mc.dragons.core.gameobject.GameObjectLoader) GameObject(mc.dragons.core.gameobject.GameObject) StorageManager(mc.dragons.core.storage.StorageManager) BukkitUtil.sync(mc.dragons.core.util.BukkitUtil.sync) Singletons(mc.dragons.core.util.singletons.Singletons) BukkitUtil.async(mc.dragons.core.util.BukkitUtil.async) ArrayList(java.util.ArrayList) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) NPCType(mc.dragons.core.gameobject.npc.NPC.NPCType) Document(org.bson.Document)

Example 4 with StorageAccess

use of mc.dragons.core.storage.StorageAccess in project DragonsOnline by UniverseCraft.

the class ItemLoader method registerNew.

public Item registerNew(Document data) {
    data.append("_id", UUID.randomUUID());
    String className = data.getString("className");
    Material material = Material.valueOf(data.getString("materialType"));
    LOGGER.trace("Registering new item of class " + className);
    Document clone = new Document(data);
    itemClassLoader.getItemClassByClassName(className).getAddons().forEach(a -> a.onCreateStorageAccess(clone));
    StorageAccess storageAccess = storageManager.getNewStorageAccess(GameObjectType.ITEM, clone);
    ItemStack itemStack = new ItemStack(material);
    Item item = new Item(itemStack, storageManager, storageAccess);
    uuidToItem.put(item.getUUID().toString(), item);
    masterRegistry.getRegisteredObjects().add(item);
    return item;
}
Also used : Material(org.bukkit.Material) Document(org.bson.Document) ItemStack(org.bukkit.inventory.ItemStack) StorageAccess(mc.dragons.core.storage.StorageAccess)

Example 5 with StorageAccess

use of mc.dragons.core.storage.StorageAccess in project DragonsOnline by UniverseCraft.

the class ItemLoader method loadObjects.

/**
 * Loads all items matching the given UUIDs in a single database query.
 *
 * <p>Indexed by UUID for convenience.
 *
 * @deprecated Use async method to avoid blocking main thread.
 *
 * @param uuids
 * @return
 */
@Deprecated
public Map<UUID, Item> loadObjects(Set<UUID> uuids) {
    LOGGER.trace("Loading items by UUID " + uuids.toArray());
    Map<UUID, Item> result = new HashMap<>();
    Set<StorageAccess> results = storageManager.getAllStorageAccess(GameObjectType.ITEM, new Document("_id", new Document("$in", uuids)));
    for (StorageAccess sa : results) {
        result.put(sa.getIdentifier().getUUID(), loadObject(sa));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) UUID(java.util.UUID) Document(org.bson.Document) StorageAccess(mc.dragons.core.storage.StorageAccess)

Aggregations

StorageAccess (mc.dragons.core.storage.StorageAccess)19 Document (org.bson.Document)16 UUID (java.util.UUID)8 StorageManager (mc.dragons.core.storage.StorageManager)5 ArrayList (java.util.ArrayList)4 LocalStorageAccess (mc.dragons.core.storage.local.LocalStorageAccess)4 HashMap (java.util.HashMap)3 List (java.util.List)3 Dragons (mc.dragons.core.Dragons)3 GameObject (mc.dragons.core.gameobject.GameObject)3 GameObjectType (mc.dragons.core.gameobject.GameObjectType)3 BukkitUtil.sync (mc.dragons.core.util.BukkitUtil.sync)3 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 GameObjectLoader (mc.dragons.core.gameobject.GameObjectLoader)2 GameObjectRegistry (mc.dragons.core.gameobject.GameObjectRegistry)2 NPCType (mc.dragons.core.gameobject.npc.NPC.NPCType)2 Identifier (mc.dragons.core.storage.Identifier)2 StorageUtil (mc.dragons.core.storage.StorageUtil)2