Search in sources :

Example 16 with SectionTypeArray

use of thpmc.engine.util.SectionTypeArray in project THP-Engine by TheHollowPlanetMC.

the class MapChunkPacketHandler method rewrite.

@Override
public Object rewrite(Object packet, EnginePlayer EnginePlayer, boolean cacheSetting) {
    ParallelUniverse universe = EnginePlayer.getUniverse();
    if (universe == null)
        return packet;
    World world = EnginePlayer.getBukkitPlayer().getWorld();
    String worldName = world.getName();
    ParallelWorld parallelWorld = universe.getWorld(worldName);
    try {
        int chunkX = a.getInt(packet);
        int chunkZ = b.getInt(packet);
        ParallelChunk parallelChunk = parallelWorld.getChunk(chunkX, chunkZ);
        if (parallelChunk == null)
            return packet;
        Object cachedPacket = parallelChunk.getCachedMapChunkPacket();
        if (cachedPacket != null)
            return cachedPacket;
        ChunkSnapshot chunkSnapshot = AsyncWorldCache.getChunkCache(worldName, chunkX, chunkZ);
        if (chunkSnapshot == null)
            return packet;
        DataPaletteBlock<IBlockData>[] cachedDataBlocks = (DataPaletteBlock<IBlockData>[]) blockids.get(chunkSnapshot);
        ChunkSection[] chunkSections = new ChunkSection[16];
        int nonEmptyChunkSections = 0;
        boolean edited = false;
        for (int index = 0; index < 16; index++) {
            ChunkSection chunkSection = Chunk.a;
            SectionTypeArray sectionTypeArray = parallelChunk.getSectionTypeArray(index);
            if (sectionTypeArray != null) {
                DataPaletteBlock<IBlockData> cachedBlockData = cachedDataBlocks[index];
                if (cachedBlockData != null) {
                    final short[] nonEmptyTemp = { 0 };
                    cachedBlockData.a((iBlockData, i) -> {
                        Fluid fluid = iBlockData.getFluid();
                        if (!iBlockData.isAir()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                        if (!fluid.isEmpty()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                    });
                    short nonEmpty = nonEmptyTemp[0];
                    chunkSection = new ChunkSection(index << 4, nonEmpty, (short) 0, (short) 0);
                    NBTTagCompound data = new NBTTagCompound();
                    cachedBlockData.a(data, "Palette", "BlockStates");
                    DataPaletteBlock<IBlockData> blocks = chunkSection.getBlocks();
                    blocks.a(data.getList("Palette", 10), data.getLongArray("BlockStates"));
                }
                if (chunkSection == Chunk.a)
                    chunkSection = new ChunkSection(index << 4);
                ChunkSection finalChunkSection = chunkSection;
                boolean notEmpty = sectionTypeArray.threadsafeIteration((x, y, z, iBlockData) -> {
                    finalChunkSection.setType(x, y, z, (IBlockData) iBlockData);
                });
                if (notEmpty)
                    edited = true;
            } else {
                if (!chunkSnapshot.isSectionEmpty(index)) {
                    DataPaletteBlock<IBlockData> dataPaletteBlock = cachedDataBlocks[index];
                    final short[] nonEmptyTemp = { 0 };
                    dataPaletteBlock.a((iBlockData, i) -> {
                        Fluid fluid = iBlockData.getFluid();
                        if (!iBlockData.isAir()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                        if (!fluid.isEmpty()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                    });
                    short nonEmpty = nonEmptyTemp[0];
                    chunkSection = new ChunkSection(index << 4, nonEmpty, (short) 0, (short) 0);
                    blockIds.set(chunkSection, dataPaletteBlock);
                }
            }
            if (chunkSection != Chunk.a)
                nonEmptyChunkSections |= (1 << index);
            chunkSections[index] = chunkSection;
        }
        if (!edited)
            return packet;
        Chunk chunk = new Chunk(((CraftWorld) world).getHandle(), new ChunkCoordIntPair(chunkX, chunkZ), null, ChunkConverter.a, TickListEmpty.b(), TickListEmpty.b(), 0L, chunkSections, null);
        int cValue = c.getInt(packet);
        PacketPlayOutMapChunk newPacket = new PacketPlayOutMapChunk(chunk, nonEmptyChunkSections | cValue);
        NBTTagCompound dValue = (NBTTagCompound) d.get(packet);
        Object eValue = e.get(packet);
        List<NBTTagCompound> gValue = (List<NBTTagCompound>) g.get(packet);
        boolean hValue = h.getBoolean(packet);
        d.set(newPacket, dValue);
        e.set(newPacket, eValue);
        g.set(newPacket, gValue);
        h.set(newPacket, hValue || nonEmptyChunkSections == 65535);
        if (cacheSetting)
            parallelChunk.setMapChunkPacketCache(newPacket);
        return newPacket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return packet;
}
Also used : ParallelWorld(thpmc.engine.api.world.parallel.ParallelWorld) ChunkSnapshot(org.bukkit.ChunkSnapshot) CraftChunkSnapshot(org.bukkit.craftbukkit.v1_16_R3.CraftChunkSnapshot) ParallelChunk(thpmc.engine.api.world.parallel.ParallelChunk) CraftWorld(org.bukkit.craftbukkit.v1_16_R3.CraftWorld) ParallelWorld(thpmc.engine.api.world.parallel.ParallelWorld) World(org.bukkit.World) List(java.util.List) ParallelChunk(thpmc.engine.api.world.parallel.ParallelChunk) SectionTypeArray(thpmc.engine.util.SectionTypeArray) ParallelUniverse(thpmc.engine.api.world.parallel.ParallelUniverse)

Example 17 with SectionTypeArray

use of thpmc.engine.util.SectionTypeArray in project THP-Engine by TheHollowPlanetMC.

the class MapChunkPacketHandler method rewrite.

@Override
public Object rewrite(Object packet, EnginePlayer EnginePlayer, boolean cacheSetting) {
    ParallelUniverse universe = EnginePlayer.getUniverse();
    if (universe == null)
        return packet;
    World world = EnginePlayer.getBukkitPlayer().getWorld();
    String worldName = world.getName();
    ParallelWorld parallelWorld = universe.getWorld(worldName);
    try {
        int chunkX = a.getInt(packet);
        int chunkZ = b.getInt(packet);
        ParallelChunk parallelChunk = parallelWorld.getChunk(chunkX, chunkZ);
        if (parallelChunk == null)
            return packet;
        Object cachedPacket = parallelChunk.getCachedMapChunkPacket();
        if (cachedPacket != null)
            return cachedPacket;
        ChunkSnapshot chunkSnapshot = AsyncWorldCache.getChunkCache(worldName, chunkX, chunkZ);
        if (chunkSnapshot == null)
            return packet;
        DataPaletteBlock<IBlockData>[] cachedDataBlocks = (DataPaletteBlock<IBlockData>[]) blockids.get(chunkSnapshot);
        ChunkSection[] chunkSections = new ChunkSection[16];
        int nonEmptyChunkSections = 0;
        boolean edited = false;
        for (int index = 0; index < 16; index++) {
            ChunkSection chunkSection = Chunk.a;
            SectionTypeArray sectionTypeArray = parallelChunk.getSectionTypeArray(index);
            if (sectionTypeArray != null) {
                DataPaletteBlock<IBlockData> cachedBlockData = cachedDataBlocks[index];
                if (cachedBlockData != null) {
                    final short[] nonEmptyTemp = { 0 };
                    cachedBlockData.a((iBlockData, i) -> {
                        Fluid fluid = iBlockData.getFluid();
                        if (!iBlockData.isAir()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                        if (!fluid.isEmpty()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                    });
                    short nonEmpty = nonEmptyTemp[0];
                    chunkSection = new ChunkSection(index << 4, nonEmpty, (short) 0, (short) 0);
                    NBTTagCompound data = new NBTTagCompound();
                    cachedBlockData.a(data, "Palette", "BlockStates");
                    DataPaletteBlock<IBlockData> blocks = chunkSection.getBlocks();
                    blocks.a(data.getList("Palette", 10), data.getLongArray("BlockStates"));
                }
                if (chunkSection == Chunk.a)
                    chunkSection = new ChunkSection(index << 4);
                ChunkSection finalChunkSection = chunkSection;
                boolean notEmpty = sectionTypeArray.threadsafeIteration((x, y, z, iBlockData) -> {
                    finalChunkSection.setType(x, y, z, (IBlockData) iBlockData);
                });
                if (notEmpty)
                    edited = true;
            } else {
                if (!chunkSnapshot.isSectionEmpty(index)) {
                    DataPaletteBlock<IBlockData> dataPaletteBlock = cachedDataBlocks[index];
                    final short[] nonEmptyTemp = { 0 };
                    dataPaletteBlock.a((iBlockData, i) -> {
                        Fluid fluid = iBlockData.getFluid();
                        if (!iBlockData.isAir()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                        if (!fluid.isEmpty()) {
                            nonEmptyTemp[0] = (short) (nonEmptyTemp[0] + i);
                        }
                    });
                    short nonEmpty = nonEmptyTemp[0];
                    chunkSection = new ChunkSection(index << 4, nonEmpty, (short) 0, (short) 0);
                    blockIds.set(chunkSection, dataPaletteBlock);
                }
            }
            if (chunkSection != Chunk.a)
                nonEmptyChunkSections |= (1 << index);
            chunkSections[index] = chunkSection;
        }
        if (!edited)
            return packet;
        Chunk chunk = new Chunk(((CraftWorld) world).getHandle(), new ChunkCoordIntPair(chunkX, chunkZ), null, ChunkConverter.a, TickListEmpty.b(), TickListEmpty.b(), 0L, chunkSections, null);
        int cValue = c.getInt(packet);
        PacketPlayOutMapChunk newPacket = new PacketPlayOutMapChunk(chunk, nonEmptyChunkSections | cValue);
        NBTTagCompound dValue = (NBTTagCompound) d.get(packet);
        BiomeStorage eValue = (BiomeStorage) e.get(packet);
        List<NBTTagCompound> gValue = (List<NBTTagCompound>) g.get(packet);
        boolean hValue = h.getBoolean(packet);
        d.set(newPacket, dValue);
        e.set(newPacket, eValue);
        g.set(newPacket, gValue);
        h.set(newPacket, hValue || nonEmptyChunkSections == 65535);
        if (cacheSetting)
            parallelChunk.setMapChunkPacketCache(newPacket);
        return newPacket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return packet;
}
Also used : ParallelWorld(thpmc.engine.api.world.parallel.ParallelWorld) ChunkSnapshot(org.bukkit.ChunkSnapshot) CraftChunkSnapshot(org.bukkit.craftbukkit.v1_15_R1.CraftChunkSnapshot) ParallelChunk(thpmc.engine.api.world.parallel.ParallelChunk) CraftWorld(org.bukkit.craftbukkit.v1_15_R1.CraftWorld) ParallelWorld(thpmc.engine.api.world.parallel.ParallelWorld) World(org.bukkit.World) List(java.util.List) ParallelChunk(thpmc.engine.api.world.parallel.ParallelChunk) SectionTypeArray(thpmc.engine.util.SectionTypeArray) ParallelUniverse(thpmc.engine.api.world.parallel.ParallelUniverse)

Example 18 with SectionTypeArray

use of thpmc.engine.util.SectionTypeArray in project THP-Engine by TheHollowPlanetMC.

the class PacketManager method sendClearChunkMultiBlockChangePacketAtPrimaryThread.

public static void sendClearChunkMultiBlockChangePacketAtPrimaryThread(Player player, ParallelChunk parallelChunk, NMSHandler nmsHandler) {
    if (!Bukkit.isPrimaryThread())
        throw new IllegalStateException("DO NOT CALL FROM ASYNC THREAD!");
    org.bukkit.World world = Bukkit.getWorld(parallelChunk.getWorld().getName());
    if (world == null)
        return;
    if (player.getWorld() != world)
        return;
    List<Short> coordList = new ArrayList<>();
    boolean has = false;
    for (int sectionIndex = 0; sectionIndex < 16; sectionIndex++) {
        SectionTypeArray sectionTypeArray = parallelChunk.getSectionTypeArray(sectionIndex);
        if (sectionTypeArray == null)
            continue;
        int finalSectionIndex = sectionIndex;
        boolean notEmpty = sectionTypeArray.threadsafeIteration((x, y, z, iBlockData) -> {
            short loc = (short) (x << 12 | z << 8 | (y + (finalSectionIndex << 4)));
            coordList.add(loc);
        });
        if (notEmpty)
            has = true;
    }
    if (!has)
        return;
    if (coordList.size() == 0)
        return;
    org.bukkit.Chunk chunk = world.getChunkAt(parallelChunk.getChunkX(), parallelChunk.getChunkZ());
    net.minecraft.server.v1_15_R1.Chunk nmsChunk = ((CraftChunk) chunk).getHandle();
    short[] array = new short[coordList.size()];
    for (int i = 0; i < coordList.size(); i++) {
        array[i] = coordList.get(i);
    }
    PacketPlayOutMultiBlockChange packet = new PacketPlayOutMultiBlockChange(coordList.size(), array, nmsChunk);
    nmsHandler.sendPacket(player, packet);
}
Also used : SectionTypeArray(thpmc.engine.util.SectionTypeArray) net.minecraft.server.v1_15_R1(net.minecraft.server.v1_15_R1) CraftChunk(org.bukkit.craftbukkit.v1_15_R1.CraftChunk)

Aggregations

SectionTypeArray (thpmc.engine.util.SectionTypeArray)18 ParallelChunk (thpmc.engine.api.world.parallel.ParallelChunk)5 ParallelWorld (thpmc.engine.api.world.parallel.ParallelWorld)5 Nullable (org.jetbrains.annotations.Nullable)4 ParallelUniverse (thpmc.engine.api.world.parallel.ParallelUniverse)3 List (java.util.List)2 net.minecraft.server.v1_16_R3 (net.minecraft.server.v1_16_R3)2 ChunkSnapshot (org.bukkit.ChunkSnapshot)2 World (org.bukkit.World)2 CraftChunk (org.bukkit.craftbukkit.v1_16_R3.CraftChunk)2 CraftWorld (org.bukkit.craftbukkit.v1_16_R3.CraftWorld)2 CraftBlockData (org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData)2 SectionLevelArray (thpmc.engine.util.SectionLevelArray)2 java.util (java.util)1 net.minecraft.server.v1_15_R1 (net.minecraft.server.v1_15_R1)1 IBlockData (net.minecraft.server.v1_16_R3.IBlockData)1 PacketPlayOutMultiBlockChange (net.minecraft.server.v1_16_R3.PacketPlayOutMultiBlockChange)1 SectionPosition (net.minecraft.server.v1_16_R3.SectionPosition)1 Bukkit (org.bukkit.Bukkit)1 BlockData (org.bukkit.block.data.BlockData)1