Search in sources :

Example 61 with LocationTag

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

the class DenizenPacketListenerImpl method a.

@Override
public void a(PacketPlayInUpdateSign packet) {
    if (fakeSignExpected != null && packet.b().equals(fakeSignExpected)) {
        fakeSignExpected = null;
        PlayerChangesSignScriptEvent evt = (PlayerChangesSignScriptEvent) PlayerChangesSignScriptEvent.instance.clone();
        evt.cancelled = false;
        evt.material = new MaterialTag(org.bukkit.Material.OAK_WALL_SIGN);
        evt.location = new LocationTag(getPlayer().getLocation());
        LocationTag loc = evt.location.clone();
        loc.setY(0);
        evt.event = new SignChangeEvent(loc.getBlock(), getPlayer(), packet.c());
        evt.fire(evt.event);
    }
    super.a(packet);
}
Also used : MaterialTag(com.denizenscript.denizen.objects.MaterialTag) LocationTag(com.denizenscript.denizen.objects.LocationTag) PlayerChangesSignScriptEvent(com.denizenscript.denizen.events.player.PlayerChangesSignScriptEvent) SignChangeEvent(org.bukkit.event.block.SignChangeEvent)

Example 62 with LocationTag

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

the class DenizenNetworkManagerImpl method processShowFakeForPacket.

public boolean processShowFakeForPacket(Packet<?> packet, GenericFutureListener<? extends Future<? super Void>> genericfuturelistener) {
    if (FakeBlock.blocks.isEmpty()) {
        return false;
    }
    try {
        if (packet instanceof ClientboundLevelChunkPacket) {
            FakeBlock.FakeBlockMap map = FakeBlock.blocks.get(player.getUUID());
            if (map == null) {
                return false;
            }
            int chunkX = ((ClientboundLevelChunkPacket) packet).getX();
            int chunkZ = ((ClientboundLevelChunkPacket) packet).getZ();
            ChunkCoordinate chunkCoord = new ChunkCoordinate(chunkX, chunkZ, player.getLevel().getWorld().getName());
            List<FakeBlock> blocks = FakeBlock.getFakeBlocksFor(player.getUUID(), chunkCoord);
            if (blocks == null || blocks.isEmpty()) {
                return false;
            }
            ClientboundLevelChunkPacket newPacket = FakeBlockHelper.handleMapChunkPacket((ClientboundLevelChunkPacket) packet, blocks);
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundSectionBlocksUpdatePacket) {
            FakeBlock.FakeBlockMap map = FakeBlock.blocks.get(player.getUUID());
            if (map == null) {
                return false;
            }
            SectionPos coord = (SectionPos) SECTIONPOS_MULTIBLOCKCHANGE.get(packet);
            ChunkCoordinate coordinateDenizen = new ChunkCoordinate(coord.getX(), coord.getZ(), player.getLevel().getWorld().getName());
            if (!map.byChunk.containsKey(coordinateDenizen)) {
                return false;
            }
            ClientboundSectionBlocksUpdatePacket newPacket = new ClientboundSectionBlocksUpdatePacket(copyPacket(packet));
            LocationTag location = new LocationTag(player.getLevel().getWorld(), 0, 0, 0);
            short[] originalOffsetArray = (short[]) OFFSETARRAY_MULTIBLOCKCHANGE.get(newPacket);
            BlockState[] originalDataArray = (BlockState[]) BLOCKARRAY_MULTIBLOCKCHANGE.get(newPacket);
            short[] offsetArray = Arrays.copyOf(originalOffsetArray, originalOffsetArray.length);
            BlockState[] dataArray = Arrays.copyOf(originalDataArray, originalDataArray.length);
            OFFSETARRAY_MULTIBLOCKCHANGE.set(newPacket, offsetArray);
            BLOCKARRAY_MULTIBLOCKCHANGE.set(newPacket, dataArray);
            for (int i = 0; i < offsetArray.length; i++) {
                short offset = offsetArray[i];
                BlockPos pos = coord.relativeToBlockPos(offset);
                location.setX(pos.getX());
                location.setY(pos.getY());
                location.setZ(pos.getZ());
                FakeBlock block = map.byLocation.get(location);
                if (block != null) {
                    dataArray[i] = FakeBlockHelper.getNMSState(block);
                }
            }
            oldManager.send(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof ClientboundBlockUpdatePacket) {
            BlockPos pos = ((ClientboundBlockUpdatePacket) packet).getPos();
            LocationTag loc = new LocationTag(player.getLevel().getWorld(), pos.getX(), pos.getY(), pos.getZ());
            FakeBlock block = FakeBlock.getFakeBlockFor(player.getUUID(), loc);
            if (block != null) {
                ClientboundBlockUpdatePacket newPacket = new ClientboundBlockUpdatePacket(((ClientboundBlockUpdatePacket) packet).getPos(), FakeBlockHelper.getNMSState(block));
                oldManager.send(newPacket, genericfuturelistener);
                return true;
            }
        } else if (packet instanceof ClientboundBlockBreakAckPacket) {
            BlockPos pos = ((ClientboundBlockBreakAckPacket) packet).getPos();
            LocationTag loc = new LocationTag(player.getLevel().getWorld(), pos.getX(), pos.getY(), pos.getZ());
            FakeBlock block = FakeBlock.getFakeBlockFor(player.getUUID(), loc);
            if (block != null) {
                ClientboundBlockBreakAckPacket newPacket = new ClientboundBlockBreakAckPacket(copyPacket(packet));
                BLOCKDATA_BLOCKBREAK.set(newPacket, FakeBlockHelper.getNMSState(block));
                oldManager.send(newPacket, genericfuturelistener);
                return true;
            }
        }
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
    return false;
}
Also used : ChunkCoordinate(com.denizenscript.denizen.utilities.blocks.ChunkCoordinate) FakeBlock(com.denizenscript.denizen.utilities.blocks.FakeBlock) LocationTag(com.denizenscript.denizen.objects.LocationTag) BlockState(net.minecraft.world.level.block.state.BlockState) BlockPos(net.minecraft.core.BlockPos) SectionPos(net.minecraft.core.SectionPos)

Example 63 with LocationTag

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

the class FakeBlockHelper method handleMapChunkPacket.

public static ClientboundLevelChunkPacket handleMapChunkPacket(ClientboundLevelChunkPacket originalPacket, List<FakeBlock> blocks) {
    try {
        ClientboundLevelChunkPacket packet = new ClientboundLevelChunkPacket(DenizenNetworkManagerImpl.copyPacket(originalPacket));
        copyPacketPaperPatch(packet, originalPacket);
        // TODO: properly update HeightMap?
        BitSet bitmask = packet.getAvailableSections();
        FriendlyByteBuf serial = originalPacket.getReadBuffer();
        FriendlyByteBuf outputSerial = new FriendlyByteBuf(Unpooled.buffer(serial.readableBytes()));
        List<net.minecraft.nbt.CompoundTag> blockEntities = new ArrayList<>(packet.getBlockEntitiesTags());
        BLOCKENTITIES_MAPCHUNK.set(packet, blockEntities);
        ListIterator<CompoundTag> iterator = blockEntities.listIterator();
        while (iterator.hasNext()) {
            net.minecraft.nbt.CompoundTag 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 && block.material != null) {
                    iterator.remove();
                    break;
                }
            }
        }
        for (FakeBlock block : blocks) {
            if (block.material != null) {
                LocationTag loc = block.location;
                net.minecraft.nbt.CompoundTag newCompound = new net.minecraft.nbt.CompoundTag();
                newCompound.putInt("x", loc.getBlockX());
                newCompound.putInt("y", loc.getBlockY());
                newCompound.putInt("z", loc.getBlockZ());
                newCompound.putString("id", block.material.getMaterial().getKey().toString());
                blockEntities.add(newCompound);
            }
        }
        for (int y = 0; y < 16; y++) {
            if (bitmask.get(y)) {
                int blockCount = serial.readShort();
                int width = serial.readUnsignedByte();
                int paletteLen = serial.readVarInt();
                int[] palette = new int[paletteLen];
                for (int p = 0; p < paletteLen; p++) {
                    palette[p] = serial.readVarInt();
                }
                int dataLen = serial.readVarInt();
                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);
                    outputSerial.writeVarInt(paletteLen);
                    for (int p = 0; p < paletteLen; p++) {
                        outputSerial.writeVarInt(palette[p]);
                    }
                    outputSerial.writeLongArray(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?)
                }
                BitStorage bits = new BitStorage(width, 4096, blockListHelper);
                int minY = y << 4;
                int maxY = (y << 4) + 16;
                for (FakeBlock block : blocks) {
                    if (block.material != null) {
                        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);
                            BlockState 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 = Mth.ceillog2(paletteLen);
                                if (newWidth > width) {
                                    BitStorage newBits = new BitStorage(newWidth, 4096);
                                    for (int i = 0; i < bits.getSize(); i++) {
                                        newBits.getAndSet(i, bits.get(i));
                                    }
                                    bits = newBits;
                                    width = newWidth;
                                }
                            }
                            bits.getAndSet(blockIndex, subPaletteId);
                        }
                    }
                }
                outputSerial.writeByte(width);
                outputSerial.writeVarInt(paletteLen);
                for (int p = 0; p < palette.length; p++) {
                    outputSerial.writeVarInt(palette[p]);
                }
                outputSerial.writeLongArray(bits.getRaw());
            }
        }
        int[] biomes = packet.getBiomes();
        if (biomes != null) {
            outputSerial.writeVarIntArray(biomes);
        }
        byte[] outputBytes = outputSerial.array();
        DATA_MAPCHUNK.set(packet, outputBytes);
        return packet;
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
    return null;
}
Also used : FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) BitSet(java.util.BitSet) ArrayList(java.util.ArrayList) FakeBlock(com.denizenscript.denizen.utilities.blocks.FakeBlock) LocationTag(com.denizenscript.denizen.objects.LocationTag) CompoundTag(net.minecraft.nbt.CompoundTag) BlockState(net.minecraft.world.level.block.state.BlockState) ClientboundLevelChunkPacket(net.minecraft.network.protocol.game.ClientboundLevelChunkPacket) CompoundTag(net.minecraft.nbt.CompoundTag) BitStorage(net.minecraft.util.BitStorage)

Example 64 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().getTileEntity(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_17.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 65 with LocationTag

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

the class EntityBoundingBox method getBoundingBox.

private ListTag getBoundingBox() {
    BoundingBox boundingBox = NMSHandler.getEntityHelper().getBoundingBox(entity.getBukkitEntity());
    ListTag list = new ListTag();
    list.addObject(new LocationTag(boundingBox.getLow().toLocation(entity.getWorld())));
    list.addObject(new LocationTag(boundingBox.getHigh().toLocation(entity.getWorld())));
    return list;
}
Also used : LocationTag(com.denizenscript.denizen.objects.LocationTag) BoundingBox(com.denizenscript.denizen.nms.util.BoundingBox) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

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