Search in sources :

Example 1 with ParallelWorld

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

the class PacketManager method createMultiBlockChangePacket.

public static Set<Object> createMultiBlockChangePacket(ParallelWorld parallelWorld, Set<BlockPosition3i> blocks) {
    Map<ChunkPosition, Set<BlockPosition3i>> chunkMap = new HashMap<>();
    for (BlockPosition3i bp : blocks) {
        chunkMap.computeIfAbsent(new ChunkPosition(bp.getX(), bp.getZ()), cp -> new HashSet<>()).add(bp);
    }
    Set<Object> packets = new HashSet<>();
    for (Map.Entry<ChunkPosition, Set<BlockPosition3i>> entry : chunkMap.entrySet()) {
        ChunkPosition chunkPosition = entry.getKey();
        Set<BlockPosition3i> bps = entry.getValue();
        ParallelChunk parallelChunk = parallelWorld.getChunk(chunkPosition.x, chunkPosition.z);
        if (parallelChunk == null)
            continue;
        PacketPlayOutMultiBlockChange packet = new PacketPlayOutMultiBlockChange();
        List<PacketPlayOutMultiBlockChange.MultiBlockChangeInfo> infoList = new ArrayList<>();
        for (BlockPosition3i bp : bps) {
            BlockData blockData = parallelChunk.getBlockData(bp.getX(), bp.getY(), bp.getZ());
            if (blockData == null)
                continue;
            short loc = (short) ((bp.getX() & 0xF) << 12 | (bp.getZ() & 0xF) << 8 | bp.getY());
            infoList.add(packet.new MultiBlockChangeInfo(loc, ((CraftBlockData) blockData).getState()));
        }
        PacketPlayOutMultiBlockChange.MultiBlockChangeInfo[] array = infoList.toArray(new PacketPlayOutMultiBlockChange.MultiBlockChangeInfo[infoList.size()]);
        try {
            MultiBlockChangePacketHandler.a.set(packet, new ChunkCoordIntPair(chunkPosition.x, chunkPosition.z));
            MultiBlockChangePacketHandler.b.set(packet, array);
            packets.add(packet);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return packets;
}
Also used : ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) java.util(java.util) CraftWorld(org.bukkit.craftbukkit.v1_15_R1.CraftWorld) BlockData(org.bukkit.block.data.BlockData) ChunkPosition(thpmc.vanilla_source.util.ChunkPosition) ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) SectionLevelArray(thpmc.vanilla_source.util.SectionLevelArray) Player(org.bukkit.entity.Player) MultiBlockChangePacketHandler(thpmc.vanilla_source.nms.v1_15_R1.MultiBlockChangePacketHandler) BlockPosition3i(thpmc.vanilla_source.util.BlockPosition3i) Nullable(org.jetbrains.annotations.Nullable) net.minecraft.server.v1_15_R1(net.minecraft.server.v1_15_R1) CraftChunk(org.bukkit.craftbukkit.v1_15_R1.CraftChunk) NMSHandler(thpmc.vanilla_source.nms.v1_15_R1.NMSHandler) CraftBlockData(org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData) BlockChangePacketHandler(thpmc.vanilla_source.nms.v1_15_R1.BlockChangePacketHandler) SectionTypeArray(thpmc.vanilla_source.util.SectionTypeArray) Bukkit(org.bukkit.Bukkit) ChunkPosition(thpmc.vanilla_source.util.ChunkPosition) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) CraftBlockData(org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData) BlockPosition3i(thpmc.vanilla_source.util.BlockPosition3i) BlockData(org.bukkit.block.data.BlockData) CraftBlockData(org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData)

Example 2 with ParallelWorld

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

the class BlockChangePacketHandler 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 {
        PacketPlayOutBlockChange blockChange = (PacketPlayOutBlockChange) packet;
        BlockPosition bp = (BlockPosition) a.get(blockChange);
        BlockData blockData = parallelWorld.getBlockData(bp.getX(), bp.getY(), bp.getZ());
        if (blockData == null)
            return packet;
        PacketPlayOutBlockChange newPacket = new PacketPlayOutBlockChange();
        a.set(newPacket, bp);
        newPacket.block = ((CraftBlockData) blockData).getState();
        return newPacket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return packet;
}
Also used : ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) BlockPosition(net.minecraft.server.v1_16_R3.BlockPosition) ParallelUniverse(thpmc.vanilla_source.api.world.parallel.ParallelUniverse) PacketPlayOutBlockChange(net.minecraft.server.v1_16_R3.PacketPlayOutBlockChange) BlockData(org.bukkit.block.data.BlockData) CraftBlockData(org.bukkit.craftbukkit.v1_16_R3.block.data.CraftBlockData)

Example 3 with ParallelWorld

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

the class LightUpdatePacketHandler 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);
        boolean iFlag = i.getBoolean(packet);
        ParallelChunk parallelChunk = parallelWorld.getChunk(chunkX, chunkZ);
        if (parallelChunk == null)
            return packet;
        Object cachedPacket = parallelChunk.getCachedLightUpdatePacket();
        if (cachedPacket != null)
            return cachedPacket;
        int cValue = c.getInt(packet);
        int dValue = d.getInt(packet);
        int eValue = e.getInt(packet);
        int fValue = f.getInt(packet);
        Deque<byte[]> gValue = new ArrayDeque<>((List<byte[]>) g.get(packet));
        Deque<byte[]> hValue = new ArrayDeque<>((List<byte[]>) h.get(packet));
        int newC = 0;
        int newD = 0;
        int newE = 0;
        int newF = 0;
        List<byte[]> newG = new ArrayList<>();
        List<byte[]> newH = new ArrayList<>();
        boolean edited = false;
        for (int index = 0; index < 18; index++) {
            int sectionIndex = index - 1;
            int cSectionBit = cValue & (1 << index);
            newC |= cSectionBit;
            newE |= eValue & (1 << index);
            int dSectionBit = dValue & (1 << index);
            newD |= dSectionBit;
            newF |= fValue & (1 << index);
            if (index == 0 || index == 17) {
                if (cSectionBit != 0) {
                    newG.add(gValue.removeFirst());
                }
                if (dSectionBit != 0) {
                    newH.add(hValue.removeFirst());
                }
                continue;
            }
            SectionLevelArray skyLevelArray = parallelChunk.getSkyLightSectionLevelArray(sectionIndex);
            SectionLevelArray blockLevelArray = parallelChunk.getBlockLightSectionLevelArray(sectionIndex);
            if (skyLevelArray == null) {
                if (cSectionBit != 0) {
                    newG.add(gValue.removeFirst());
                }
            } else {
                if (cSectionBit == 0) {
                    NibbleArray nibbleArray = new NibbleArray();
                    boolean notEmpty = skyLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newG.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newC |= 1 << index;
                        newE &= ~(1 << index);
                    } else {
                        newE |= 1 << index;
                    }
                } else {
                    NibbleArray nibbleArray = new NibbleArray(gValue.removeFirst().clone());
                    boolean notEmpty = skyLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newG.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newE &= ~(1 << index);
                    }
                }
            }
            if (blockLevelArray == null) {
                if (dSectionBit != 0) {
                    newH.add(hValue.removeFirst());
                }
            } else {
                if (dSectionBit == 0) {
                    NibbleArray nibbleArray = new NibbleArray();
                    boolean notEmpty = blockLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newH.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newD |= 1 << index;
                        newF &= ~(1 << index);
                    } else {
                        newF |= 1 << index;
                    }
                } else {
                    NibbleArray nibbleArray = new NibbleArray(hValue.removeFirst().clone());
                    boolean notEmpty = blockLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newH.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newF &= ~(1 << index);
                    }
                }
            }
        }
        if (!edited)
            return packet;
        PacketPlayOutLightUpdate newPacket = new PacketPlayOutLightUpdate();
        a.set(newPacket, chunkX);
        b.set(newPacket, chunkZ);
        c.set(newPacket, newC);
        d.set(newPacket, newD);
        e.set(newPacket, newE);
        f.set(newPacket, newF);
        g.set(newPacket, newG);
        h.set(newPacket, newH);
        i.set(newPacket, iFlag);
        if (cacheSetting)
            parallelChunk.setLightUpdatePacketCache(newPacket);
        return newPacket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return packet;
}
Also used : ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) PacketPlayOutLightUpdate(net.minecraft.server.v1_16_R3.PacketPlayOutLightUpdate) World(org.bukkit.World) ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) NibbleArray(net.minecraft.server.v1_16_R3.NibbleArray) ParallelUniverse(thpmc.vanilla_source.api.world.parallel.ParallelUniverse) SectionLevelArray(thpmc.vanilla_source.util.SectionLevelArray)

Example 4 with ParallelWorld

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

the class LightUpdatePacketHandler 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.getCachedLightUpdatePacket();
        if (cachedPacket != null)
            return cachedPacket;
        int cValue = c.getInt(packet);
        int dValue = d.getInt(packet);
        int eValue = e.getInt(packet);
        int fValue = f.getInt(packet);
        Deque<byte[]> gValue = new ArrayDeque<>((List<byte[]>) g.get(packet));
        Deque<byte[]> hValue = new ArrayDeque<>((List<byte[]>) h.get(packet));
        int newC = 0;
        int newD = 0;
        int newE = 0;
        int newF = 0;
        List<byte[]> newG = new ArrayList<>();
        List<byte[]> newH = new ArrayList<>();
        boolean edited = false;
        for (int index = 0; index < 18; index++) {
            int sectionIndex = index - 1;
            int cSectionBit = cValue & (1 << index);
            newC |= cSectionBit;
            newE |= eValue & (1 << index);
            int dSectionBit = dValue & (1 << index);
            newD |= dSectionBit;
            newF |= fValue & (1 << index);
            if (index == 0 || index == 17) {
                if (cSectionBit != 0) {
                    newG.add(gValue.removeFirst());
                }
                if (dSectionBit != 0) {
                    newH.add(hValue.removeFirst());
                }
                continue;
            }
            SectionLevelArray skyLevelArray = parallelChunk.getSkyLightSectionLevelArray(sectionIndex);
            SectionLevelArray blockLevelArray = parallelChunk.getBlockLightSectionLevelArray(sectionIndex);
            if (skyLevelArray == null) {
                if (cSectionBit != 0) {
                    newG.add(gValue.removeFirst());
                }
            } else {
                if (cSectionBit == 0) {
                    NibbleArray nibbleArray = new NibbleArray();
                    boolean notEmpty = skyLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newG.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newC |= 1 << index;
                        newE &= ~(1 << index);
                    } else {
                        newE |= 1 << index;
                    }
                } else {
                    NibbleArray nibbleArray = new NibbleArray(gValue.removeFirst().clone());
                    boolean notEmpty = skyLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newG.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newE &= ~(1 << index);
                    }
                }
            }
            if (blockLevelArray == null) {
                if (dSectionBit != 0) {
                    newH.add(hValue.removeFirst());
                }
            } else {
                if (dSectionBit == 0) {
                    NibbleArray nibbleArray = new NibbleArray();
                    boolean notEmpty = blockLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newH.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newD |= 1 << index;
                        newF &= ~(1 << index);
                    } else {
                        newF |= 1 << index;
                    }
                } else {
                    NibbleArray nibbleArray = new NibbleArray(hValue.removeFirst().clone());
                    boolean notEmpty = blockLevelArray.threadsafeIteration(nibbleArray::a);
                    if (notEmpty)
                        edited = true;
                    newH.add(nibbleArray.asBytes());
                    if (notEmpty) {
                        newF &= ~(1 << index);
                    }
                }
            }
        }
        if (!edited)
            return packet;
        PacketPlayOutLightUpdate newPacket = new PacketPlayOutLightUpdate();
        a.set(newPacket, chunkX);
        b.set(newPacket, chunkZ);
        c.set(newPacket, newC);
        d.set(newPacket, newD);
        e.set(newPacket, newE);
        f.set(newPacket, newF);
        g.set(newPacket, newG);
        h.set(newPacket, newH);
        if (cacheSetting)
            parallelChunk.setLightUpdatePacketCache(newPacket);
        return newPacket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return packet;
}
Also used : ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) PacketPlayOutLightUpdate(net.minecraft.server.v1_15_R1.PacketPlayOutLightUpdate) World(org.bukkit.World) ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) NibbleArray(net.minecraft.server.v1_15_R1.NibbleArray) ParallelUniverse(thpmc.vanilla_source.api.world.parallel.ParallelUniverse) SectionLevelArray(thpmc.vanilla_source.util.SectionLevelArray)

Example 5 with ParallelWorld

use of thpmc.vanilla_source.api.world.parallel.ParallelWorld 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 {
        ChunkCoordIntPair aValue = (ChunkCoordIntPair) a.get(packet);
        PacketPlayOutMultiBlockChange.MultiBlockChangeInfo[] bValue = (PacketPlayOutMultiBlockChange.MultiBlockChangeInfo[]) b.get(packet);
        int chunkX = aValue.x;
        int chunkZ = aValue.z;
        ParallelChunk parallelChunk = parallelWorld.getChunk(chunkX, chunkZ);
        if (parallelChunk == null)
            return packet;
        PacketPlayOutMultiBlockChange newPacket = new PacketPlayOutMultiBlockChange();
        a.set(newPacket, aValue);
        int length = bValue.length;
        PacketPlayOutMultiBlockChange.MultiBlockChangeInfo[] newInfo = new PacketPlayOutMultiBlockChange.MultiBlockChangeInfo[length];
        for (int index = 0; index < length; index++) {
            PacketPlayOutMultiBlockChange.MultiBlockChangeInfo info = bValue[index];
            BlockPosition bp = info.a();
            BlockData blockData = parallelChunk.getBlockData(bp.getX(), bp.getY(), bp.getZ());
            if (blockData == null) {
                newInfo[index] = info;
            } else {
                newInfo[index] = newPacket.new MultiBlockChangeInfo(info.b(), ((CraftBlockData) blockData).getState());
            }
        }
        b.set(newPacket, newInfo);
        return newPacket;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return packet;
}
Also used : ParallelWorld(thpmc.vanilla_source.api.world.parallel.ParallelWorld) ChunkCoordIntPair(net.minecraft.server.v1_15_R1.ChunkCoordIntPair) PacketPlayOutMultiBlockChange(net.minecraft.server.v1_15_R1.PacketPlayOutMultiBlockChange) BlockPosition(net.minecraft.server.v1_15_R1.BlockPosition) ParallelChunk(thpmc.vanilla_source.api.world.parallel.ParallelChunk) CraftBlockData(org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData) ParallelUniverse(thpmc.vanilla_source.api.world.parallel.ParallelUniverse) BlockData(org.bukkit.block.data.BlockData) CraftBlockData(org.bukkit.craftbukkit.v1_15_R1.block.data.CraftBlockData)

Aggregations

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