Search in sources :

Example 1 with InvalidArgumentsRuntimeException

use of com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException in project Denizen-For-Bukkit by DenizenScript.

the class DisguiseCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    NetworkInterceptHelper.enable();
    EntityTag entity = scriptEntry.getObjectTag("entity");
    EntityTag as = scriptEntry.argForPrefix("as", EntityTag.class, true);
    boolean cancel = scriptEntry.argAsBoolean("cancel");
    boolean global = scriptEntry.argAsBoolean("global");
    boolean self = scriptEntry.argAsBoolean("self");
    List<PlayerTag> players = scriptEntry.argForPrefixList("players", PlayerTag.class, true);
    if (as == null && !cancel) {
        throw new InvalidArgumentsRuntimeException("Must specify a valid type to disguise as!");
    }
    if (players == null && !global) {
        PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
        if (player != null && player.isOnline()) {
            players = Collections.singletonList(player);
        } else {
            throw new InvalidArgumentsRuntimeException("Must have a valid player attached, or 'global' set!");
        }
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), entity, db("cancel", cancel), as, db("global", global), db("self", self), db("players", players));
    }
    HashMap<UUID, TrackedDisguise> playerMap = disguises.get(entity.getUUID());
    if (playerMap != null) {
        if (global) {
            for (Map.Entry<UUID, TrackedDisguise> entry : playerMap.entrySet()) {
                entry.getValue().isActive = false;
                if (entry.getKey() == null) {
                    if (entry.getValue().toOthers != null) {
                        FakeEntity.idsToEntities.remove(entry.getValue().toOthers.overrideUUID);
                    }
                    for (Player player : entity.getWorld().getPlayers()) {
                        if (!EntityTag.isNPC(player)) {
                            entry.getValue().removeFor(new PlayerTag(player));
                        }
                    }
                } else {
                    PlayerTag player = new PlayerTag(entry.getKey());
                    entry.getValue().removeFor(player);
                }
            }
            disguises.remove(entity.getUUID());
        } else {
            for (PlayerTag player : players) {
                TrackedDisguise disguise = playerMap.remove(player.getUUID());
                if (disguise != null) {
                    disguise.isActive = false;
                    disguise.removeFor(player);
                    if (disguise.toOthers != null) {
                        FakeEntity.idsToEntities.remove(disguise.toOthers.overrideUUID);
                    }
                    if (playerMap.isEmpty()) {
                        disguises.remove(entity.getUUID());
                    }
                }
            }
        }
    }
    if (!cancel) {
        TrackedDisguise disguise = new TrackedDisguise(entity, as);
        disguise.as.entity = NMSHandler.getPlayerHelper().sendEntitySpawn(new ArrayList<>(), as.getEntityType(), entity.getLocation(), as.mechanisms == null ? null : new ArrayList<>(as.mechanisms), -1, null, false).entity.getBukkitEntity();
        if (global) {
            playerMap = disguises.computeIfAbsent(entity.getUUID(), k -> new HashMap<>());
            playerMap.put(null, disguise);
            disguise.isActive = true;
            ArrayList<PlayerTag> playerSet = players == null ? new ArrayList<>() : new ArrayList<>(players);
            for (Player player : entity.getWorld().getPlayers()) {
                if (!EntityTag.isNPC(player) && !playerSet.contains(new PlayerTag(player)) && (self || !player.getUniqueId().equals(entity.getUUID()))) {
                    playerSet.add(new PlayerTag(player));
                }
            }
            disguise.sendTo(playerSet);
        } else {
            for (PlayerTag player : players) {
                playerMap = disguises.computeIfAbsent(entity.getUUID(), k -> new HashMap<>());
                playerMap.put(player.getUUID(), disguise);
                disguise.isActive = true;
            }
            disguise.sendTo(players);
        }
    }
}
Also used : HandlerList(org.bukkit.event.HandlerList) Utilities(com.denizenscript.denizen.utilities.Utilities) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) java.util(java.util) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) PlayerMoveEvent(org.bukkit.event.player.PlayerMoveEvent) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Argument(com.denizenscript.denizencore.objects.Argument) EventHandler(org.bukkit.event.EventHandler) Location(org.bukkit.Location) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) Bukkit(org.bukkit.Bukkit) Listener(org.bukkit.event.Listener) NetworkInterceptHelper(com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper) PlayerJoinEvent(org.bukkit.event.player.PlayerJoinEvent) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) EntityType(org.bukkit.entity.EntityType) Denizen(com.denizenscript.denizen.Denizen) FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) EventPriority(org.bukkit.event.EventPriority) PlayerTeleportEvent(org.bukkit.event.player.PlayerTeleportEvent) EntityTag(com.denizenscript.denizen.objects.EntityTag) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) EntityTag(com.denizenscript.denizen.objects.EntityTag)

Example 2 with InvalidArgumentsRuntimeException

use of com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException in project Denizen-For-Bukkit by DenizenScript.

the class EngageCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    if (!Utilities.entryHasNPC(scriptEntry)) {
        throw new InvalidArgumentsRuntimeException("This command requires a linked NPC!");
    }
    DurationTag duration = scriptEntry.getObjectTag("duration");
    boolean linkedPlayer = scriptEntry.argAsBoolean("player");
    NPCTag npc = Utilities.getEntryNPC(scriptEntry);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), npc, duration, db("player", linkedPlayer));
    }
    if (duration.getSecondsAsInt() > 0) {
        setEngaged(npc.getCitizen(), linkedPlayer ? Utilities.getEntryPlayer(scriptEntry) : null, duration.getSecondsAsInt());
    } else {
        setEngaged(npc.getCitizen(), linkedPlayer ? Utilities.getEntryPlayer(scriptEntry) : null, true);
    }
}
Also used : InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) NPCTag(com.denizenscript.denizen.objects.NPCTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 3 with InvalidArgumentsRuntimeException

use of com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException in project Denizen-For-Bukkit by DenizenScript.

the class AssignmentCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ScriptTag script = scriptEntry.getObjectTag("script");
    Action action = (Action) scriptEntry.getObject("action");
    List<NPCTag> npcs = (List<NPCTag>) scriptEntry.getObject("npcs");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("action", action), script, db("npc", npcs));
    }
    PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
    for (NPCTag npc : npcs) {
        switch(action) {
            case SET:
                {
                    if (script == null) {
                        throw new InvalidArgumentsRuntimeException("Missing script!");
                    }
                    AssignmentTrait assignment = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
                    assignment.clearAssignments(player);
                    assignment.addAssignmentScript((AssignmentScriptContainer) script.getContainer(), player);
                    break;
                }
            case ADD:
                if (script == null) {
                    throw new InvalidArgumentsRuntimeException("Missing script!");
                }
                npc.getCitizen().getOrAddTrait(AssignmentTrait.class).addAssignmentScript((AssignmentScriptContainer) script.getContainer(), player);
                break;
            case REMOVE:
                if (script == null) {
                    Deprecations.assignmentRemove.warn(scriptEntry);
                    if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                        npc.getCitizen().getOrAddTrait(AssignmentTrait.class).clearAssignments(player);
                        npc.getCitizen().removeTrait(AssignmentTrait.class);
                    }
                } else {
                    if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                        AssignmentTrait trait = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
                        trait.removeAssignmentScript(script.getName(), player);
                        trait.checkAutoRemove();
                    }
                }
                break;
            case CLEAR:
                if (npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                    npc.getCitizen().getOrAddTrait(AssignmentTrait.class).clearAssignments(player);
                    npc.getCitizen().removeTrait(AssignmentTrait.class);
                }
                break;
        }
    }
}
Also used : AssignmentTrait(com.denizenscript.denizen.npc.traits.AssignmentTrait) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) List(java.util.List) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)

Example 4 with InvalidArgumentsRuntimeException

use of com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException in project Denizen-For-Bukkit by DenizenScript.

the class ClickableCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ScriptTag script = scriptEntry.getObjectTag("script");
    ElementTag path = scriptEntry.getElement("path");
    ElementTag cancel = scriptEntry.argForPrefixAsElement("cancel", null);
    List<PlayerTag> forPlayers = scriptEntry.argForPrefixList("for", PlayerTag.class, true);
    ElementTag usages = scriptEntry.argForPrefixAsElement("usages", null);
    ListTag definitions = scriptEntry.argForPrefix("def", ListTag.class, true);
    DurationTag until = scriptEntry.argForPrefix("until", DurationTag.class, true);
    MapTag defMap = scriptEntry.getObjectTag("def_map");
    if (script == null && cancel == null) {
        throw new InvalidArgumentsRuntimeException("Missing script argument!");
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), script, cancel, path, usages, definitions, defMap, until, db("for", forPlayers));
    }
    if (cancel != null) {
        UUID id;
        try {
            id = UUID.fromString(cancel.asString());
        } catch (IllegalArgumentException ex) {
            Debug.echoError("Invalid cancel ID: " + ex.getMessage());
            return;
        }
        Clickable clicky = clickables.remove(id);
        if (clicky == null) {
            Debug.echoDebug(scriptEntry, "Cancelled ID didn't exist, nothing to cancel.");
        } else {
            Debug.echoDebug(scriptEntry, "Cancelled.");
        }
        return;
    }
    UUID id = UUID.randomUUID();
    Clickable newClickable = new Clickable();
    newClickable.script = script;
    newClickable.path = path == null ? null : path.asString();
    newClickable.definitions = definitions;
    newClickable.remainingUsages = usages == null ? -1 : usages.asInt();
    newClickable.until = until == null ? 0 : (System.currentTimeMillis() + until.getMillis());
    newClickable.context = scriptEntry.context;
    newClickable.npc = Utilities.getEntryNPC(scriptEntry);
    newClickable.defMap = defMap;
    if (forPlayers != null) {
        newClickable.forPlayers = new HashSet<>(forPlayers.size());
        for (PlayerTag player : forPlayers) {
            newClickable.forPlayers.add(player.getUUID());
        }
    }
    clickables.put(id, newClickable);
    scriptEntry.addObject("command", new ElementTag("/denizenclickable " + id));
    scriptEntry.addObject("id", new ElementTag(id.toString()));
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException)

Aggregations

InvalidArgumentsRuntimeException (com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException)4 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)3 NPCTag (com.denizenscript.denizen.objects.NPCTag)2 Denizen (com.denizenscript.denizen.Denizen)1 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)1 AssignmentTrait (com.denizenscript.denizen.npc.traits.AssignmentTrait)1 EntityTag (com.denizenscript.denizen.objects.EntityTag)1 AssignmentScriptContainer (com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer)1 Utilities (com.denizenscript.denizen.utilities.Utilities)1 Debug (com.denizenscript.denizen.utilities.debugging.Debug)1 FakeEntity (com.denizenscript.denizen.utilities.entity.FakeEntity)1 NetworkInterceptHelper (com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper)1 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)1 Argument (com.denizenscript.denizencore.objects.Argument)1 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)1 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)1 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)1 AbstractCommand (com.denizenscript.denizencore.scripts.commands.AbstractCommand)1 java.util (java.util)1 List (java.util.List)1