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;
}
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);
}
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());
}
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;
}
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;
}
Aggregations