package net.tylermurphy.Minecraft.Chunk; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.HashMap; import java.util.stream.Stream; import net.tylermurphy.Minecraft.Render.Data.Texture; import org.joml.Vector2f; import org.joml.Vector3f; import org.json.JSONObject; public class Cube{ public static final byte NULL = -2; public static final byte AIR = -1; public static int TEXTURE; public int tex_px, tex_nx, tex_py, tex_ny, tex_pz, tex_nz; public boolean transparent; public boolean alwaysRenderNeighbors; public boolean placeable; public String name; public long tick_update_delay; public JSONObject positions; public static HashMap models = new HashMap<>(); public static HashMap textures = new HashMap<>(); public static HashMap texturePairings = new HashMap<>(); public static HashMap blocks = new HashMap<>(); public static HashMap blockPairings = new HashMap<>(); public static Cube getBlock(byte b){ return blocks.get((int) b); } public static Cube getBlock(String s){ return blocks.get(blockPairings.get(s)); } private static JSONObject loadJsonFile(File file){ StringBuilder contentBuilder = new StringBuilder(); try (Stream stream = Files.lines( Paths.get(file.getPath()), StandardCharsets.UTF_8)){ stream.forEach(s -> contentBuilder.append(s).append("\n")); } catch (IOException e) { e.printStackTrace(); } return new JSONObject(contentBuilder.toString()); } private static void loadModels(){ models = new HashMap<>(); File dir = new File("assets/models"); File[] directoryListing = dir.listFiles(); assert directoryListing != null; for(File file : directoryListing){ JSONObject json = loadJsonFile(file); models.put(file.getName().replaceFirst("[.][^.]+$", ""), json); } } private static void loadTextures(){ textures = new HashMap<>(); texturePairings = new HashMap<>(); File dir = new File("assets/textures/blocks"); File[] directoryListing = dir.listFiles(); int i = 0; assert directoryListing != null; for(File file : directoryListing){ String name = file.getName().replaceFirst("[.][^.]+$", ""); textures.put(i, Texture.loadRawTextureData("blocks/"+name)); texturePairings.put(name, i); i++; } TEXTURE = Texture.loadTexture2DArray(textures); } private static void loadBlocks(){ blocks = new HashMap<>(); blockPairings = new HashMap<>(); File file = new File("assets/blocks/blocks.json"); JSONObject json = loadJsonFile(file); for(String key : json.keySet()){ String name = json.getString(key); int id = Integer.parseInt(key); blockPairings.put(name, id); File block_file = new File("assets/blocks/" + name + ".json"); JSONObject block = loadJsonFile(block_file); blocks.put(id, createCube(name, block)); } } private static Cube createCube(String name, JSONObject json){ Cube cube = new Cube(); cube.positions = models.get(json.getString("model")); cube.transparent = json.getBoolean("transparent"); cube.alwaysRenderNeighbors = json.getBoolean("renderNeighbors"); cube.placeable = json.getBoolean("placeable"); cube.name = name; cube.tick_update_delay = 20; JSONObject textureList = json.getJSONObject("textures"); if(json.getString("texture").equals("all")){ cube.tex_px = texturePairings.get(textureList.getString("face")); cube.tex_nx = texturePairings.get(textureList.getString("face")); cube.tex_py = texturePairings.get(textureList.getString("face")); cube.tex_ny = texturePairings.get(textureList.getString("face")); cube.tex_pz = texturePairings.get(textureList.getString("face")); cube.tex_nz = texturePairings.get(textureList.getString("face")); } else if(json.getString("texture").equals("side")){ cube.tex_px = texturePairings.get(textureList.getString("side")); cube.tex_nx = texturePairings.get(textureList.getString("side")); cube.tex_py = texturePairings.get(textureList.getString("top")); cube.tex_ny = texturePairings.get(textureList.getString("bottom")); cube.tex_pz = texturePairings.get(textureList.getString("side")); cube.tex_nz = texturePairings.get(textureList.getString("side")); } return cube; } public static void init() { loadModels(); loadTextures(); loadBlocks(); } public static Vector3f[] NORMALS = { new Vector3f(0.f, 0.f, 0.f), new Vector3f(0.f, 0.f, 0.f), new Vector3f(0.f, 0.f, 0.f), new Vector3f(0.f, 0.f, 0.f), new Vector3f(0.f, 0.f, 0.f), new Vector3f(0.f, 0.f, 0.f) }; public static Vector2f[] TEXTURE_CORDS = { new Vector2f(0.f, 0.f), new Vector2f(0.f, 1.f), new Vector2f(1.f, 1.f), new Vector2f(1.f, 1.f), new Vector2f(1.f, 0.f), new Vector2f(0.f, 0.f) }; }