Package net.aufdemrand.denizen.scripts.commands.npc

Source Code of net.aufdemrand.denizen.scripts.commands.npc.DespawnCommand

package net.aufdemrand.denizen.scripts.commands.npc;

import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.trait.trait.Spawned;

import java.util.Arrays;
import java.util.List;


public class DespawnCommand extends AbstractCommand {

    @Override
    public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

        for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

            if (!scriptEntry.hasObject("npcs")
                    && arg.matchesArgumentList(dNPC.class)) {
                scriptEntry.addObject("npcs", arg.asType(dList.class).filter(dNPC.class));
            }

            else arg.reportUnhandled();
        }

        if (!scriptEntry.hasObject("npcs")) {
            if (((BukkitScriptEntryData)scriptEntry.entryData).hasNPC())
                scriptEntry.addObject("npcs", Arrays.asList(((BukkitScriptEntryData)scriptEntry.entryData).getNPC()));
            else
                throw new InvalidArgumentsException("Must specify a valid list of NPCs!");
        }

    }

    @SuppressWarnings("unchecked")
    @Override
    public void execute(final ScriptEntry scriptEntry) throws CommandExecutionException {

        // Get objects
        List<dNPC> npcs = (List<dNPC>) scriptEntry.getObject("npcs");

        // Report to dB
        dB.report(scriptEntry, getName(),
                aH.debugObj("NPCs", npcs.toString()));

        for (dNPC npc : npcs) {
            if (npc.isSpawned()) {
                if (npc.getCitizen().hasTrait(Spawned.class))
                        npc.getCitizen().getTrait(Spawned.class).setSpawned(false);
                npc.getCitizen().despawn(DespawnReason.PLUGIN);
            }
        }
    }
}
TOP

Related Classes of net.aufdemrand.denizen.scripts.commands.npc.DespawnCommand

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.