From 6f09c992529a82b41f4192bf15aac7722c4c7bc7 Mon Sep 17 00:00:00 2001
From: valoeghese <valoeghesemods@gmail.com>
Date: Mon, 24 Feb 2020 19:09:14 +1300
Subject: [PATCH] chunk stuff

---
 .../litecraft/types/block/Block.java          |  9 ++-
 .../halotroop/litecraft/world/Chunk.java      | 72 ++++++++++++++++++-
 2 files changed, 79 insertions(+), 2 deletions(-)

diff --git a/src/main/java/io/github/halotroop/litecraft/types/block/Block.java b/src/main/java/io/github/halotroop/litecraft/types/block/Block.java
index fb9d768..2ba720b 100644
--- a/src/main/java/io/github/halotroop/litecraft/types/block/Block.java
+++ b/src/main/java/io/github/halotroop/litecraft/types/block/Block.java
@@ -13,14 +13,21 @@ public class Block
 	protected Block(TexturedModel model, Properties properties)
 	{
 		this.model = model;
+		this.visible = properties.visible;
 	}
 
 	public final TexturedModel model;
+	public final boolean visible;
 
 	public static final Block GRASS = new Block("block/cubes/soil/gravel.png", new Properties());
 	public static final Block DIRT = new Block("block/cubes/soil/dirt.png", new Properties());
 
 	public static class Properties { // add properties to this builder!
-		
+		private boolean visible = false;
+
+		public Properties visible(boolean invisible) {
+			this.visible = invisible;
+			return this;
+		}
 	}
 }
diff --git a/src/main/java/io/github/halotroop/litecraft/world/Chunk.java b/src/main/java/io/github/halotroop/litecraft/world/Chunk.java
index 1dd0a1f..485b6e8 100644
--- a/src/main/java/io/github/halotroop/litecraft/world/Chunk.java
+++ b/src/main/java/io/github/halotroop/litecraft/world/Chunk.java
@@ -1,5 +1,75 @@
 package io.github.halotroop.litecraft.world;
 
-public class Chunk {
+import java.util.ArrayList;
+import java.util.List;
+
+import io.github.halotroop.litecraft.types.block.Block;
+import io.github.halotroop.litecraft.types.block.BlockEntity;
+import it.unimi.dsi.fastutil.longs.Long2ObjectArrayMap;
+import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
+
+public class Chunk
+{
+	public Chunk(int chunkX, int chunkY, int chunkZ)
+	{
+		this.chunkX = chunkX;
+	}
+
+	private final Long2ObjectMap<Block> blocks = new Long2ObjectArrayMap<>();
+	private final List<BlockEntity> blockEntities = new ArrayList<>();
+	private boolean render = false;
 	
+	public final int chunkX;
+
+	public void setBlock(int x, int y, int z, Block block)
+	{
+		if (x > 7) x = 7;
+		else if (x < 0) x = 0;
+		if (y > 7) y = 7;
+		else if (y < 0) y = 0;
+		if (z > 7) z = 7;
+		else if (z < 0) z = 0;
+
+		long hash = posHash(x, y, z);
+		this.blocks.put(hash, block);
+	}
+
+	public Block getBlock(int x, int y, int z)
+	{
+		long hash = posHash(x, y, z);
+		return this.blocks.get(hash);
+	}
+	
+	public void setRender(boolean render)
+	{
+		if (render && !this.render) // if it has been changed to true
+		{
+			this.blocks.forEach((coord, block) -> {
+				if (block.visible) this.blockEntities.add(new BlockEntity(block, new))
+			});
+		}
+		else if (this.render) // else if it has been changed to false
+		{
+			int length = blockEntities.size();
+
+			for (int i = length; i >= 0; --i)
+			{
+				this.blockEntities.remove(i);
+			}
+		}
+
+		this.render = render;
+	}
+
+	public boolean doRender()
+	{ return this.render; }
+
+	/**
+	 * @param x in-chunk x coordinate.
+	 * @param y in-chunk y coordinate.
+	 * @param z in-chunk z coordinate.
+	 * @return creates a long that represents a coordinate, for use as a key in maps.
+	 */
+	private static long posHash(int x, int y, int z)
+	{ return ((long) x & 0b111) | (((long) y & 0b111) << 3) | (((long) z & 0b111) << 6); }
 }