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
|
package makamys.lodmod.renderer;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import makamys.lodmod.LODMod;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.texture.TextureMap;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.common.util.Constants.NBT;
public class LODRegion {
private LODChunk[][] data = new LODChunk[32][32];
int regionX, regionZ;
public LODRegion(int regionX, int regionZ) {
this.regionX = regionX;
this.regionZ = regionZ;
for(int i = 0; i < 32; i++) {
for(int j = 0; j < 32; j++) {
data[i][j] = new LODChunk(regionX * 32 + i, regionZ * 32 + j);
}
}
}
public LODRegion(int regionX, int regionZ, NBTTagCompound nbt) {
this.regionX = regionX;
this.regionZ = regionZ;
NBTTagList list = nbt.getTagList("chunks", NBT.TAG_COMPOUND);
List<String> stringTable = Arrays.asList(nbt.getString("stringTable").split("\\n"));
int idx = 0;
for(int i = 0; i < 32; i++) {
for(int j = 0; j < 32; j++) {
data[i][j] = new LODChunk(list.getCompoundTagAt(idx++), stringTable);
if(data[i][j].hasChunkMeshes()) {
LODMod.renderer.setVisible(data[i][j], true);
}
}
}
}
public static LODRegion load(Path saveDir, int regionX, int regionZ) {
File saveFile = getSavePath(saveDir, regionX, regionZ).toFile();
if(saveFile.exists()) {
try {
NBTTagCompound nbt = CompressedStreamTools.readCompressed(new FileInputStream(saveFile));
return new LODRegion(regionX, regionZ, nbt);
} catch (Exception e) {
e.printStackTrace();
}
}
return new LODRegion(regionX, regionZ);
}
private static Path getSavePath(Path saveDir, int regionX, int regionZ) {
return saveDir.resolve("lod").resolve(regionX + "," + regionZ + ".lod");
}
public void save(Path saveDir) {
try {
File saveFile = getSavePath(saveDir, regionX, regionZ).toFile();
saveFile.getParentFile().mkdirs();
NBTTagCompound nbt = new NBTTagCompound();
nbt.setByte("V", (byte)0);
nbt.setString("stringTable", String.join("\n", (List<String>) ((TextureMap)Minecraft.getMinecraft().getTextureManager().getTexture(TextureMap.locationBlocksTexture)).mapUploadedSprites.keySet().stream().collect(Collectors.toList())));
NBTTagList list = new NBTTagList();
for(int i = 0; i < 32; i++) {
for(int j = 0; j < 32; j++) {
list.appendTag(data[i][j].saveToNBT());
}
}
nbt.setTag("chunks", list);
CompressedStreamTools.writeCompressed(nbt, new FileOutputStream(saveFile));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public LODChunk getChunkAbsolute(int chunkXAbs, int chunkZAbs) {
return getChunk(chunkXAbs - regionX * 32, chunkZAbs - regionZ * 32);
}
public LODChunk getChunk(int x, int z) {
if(x >= 0 && x < 32 && z >= 0 && z < 32) {
return data[x][z];
} else {
return null;
}
}
public LODChunk putChunk(Chunk chunk) {
int relX = chunk.xPosition - regionX * 32;
int relZ = chunk.zPosition - regionZ * 32;
if(relX >= 0 && relX < 32 && relZ >= 0 && relZ < 32) {
data[relX][relZ].chunk = chunk;
data[relX][relZ].waitingForData = false;
return data[relX][relZ];
}
return null;
}
public boolean tick(Entity player) {
int visibleChunks = 0;
for(int i = 0; i < 32; i++) {
for(int j = 0; j < 32; j++) {
LODChunk chunk = data[i][j];
if(chunk != null) {
chunk.tick(player);
if(chunk.visible) {
visibleChunks++;
}
}
}
}
return visibleChunks > 0;
}
public void destroy(Path saveDir) {
save(saveDir);
for(int i = 0; i < 32; i++) {
for(int j = 0; j < 32; j++) {
LODChunk chunk = data[i][j];
if(chunk != null) {
LODMod.renderer.setVisible(chunk, false);
}
}
}
}
}
|