Search in sources :

Example 1 with ChunkCoordinate

use of com.denizenscript.denizen.utilities.blocks.ChunkCoordinate in project Denizen-For-Bukkit by DenizenScript.

the class ChunkLoadCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    ElementTag action = scriptEntry.getElement("action");
    ListTag chunklocs = scriptEntry.getObjectTag("location");
    DurationTag length = scriptEntry.getObjectTag("duration");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), action, chunklocs, length);
    }
    for (String chunkText : chunklocs) {
        Chunk chunk;
        if (ChunkTag.matches(chunkText)) {
            chunk = ChunkTag.valueOf(chunkText, scriptEntry.context).getChunk();
        } else if (LocationTag.matches(chunkText)) {
            chunk = LocationTag.valueOf(chunkText, scriptEntry.context).getChunk();
        } else {
            Debug.echoError("Chunk input '" + chunkText + "' is invalid.");
            return;
        }
        ChunkCoordinate coord = new ChunkCoordinate(chunk);
        switch(Action.valueOf(action.asString())) {
            case ADD:
                if (length.getSeconds() != 0) {
                    chunkDelays.put(coord, System.currentTimeMillis() + length.getMillis());
                } else {
                    chunkDelays.put(coord, (long) 0);
                }
                Debug.echoDebug(scriptEntry, "...added chunk " + chunk.getX() + ", " + chunk.getZ() + " with a delay of " + length.getSeconds() + " seconds.");
                if (!chunk.isLoaded()) {
                    chunk.load();
                }
                chunk.addPluginChunkTicket(Denizen.getInstance());
                if (length.getSeconds() > 0) {
                    Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
                        if (chunkDelays.containsKey(coord) && chunkDelays.get(coord) <= System.currentTimeMillis()) {
                            chunk.removePluginChunkTicket(Denizen.getInstance());
                            chunkDelays.remove(coord);
                        }
                    }, length.getTicks() + 20);
                }
                break;
            case REMOVE:
                if (chunkDelays.containsKey(coord)) {
                    chunkDelays.remove(coord);
                    chunk.removePluginChunkTicket(Denizen.getInstance());
                    Debug.echoDebug(scriptEntry, "...allowing unloading of chunk " + chunk.getX() + ", " + chunk.getZ());
                } else {
                    Debug.echoDebug(scriptEntry, "Chunk '" + coord + "' was not on the load list, ignoring.");
                }
                break;
            case REMOVEALL:
                Debug.echoDebug(scriptEntry, "...allowing unloading of all stored chunks");
                for (ChunkCoordinate loopCoord : chunkDelays.keySet()) {
                    loopCoord.getChunk().getChunk().removePluginChunkTicket(Denizen.getInstance());
                }
                chunkDelays.clear();
                break;
        }
    }
}
Also used : ChunkCoordinate(com.denizenscript.denizen.utilities.blocks.ChunkCoordinate) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) DurationTag(com.denizenscript.denizencore.objects.core.DurationTag) Chunk(org.bukkit.Chunk) ListTag(com.denizenscript.denizencore.objects.core.ListTag)

Example 2 with ChunkCoordinate

use of com.denizenscript.denizen.utilities.blocks.ChunkCoordinate 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 PacketPlayOutMapChunk) {
            FakeBlock.FakeBlockMap map = FakeBlock.blocks.get(player.getUniqueID());
            if (map == null) {
                return false;
            }
            int chunkX = CHUNKX_MAPCHUNK.getInt(packet);
            int chunkZ = CHUNKZ_MAPCHUNK.getInt(packet);
            ChunkCoordinate chunkCoord = new ChunkCoordinate(chunkX, chunkZ, player.getWorld().getWorld().getName());
            List<FakeBlock> blocks = FakeBlock.getFakeBlocksFor(player.getUniqueID(), chunkCoord);
            if (blocks == null || blocks.isEmpty()) {
                return false;
            }
            PacketPlayOutMapChunk newPacket = FakeBlockHelper.handleMapChunkPacket((PacketPlayOutMapChunk) packet, blocks);
            oldManager.sendPacket(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof PacketPlayOutMultiBlockChange) {
            FakeBlock.FakeBlockMap map = FakeBlock.blocks.get(player.getUniqueID());
            if (map == null) {
                return false;
            }
            SectionPosition coord = (SectionPosition) SECTIONPOS_MULTIBLOCKCHANGE.get(packet);
            ChunkCoordinate coordinateDenizen = new ChunkCoordinate(coord.getX(), coord.getZ(), player.getWorld().getWorld().getName());
            if (!map.byChunk.containsKey(coordinateDenizen)) {
                return false;
            }
            PacketPlayOutMultiBlockChange newPacket = new PacketPlayOutMultiBlockChange();
            copyPacket(packet, newPacket);
            LocationTag location = new LocationTag(player.getWorld().getWorld(), 0, 0, 0);
            short[] originalOffsetArray = (short[]) OFFSETARRAY_MULTIBLOCKCHANGE.get(newPacket);
            IBlockData[] originalDataArray = (IBlockData[]) BLOCKARRAY_MULTIBLOCKCHANGE.get(newPacket);
            short[] offsetArray = Arrays.copyOf(originalOffsetArray, originalOffsetArray.length);
            IBlockData[] 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];
                BlockPosition pos = coord.g(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.sendPacket(newPacket, genericfuturelistener);
            return true;
        } else if (packet instanceof PacketPlayOutBlockChange) {
            BlockPosition pos = (BlockPosition) BLOCKPOS_BLOCKCHANGE.get(packet);
            LocationTag loc = new LocationTag(player.getWorld().getWorld(), pos.getX(), pos.getY(), pos.getZ());
            FakeBlock block = FakeBlock.getFakeBlockFor(player.getUniqueID(), loc);
            if (block != null) {
                PacketPlayOutBlockChange newPacket = new PacketPlayOutBlockChange();
                copyPacket(packet, newPacket);
                newPacket.block = FakeBlockHelper.getNMSState(block);
                oldManager.sendPacket(newPacket, genericfuturelistener);
                return true;
            }
        } else if (packet instanceof PacketPlayOutBlockBreak) {
            BlockPosition pos = (BlockPosition) BLOCKPOS_BLOCKBREAK.get(packet);
            LocationTag loc = new LocationTag(player.getWorld().getWorld(), pos.getX(), pos.getY(), pos.getZ());
            FakeBlock block = FakeBlock.getFakeBlockFor(player.getUniqueID(), loc);
            if (block != null) {
                PacketPlayOutBlockBreak newPacket = new PacketPlayOutBlockBreak();
                copyPacket(packet, newPacket);
                BLOCKDATA_BLOCKBREAK.set(newPacket, FakeBlockHelper.getNMSState(block));
                oldManager.sendPacket(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) IOException(java.io.IOException) LocationTag(com.denizenscript.denizen.objects.LocationTag)

Example 3 with ChunkCoordinate

use of com.denizenscript.denizen.utilities.blocks.ChunkCoordinate in project Denizen-For-Bukkit by DenizenScript.

the class BlockLightImpl method checkIfLightsBrokenByPacket.

public static void checkIfLightsBrokenByPacket(PacketPlayOutLightUpdate packet, World world) {
    if (doNotCheck) {
        return;
    }
    try {
        int cX = PACKETPLAYOUTLIGHTUPDATE_CHUNKX.getInt(packet);
        int cZ = PACKETPLAYOUTLIGHTUPDATE_CHUNKZ.getInt(packet);
        int bitMask = PACKETPLAYOUTLIGHTUPDATE_BLOCKLIGHT_BITMASK.getInt(packet);
        List<byte[]> blockData = (List<byte[]>) PACKETPLAYOUTLIGHTUPDATE_BLOCKLIGHT_DATA.get(packet);
        Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> {
            IChunkAccess chk = world.getChunkAt(cX, cZ, ChunkStatus.FULL, false);
            if (!(chk instanceof Chunk)) {
                return;
            }
            List<BlockLight> lights = lightsByChunk.get(new ChunkCoordinate(((Chunk) chk).bukkitChunk));
            if (lights == null) {
                return;
            }
            boolean any = false;
            for (BlockLight light : lights) {
                if (((BlockLightImpl) light).checkIfChangedBy(bitMask, blockData)) {
                    Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> light.update(light.intendedLevel, false), 1);
                    any = true;
                }
            }
            if (any) {
                Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> sendNearbyChunkUpdates((Chunk) chk), 3);
            }
        }, 1);
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
}
Also used : ChunkCoordinate(com.denizenscript.denizen.utilities.blocks.ChunkCoordinate) BlockLight(com.denizenscript.denizen.nms.abstracts.BlockLight) ArrayList(java.util.ArrayList) List(java.util.List) CraftChunk(org.bukkit.craftbukkit.v1_16_R3.CraftChunk)

Example 4 with ChunkCoordinate

use of com.denizenscript.denizen.utilities.blocks.ChunkCoordinate 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 5 with ChunkCoordinate

use of com.denizenscript.denizen.utilities.blocks.ChunkCoordinate in project Denizen-For-Bukkit by DenizenScript.

the class BlockLightImpl method checkIfLightsBrokenByPacket.

public static void checkIfLightsBrokenByPacket(ClientboundBlockUpdatePacket packet, Level world) {
    try {
        BlockPos pos = packet.getPos();
        int chunkX = pos.getX() >> 4;
        int chunkZ = pos.getZ() >> 4;
        Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> {
            LevelChunk chunk = world.getChunkAt(chunkX, chunkZ);
            boolean any = false;
            for (Vector vec : RELATIVE_CHUNKS) {
                ChunkAccess other = world.getChunk(chunkX + vec.getBlockX(), chunkZ + vec.getBlockZ(), ChunkStatus.FULL, false);
                if (other instanceof LevelChunk) {
                    List<BlockLight> lights = lightsByChunk.get(new ChunkCoordinate(((LevelChunk) other).bukkitChunk));
                    if (lights != null) {
                        any = true;
                        for (BlockLight light : lights) {
                            Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> light.update(light.intendedLevel, false), 1);
                        }
                    }
                }
            }
            if (any) {
                Bukkit.getScheduler().scheduleSyncDelayedTask(NMSHandler.getJavaPlugin(), () -> sendNearbyChunkUpdates(chunk), 3);
            }
        }, 1);
    } catch (Exception ex) {
        Debug.echoError(ex);
    }
}
Also used : ChunkAccess(net.minecraft.world.level.chunk.ChunkAccess) LevelChunk(net.minecraft.world.level.chunk.LevelChunk) ChunkCoordinate(com.denizenscript.denizen.utilities.blocks.ChunkCoordinate) BlockLight(com.denizenscript.denizen.nms.abstracts.BlockLight) BlockPos(net.minecraft.core.BlockPos) Vector(org.bukkit.util.Vector)

Aggregations

ChunkCoordinate (com.denizenscript.denizen.utilities.blocks.ChunkCoordinate)10 BlockLight (com.denizenscript.denizen.nms.abstracts.BlockLight)6 BlockPos (net.minecraft.core.BlockPos)4 ChunkAccess (net.minecraft.world.level.chunk.ChunkAccess)4 LevelChunk (net.minecraft.world.level.chunk.LevelChunk)4 LocationTag (com.denizenscript.denizen.objects.LocationTag)3 FakeBlock (com.denizenscript.denizen.utilities.blocks.FakeBlock)3 Vector (org.bukkit.util.Vector)3 BitSet (java.util.BitSet)2 SectionPos (net.minecraft.core.SectionPos)2 BlockState (net.minecraft.world.level.block.state.BlockState)2 CraftChunk (org.bukkit.craftbukkit.v1_16_R3.CraftChunk)2 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Chunk (org.bukkit.Chunk)1