1 package me.shedaniel.lightoverlay.common;
3 import it.unimi.dsi.fastutil.longs.Long2ByteMap;
4 import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
5 import net.minecraft.core.BlockPos;
6 import net.minecraft.core.Direction;
7 import net.minecraft.world.level.Level;
8 import net.minecraft.world.phys.shapes.CollisionContext;
9 import net.minecraft.world.phys.shapes.VoxelShape;
10 import org.lwjgl.opengl.GL11;
12 import java.io.Closeable;
14 import static me.shedaniel.lightoverlay.common.LightOverlay.*;
16 public class ChunkData implements Closeable {
17 private static final IllegalStateException WRONG_TYPE = new IllegalStateException("Wrong type accessed!");
18 private Long2ByteMap data;
19 private int glListIndex = 0;
20 private boolean generatedList = false;
23 this(new Long2ByteOpenHashMap());
26 public ChunkData(Long2ByteMap data) {
30 public Long2ByteMap data() {
34 private void compileList(Level level, CollisionContext collisionContext) {
37 if (data().isEmpty()) {
42 glListIndex = GL11.glGenLists(3);
43 GL11.glNewList(glListIndex, GL11.GL_COMPILE);
44 GL11.glBegin(GL11.GL_LINES);
47 BlockPos.MutableBlockPos mutable = new BlockPos.MutableBlockPos();
48 for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
49 byte crossType = objectEntry.getByteValue();
50 mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
51 if (crossType == CROSS_RED) {
52 renderCross(level, mutable, collisionContext);
59 GL11.glNewList(glListIndex + 1, GL11.GL_COMPILE);
60 GL11.glBegin(GL11.GL_LINES);
63 for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
64 byte crossType = objectEntry.getByteValue();
65 mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
66 if (crossType == CROSS_YELLOW) {
67 renderCross(level, mutable, collisionContext);
74 GL11.glNewList(glListIndex + 2, GL11.GL_COMPILE);
75 GL11.glBegin(GL11.GL_LINES);
76 color(secondaryColor);
78 for (Long2ByteMap.Entry objectEntry : data().long2ByteEntrySet()) {
79 byte crossType = objectEntry.getByteValue();
80 mutable.set(BlockPos.getX(objectEntry.getLongKey()), BlockPos.getY(objectEntry.getLongKey()), BlockPos.getZ(objectEntry.getLongKey()));
81 if (crossType == CROSS_SECONDARY) {
82 renderCross(level, mutable, collisionContext);
90 public void renderList(Level level, CollisionContext collisionContext) {
92 compileList(level, collisionContext);
95 if (glListIndex != 0) {
96 GL11.glCallList(glListIndex);
97 GL11.glCallList(glListIndex + 1);
98 GL11.glCallList(glListIndex + 2);
102 private static void color(int color) {
103 int red = (color >> 16) & 255;
104 int green = (color >> 8) & 255;
105 int blue = color & 255;
106 GL11.glColor4f(red / 255f, green / 255f, blue / 255f, 1f);
109 public static void renderCross(Level level, BlockPos pos, CollisionContext collisionContext) {
110 double blockOffset = 0;
111 VoxelShape upperOutlineShape = level.getBlockState(pos).getShape(level, pos, collisionContext);
112 if (!upperOutlineShape.isEmpty())
113 blockOffset += upperOutlineShape.max(Direction.Axis.Y);
119 GL11.glVertex3d(x + .01, y + blockOffset, z + .01);
120 GL11.glVertex3d(x - .01 + 1, y + blockOffset, z - .01 + 1);
121 GL11.glVertex3d(x - .01 + 1, y + blockOffset, z + .01);
122 GL11.glVertex3d(x + .01, y + blockOffset, z - .01 + 1);
126 public void close() {
127 if (glListIndex != 0) {
128 GL11.glDeleteLists(glListIndex, 3);