Search in sources :

Example 16 with ParallelChunk

use of thpmc.vanilla_source.api.world.parallel.ParallelChunk in project VanillaSource 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.vanilla_source.api.world.parallel.ParallelWorld) ChunkSnapshot(org.bukkit.ChunkSnapshot) CraftChunkSnapshot(org.bukkit.craftbukkit.v1_15_R1.CraftChunkSnapshot) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) CraftWorld(org.bukkit.craftbukkit.v1_15_R1.CraftWorld) ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) World(org.bukkit.World) List(java.util.List) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) SectionTypeArray(thpmc.vanilla_source.util.SectionTypeArray) ParallelUniverse(thpmc.vanilla_source.api.world.parallel.ParallelUniverse)

Example 17 with ParallelChunk

use of thpmc.vanilla_source.api.world.parallel.ParallelChunk in project VanillaSource 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.vanilla_source.api.world.parallel.ParallelWorld) ChunkSnapshot(org.bukkit.ChunkSnapshot) CraftChunkSnapshot(org.bukkit.craftbukkit.v1_16_R3.CraftChunkSnapshot) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) CraftWorld(org.bukkit.craftbukkit.v1_16_R3.CraftWorld) ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) World(org.bukkit.World) List(java.util.List) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) SectionTypeArray(thpmc.vanilla_source.util.SectionTypeArray) ParallelUniverse(thpmc.vanilla_source.api.world.parallel.ParallelUniverse)

Example 18 with ParallelChunk

use of thpmc.vanilla_source.api.world.parallel.ParallelChunk in project VanillaSource by TheHollowPlanetMC.

the class MultiBlockChangePacketHandler method rewrite.

@Override
public Object rewrite(Object packet, EnginePlayer EnginePlayer, boolean cacheSetting) {
    ParallelUniverse universe = EnginePlayer.getUniverse();
    if (universe == null)
        return packet;
    String worldName = EnginePlayer.getBukkitPlayer().getWorld().getName();
    ParallelWorld parallelWorld = universe.getWorld(worldName);
    try {
        SectionPosition aValue = (SectionPosition) a.get(packet);
        int chunkX = aValue.getX();
        int chunkZ = aValue.getZ();
        ParallelChunk parallelChunk = parallelWorld.getChunk(chunkX, chunkZ);
        if (parallelChunk == null)
            return packet;
        SectionTypeArray sectionTypeArray = parallelChunk.getSectionTypeArray(aValue.getY());
        if (sectionTypeArray == null)
            return packet;
        short[] bValue = (short[]) b.get(packet);
        IBlockData[] cValueClone = ((IBlockData[]) c.get(packet)).clone();
        for (int i = 0; i < bValue.length; i++) {
            short coord = bValue[i];
            int x = coord >> 8;
            int y = coord & 0xF;
            int z = (coord >> 4) & 0xF;
            IBlockData iBlockData = (IBlockData) sectionTypeArray.getType(x, y, z);
            if (iBlockData != null) {
                cValueClone[i] = iBlockData;
            }
        }
        boolean dValue = d.getBoolean(packet);
        PacketPlayOutMultiBlockChange newPacket = new PacketPlayOutMultiBlockChange();
        a.set(newPacket, aValue);
        b.set(newPacket, bValue);
        c.set(newPacket, cValueClone);
        d.set(newPacket, dValue);
        return newPacket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return packet;
}
Also used : ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) SectionPosition(net.minecraft.server.v1_16_R3.SectionPosition) PacketPlayOutMultiBlockChange(net.minecraft.server.v1_16_R3.PacketPlayOutMultiBlockChange) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) SectionTypeArray(thpmc.vanilla_source.util.SectionTypeArray) IBlockData(net.minecraft.server.v1_16_R3.IBlockData) ParallelUniverse(thpmc.vanilla_source.api.world.parallel.ParallelUniverse)

Example 19 with ParallelChunk

use of thpmc.vanilla_source.api.world.parallel.ParallelChunk in project VanillaSource by TheHollowPlanetMC.

the class ImplParallelWorld method setBlockLightLevel.

@Override
public void setBlockLightLevel(int blockX, int blockY, int blockZ, int level) {
    int chunkX = blockX >> 4;
    int chunkZ = blockZ >> 4;
    long coord = ChunkUtil.getCoordinateKey(chunkX, chunkZ);
    ParallelChunk parallelChunk = chunkMap.computeIfAbsent(coord, c -> new ImplParallelChunk(this, chunkX, chunkZ));
    parallelChunk.setBlockLightLevel(blockX, blockY, blockZ, level);
}
Also used : ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk)

Example 20 with ParallelChunk

use of thpmc.vanilla_source.api.world.parallel.ParallelChunk in project VanillaSource by TheHollowPlanetMC.

the class PacketManager method createMultiBlockChangePacket.

public static Set<Object> createMultiBlockChangePacket(ParallelWorld parallelWorld, Set<BlockPosition3i> blocks) {
    Map<BlockPosition3i, Set<BlockPosition3i>> chunkMap = new HashMap<>();
    for (BlockPosition3i bp : blocks) {
        chunkMap.computeIfAbsent(new BlockPosition3i(bp.getX() >> 4, bp.getY() >> 4, bp.getZ() >> 4), cp -> new HashSet<>()).add(bp);
    }
    Set<Object> packets = new HashSet<>();
    for (Map.Entry<BlockPosition3i, Set<BlockPosition3i>> entry : chunkMap.entrySet()) {
        BlockPosition3i sectionPosition = entry.getKey();
        Set<BlockPosition3i> bps = entry.getValue();
        ParallelChunk parallelChunk = parallelWorld.getChunk(sectionPosition.getX(), sectionPosition.getZ());
        if (parallelChunk == null)
            continue;
        SectionTypeArray sectionTypeArray = parallelChunk.getSectionTypeArray(sectionPosition.getY());
        if (sectionTypeArray == null)
            continue;
        List<Short> coordList = new ArrayList<>();
        List<IBlockData> dataList = new ArrayList<>();
        for (BlockPosition3i bp : bps) {
            IBlockData iBlockData = (IBlockData) sectionTypeArray.getType(bp.getX() & 0xF, bp.getY() & 0xF, bp.getZ() & 0xF);
            if (iBlockData == null)
                continue;
            coordList.add((short) ((bp.getX() & 0xF) << 8 | (bp.getZ() & 0xF) << 4 | bp.getY() & 0xF));
            dataList.add(iBlockData);
        }
        short[] coordArray = new short[coordList.size()];
        IBlockData[] dataArray = new IBlockData[dataList.size()];
        for (int i = 0; i < coordList.size(); i++) {
            coordArray[i] = coordList.get(i);
            dataArray[i] = dataList.get(i);
        }
        try {
            PacketPlayOutMultiBlockChange packet = new PacketPlayOutMultiBlockChange();
            MultiBlockChangePacketHandler.a.set(packet, SectionPosition.a(sectionPosition.getX(), sectionPosition.getY(), sectionPosition.getZ()));
            MultiBlockChangePacketHandler.b.set(packet, coordArray);
            MultiBlockChangePacketHandler.c.set(packet, dataArray);
            MultiBlockChangePacketHandler.d.setBoolean(packet, true);
            packets.add(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return packets;
}
Also used : ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) java.util(java.util) BlockData(org.bukkit.block.data.BlockData) BlockChangePacketHandler(thpmc.vanilla_source.nms.v1_16_R3.BlockChangePacketHandler) CraftWorld(org.bukkit.craftbukkit.v1_16_R3.CraftWorld) ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) SectionLevelArray(thpmc.vanilla_source.util.SectionLevelArray) Player(org.bukkit.entity.Player) net.minecraft.server.v1_16_R3(net.minecraft.server.v1_16_R3) BlockPosition3i(thpmc.vanilla_source.util.BlockPosition3i) Nullable(org.jetbrains.annotations.Nullable) NMSHandler(thpmc.vanilla_source.nms.v1_16_R3.NMSHandler) CraftBlockData(org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData) CraftChunk(org.bukkit.craftbukkit.v1_16_R3.CraftChunk) MultiBlockChangePacketHandler(thpmc.vanilla_source.nms.v1_16_R3.MultiBlockChangePacketHandler) SectionTypeArray(thpmc.vanilla_source.util.SectionTypeArray) Bukkit(org.bukkit.Bukkit) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) SectionTypeArray(thpmc.vanilla_source.util.SectionTypeArray) BlockPosition3i(thpmc.vanilla_source.util.BlockPosition3i)

Aggregations

ParallelChunk (thpmc.vanilla_source.api.world.parallel.ParallelChunk)26 ParallelWorld (thpmc.vanilla_source.api.world.parallel.ParallelWorld)10 ParallelUniverse (thpmc.vanilla_source.api.world.parallel.ParallelUniverse)6 SectionTypeArray (thpmc.vanilla_source.util.SectionTypeArray)6 Nullable (org.jetbrains.annotations.Nullable)5 SectionLevelArray (thpmc.vanilla_source.util.SectionLevelArray)5 World (org.bukkit.World)4 BlockData (org.bukkit.block.data.BlockData)3 java.util (java.util)2 List (java.util.List)2 Bukkit (org.bukkit.Bukkit)2 ChunkSnapshot (org.bukkit.ChunkSnapshot)2 CraftWorld (org.bukkit.craftbukkit.v1_15_R1.CraftWorld)2 CraftBlockData (org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData)2 CraftWorld (org.bukkit.craftbukkit.v1_16_R3.CraftWorld)2 Player (org.bukkit.entity.Player)2 BlockPosition3i (thpmc.vanilla_source.util.BlockPosition3i)2 net.minecraft.server.v1_15_R1 (net.minecraft.server.v1_15_R1)1 BlockPosition (net.minecraft.server.v1_15_R1.BlockPosition)1 ChunkCoordIntPair (net.minecraft.server.v1_15_R1.ChunkCoordIntPair)1