Search in sources :

Example 46 with PlayerTag

use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.

the class BukkitQueueProperties method registerTags.

public static void registerTags() {
    // <--[tag]
    // @attribute <QueueTag.npc>
    // @returns NPCTag
    // @mechanism QueueTag.linked_npc
    // @description
    // Returns the NPCTag linked to a queue.
    // -->
    PropertyParser.<BukkitQueueProperties, NPCTag>registerTag(NPCTag.class, "npc", (attribute, object) -> {
        NPCTag npc = null;
        if (object.queue.getLastEntryExecuted() != null) {
            npc = ((BukkitScriptEntryData) object.queue.getLastEntryExecuted().entryData).getNPC();
        } else if (object.queue.getEntries().size() > 0) {
            npc = ((BukkitScriptEntryData) object.queue.getEntries().get(0).entryData).getNPC();
        } else if (!attribute.hasAlternative()) {
            attribute.echoError("Can't determine a linked NPC.");
        }
        return npc;
    });
    // <--[tag]
    // @attribute <QueueTag.player>
    // @returns PlayerTag
    // @mechanism QueueTag.linked_player
    // @description
    // Returns the PlayerTag linked to a queue.
    // -->
    PropertyParser.<BukkitQueueProperties, PlayerTag>registerTag(PlayerTag.class, "player", (attribute, object) -> {
        PlayerTag player = null;
        if (object.queue.getLastEntryExecuted() != null) {
            player = ((BukkitScriptEntryData) object.queue.getLastEntryExecuted().entryData).getPlayer();
        } else if (object.queue.getEntries().size() > 0) {
            player = ((BukkitScriptEntryData) object.queue.getEntries().get(0).entryData).getPlayer();
        } else {
            attribute.echoError("Can't determine a linked player.");
        }
        return player;
    });
}
Also used : BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NPCTag(com.denizenscript.denizen.objects.NPCTag)

Example 47 with PlayerTag

use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.

the class BukkitQueueProperties method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("linked_player") && mechanism.requireObject(PlayerTag.class)) {
        PlayerTag player = mechanism.valueAsType(PlayerTag.class);
        for (ScriptEntry entry : queue.getEntries()) {
            BukkitScriptEntryData data = (BukkitScriptEntryData) entry.entryData;
            data.setPlayer(player);
        }
    }
    // -->
    if (mechanism.matches("linked_npc") && mechanism.requireObject(NPCTag.class)) {
        NPCTag npc = mechanism.valueAsType(NPCTag.class);
        for (ScriptEntry entry : queue.getEntries()) {
            BukkitScriptEntryData data = (BukkitScriptEntryData) entry.entryData;
            data.setNPC(npc);
        }
    }
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BukkitScriptEntryData(com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData) NPCTag(com.denizenscript.denizen.objects.NPCTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry)

Example 48 with PlayerTag

use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.

the class ZapCommand method parseArgs.

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
    for (Argument arg : scriptEntry) {
        if (!scriptEntry.hasObject("script") && !scriptEntry.hasObject("step") && arg.hasPrefix() && arg.getPrefix().matchesArgumentType(ScriptTag.class)) {
            Deprecations.zapPrefix.warn(scriptEntry);
            scriptEntry.addObject("script", arg.getPrefix().asType(ScriptTag.class));
            scriptEntry.addObject("step", arg.asElement());
        } else if (!scriptEntry.hasObject("script") && arg.matchesArgumentType(ScriptTag.class) && arg.asType(ScriptTag.class).getContainer() instanceof InteractScriptContainer && arg.limitToOnlyPrefix("script")) {
            scriptEntry.addObject("script", arg.asType(ScriptTag.class));
        } else if (!scriptEntry.hasObject("step") && arg.limitToOnlyPrefix("step")) {
            scriptEntry.addObject("step", arg.asElement());
        } else if (!scriptEntry.hasObject("duration") && arg.matchesArgumentType(DurationTag.class) && arg.limitToOnlyPrefix("duration")) {
            scriptEntry.addObject("duration", arg.asType(DurationTag.class));
        } else {
            arg.reportUnhandled();
        }
    }
    PlayerTag player = Utilities.getEntryPlayer(scriptEntry);
    if (player == null || !player.isValid()) {
        throw new InvalidArgumentsException("Must have player context!");
    }
    if (!scriptEntry.hasObject("script")) {
        ScriptTag script = scriptEntry.getScript();
        if (script != null) {
            if (script.getContainer() instanceof InteractScriptContainer) {
                scriptEntry.addObject("script", script);
            } else if (script.getContainer() instanceof AssignmentScriptContainer) {
                InteractScriptContainer interact = ((AssignmentScriptContainer) script.getContainer()).interact;
                if (interact != null) {
                    scriptEntry.addObject("script", new ScriptTag(interact));
                }
            }
        }
        if (!scriptEntry.hasObject("script")) {
            NPCTag npc = Utilities.getEntryNPC(scriptEntry);
            if (npc != null && npc.getCitizen().hasTrait(AssignmentTrait.class)) {
                AssignmentTrait trait = npc.getCitizen().getOrAddTrait(AssignmentTrait.class);
                for (AssignmentScriptContainer container : trait.containerCache) {
                    if (container != null && container.getInteract() != null) {
                        scriptEntry.addObject("script", new ScriptTag(container.getInteract()));
                        break;
                    }
                }
            }
        }
        if (!scriptEntry.hasObject("script")) {
            throw new InvalidArgumentsException("No script to zap! Must be in an interact script, or have a linked NPC with an associated interact script.");
        }
    }
}
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) InteractScriptContainer(com.denizenscript.denizen.scripts.containers.core.InteractScriptContainer) AssignmentScriptContainer(com.denizenscript.denizen.scripts.containers.core.AssignmentScriptContainer) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException)

Example 49 with PlayerTag

use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.

the class AttachCommand method execute.

@Override
public void execute(final ScriptEntry scriptEntry) {
    LocationTag offset = scriptEntry.getObjectTag("offset");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    EntityTag target = scriptEntry.getObjectTag("to");
    List<PlayerTag> forPlayers = (List<PlayerTag>) scriptEntry.getObject("for");
    ElementTag cancel = scriptEntry.getElement("cancel");
    ElementTag relative = scriptEntry.getElement("relative");
    ElementTag sync_server = scriptEntry.getElement("sync_server");
    ElementTag no_rotate = scriptEntry.getElement("no_rotate");
    ElementTag no_pitch = scriptEntry.getElement("no_pitch");
    ElementTag yaw_offset = scriptEntry.getElement("yaw_offset");
    ElementTag pitch_offset = scriptEntry.getElement("pitch_offset");
    boolean shouldCancel = cancel.asBoolean();
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), db("entities", entities), shouldCancel ? cancel : target, relative, offset, yaw_offset, pitch_offset, sync_server, no_rotate, no_pitch, db("for", forPlayers));
    }
    BiConsumer<EntityTag, UUID> procPlayer = (entity, player) -> {
        if (shouldCancel) {
            EntityAttachmentHelper.removeAttachment(entity.getUUID(), player);
        } else {
            EntityAttachmentHelper.AttachmentData attachment = new EntityAttachmentHelper.AttachmentData();
            attachment.attached = entity;
            attachment.to = target;
            attachment.positionalOffset = offset == null ? null : offset.clone();
            attachment.offsetRelative = relative.asBoolean();
            attachment.yawAngleOffset = yaw_offset.asFloat();
            attachment.pitchAngleOffset = pitch_offset.asFloat();
            attachment.syncServer = sync_server.asBoolean();
            attachment.forPlayer = player;
            attachment.noRotate = no_rotate.asBoolean();
            attachment.noPitch = no_pitch.asBoolean();
            EntityAttachmentHelper.registerAttachment(attachment);
        }
    };
    for (EntityTag entity : entities) {
        if (!entity.isSpawned() && !entity.isFake && !shouldCancel) {
            Debug.echoError("Cannot attach entity '" + entity + "': entity is not spawned.");
            continue;
        }
        if (forPlayers == null) {
            procPlayer.accept(entity, null);
        } else {
            for (PlayerTag player : forPlayers) {
                procPlayer.accept(entity, player.getUUID());
            }
        }
    }
}
Also used : ListTag(com.denizenscript.denizencore.objects.core.ListTag) LocationTag(com.denizenscript.denizen.objects.LocationTag) UUID(java.util.UUID) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) InvalidArgumentsException(com.denizenscript.denizencore.exceptions.InvalidArgumentsException) Argument(com.denizenscript.denizencore.objects.Argument) List(java.util.List) BiConsumer(java.util.function.BiConsumer) EntityTag(com.denizenscript.denizen.objects.EntityTag) ScriptEntry(com.denizenscript.denizencore.scripts.ScriptEntry) Debug(com.denizenscript.denizen.utilities.debugging.Debug) AbstractCommand(com.denizenscript.denizencore.scripts.commands.AbstractCommand) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) EntityAttachmentHelper(com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) LocationTag(com.denizenscript.denizen.objects.LocationTag) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) EntityAttachmentHelper(com.denizenscript.denizen.utilities.entity.EntityAttachmentHelper) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) UUID(java.util.UUID)

Example 50 with PlayerTag

use of com.denizenscript.denizen.objects.PlayerTag in project Denizen-For-Bukkit by DenizenScript.

the class PlayerHelperImpl method sendEntitySpawn.

@Override
public FakeEntity sendEntitySpawn(List<PlayerTag> players, DenizenEntityType entityType, LocationTag location, ArrayList<Mechanism> mechanisms, int customId, UUID customUUID, boolean autoTrack) {
    CraftWorld world = ((CraftWorld) location.getWorld());
    net.minecraft.world.entity.Entity nmsEntity;
    if (entityType.isCustom()) {
        if (entityType.customEntityType == CustomEntityType.ITEM_PROJECTILE) {
            org.bukkit.inventory.ItemStack itemStack = new ItemStack(Material.STONE);
            for (Mechanism mechanism : mechanisms) {
                if (mechanism.matches("item") && mechanism.requireObject(ItemTag.class)) {
                    itemStack = mechanism.valueAsType(ItemTag.class).getItemStack();
                }
            }
            nmsEntity = new EntityItemProjectileImpl(world.getHandle(), location, CraftItemStack.asNMSCopy(itemStack));
        } else if (entityType.customEntityType == CustomEntityType.FAKE_PLAYER) {
            String name = null;
            String skin = null;
            for (Mechanism mechanism : new ArrayList<>(mechanisms)) {
                if (mechanism.matches("name")) {
                    name = mechanism.getValue().asString();
                    mechanisms.remove(mechanism);
                } else if (mechanism.matches("skin")) {
                    skin = mechanism.getValue().asString();
                    mechanisms.remove(mechanism);
                }
                if (name != null && skin != null) {
                    break;
                }
            }
            nmsEntity = ((CraftFakePlayerImpl) CustomEntityHelperImpl.spawnFakePlayer(location, name, skin, false)).getHandle();
        } else {
            throw new IllegalArgumentException("entityType");
        }
    } else {
        nmsEntity = world.createEntity(location, entityType.getBukkitEntityType().getEntityClass());
    }
    if (customUUID != null) {
        nmsEntity.setId(customId);
        nmsEntity.setUUID(customUUID);
    }
    EntityTag entity = new EntityTag(nmsEntity.getBukkitEntity());
    entity.isFake = true;
    entity.isFakeValid = true;
    for (Mechanism mechanism : mechanisms) {
        entity.safeAdjustDuplicate(mechanism);
    }
    nmsEntity.unsetRemoved();
    FakeEntity fake = new FakeEntity(players, location, entity.getBukkitEntity().getEntityId());
    fake.entity = new EntityTag(entity.getBukkitEntity());
    fake.entity.isFake = true;
    fake.entity.isFakeValid = true;
    List<TrackerData> trackers = new ArrayList<>();
    fake.triggerSpawnPacket = (player) -> {
        ServerPlayer nmsPlayer = ((CraftPlayer) player.getPlayerEntity()).getHandle();
        ServerGamePacketListenerImpl conn = nmsPlayer.connection;
        final ServerEntity tracker = new ServerEntity(world.getHandle(), nmsEntity, 1, true, conn::send, Collections.singleton(nmsPlayer.connection));
        tracker.addPairing(nmsPlayer);
        final TrackerData data = new TrackerData();
        data.player = player;
        data.tracker = tracker;
        trackers.add(data);
        if (autoTrack) {
            new BukkitRunnable() {

                boolean wasOnline = true;

                @Override
                public void run() {
                    if (!fake.entity.isFakeValid) {
                        cancel();
                        return;
                    }
                    if (player.isOnline()) {
                        if (!wasOnline) {
                            tracker.addPairing(((CraftPlayer) player.getPlayerEntity()).getHandle());
                            wasOnline = true;
                        }
                        tracker.sendChanges();
                    } else if (wasOnline) {
                        wasOnline = false;
                    }
                }
            }.runTaskTimer(Denizen.getInstance(), 1, 1);
        }
    };
    for (PlayerTag player : players) {
        fake.triggerSpawnPacket.accept(player);
    }
    fake.triggerUpdatePacket = () -> {
        for (TrackerData tracker : trackers) {
            if (tracker.player.isOnline()) {
                tracker.tracker.sendChanges();
            }
        }
    };
    fake.triggerDestroyPacket = () -> {
        for (TrackerData tracker : trackers) {
            if (tracker.player.isOnline()) {
                tracker.tracker.removePairing(((CraftPlayer) tracker.player.getPlayerEntity()).getHandle());
            }
        }
        trackers.clear();
    };
    return fake;
}
Also used : FakeEntity(com.denizenscript.denizen.utilities.entity.FakeEntity) EntityItemProjectileImpl(com.denizenscript.denizen.nms.v1_17.impl.entities.EntityItemProjectileImpl) CraftFakePlayerImpl(com.denizenscript.denizen.nms.v1_17.impl.entities.CraftFakePlayerImpl) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) Mechanism(com.denizenscript.denizencore.objects.Mechanism) ServerEntity(net.minecraft.server.level.ServerEntity) EntityTag(com.denizenscript.denizen.objects.EntityTag) CraftWorld(org.bukkit.craftbukkit.v1_17_R1.CraftWorld) ServerGamePacketListenerImpl(net.minecraft.server.network.ServerGamePacketListenerImpl) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) CraftPlayer(org.bukkit.craftbukkit.v1_17_R1.entity.CraftPlayer) ItemStack(org.bukkit.inventory.ItemStack) ServerPlayer(net.minecraft.server.level.ServerPlayer) ItemStack(org.bukkit.inventory.ItemStack) CraftItemStack(org.bukkit.craftbukkit.v1_17_R1.inventory.CraftItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Aggregations

PlayerTag (com.denizenscript.denizen.objects.PlayerTag)81 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)41 Player (org.bukkit.entity.Player)26 List (java.util.List)23 NPCTag (com.denizenscript.denizen.objects.NPCTag)22 EntityTag (com.denizenscript.denizen.objects.EntityTag)16 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)15 ListTag (com.denizenscript.denizencore.objects.core.ListTag)15 LocationTag (com.denizenscript.denizen.objects.LocationTag)14 ItemTag (com.denizenscript.denizen.objects.ItemTag)12 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)11 EventHandler (org.bukkit.event.EventHandler)11 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)8 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)8 ScriptEntry (com.denizenscript.denizencore.scripts.ScriptEntry)8 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)7 BukkitScriptEntryData (com.denizenscript.denizen.utilities.implementation.BukkitScriptEntryData)7 ObjectTag (com.denizenscript.denizencore.objects.ObjectTag)7 FakeEntity (com.denizenscript.denizen.utilities.entity.FakeEntity)6 Argument (com.denizenscript.denizencore.objects.Argument)6