Search in sources :

Example 71 with LocationTag

use of com.denizenscript.denizen.objects.LocationTag 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 72 with LocationTag

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

the class LookCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    final LocationTag loc = scriptEntry.getObjectTag("location");
    List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
    final DurationTag duration = scriptEntry.getObjectTag("duration");
    ElementTag yaw = scriptEntry.getElement("yaw");
    ElementTag pitch = scriptEntry.getElement("pitch");
    ElementTag cancel = scriptEntry.getElement("cancel");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), cancel, loc, duration, yaw, pitch, db("entities", entities));
    }
    for (EntityTag entity : entities) {
        if (entity.isSpawned()) {
            BukkitTask task = lookTasks.remove(entity.getUUID());
            if (task != null) {
                task.cancel();
            }
        }
    }
    if (cancel != null && cancel.asBoolean()) {
        return;
    }
    final float yawRaw = yaw == null ? 0 : yaw.asFloat();
    final float pitchRaw = pitch == null ? 0 : pitch.asFloat();
    for (EntityTag entity : entities) {
        if (entity.isSpawned()) {
            if (loc != null) {
                NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
            } else {
                NMSHandler.getEntityHelper().rotate(entity.getBukkitEntity(), yawRaw, pitchRaw);
            }
        }
    }
    if (duration != null && duration.getTicks() > 2) {
        for (EntityTag entity : entities) {
            BukkitRunnable task = new BukkitRunnable() {

                long bounces = 0;

                public void run() {
                    bounces += 2;
                    if (bounces > duration.getTicks()) {
                        this.cancel();
                        lookTasks.remove(entity.getUUID());
                        return;
                    }
                    if (entity.isSpawned()) {
                        if (loc != null) {
                            NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
                        } else {
                            NMSHandler.getEntityHelper().rotate(entity.getBukkitEntity(), yawRaw, pitchRaw);
                        }
                    }
                }
            };
            BukkitTask newTask = task.runTaskTimer(Denizen.getInstance(), 0, 2);
            lookTasks.put(entity.getUUID(), newTask);
        }
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) BukkitTask(org.bukkit.scheduler.BukkitTask) BukkitRunnable(org.bukkit.scheduler.BukkitRunnable) EntityTag(com.denizenscript.denizen.objects.EntityTag) List(java.util.List) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag)

Example 73 with LocationTag

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

the class FakeBlockHelper method handleMapChunkPacket.

public static PacketPlayOutMapChunk handleMapChunkPacket(PacketPlayOutMapChunk originalPacket, List<FakeBlock> blocks) {
    try {
        PacketPlayOutMapChunk packet = new PacketPlayOutMapChunk();
        DenizenNetworkManagerImpl.copyPacket(originalPacket, packet);
        // TODO: properly update HeightMap?
        int bitmask = BITMASK_MAPCHUNK.getInt(packet);
        byte[] data = (byte[]) DATA_MAPCHUNK.get(packet);
        PacketDataSerializer serial = new PacketDataSerializer(Unpooled.wrappedBuffer(data));
        PacketDataSerializer outputSerial = new PacketDataSerializer(Unpooled.buffer(data.length));
        boolean isFull = packet.f();
        List<NBTTagCompound> blockEntities = new ArrayList<>((List<NBTTagCompound>) BLOCKENTITIES_MAPCHUNK.get(packet));
        BLOCKENTITIES_MAPCHUNK.set(packet, blockEntities);
        ListIterator<NBTTagCompound> iterator = blockEntities.listIterator();
        while (iterator.hasNext()) {
            NBTTagCompound blockEnt = iterator.next();
            int x = blockEnt.getInt("x");
            int y = blockEnt.getInt("y");
            int z = blockEnt.getInt("z");
            for (FakeBlock block : blocks) {
                LocationTag loc = block.location;
                if (loc.getBlockX() == x && loc.getBlockY() == y && loc.getBlockZ() == z) {
                    iterator.remove();
                    break;
                }
            }
        }
        for (FakeBlock block : blocks) {
            LocationTag loc = block.location;
            NBTTagCompound newCompound = new NBTTagCompound();
            newCompound.setInt("x", loc.getBlockX());
            newCompound.setInt("y", loc.getBlockY());
            newCompound.setInt("z", loc.getBlockZ());
            newCompound.setString("id", block.material.getMaterial().getKey().toString());
            blockEntities.add(newCompound);
        }
        for (int y = 0; y < 16; y++) {
            if ((bitmask & (1 << y)) != 0) {
                int blockCount = serial.readShort();
                int width = serial.readUnsignedByte();
                // readVarInt
                int paletteLen = serial.i();
                int[] palette = new int[paletteLen];
                for (int p = 0; p < paletteLen; p++) {
                    palette[p] = serial.i();
                }
                int dataLen = serial.i();
                long[] blockListHelper = new long[dataLen];
                for (int i = 0; i < blockListHelper.length; i++) {
                    blockListHelper[i] = serial.readLong();
                }
                outputSerial.writeShort(blockCount);
                if (!anyBlocksInSection(blocks, y)) {
                    outputSerial.writeByte(width);
                    // writeVarInt
                    outputSerial.d(paletteLen);
                    for (int p = 0; p < paletteLen; p++) {
                        outputSerial.d(palette[p]);
                    }
                    // writeLongs
                    outputSerial.a(blockListHelper);
                    continue;
                }
                char dataBitsF = (char) (64 / width);
                int expectedLength = (4096 + dataBitsF - 1) / dataBitsF;
                if (blockListHelper.length != expectedLength) {
                    // This chunk is too-complex and is using non-standard chunk format. For now, just ignore it.
                    return originalPacket;
                // TODO: Add support for processing very-complex chunks (DataPaletteHash might be responsible for the unique format?)
                }
                DataBits bits = new DataBits(width, 4096, blockListHelper);
                int minY = y << 4;
                int maxY = (y << 4) + 16;
                for (FakeBlock block : blocks) {
                    int blockY = block.location.getBlockY();
                    if (blockY >= minY && blockY < maxY) {
                        int blockX = block.location.getBlockX();
                        int blockZ = block.location.getBlockZ();
                        blockX -= (blockX >> 4) * 16;
                        blockY -= (blockY >> 4) * 16;
                        blockZ -= (blockZ >> 4) * 16;
                        int blockIndex = blockArrayIndex(blockX, blockY, blockZ);
                        IBlockData replacementData = getNMSState(block);
                        int globalPaletteIndex = indexInPalette(replacementData);
                        int subPaletteId = getPaletteSubId(palette, globalPaletteIndex);
                        if (subPaletteId == -1) {
                            int[] newPalette = new int[paletteLen + 1];
                            if (paletteLen >= 0)
                                System.arraycopy(palette, 0, newPalette, 0, paletteLen);
                            newPalette[paletteLen] = globalPaletteIndex;
                            subPaletteId = paletteLen;
                            paletteLen++;
                            palette = newPalette;
                            int newWidth = MathHelper.e(paletteLen);
                            if (newWidth > width) {
                                DataBits newBits = new DataBits(newWidth, 4096);
                                for (int i = 0; i < bits.b(); i++) {
                                    newBits.a(i, bits.a(i));
                                }
                                bits = newBits;
                                width = newWidth;
                            }
                        }
                        bits.a(blockIndex, subPaletteId);
                    }
                }
                outputSerial.writeByte(width);
                outputSerial.d(paletteLen);
                for (int p = 0; p < palette.length; p++) {
                    outputSerial.d(palette[p]);
                }
                outputSerial.a(bits.a());
            }
        }
        if (isFull) {
            int[] biomes = (int[]) BIOMESTORAGE_MAPCHUNK.get(packet);
            if (biomes != null) {
                outputSerial.a(biomes);
            }
        }
        byte[] outputBytes = outputSerial.array();
        DATA_MAPCHUNK.set(packet, outputBytes);
        return packet;
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) FakeBlock(com.denizenscript.denizen.utilities.blocks.FakeBlock) LocationTag(com.denizenscript.denizen.objects.LocationTag)

Example 74 with LocationTag

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

the class PacketHelperImpl method showSignEditor.

@Override
public boolean showSignEditor(Player player, Location location) {
    if (location == null) {
        LocationTag fakeSign = new LocationTag(player.getLocation());
        fakeSign.setY(0);
        FakeBlock.showFakeBlockTo(Collections.singletonList(new PlayerTag(player)), fakeSign, new MaterialTag(org.bukkit.Material.OAK_WALL_SIGN), new DurationTag(1), true);
        BlockPos pos = new BlockPos(fakeSign.getX(), 0, fakeSign.getZ());
        ((DenizenNetworkManagerImpl) ((CraftPlayer) player).getHandle().connection.connection).packetListener.fakeSignExpected = pos;
        send(player, new ClientboundOpenSignEditorPacket(pos));
        return true;
    }
    BlockEntity tileEntity = ((CraftWorld) location.getWorld()).getHandle().getBlockEntity(new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), true);
    if (tileEntity instanceof SignBlockEntity) {
        SignBlockEntity sign = (SignBlockEntity) tileEntity;
        // Prevent client crashing by sending current state of the sign
        send(player, sign.getUpdatePacket());
        sign.isEditable = true;
        sign.setAllowedPlayerEditor(player.getUniqueId());
        send(player, new ClientboundOpenSignEditorPacket(sign.getBlockPos()));
        return true;
    } else {
        return false;
    }
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) MaterialTag(com.denizenscript.denizen.objects.MaterialTag) SignBlockEntity(net.minecraft.world.level.block.entity.SignBlockEntity) PlayerTag(com.denizenscript.denizen.objects.PlayerTag) BlockPos(net.minecraft.core.BlockPos) DenizenNetworkManagerImpl(com.denizenscript.denizen.nms.v1_18.impl.network.handlers.DenizenNetworkManagerImpl) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) BlockEntity(net.minecraft.world.level.block.entity.BlockEntity) SignBlockEntity(net.minecraft.world.level.block.entity.SignBlockEntity)

Example 75 with LocationTag

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

the class FakeBlockHelper method handleMapChunkPacket.

public static ClientboundLevelChunkWithLightPacket handleMapChunkPacket(World world, ClientboundLevelChunkWithLightPacket originalPacket, int chunkX, int chunkZ, List<FakeBlock> blocks) {
    try {
        ClientboundLevelChunkWithLightPacket duplicateCorePacket = new ClientboundLevelChunkWithLightPacket(DenizenNetworkManagerImpl.copyPacket(originalPacket));
        copyPacketPaperPatch(duplicateCorePacket, originalPacket);
        FriendlyByteBuf copier = new FriendlyByteBuf(Unpooled.buffer());
        originalPacket.getChunkData().write(copier);
        ClientboundLevelChunkPacketData packet = new ClientboundLevelChunkPacketData(copier, chunkX, chunkZ);
        FriendlyByteBuf serial = originalPacket.getChunkData().getReadBuffer();
        FriendlyByteBuf outputSerial = new FriendlyByteBuf(Unpooled.buffer(serial.readableBytes()));
        List blockEntities = new ArrayList((List) CHUNKDATA_BLOCK_ENTITIES.get(originalPacket.getChunkData()));
        CHUNKDATA_BLOCK_ENTITIES.set(packet, blockEntities);
        ListIterator iterator = blockEntities.listIterator();
        while (iterator.hasNext()) {
            Object blockEnt = iterator.next();
            int xz = CHUNKDATA_BLOCKENTITYINFO_PACKEDXZ.getInt(blockEnt);
            int y = CHUNKDATA_BLOCKENTITYINFO_Y.getInt(blockEnt);
            int x = (chunkX << 4) + ((xz >> 4) & 15);
            int z = (chunkZ << 4) + (xz & 15);
            for (FakeBlock block : blocks) {
                LocationTag loc = block.location;
                if (loc.getBlockX() == x && loc.getBlockY() == y && loc.getBlockZ() == z && block.material != null) {
                    iterator.remove();
                    break;
                }
            }
        }
        int worldMinY = world.getMinHeight();
        int worldMaxY = world.getMaxHeight();
        int minChunkY = worldMinY >> 4;
        int maxChunkY = worldMaxY >> 4;
        Registry<Biome> biomeRegistry = ((CraftWorld) world).getHandle().registryAccess().registryOrThrow(Registry.BIOME_REGISTRY);
        for (int y = minChunkY; y < maxChunkY; y++) {
            int blockCount = serial.readShort();
            // reflected constructors as workaround for spigot remapper bug - Mojang "IdMap" became Spigot "IRegistry" but should be "Registry"
            PalettedContainer<BlockState> states = (PalettedContainer<BlockState>) PALETTEDCONTAINER_CTOR.newInstance(Block.BLOCK_STATE_REGISTRY, Blocks.AIR.defaultBlockState(), PalettedContainer.Strategy.SECTION_STATES);
            states.read(serial);
            PalettedContainer<Biome> biomes = (PalettedContainer<Biome>) PALETTEDCONTAINER_CTOR.newInstance(biomeRegistry, biomeRegistry.getOrThrow(Biomes.PLAINS), PalettedContainer.Strategy.SECTION_BIOMES);
            biomes.read(serial);
            if (anyBlocksInSection(blocks, y)) {
                int minY = y << 4;
                int maxY = (y << 4) + 16;
                for (FakeBlock block : blocks) {
                    int blockY = block.location.getBlockY();
                    if (blockY >= minY && blockY < maxY && block.material != null) {
                        int blockX = block.location.getBlockX();
                        int blockZ = block.location.getBlockZ();
                        blockX -= (blockX >> 4) * 16;
                        blockY -= (blockY >> 4) * 16;
                        blockZ -= (blockZ >> 4) * 16;
                        BlockState oldState = states.get(blockX, blockY, blockZ);
                        BlockState newState = getNMSState(block);
                        if (oldState.isAir() && !newState.isAir()) {
                            blockCount++;
                        } else if (newState.isAir() && !oldState.isAir()) {
                            blockCount--;
                        }
                        states.set(blockX, blockY, blockZ, newState);
                    }
                }
            }
            outputSerial.writeShort(blockCount);
            states.write(outputSerial);
            biomes.write(outputSerial);
        }
        byte[] outputBytes = outputSerial.array();
        CHUNKDATA_BUFFER_SETTER.invoke(packet, outputBytes);
        CHUNKPACKET_CHUNKDATA_SETTER.invoke(duplicateCorePacket, packet);
        return duplicateCorePacket;
    } catch (Throwable ex) {
        Debug.echoError(ex);
    }
    return null;
}
Also used : ClientboundLevelChunkWithLightPacket(net.minecraft.network.protocol.game.ClientboundLevelChunkWithLightPacket) FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) ClientboundLevelChunkPacketData(net.minecraft.network.protocol.game.ClientboundLevelChunkPacketData) ArrayList(java.util.ArrayList) FakeBlock(com.denizenscript.denizen.utilities.blocks.FakeBlock) ListIterator(java.util.ListIterator) LocationTag(com.denizenscript.denizen.objects.LocationTag) Biome(net.minecraft.world.level.biome.Biome) BlockState(net.minecraft.world.level.block.state.BlockState) ArrayList(java.util.ArrayList) List(java.util.List) CraftWorld(org.bukkit.craftbukkit.v1_18_R1.CraftWorld) PalettedContainer(net.minecraft.world.level.chunk.PalettedContainer)

Aggregations

LocationTag (com.denizenscript.denizen.objects.LocationTag)133 EventHandler (org.bukkit.event.EventHandler)69 EntityTag (com.denizenscript.denizen.objects.EntityTag)45 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)40 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)33 List (java.util.List)21 ItemTag (com.denizenscript.denizen.objects.ItemTag)18 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)15 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)14 ListTag (com.denizenscript.denizencore.objects.core.ListTag)13 NPCTag (com.denizenscript.denizen.objects.NPCTag)12 Location (org.bukkit.Location)11 ArrayList (java.util.ArrayList)8 Entity (org.bukkit.entity.Entity)8 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)6 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)6 Player (org.bukkit.entity.Player)6 Vector (org.bukkit.util.Vector)6 UUID (java.util.UUID)5 BukkitRunnable (org.bukkit.scheduler.BukkitRunnable)5