Search in sources :

Example 6 with ScriptEntry

use of com.denizenscript.denizencore.scripts.ScriptEntry 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 7 with ScriptEntry

use of com.denizenscript.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class TeamCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag id = scriptEntry.getElement("id");
    ElementTag name = scriptEntry.getElement("name");
    ListTag add = scriptEntry.getObjectTag("add");
    ListTag remove = scriptEntry.getObjectTag("remove");
    ElementTag prefix = scriptEntry.getElement("prefix");
    ElementTag suffix = scriptEntry.getElement("suffix");
    ElementTag option = scriptEntry.getElement("option");
    ElementTag status = scriptEntry.getElement("status");
    ElementTag color = scriptEntry.getElement("color");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), id, name, add, remove, prefix, suffix, color, option, status);
    }
    Scoreboard board;
    if (id.asString().equalsIgnoreCase("main")) {
        board = ScoreboardHelper.getMain();
    } else {
        if (ScoreboardHelper.hasScoreboard(id.asString())) {
            board = ScoreboardHelper.getScoreboard(id.asString());
        } else {
            board = ScoreboardHelper.createScoreboard(id.asString());
        }
    }
    Team team = board.getTeam(name.asString());
    if (team == null) {
        String low = CoreUtilities.toLowerCase(name.asString());
        team = board.getTeams().stream().filter(t -> CoreUtilities.toLowerCase(t.getName()).equals(low)).findFirst().orElse(null);
        if (team == null) {
            team = board.registerNewTeam(name.asString());
        }
    }
    if (add != null) {
        for (String string : add) {
            string = translateEntry(string, scriptEntry.context);
            if (!team.hasEntry(string)) {
                team.addEntry(string);
            }
        }
    }
    if (remove != null) {
        for (String string : remove) {
            string = translateEntry(string, scriptEntry.context);
            if (team.hasEntry(string)) {
                team.removeEntry(string);
            }
        }
    }
    if (option != null) {
        String optName = CoreUtilities.toLowerCase(option.asString());
        String statusName = CoreUtilities.toLowerCase(status.asString());
        if (optName.equals("friendly_fire")) {
            team.setAllowFriendlyFire(statusName.equals("always"));
        } else if (optName.equals("see_invisible")) {
            team.setCanSeeFriendlyInvisibles(statusName.equals("always"));
        } else {
            team.setOption(Team.Option.valueOf(optName.toUpperCase()), Team.OptionStatus.valueOf(statusName.toUpperCase()));
        }
    }
    if (prefix != null) {
        team.setPrefix(prefix.asString());
    }
    if (suffix != null) {
        team.setSuffix(suffix.asString());
    }
    if (color != null) {
        team.setColor(ChatColor.valueOf(color.asString().toUpperCase()));
    }
    if (team.getEntries().isEmpty()) {
        team.unregister();
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) Team(org.bukkit.scoreboard.Team) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Argument(com.denizenscript.denizencore.objects.Argument) Scoreboard(org.bukkit.scoreboard.Scoreboard) ScoreboardHelper(com.denizenscript.denizen.utilities.ScoreboardHelper) TagContext(com.denizenscript.denizencore.tags.TagContext) EntityTag(com.denizenscript.denizen.objects.EntityTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ChatColor(org.bukkit.ChatColor) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) Scoreboard(org.bukkit.scoreboard.Scoreboard) Team(org.bukkit.scoreboard.Team) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 8 with ScriptEntry

use of com.denizenscript.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class BreakCommand method execute.

// <--[action]
// @Actions
// dig
// 
// @Triggers when the NPC breaks a block with the Break Command
// 
// @Context
// <context.location> returns the location the NPC Dug
// <context.material> Returns the Block dug
// 
// -->
@Override
public void execute(ScriptEntry scriptEntry) {
    final LocationTag location = scriptEntry.getObjectTag("location");
    final NPCTag npc = scriptEntry.getObjectTag("npc");
    ElementTag radius = scriptEntry.getElement("radius");
    final HashMap<String, ObjectTag> context = new HashMap<>();
    MaterialTag material = new MaterialTag(location.getBlock());
    context.put("location", location);
    context.put("material", material);
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), location, npc, radius);
    }
    final ScriptEntry se = scriptEntry;
    BlockBreaker.BlockBreakerConfiguration config = new BlockBreaker.BlockBreakerConfiguration();
    config.item(npc.getLivingEntity().getEquipment().getItemInMainHand());
    config.radius(radius.asDouble());
    config.callback(() -> {
        npc.action("dig", null, context);
        se.setFinished(true);
    });
    BlockBreaker breaker = npc.getCitizen().getBlockBreaker(location.getBlock(), config);
    if (breaker.shouldExecute()) {
        TaskRunnable run = new TaskRunnable(breaker);
        run.taskId = Bukkit.getScheduler().scheduleSyncRepeatingTask(Denizen.getInstance(), run, 0, 1);
    } else {
        se.setFinished(true);
    }
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) HashMap(java.util.HashMap) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) BlockBreaker(net.citizensnpcs.api.npc.BlockBreaker) LocationTag(com.denizenscript.denizen.objects.LocationTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 9 with ScriptEntry

use of com.denizenscript.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class AbstractTrigger method parse.

public boolean parse(NPCTag npc, PlayerTag player, InteractScriptContainer script, String id, Map<String, ObjectTag> context) {
    if (npc == null || player == null || script == null) {
        return false;
    }
    List<ScriptEntry> entries = script.getEntriesFor(this.getClass(), player, npc, id, true);
    if (entries.isEmpty()) {
        return false;
    }
    Debug.echoDebug(script, DebugElement.Header, "Parsing " + name + " trigger: n@" + npc.getName() + "/p@" + player.getName());
    // Create Queue
    long speedTicks;
    if (script.contains("SPEED", String.class)) {
        speedTicks = DurationTag.valueOf(script.getString("SPEED", "0"), new BukkitTagContext(script)).getTicks();
    } else {
        speedTicks = DurationTag.valueOf(Settings.interactQueueSpeed(), new BukkitTagContext(script)).getTicks();
    }
    ScriptQueue queue;
    if (speedTicks > 0) {
        queue = new TimedQueue(script.getName()).setSpeed(speedTicks);
    } else {
        queue = new InstantQueue(script.getName());
    }
    // Add all entries to set it up
    queue.addEntries(entries);
    // Add context
    if (context != null) {
        ContextSource.SimpleMap src = new ContextSource.SimpleMap();
        src.contexts = context;
        queue.setContextSource(src);
    }
    if (!npc.getTriggerTrait().properly_set.get(name)) {
        if (missetWarning.testShouldWarn()) {
            Debug.echoError(missetWarning.message.replace("{NAME}", name).replace("{NPC}", npc.getId() + "/" + npc.getName()));
        }
    }
    // Start it
    queue.start();
    return true;
}
Also used : BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) TimedQueue(com.denizenscript.denizencore.scripts.queues.core.TimedQueue) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 10 with ScriptEntry

use of com.denizenscript.denizencore.scripts.ScriptEntry in project Denizen-For-Bukkit by DenizenScript.

the class CommandScriptContainer method runAllowedHelpProcedure.

public boolean runAllowedHelpProcedure(PlayerTag player, NPCTag npc, Map<String, ObjectTag> context) {
    List<ScriptEntry> entries = getEntries(new BukkitScriptEntryData(player, npc), "allowed help");
    ScriptQueue queue = new InstantQueue(getName());
    queue.addEntries(entries);
    if (context != null) {
        ContextSource.SimpleMap src = new ContextSource.SimpleMap();
        src.contexts = context;
        queue.setContextSource(src);
    }
    queue.start();
    return queue.determinations != null && queue.determinations.size() > 0 && queue.determinations.get(0).equalsIgnoreCase("true");
}
Also used : BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) ContextSource(com.denizenscript.denizencore.scripts.queues.ContextSource) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) InstantQueue(com.denizenscript.denizencore.scripts.queues.core.InstantQueue) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Aggregations

ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)23 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)11 EntityTag (com.denizenscript.denizen.objects.EntityTag)10 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)9 NPCTag (com.denizenscript.denizen.objects.NPCTag)8 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)8 ListTag (com.denizenscript.denizencore.objects.core.ListTag)8 AbstractCommand (com.denizenscript.denizencore.scripts.commands.AbstractCommand)8 List (java.util.List)8 Debug (com.denizenscript.denizen.utilities.debugging.Debug)7 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)7 ScriptQueue (com.denizenscript.denizencore.scripts.queues.ScriptQueue)7 InstantQueue (com.denizenscript.denizencore.scripts.queues.core.InstantQueue)7 NMSHandler (com.denizenscript.denizen.nms.NMSHandler)6 Utilities (com.denizenscript.denizen.utilities.Utilities)6 Argument (com.denizenscript.denizencore.objects.Argument)6 ContextSource (com.denizenscript.denizencore.scripts.queues.ContextSource)5 Player (org.bukkit.entity.Player)5 Denizen (com.denizenscript.denizen.Denizen)4 LocationTag (com.denizenscript.denizen.objects.LocationTag)4