package me.eddiep;


import net.minecraft.server.v1_8_R3.*;
import org.bukkit.Material;

public class ChunkEdit {
    public World world;

    public ChunkEdit(World w) {
        this.world = w;

    }

    public void setBlock(int x, int y, int z, Material type, byte data) {
        int columnX = x >> 4;
        int columnZ = z >> 4;
        int chunkY = y >> 4;
        int blockX = x % 16;
        int blockY = y % 16;
        int blockZ = z % 16;
        if (blockX < 0)
            blockX += 16;
        if (blockX > 15) {
            blockX = blockX % 16;
            columnX++;
        }
        if (blockY < 0)
            blockY += 16;
        if (blockY > 15) {
            blockY = blockY % 16;
            chunkY++;
        }
        if (blockZ < 0)
            blockZ += 16;
        if (blockZ > 15) {
            blockZ = blockZ % 16;
            columnZ++;
        }
        Chunk c = this.world.getChunkAt(columnX, columnZ);
        ChunkSection[] sections = c.getSections();
        ChunkSection section = sections[chunkY];
        if (section == null)
            section = new ChunkSection(chunkY, true);
        NibbleArray blockLight;
        try {
            blockLight = section.getEmittedLightArray();
        } catch (Exception e) {
            blockLight = new NibbleArray();
        }
        IBlockData d = Block.getByCombinedId(type.getId() + (data << 12));
        TileEntity tileEntity = this.world.getTileEntity(new BlockPosition(x, y, z));
        if (tileEntity != null)
            c.a(new BlockPosition(x, y, z), d);
        else
            section.setType(blockX, blockY, blockZ, d);
        blockLight.a(blockX, blockY, blockZ, getLight(type));
        section.a(blockLight);
        sections[chunkY] = section;
        c.a(sections);
    }

    public void setBlock(int x, int y, int z, Material type) {
        setBlock(x, y, z, type, (byte) 0);
    }

    private int getLight(Material type) {
        switch (type) {
            case BEACON: case ENDER_PORTAL: case FIRE: case GLOWSTONE: case JACK_O_LANTERN: case LAVA: case STATIONARY_LAVA:
            case REDSTONE_LAMP_ON: case SEA_LANTERN: case WATER: case STATIONARY_WATER:
                return 15;
            case TORCH:// case END_ROD:
                return 14;
            case BURNING_FURNACE:
                return 13;
            case PORTAL:
                return 11;
            case GLOWING_REDSTONE_ORE:
                return 9;
            case ENDER_CHEST: case REDSTONE_TORCH_ON:
                return 7;
            case BREWING_STAND: case BROWN_MUSHROOM: case DRAGON_EGG: case ENDER_PORTAL_FRAME:
                return 1;
            default:
                return 0;
        }
    }
}
@@ -1151,16 +1151,46 @@
 kLight;%0A
+        NibbleArray skyLight;%0A
         
@@ -1195,16 +1195,16 @@
   try %7B%0A
-
         
@@ -1330,32 +1330,181 @@
 ay();%0A        %7D%0A
+        try %7B%0A            skyLight = section.getSkyLightArray();%0A        %7D catch (Exception e) %7B%0A            skyLight = new NibbleArray();%0A        %7D%0A
         IBlockDa
@@ -1855,24 +1855,88 @@
 ght(type));%0A
+        skyLight.a(blockX, blockY, blockZ, 0);//getSkyLight());%0A
         sect
@@ -1954,16 +1954,45 @@
 Light);%0A
+        section.b(skyLight);%0A
         
@@ -2104,32 +2104,32 @@
 aterial type) %7B%0A
-
         setBlock
@@ -2158,24 +2158,233 @@
  0);%0A    %7D%0A%0A
+    private int getSkyLight() %7B%0A        long time = this.world.getTime();%0A        time = time %25 24000;%0A        if (time %3E 12000)%0A            return 4;%0A        else//if (time %3E 0)%0A            return 15;%0A    %7D%0A%0A
     private 

Fix flashing