use of mc.dragons.core.gameobject.npc.NPC.NPCType 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.gameobject.npc.NPC.NPCType in project DragonsOnline by UniverseCraft.
the class NPCCommand method createClass.
private void createClass(CommandSender sender, String[] args) {
if (args.length < 6) {
sender.sendMessage(ChatColor.RED + "Insufficient arguments! /npc create <ClassName> <EntityType> <MaxHealth> <Level> <NPCType>");
return;
}
String npcClassName = args[1];
EntityType type = StringUtil.parseEntityType(sender, args[2]);
Double maxHealth = parseDouble(sender, args[3]);
Integer level = parseInt(sender, args[4]);
NPCType npcType = StringUtil.parseEnum(sender, NPCType.class, args[5]);
if (type == null || maxHealth == null || level == null || npcType == null)
return;
NPCClass npcClass = npcClassLoader.registerNew(npcClassName, "Unnamed Entity", type, maxHealth, level, npcType);
if (npcClass == null) {
sender.sendMessage(ChatColor.RED + "An error occurred! Does an NPC class by this name already exist?");
return;
}
// MetadataConstants.addBlankMetadata(npcClass, user(sender));
AUDIT_LOG.saveEntry(npcClass, user(sender), "Created");
sender.sendMessage(ChatColor.GREEN + "Successfully created NPC class " + npcClassName);
}
use of mc.dragons.core.gameobject.npc.NPC.NPCType in project DragonsOnline by UniverseCraft.
the class NPCCommand method setNPCType.
private void setNPCType(CommandSender sender, String[] args) {
NPCClass npcClass = lookupNPCClass(sender, args[0]);
NPCType npcType = StringUtil.parseEnum(sender, NPCType.class, args[2]);
Document base = Document.parse(npcClass.getData().toJson());
npcClass.setNPCType(npcType);
sender.sendMessage(ChatColor.GREEN + "Updated NPC type successfully.");
if (!npcClass.getNPCType().hasAIByDefault() && npcClass.hasAI()) {
npcClass.setAI(false);
sender.sendMessage(ChatColor.GREEN + "Automatically toggled off AI for this class based on the NPC type.");
}
AUDIT_LOG.saveEntry(npcClass, user(sender), base, "Set NPC type to " + npcType);
propagateRevisions(npcClass);
}
use of mc.dragons.core.gameobject.npc.NPC.NPCType in project DragonsOnline by UniverseCraft.
the class NPCLoader method loadObject.
public NPC loadObject(StorageAccess storageAccess, List<BukkitRunnable> spawnRunnables) {
lazyLoadAllPermanent();
UUID uuid = storageAccess.getIdentifier().getUUID();
if (uuidToNpc.containsKey(uuid)) {
LOGGER.debug("Prevented spawn of duplicate NPC " + uuid);
return uuidToNpc.get(uuid);
}
LOGGER.trace("Loading NPC " + storageAccess.getIdentifier());
NPC.NPCType npcType = NPC.NPCType.valueOf((String) storageAccess.get("npcType"));
Location loc = StorageUtil.docToLoc((Document) storageAccess.get("lastLocation"));
NPC npc = new NPC(loc, spawnRunnables, npcType.isPersistent() ? storageManager : (StorageManager) localStorageManager, npcType.isPersistent() ? storageAccess : (StorageAccess) localStorageManager.downgrade(storageAccess));
masterRegistry.getRegisteredObjects().add(npc);
return npc;
}
Aggregations