Search in sources :

Example 11 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.

the class InventoryHolder method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <InventoryTag.id_holder>
    // @returns ObjectTag
    // @group properties
    // @description
    // Returns Denizen's holder ID for this inventory. (player object, location object, etc.)
    // -->
    PropertyParser.<InventoryHolder, ObjectTag>registerTag(ObjectTag.class, "id_holder", (attribute, object) -> {
        return object.inventory.getIdHolder();
    });
    // <--[tag]
    // @attribute <InventoryTag.script>
    // @returns ScriptTag
    // @group properties
    // @description
    // Returns the script that this inventory came from (if any).
    // -->
    PropertyParser.<InventoryHolder, ScriptTag>registerTag(ScriptTag.class, "script", (attribute, object) -> {
        ObjectTag holder = object.inventory.getIdHolder();
        if (holder instanceof ScriptTag) {
            return (ScriptTag) holder;
        }
        return null;
    });
}
Also used : ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag)

Example 12 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.

the class RenameCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    final ElementTag name = scriptEntry.getElement("name");
    ElementTag perPlayer = scriptEntry.getElement("per_player");
    ElementTag listNameOnly = scriptEntry.getElement("list_name_only");
    ListTag targets = scriptEntry.getObjectTag("targets");
    List<PlayerTag> players = (List<PlayerTag>) scriptEntry.getObject("players");
    if (perPlayer != null && perPlayer.asBoolean()) {
        NetworkInterceptHelper.enable();
        if (scriptEntry.dbCallShouldDebug()) {
            Debug.report(scriptEntry, getName(), name, targets, perPlayer, listNameOnly, db("for", players));
        }
        for (ObjectTag target : targets.objectForms) {
            EntityTag entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
            if (entity != null) {
                Entity bukkitEntity = entity.getBukkitEntity();
                if (bukkitEntity == null) {
                    Debug.echoError("Invalid entity in rename command.");
                    continue;
                }
                if (name.asString().equals("cancel")) {
                    customNames.remove(bukkitEntity.getUniqueId());
                    if (bukkitEntity.isCustomNameVisible()) {
                        if (players == null) {
                            for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
                                NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, bukkitEntity.getCustomName(), false);
                            }
                        } else {
                            for (PlayerTag player : players) {
                                NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, bukkitEntity.getCustomName(), false);
                            }
                        }
                    } else {
                        bukkitEntity.setCustomNameVisible(true);
                        // Force a metadata update
                        bukkitEntity.setCustomNameVisible(false);
                    }
                } else {
                    final BukkitTagContext originalContext = (BukkitTagContext) scriptEntry.context.clone();
                    HashMap<UUID, RenameData> playerToFuncMap = customNames.computeIfAbsent(bukkitEntity.getUniqueId(), k -> new HashMap<>());
                    Function<Player, String> nameGetter = p -> {
                        originalContext.player = new PlayerTag(p);
                        return TagManager.tag(name.asString(), originalContext);
                    };
                    RenameData renamer = new RenameData();
                    renamer.nameFunction = nameGetter;
                    renamer.listOnly = listNameOnly != null && listNameOnly.asBoolean();
                    if (players == null) {
                        playerToFuncMap.put(null, renamer);
                    } else {
                        for (PlayerTag player : players) {
                            playerToFuncMap.put(player.getUUID(), renamer);
                        }
                    }
                    if (players == null) {
                        for (Player player : NMSHandler.getEntityHelper().getPlayersThatSee(bukkitEntity)) {
                            NMSHandler.getPacketHelper().sendRename(player, bukkitEntity, "", renamer.listOnly);
                        }
                    } else {
                        for (PlayerTag player : players) {
                            NMSHandler.getPacketHelper().sendRename(player.getPlayerEntity(), bukkitEntity, "", renamer.listOnly);
                        }
                    }
                }
            }
        }
        return;
    }
    String nameString = TagManager.tag(name.asString(), scriptEntry.context);
    if (nameString.length() > 256) {
        nameString = nameString.substring(0, 256);
    }
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("name", nameString), listNameOnly, targets);
    }
    for (ObjectTag target : targets.objectForms) {
        EntityFormObject entity = target.asType(EntityTag.class, CoreUtilities.noDebugContext);
        if (entity == null) {
            entity = target.asType(NPCTag.class, scriptEntry.context);
        } else {
            entity = ((EntityTag) entity).getDenizenObject();
        }
        if (entity == null) {
            Debug.echoError("Invalid entity in rename command.");
            continue;
        }
        if (entity instanceof NPCTag) {
            NPC npc = ((NPCTag) entity).getCitizen();
            if (npc.isSpawned()) {
                Location prev = npc.getStoredLocation().clone();
                npc.despawn(DespawnReason.PENDING_RESPAWN);
                npc.setName(nameString);
                npc.spawn(prev);
            } else {
                npc.setName(nameString);
            }
        } else if (entity instanceof PlayerTag) {
            if (listNameOnly != null && listNameOnly.asBoolean()) {
                AdvancedTextImpl.instance.setPlayerListName(((PlayerTag) entity).getPlayerEntity(), nameString);
            } else {
                String limitedName = nameString.length() > 16 ? nameString.substring(0, 16) : nameString;
                NMSHandler.getInstance().getProfileEditor().setPlayerName(((PlayerTag) entity).getPlayerEntity(), limitedName);
            }
        } else {
            Entity bukkitEntity = entity.getDenizenEntity().getBukkitEntity();
            customNames.remove(bukkitEntity.getUniqueId());
            bukkitEntity.setCustomName(nameString);
            bukkitEntity.setCustomNameVisible(true);
        }
    }
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) TagManager(com.denizenscript.denizencore.tags.TagManager) AdvancedTextImpl(com.denizenscript.denizen.utilities.AdvancedTextImpl) NMSHandler(com.denizenscript.denizen.nms.NMSHandler) HashMap(java.util.HashMap) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Function(java.util.function.Function) Argument(com.denizenscript.denizencore.objects.Argument) NPC(net.citizensnpcs.api.npc.NPC) Location(org.bukkit.Location) EntityFormObject(com.denizenscript.denizen.objects.EntityFormObject) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) NetworkInterceptHelper(com.denizenscript.denizen.utilities.packets.NetworkInterceptHelper) Entity(org.bukkit.entity.Entity) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) UUID(java.util.UUID) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) CoreUtilities(com.denizenscript.denizencore.utilities.CoreUtilities) DespawnReason(net.citizensnpcs.api.event.DespawnReason) NPC(net.citizensnpcs.api.npc.NPC) Entity(org.bukkit.entity.Entity) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) EntityFormObject(com.denizenscript.denizen.objects.EntityFormObject) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) List(java.util.List) EntityTag(com.denizenscript.denizen.objects.EntityTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID) Location(org.bukkit.Location)

Example 13 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.

the class ClickableCommand method runClickable.

public static void runClickable(UUID id, Player player) {
    Clickable clickable = clickables.get(id);
    if (clickable == null) {
        return;
    }
    if (clickable.until != 0 && System.currentTimeMillis() > clickable.until) {
        clickables.remove(id);
        return;
    }
    if (clickable.forPlayers != null && !clickable.forPlayers.contains(player.getUniqueId())) {
        return;
    }
    if (clickable.remainingUsages > 0) {
        clickable.remainingUsages--;
        if (clickable.remainingUsages <= 0) {
            clickables.remove(id);
        }
    }
    Consumer<ScriptQueue> configure = (queue) -> {
        if (clickable.defMap != null) {
            for (Map.Entry<StringHolder, ObjectTag> val : clickable.defMap.map.entrySet()) {
                queue.addDefinition(val.getKey().str, val.getValue());
            }
        }
    };
    ScriptUtilities.createAndStartQueue(clickable.script.getContainer(), clickable.path, new BukkitScriptEntryData(new PlayerTag(player), clickable.npc), null, configure, null, null, clickable.definitions, clickable.context);
}
Also used : Utilities(com.denizenscript.denizen.utilities.Utilities) InvalidArgumentsRuntimeException(com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException) java.util(java.util) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) com.denizenscript.denizencore.objects.core(com.denizenscript.denizencore.objects.core) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) Player(org.bukkit.entity.Player) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Argument(com.denizenscript.denizencore.objects.Argument) Consumer(java.util.function.Consumer) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue) ScriptUtilities(com.denizenscript.denizencore.utilities.ScriptUtilities) NPCTag(com.denizenscript.denizen.objects.NPCTag) TagContext(com.denizenscript.denizencore.tags.TagContext) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) TaskScriptContainer(com.denizenscript.denizencore.scripts.containers.core.TaskScriptContainer) StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ScriptQueue(com.denizenscript.denizencore.scripts.queues.ScriptQueue)

Example 14 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag 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 15 with ObjectTag

use of com.denizenscript.denizencore.objects.ObjectTag in project Denizen-For-Bukkit by DenizenScript.

the class DamageTrigger method damageTrigger.

// <--[language]
// @name Damage Triggers
// @group NPC Interact Scripts
// @description
// Damage Triggers are triggered when when a player left clicks the NPC.
// Despite the name, these do not actually require the NPC take any damage, only that the player left clicks the NPC.
// 
// In scripts, use <context.damage> to measure how much damage was done to the NPC
// (though note that invincible NPCs don't necessarily take any damage even when this is non-zero).
// 
// These are very basic with no extraneous complexity.
// 
// -->
// <--[action]
// @Actions
// no damage trigger
// 
// @Triggers when the NPC is damaged by a player but no damage trigger fires.
// 
// @Context
// None
// 
// -->
// Technically defined in TriggerTrait, but placing here instead.
// <--[action]
// @Actions
// damage
// 
// @Triggers when the NPC is damaged by a player.
// 
// @Context
// <context.damage> returns how much damage was done.
// 
// @Determine
// "cancelled" to cancel the damage event.
// 
// -->
// <--[action]
// @Actions
// damaged
// 
// @Triggers when the NPC is damaged by an entity.
// 
// @Context
// <context.damage> returns how much damage was done.
// <context.damager> returns the entity that did the damage.
// 
// @Determine
// "cancelled" to cancel the damage event.
// 
// -->
@EventHandler
public void damageTrigger(EntityDamageByEntityEvent event) {
    Map<String, ObjectTag> context = new HashMap<>();
    context.put("damage", new ElementTag(event.getDamage()));
    if (CitizensAPI.getNPCRegistry().isNPC(event.getEntity())) {
        NPCTag npc = new NPCTag(CitizensAPI.getNPCRegistry().getNPC(event.getEntity()));
        if (npc == null) {
            return;
        }
        if (npc.getCitizen() == null) {
            return;
        }
        EntityTag damager = new EntityTag(event.getDamager());
        if (damager.isProjectile() && damager.hasShooter()) {
            damager = damager.getShooter();
        }
        context.put("damager", damager.getDenizenObject());
        ListTag determ = npc.action("damaged", null, context);
        if (determ != null && determ.containsCaseInsensitive("cancelled")) {
            event.setCancelled(true);
            return;
        }
        if (!damager.isPlayer()) {
            return;
        }
        PlayerTag dplayer = damager.getDenizenPlayer();
        if (!npc.getCitizen().hasTrait(TriggerTrait.class)) {
            return;
        }
        if (!npc.getTriggerTrait().isEnabled(name)) {
            return;
        }
        TriggerTrait.TriggerContext trigger = npc.getTriggerTrait().trigger(this, dplayer);
        if (!trigger.wasTriggered()) {
            return;
        }
        if (trigger.hasDetermination() && trigger.getDeterminations().containsCaseInsensitive("cancelled")) {
            event.setCancelled(true);
            return;
        }
        List<InteractScriptContainer> scripts = InteractScriptHelper.getInteractScripts(npc, dplayer, true, ClickTrigger.class);
        boolean any = false;
        if (scripts != null) {
            for (InteractScriptContainer script : scripts) {
                String id = null;
                Map<String, String> idMap = script.getIdMapFor(ClickTrigger.class, dplayer);
                if (!idMap.isEmpty()) {
                    for (Map.Entry<String, String> entry : idMap.entrySet()) {
                        String entry_value = TagManager.tag(entry.getValue(), new BukkitTagContext(dplayer, npc, null, false, new ScriptTag(script)));
                        if (ItemTag.valueOf(entry_value, script).comparesTo(dplayer.getPlayerEntity().getEquipment().getItemInMainHand()) >= 0) {
                            id = entry.getKey();
                        }
                    }
                }
                if (parse(npc, dplayer, script, id, context)) {
                    any = true;
                }
            }
        }
        if (!any) {
            npc.action("no damage trigger", dplayer);
        }
    }
}
Also used : TriggerTrait(com.denizenscript.denizen.npc.traits.TriggerTrait) HashMap(java.util.HashMap) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) BukkitTagContext(com.denizenscript.denizen.tags.BukkitTagContext) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptTag(com.denizenscript.denizencore.objects.core.ScriptTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) HashMap(java.util.HashMap) Map(java.util.Map) EventHandler(org.bukkit.event.EventHandler)

Aggregations

ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)38 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)21 MapTag (com.denizenscript.denizencore.objects.core.MapTag)19 ListTag (com.denizenscript.denizencore.objects.core.ListTag)16 StringHolder (com.denizenscript.denizencore.utilities.text.StringHolder)11 NPCTag (com.denizenscript.denizen.objects.NPCTag)8 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)7 ItemTag (com.denizenscript.denizen.objects.ItemTag)6 ArrayList (java.util.ArrayList)6 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 EntityTag (com.denizenscript.denizen.objects.EntityTag)4 UUID (java.util.UUID)4 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)3 AbstractCommand (com.denizenscript.denizencore.scripts.commands.AbstractCommand)3 AttributeModifier (org.bukkit.attribute.AttributeModifier)3 Player (org.bukkit.entity.Player)3 TriggerTrait (com.denizenscript.denizen.npc.traits.TriggerTrait)2