-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathChunkyMap.java
More file actions
221 lines (199 loc) · 8.09 KB
/
ChunkyMap.java
File metadata and controls
221 lines (199 loc) · 8.09 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package de.lemaik.chunkymap.dynmap;
import de.lemaik.chunkymap.ChunkyMapPlugin;
import de.lemaik.chunkymap.rendering.Renderer;
import de.lemaik.chunkymap.rendering.local.ChunkyRenderer;
import de.lemaik.chunkymap.rendering.rs.RemoteRenderer;
import de.lemaik.chunkymap.util.MinecraftDownloader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.stream.Collectors;
import okhttp3.Response;
import okhttp3.ResponseBody;
import okio.BufferedSink;
import okio.Okio;
import org.bukkit.Bukkit;
import org.dynmap.ConfigurationNode;
import org.dynmap.DynmapCore;
import org.dynmap.DynmapWorld;
import org.dynmap.MapTile;
import org.dynmap.MapType;
import org.dynmap.hdmap.HDMap;
import org.dynmap.hdmap.HDPerspective;
import org.dynmap.hdmap.IsoHDPerspective;
import org.dynmap.utils.TileFlags;
import se.llbit.chunky.renderer.scene.Scene;
import se.llbit.json.JsonNumber;
import se.llbit.json.JsonObject;
import se.llbit.json.JsonParser;
/**
* A map that uses the RenderService for rendering the tiles.
*/
public class ChunkyMap extends HDMap {
private static final String DEFAULT_TEXTUREPACK_VERSION = "1.16.2";
public final DynmapCameraAdapter cameraAdapter;
private final Renderer renderer;
private File defaultTexturepackPath;
private File texturepackPath;
private File worldPath;
private final Object worldPathLock = new Object();
private JsonObject templateScene;
private final int chunkPadding;
private final boolean requeueFailedTiles;
public ChunkyMap(DynmapCore dynmap, ConfigurationNode config) {
super(dynmap, config);
cameraAdapter = new DynmapCameraAdapter((IsoHDPerspective) getPerspective());
if (config.getBoolean("chunkycloud/enabled", false)) {
renderer = new RemoteRenderer(config.getString("chunkycloud/apiKey", ""),
config.getInteger("samplesPerPixel", 100),
config.getString("texturepack", null),
config.getBoolean("chunkycloud/initializeLocally", true));
if (config.getString("chunkycloud/apiKey", "").isEmpty()) {
ChunkyMapPlugin.getPlugin(ChunkyMapPlugin.class).getLogger()
.warning("No ChunkyCloud API Key configured.");
}
} else {
renderer = new ChunkyRenderer(
config.getInteger("samplesPerPixel", 100),
config.getBoolean("denoiser/enabled", false),
config.getInteger("denoiser/albedoSamplesPerPixel", 16),
config.getInteger("denoiser/normalSamplesPerPixel", 16),
config.getInteger("chunkyThreads", 2),
Math.min(100, Math.max(0, config.getInteger("chunkyCpuLoad", 100)))
);
}
chunkPadding = config.getInteger("chunkPadding", 0);
requeueFailedTiles = config.getBoolean("requeueFailedTiles", true);
String texturepackVersion = config.getString("texturepackVersion", DEFAULT_TEXTUREPACK_VERSION);
File texturepackPath = new File(
ChunkyMapPlugin.getPlugin(ChunkyMapPlugin.class).getDataFolder(),
texturepackVersion + ".jar");
if (texturepackPath.exists()) {
defaultTexturepackPath = texturepackPath;
} else {
ChunkyMapPlugin.getPlugin(ChunkyMapPlugin.class).getLogger()
.info("Downloading additional textures for Minecraft " + texturepackVersion);
try (
Response response = MinecraftDownloader.downloadMinecraft(texturepackVersion).get();
ResponseBody body = response.body();
BufferedSink sink = Okio.buffer(Okio.sink(texturepackPath))
) {
sink.writeAll(body.source());
defaultTexturepackPath = texturepackPath;
} catch (IOException | ExecutionException | InterruptedException e) {
ChunkyMapPlugin.getPlugin(ChunkyMapPlugin.class).getLogger()
.log(Level.SEVERE,
"Downloading the textures failed, your Chunky dynmap might look bad!", e);
}
}
if (config.containsKey("texturepack")) {
this.texturepackPath = Bukkit.getPluginManager().getPlugin("dynmap").getDataFolder().toPath()
.resolve(config.getString("texturepack"))
.toFile();
} else {
ChunkyMapPlugin.getPlugin(ChunkyMapPlugin.class).getLogger()
.warning("You didn't specify a texturepack for a map that is rendered with Chunky. " +
"The Minecraft " + texturepackVersion + " textures will be used.");
}
if (config.containsKey("templateScene")) {
try (InputStream inputStream = new FileInputStream(
Bukkit.getPluginManager().getPlugin("dynmap").getDataFolder().toPath()
.resolve(config.getString("templateScene"))
.toFile())) {
templateScene = new JsonParser(inputStream).parse().asObject();
templateScene.remove("world");
templateScene.set("spp", new JsonNumber(0));
templateScene.set("renderTime", new JsonNumber(0));
templateScene.remove("chunkList");
templateScene.remove("entities");
templateScene.remove("actors");
} catch (IOException | JsonParser.SyntaxError e) {
ChunkyMapPlugin.getPlugin(ChunkyMapPlugin.class).getLogger()
.log(Level.SEVERE, "Could not read the template scene.", e);
}
}
}
@Override
public void addMapTiles(List<MapTile> list, DynmapWorld world, int tx, int ty) {
list.add(new ChunkyMapTile(world, getPerspective(), tx, ty, getBoostZoom()));
}
public List<TileFlags.TileCoord> getTileCoords(DynmapWorld world, int x, int y, int z) {
return getPerspective().getTileCoords(world, x, y, z);
}
public List<TileFlags.TileCoord> getTileCoords(DynmapWorld world, int minx, int miny, int minz,
int maxx, int maxy, int maxz) {
return getPerspective().getTileCoords(world, minx, miny, minz, maxx, maxy, maxz);
}
@Override
public MapTile[] getAdjecentTiles(MapTile tile) {
return getAdjecentTilesOfTile(tile, getPerspective());
}
public static MapTile[] getAdjecentTilesOfTile(MapTile tile, HDPerspective perspective) {
ChunkyMapTile t = (ChunkyMapTile) tile;
DynmapWorld w = t.getDynmapWorld();
int x = t.tileOrdinalX();
int y = t.tileOrdinalY();
return new MapTile[]{
new ChunkyMapTile(w, perspective, x - 1, y - 1, t.boostzoom),
new ChunkyMapTile(w, perspective, x - 1, y + 1, t.boostzoom),
new ChunkyMapTile(w, perspective, x + 1, y - 1, t.boostzoom),
new ChunkyMapTile(w, perspective, x + 1, y + 1, t.boostzoom),
new ChunkyMapTile(w, perspective, x, y - 1, t.boostzoom),
new ChunkyMapTile(w, perspective, x + 1, y, t.boostzoom),
new ChunkyMapTile(w, perspective, x, y + 1, t.boostzoom),
new ChunkyMapTile(w, perspective, x - 1, y, t.boostzoom)};
}
@Override
public List<MapType> getMapsSharingRender(DynmapWorld world) {
ArrayList<MapType> maps = new ArrayList<>();
for (MapType mt : world.maps) {
if (mt instanceof ChunkyMap) {
ChunkyMap chunkyMap = (ChunkyMap) mt;
if (chunkyMap.getPerspective() == getPerspective()
&& chunkyMap.getBoostZoom() == getBoostZoom()) {
maps.add(mt);
}
}
}
return maps;
}
@Override
public List<String> getMapNamesSharingRender(DynmapWorld dynmapWorld) {
return getMapsSharingRender(dynmapWorld).stream().map(MapType::getName)
.collect(Collectors.toList());
}
Renderer getRenderer() {
return renderer;
}
File getDefaultTexturepackPath() {
return defaultTexturepackPath;
}
File getTexturepackPath() {
return texturepackPath;
}
int getChunkPadding() {
return chunkPadding;
}
public boolean getRequeueFailedTiles() {
return requeueFailedTiles;
}
void applyTemplateScene(Scene scene) {
if (this.templateScene != null) {
scene.importFromJson(templateScene);
}
}
File getWorldFolder(DynmapWorld world) {
if (worldPath == null) {
// Fixes a ConcurrentModificationException, see https://github.com/leMaik/ChunkyMap/issues/30
synchronized (worldPathLock) {
worldPath = Bukkit.getWorld(world.getRawName()).getWorldFolder();
}
}
return worldPath;
}
}