Search in sources :

Example 1 with SectionLevelArray

use of thpmc.engine.util.SectionLevelArray in project THP-Engine 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.engine.api.world.parallel.ParallelWorld) ParallelChunk(thpmc.engine.api.world.parallel.ParallelChunk) PacketPlayOutLightUpdate(net.minecraft.server.v1_15_R1.PacketPlayOutLightUpdate) World(org.bukkit.World) ParallelWorld(thpmc.engine.api.world.parallel.ParallelWorld) NibbleArray(net.minecraft.server.v1_15_R1.NibbleArray) ParallelUniverse(thpmc.engine.api.world.parallel.ParallelUniverse) SectionLevelArray(thpmc.engine.util.SectionLevelArray)

Example 2 with SectionLevelArray

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

the class PacketManager method createLightUpdatePacketAtPrimaryThread.

@Nullable
public static Object createLightUpdatePacketAtPrimaryThread(ParallelChunk parallelChunk) {
    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 null;
    boolean has = false;
    for (int sectionIndex = 0; sectionIndex < 16; sectionIndex++) {
        SectionLevelArray blockLevelArray = parallelChunk.getBlockLightSectionLevelArray(sectionIndex);
        SectionLevelArray skyLevelArray = parallelChunk.getSkyLightSectionLevelArray(sectionIndex);
        if (blockLevelArray != null) {
            if (blockLevelArray.getSize() != 0)
                has = true;
        }
        if (skyLevelArray != null) {
            if (skyLevelArray.getSize() != 0)
                has = true;
        }
    }
    if (!has)
        return null;
    return new PacketPlayOutLightUpdate(new ChunkCoordIntPair(parallelChunk.getChunkX(), parallelChunk.getChunkZ()), ((CraftWorld) world).getHandle().getChunkProvider().getLightEngine());
}
Also used : SectionLevelArray(thpmc.engine.util.SectionLevelArray) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with SectionLevelArray

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

the class PacketManager method createLightUpdatePacketAtPrimaryThread.

@Nullable
public static Object createLightUpdatePacketAtPrimaryThread(ParallelChunk parallelChunk) {
    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 null;
    boolean has = false;
    for (int sectionIndex = 0; sectionIndex < 16; sectionIndex++) {
        SectionLevelArray blockLevelArray = parallelChunk.getBlockLightSectionLevelArray(sectionIndex);
        SectionLevelArray skyLevelArray = parallelChunk.getSkyLightSectionLevelArray(sectionIndex);
        if (blockLevelArray != null) {
            if (blockLevelArray.getSize() != 0)
                has = true;
        }
        if (skyLevelArray != null) {
            if (skyLevelArray.getSize() != 0)
                has = true;
        }
    }
    if (!has)
        return null;
    return new PacketPlayOutLightUpdate(new ChunkCoordIntPair(parallelChunk.getChunkX(), parallelChunk.getChunkZ()), ((CraftWorld) world).getHandle().getChunkProvider().getLightEngine(), true);
}
Also used : SectionLevelArray(thpmc.engine.util.SectionLevelArray) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with SectionLevelArray

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

the class ImplParallelChunk method removeBlockLight.

@Override
public void removeBlockLight(int blockX, int blockY, int blockZ) {
    int sectionIndex = ChunkUtil.getSectionIndex(blockY);
    SectionLevelArray sectionLevelArray = blockLightArrays[sectionIndex];
    if (sectionLevelArray == null)
        return;
    sectionLevelArray.remove(blockX & 0xF, blockY & 0xF, blockZ & 0xF);
    lightUpdatePacketCache = null;
}
Also used : SectionLevelArray(thpmc.engine.util.SectionLevelArray)

Example 5 with SectionLevelArray

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

the class ImplParallelChunk method createBlockLightSectionLevelArrayIfAbsent.

public SectionLevelArray createBlockLightSectionLevelArrayIfAbsent(int sectionY) {
    int sectionIndex = ChunkUtil.getSectionIndex(sectionY << 4);
    SectionLevelArray sectionLevelArray = blockLightArrays[sectionIndex];
    if (sectionLevelArray == null) {
        sectionLevelArray = new SectionLevelArray();
        blockLightArrays[sectionIndex] = sectionLevelArray;
    }
    return sectionLevelArray;
}
Also used : SectionLevelArray(thpmc.engine.util.SectionLevelArray)

Aggregations

SectionLevelArray (thpmc.engine.util.SectionLevelArray)15 ParallelChunk (thpmc.engine.api.world.parallel.ParallelChunk)3 ParallelWorld (thpmc.engine.api.world.parallel.ParallelWorld)3 World (org.bukkit.World)2 Nullable (org.jetbrains.annotations.Nullable)2 ParallelUniverse (thpmc.engine.api.world.parallel.ParallelUniverse)2 NibbleArray (net.minecraft.server.v1_15_R1.NibbleArray)1 PacketPlayOutLightUpdate (net.minecraft.server.v1_15_R1.PacketPlayOutLightUpdate)1 NibbleArray (net.minecraft.server.v1_16_R3.NibbleArray)1 PacketPlayOutLightUpdate (net.minecraft.server.v1_16_R3.PacketPlayOutLightUpdate)1 EnginePlayer (thpmc.engine.api.player.EnginePlayer)1 SectionTypeArray (thpmc.engine.util.SectionTypeArray)1