1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
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<String,JSONObject> models = new HashMap<>();
public static HashMap<Integer, Texture> textures = new HashMap<>();
public static HashMap<String, Integer> texturePairings = new HashMap<>();
public static HashMap<Integer,Cube> blocks = new HashMap<>();
public static HashMap<String,Integer> 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<String> 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)
};
}
|