Search in sources :

Example 1 with NPCType

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());
}
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 2 with NPCType

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);
}
Also used : EntityType(org.bukkit.entity.EntityType) NPCClass(mc.dragons.core.gameobject.npc.NPCClass) NPCType(mc.dragons.core.gameobject.npc.NPC.NPCType)

Example 3 with NPCType

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);
}
Also used : NPCClass(mc.dragons.core.gameobject.npc.NPCClass) NPCType(mc.dragons.core.gameobject.npc.NPC.NPCType) Document(org.bson.Document)

Example 4 with NPCType

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;
}
Also used : LocalStorageManager(mc.dragons.core.storage.local.LocalStorageManager) StorageManager(mc.dragons.core.storage.StorageManager) UUID(java.util.UUID) NPCType(mc.dragons.core.gameobject.npc.NPC.NPCType) LocalStorageAccess(mc.dragons.core.storage.local.LocalStorageAccess) StorageAccess(mc.dragons.core.storage.StorageAccess) Location(org.bukkit.Location)

Aggregations

NPCType (mc.dragons.core.gameobject.npc.NPC.NPCType)4 UUID (java.util.UUID)2 NPCClass (mc.dragons.core.gameobject.npc.NPCClass)2 StorageAccess (mc.dragons.core.storage.StorageAccess)2 StorageManager (mc.dragons.core.storage.StorageManager)2 LocalStorageAccess (mc.dragons.core.storage.local.LocalStorageAccess)2 LocalStorageManager (mc.dragons.core.storage.local.LocalStorageManager)2 Document (org.bson.Document)2 Location (org.bukkit.Location)2 EntityType (org.bukkit.entity.EntityType)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Dragons (mc.dragons.core.Dragons)1 GameObject (mc.dragons.core.gameobject.GameObject)1 GameObjectLoader (mc.dragons.core.gameobject.GameObjectLoader)1 GameObjectRegistry (mc.dragons.core.gameobject.GameObjectRegistry)1