-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
DenizenMapManager.java
240 lines (226 loc) · 10.7 KB
/
DenizenMapManager.java
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package com.denizenscript.denizen.utilities.maps;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizen.utilities.DenizenAPI;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.NaturalOrderComparator;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.map.MapRenderer;
import org.bukkit.map.MapView;
import javax.imageio.stream.FileImageOutputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;
public class DenizenMapManager {
private final static Map<Integer, DenizenMapRenderer> mapRenderers = new HashMap<>();
private final static Map<String, String> downloadedByUrl = new HashMap<>();
private final static File imagesFolder = new File(DenizenAPI.getCurrentInstance().getDataFolder(), "images");
private final static File imageDownloads = new File(imagesFolder, "downloaded");
private final static File mapsFile = new File(DenizenAPI.getCurrentInstance().getDataFolder(), "maps.yml");
private static int downloadCount = (imageDownloads.exists() ? imageDownloads.listFiles().length : 0) + 1;
private static YamlConfiguration mapsConfig;
public static void reloadMaps() {
Map<Integer, List<MapRenderer>> oldMapRenderers = new HashMap<>();
for (Map.Entry<Integer, DenizenMapRenderer> entry : mapRenderers.entrySet()) {
DenizenMapRenderer renderer = entry.getValue();
oldMapRenderers.put(entry.getKey(), renderer.getOldRenderers());
renderer.deactivate();
}
mapRenderers.clear();
downloadedByUrl.clear();
mapsConfig = YamlConfiguration.loadConfiguration(mapsFile);
ConfigurationSection mapsSection = mapsConfig.getConfigurationSection("MAPS");
if (mapsSection == null) {
return;
}
for (String key : mapsSection.getKeys(false)) {
int mapId = Integer.valueOf(key);
MapView mapView = Bukkit.getServer().getMap((short) mapId); // TODO: ??? (deprecated short method)
if (mapView == null) {
dB.echoError("Map #" + key + " does not exist. Has it been removed? Deleting from maps.yml...");
mapsSection.set(key, null);
continue;
}
ConfigurationSection objectsData = mapsSection.getConfigurationSection(key + ".objects");
List<MapRenderer> oldRenderers;
if (oldMapRenderers.containsKey(mapId)) {
oldRenderers = oldMapRenderers.get(mapId);
}
else {
oldRenderers = mapView.getRenderers();
for (MapRenderer oldRenderer : oldRenderers) {
mapView.removeRenderer(oldRenderer);
}
}
DenizenMapRenderer renderer = new DenizenMapRenderer(oldRenderers,
mapsSection.getBoolean(key + ".auto update", false));
renderer.displayOriginal = mapsSection.getBoolean(key + ".original", true);
List<String> objects = new ArrayList<>(objectsData.getKeys(false));
Collections.sort(objects, new NaturalOrderComparator());
for (String objectKey : objects) {
String type = objectsData.getString(objectKey + ".type").toUpperCase();
String xTag = objectsData.getString(objectKey + ".x");
String yTag = objectsData.getString(objectKey + ".y");
String visibilityTag = objectsData.getString(objectKey + ".visibility");
boolean debug = ArgumentHelper.getBooleanFrom(objectsData.getString(objectKey + ".debug", "false"));
boolean worldC = ArgumentHelper.getBooleanFrom(objectsData.getString(objectKey + ".world_coordinates", "false"));
MapObject object = null;
if (type.equals("CURSOR")) {
object = new MapCursor(xTag, yTag, visibilityTag, debug,
objectsData.getString(objectKey + ".direction"),
objectsData.getString(objectKey + ".cursor"));
}
else if (type.equals("IMAGE")) {
String file = objectsData.getString(objectKey + ".image");
int width = objectsData.getInt(objectKey + ".width", 0);
int height = objectsData.getInt(objectKey + ".height", 0);
if (CoreUtilities.toLowerCase(file).endsWith(".gif")) {
object = new MapAnimatedImage(xTag, yTag, visibilityTag, debug, file, width, height);
}
else {
object = new MapImage(xTag, yTag, visibilityTag, debug, file, width, height);
}
}
else if (type.equals("TEXT")) {
object = new MapText(xTag, yTag, visibilityTag, debug,
objectsData.getString(objectKey + ".text"));
}
if (object != null) {
object.worldCoordinates = worldC;
renderer.addObject(object);
}
}
mapView.addRenderer(renderer);
mapRenderers.put(mapId, renderer);
}
for (Map.Entry<Integer, List<MapRenderer>> entry : oldMapRenderers.entrySet()) {
int id = entry.getKey();
if (!mapRenderers.containsKey(id)) {
MapView mapView = Bukkit.getServer().getMap((short) id); // TODO: ??? (deprecated short method)
if (mapView != null) {
for (MapRenderer renderer : entry.getValue()) {
mapView.addRenderer(renderer);
}
}
// If it's null, the server no longer has the map - don't do anything about it
}
}
ConfigurationSection downloadedImages = mapsConfig.getConfigurationSection("DOWNLOADED");
if (downloadedImages == null) {
return;
}
for (String image : downloadedImages.getKeys(false)) {
downloadedByUrl.put(CoreUtilities.toLowerCase(downloadedImages.getString(image)), image.replace("DOT", "."));
}
}
public static void saveMaps() {
for (Map.Entry<Integer, DenizenMapRenderer> entry : mapRenderers.entrySet()) {
if (entry.getValue().isActive()) {
mapsConfig.set("MAPS." + entry.getKey(), entry.getValue().getSaveData());
}
}
for (Map.Entry<String, String> entry : downloadedByUrl.entrySet()) {
mapsConfig.set("DOWNLOADED." + entry.getValue().replace(".", "DOT"), entry.getKey());
}
try {
mapsConfig.save(mapsFile);
}
catch (Exception e) {
dB.echoError(e);
}
}
public static void setMap(MapView map, DenizenMapRenderer denizenMapRenderer) {
List<MapRenderer> oldRenderers = map.getRenderers();
for (MapRenderer renderer : oldRenderers) {
map.removeRenderer(renderer);
}
map.addRenderer(denizenMapRenderer);
mapRenderers.put(map.getId(), denizenMapRenderer);
}
public static DenizenMapRenderer getDenizenRenderer(MapView map) {
int mapId = map.getId();
DenizenMapRenderer dmr;
if (!mapRenderers.containsKey(mapId)) {
dmr = new DenizenMapRenderer(map.getRenderers(), false);
setMap(map, dmr);
}
else {
dmr = mapRenderers.get(mapId);
}
return dmr;
}
public static List<MapRenderer> removeDenizenRenderers(MapView map) {
List<MapRenderer> oldRenderers = new ArrayList<>();
for (MapRenderer renderer : map.getRenderers()) {
if (renderer instanceof DenizenMapRenderer) {
map.removeRenderer(renderer);
oldRenderers.addAll(((DenizenMapRenderer) renderer).getOldRenderers());
((DenizenMapRenderer) renderer).deactivate();
mapRenderers.remove(map.getId());
}
}
return oldRenderers;
}
public static String getActualFile(String file) {
String fileLower = CoreUtilities.toLowerCase(file);
if (!fileLower.startsWith("http://") && !fileLower.startsWith("https://")) {
File f = new File(imagesFolder, file);
if (!Utilities.canReadFile(f)) {
dB.echoError("Server config denies reading files in that location.");
return null;
}
return f.getPath();
}
else {
try {
return downloadImage(new URL(file));
}
catch (MalformedURLException e) {
dB.echoError("URL is malformed: " + file);
return null;
}
}
}
private static String downloadImage(URL url) {
try {
if (!imageDownloads.exists()) {
imageDownloads.mkdirs();
}
String urlString = CoreUtilities.toLowerCase(url.toString());
if (downloadedByUrl.containsKey(urlString)) {
File image = new File(imageDownloads, downloadedByUrl.get(urlString));
if (image.exists()) {
return image.getPath();
}
}
URLConnection connection = url.openConnection();
BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
int lastDot = urlString.lastIndexOf('.');
String fileName = String.format("%0" + (6 - String.valueOf(downloadCount).length()) + "d", downloadCount)
+ (lastDot > 0 ? urlString.substring(lastDot) : "");
File output = new File(imageDownloads, fileName);
FileImageOutputStream out = new FileImageOutputStream(output);
int i;
while ((i = in.read()) != -1) {
out.write(i);
}
out.flush();
out.close();
in.close();
downloadedByUrl.put(urlString, fileName);
downloadCount++;
return output.getPath();
}
catch (IOException e) {
dB.echoError(e);
}
return null;
}
}