-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathImageFrame.java
More file actions
404 lines (337 loc) · 17.4 KB
/
ImageFrame.java
File metadata and controls
404 lines (337 loc) · 17.4 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* This file is part of ImageFrame.
*
* Copyright (C) 2025. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2025. Contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.loohp.imageframe;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import javax.imageio.spi.IIORegistry;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import com.loohp.imageframe.config.Config;
import com.loohp.imageframe.debug.Debug;
import com.loohp.imageframe.invisibleframe.InvisibleFrameManager;
import com.loohp.imageframe.language.LanguageManager;
import com.loohp.imageframe.listeners.Events;
import com.loohp.imageframe.metrics.Charts;
import com.loohp.imageframe.metrics.Metrics;
import com.loohp.imageframe.objectholders.AnimatedFakeMapManager;
import com.loohp.imageframe.objectholders.CombinedMapItemHandler;
import com.loohp.imageframe.objectholders.CustomClientNetworkManager;
import com.loohp.imageframe.objectholders.IFPlayerManager;
import com.loohp.imageframe.objectholders.IFPlayerPreference;
import com.loohp.imageframe.objectholders.ImageMap;
import com.loohp.imageframe.objectholders.ImageMapAccessPermissionType;
import com.loohp.imageframe.objectholders.ImageMapCacheControlMode;
import com.loohp.imageframe.objectholders.ImageMapCreationTaskManager;
import com.loohp.imageframe.objectholders.ImageMapLoaders;
import com.loohp.imageframe.objectholders.ImageMapManager;
import com.loohp.imageframe.objectholders.IntRange;
import com.loohp.imageframe.objectholders.IntRangeList;
import com.loohp.imageframe.objectholders.ItemFrameSelectionManager;
import com.loohp.imageframe.objectholders.MapMarkerEditManager;
import com.loohp.imageframe.objectholders.RateLimitedPacketSendingManager;
import com.loohp.imageframe.objectholders.UnsetState;
import com.loohp.imageframe.placeholderapi.Placeholders;
import com.loohp.imageframe.storage.ImageFrameStorage;
import com.loohp.imageframe.storage.ImageFrameStorageLoaders;
import com.loohp.imageframe.updater.Updater;
import com.loohp.imageframe.upload.ImageUploadManager;
import com.loohp.imageframe.utils.ChatColorUtils;
import com.loohp.imageframe.utils.KeyUtils;
import com.loohp.imageframe.utils.MCVersion;
import com.loohp.imageframe.utils.ModernEventsUtils;
import com.loohp.platformscheduler.ScheduledTask;
import com.loohp.platformscheduler.Scheduler;
import com.twelvemonkeys.imageio.plugins.webp.WebPImageReaderSpi;
import net.kyori.adventure.key.Key;
public class ImageFrame extends JavaPlugin {
public static final int BSTATS_PLUGIN_ID = 16773;
public static final String CONFIG_ID = "config";
public static ImageFrame plugin;
public static MCVersion version;
public Metrics metrics;
public static boolean viaHook = false;
public static boolean viaDisableSmoothAnimationForLegacyPlayers = false;
public static boolean debugLogging;
public static boolean updaterEnabled;
public static ScheduledTask updaterTask = null;
public static String language;
public static SimpleDateFormat dateFormat;
public static String mapItemFormat;
public static boolean requireEmptyMaps;
public static int mapMaxSize;
public static boolean restrictImageUrlEnabled;
public static List<URL> restrictImageUrls;
public static Map<String, Integer> playerCreationLimit;
public static int mapMarkerLimit;
public static long maxImageFileSize;
public static int maxProcessingTime;
public static int parallelProcessingLimit;
public static int rateLimit;
public static IntRangeList exemptMapIdsFromDeletion;
public static boolean mapRenderersContextual;
public static boolean handleAnimatedMapsOnMainThread;
public static boolean sendAnimatedMapsOnMainThread;
public static ImageMapCacheControlMode<?> cacheControlMode;
public static boolean tryDeleteBlankMapFiles;
public static boolean combinedByDefault;
public static boolean uploadServiceEnabled;
public static URI uploadServiceBaseURI;
public static long uploadServiceMaxCacheSize;
public static int uploadServiceMaxImageWidth;
public static int uploadServiceMaxImageHeight;
public static boolean imageFrameClientEnabled;
public static int invisibleFrameMaxConversionsPerSplash;
public static boolean invisibleFrameGlowEmptyFrames;
public static Key storageType;
public static Map<String, String> storageOptions;
public static LanguageManager languageManager;
public static ImageFrameStorage imageFrameStorage;
public static ImageMapManager imageMapManager;
public static IFPlayerManager ifPlayerManager;
public static ItemFrameSelectionManager itemFrameSelectionManager;
public static MapMarkerEditManager mapMarkerEditManager;
public static CombinedMapItemHandler combinedMapItemHandler;
public static AnimatedFakeMapManager animatedFakeMapManager;
public static RateLimitedPacketSendingManager rateLimitedPacketSendingManager;
public static InvisibleFrameManager invisibleFrameManager;
public static ImageMapCreationTaskManager imageMapCreationTaskManager;
public static ImageUploadManager imageUploadManager;
public static CustomClientNetworkManager customClientNetworkManager;
public static boolean isURLAllowed(String link) {
if (!restrictImageUrlEnabled || "upload".equals(link) || imageUploadManager.wasUploaded(link)) {
return true;
}
try {
URL url = new URL(link);
return restrictImageUrls.stream().anyMatch(whitelisted -> {
if (!url.getProtocol().equalsIgnoreCase(whitelisted.getProtocol())) {
return false;
}
if (!url.getHost().equalsIgnoreCase(whitelisted.getHost())) {
return false;
}
return url.getPath().toLowerCase().startsWith(whitelisted.getPath().toLowerCase());
});
} catch (MalformedURLException e) {
return false;
}
}
public static <T extends UnsetState> T getPreferenceUnsetValue(Player player, IFPlayerPreference<T> preference) {
String prefix = "imageframe.preference.unsetdefault." + preference.name().toLowerCase() + ".";
for (Map.Entry<String, T> entry : preference.getSuggestedValues().entrySet()) {
T value = entry.getValue();
if (!value.isUnset() && player.hasPermission(prefix + entry.getKey().toLowerCase())) {
return value;
}
}
return preference.getDefaultValue(ifPlayerManager.getIFPlayer(player.getUniqueId()));
}
public static int getPlayerCreationLimit(Player player) {
if (player.hasPermission("imageframe.createlimit.unlimited")) {
return -1;
}
int limit = Integer.MIN_VALUE;
for (Map.Entry<String, Integer> entry : playerCreationLimit.entrySet()) {
if (player.hasPermission("imageframe.createlimit." + entry.getKey())) {
int value = entry.getValue();
if (value < 0) {
return -1;
} else if (value > limit) {
limit = value;
}
}
}
if (limit == Integer.MIN_VALUE) {
return playerCreationLimit.getOrDefault("default", -1);
}
return limit;
}
public static boolean hasImageMapPermission(ImageMap imageMap, CommandSender sender, ImageMapAccessPermissionType permissionType) {
if (permissionType == null) {
return true;
}
if (sender.hasPermission("imageframe.adminbypass")) {
return true;
}
if (!(sender instanceof Player)) {
return false;
}
return imageMap.getAccessControl().hasPermission(((Player) sender).getUniqueId(), permissionType);
}
public static boolean isPluginEnabled(String name) {
return isPluginEnabled(name, true);
}
public static boolean isPluginEnabled(String name, boolean checkRunning) {
Plugin plugin = Bukkit.getPluginManager().getPlugin(name);
return plugin != null && (!checkRunning || plugin.isEnabled());
}
@Override
public void onEnable() {
plugin = this;
version = MCVersion.resolve();
if (!version.isSupported()) {
getServer().getConsoleSender().sendMessage(ChatColor.RED + "[ImageFrame] This version of minecraft is unsupported! (" + version.toString() + ")");
}
metrics = new Metrics(this, BSTATS_PLUGIN_ID);
Charts.setup(metrics);
getDataFolder().mkdirs();
if (!ImageIO.getImageReadersByFormatName("webp").hasNext()) {
IIORegistry.getDefaultInstance().registerServiceProvider(new WebPImageReaderSpi());
}
try {
Config.loadConfig(CONFIG_ID, new File(getDataFolder(), "config.yml"), getClass().getClassLoader().getResourceAsStream("config.yml"), getClass().getClassLoader().getResourceAsStream("config.yml"), true);
} catch (IOException e) {
e.printStackTrace();
getServer().getPluginManager().disablePlugin(this);
return;
}
reloadConfig();
getCommand("imageframe").setExecutor(new Commands());
if (isPluginEnabled("ViaVersion")) {
getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "[ImageFrame] ImageFrame has hooked into ViaVersion!");
viaHook = true;
}
getServer().getPluginManager().registerEvents(new Debug(), this);
getServer().getPluginManager().registerEvents(new Updater(), this);
getServer().getPluginManager().registerEvents(new Events(), this);
if (ModernEventsUtils.modernEventsExists()) {
getServer().getPluginManager().registerEvents(new Events.ModernEvents(), this);
}
languageManager = new LanguageManager();
imageFrameStorage = ImageFrameStorageLoaders.create(storageType, getDataFolder(), storageOptions);
imageMapManager = new ImageMapManager(imageFrameStorage);
ifPlayerManager = new IFPlayerManager(imageFrameStorage);
itemFrameSelectionManager = new ItemFrameSelectionManager();
mapMarkerEditManager = new MapMarkerEditManager();
combinedMapItemHandler = new CombinedMapItemHandler();
animatedFakeMapManager = new AnimatedFakeMapManager();
rateLimitedPacketSendingManager = new RateLimitedPacketSendingManager();
invisibleFrameManager = new InvisibleFrameManager();
imageMapCreationTaskManager = new ImageMapCreationTaskManager(ImageFrame.parallelProcessingLimit);
imageUploadManager = new ImageUploadManager(uploadServiceEnabled, uploadServiceBaseURI, uploadServiceMaxCacheSize, uploadServiceMaxImageWidth, uploadServiceMaxImageHeight);
customClientNetworkManager = new CustomClientNetworkManager(imageFrameClientEnabled);
if (isPluginEnabled("PlaceholderAPI")) {
new Placeholders().register();
}
ImageMapLoaders.init();
Scheduler.runTaskAsynchronously(this, () -> imageMapManager.loadMaps(ifPlayerManager));
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[ImageFrame] ImageFrame has been Enabled!");
}
@Override
public void onDisable() {
if (mapMarkerEditManager != null) {
mapMarkerEditManager.close();
}
if (itemFrameSelectionManager != null) {
itemFrameSelectionManager.close();
}
if (imageMapManager != null) {
imageMapManager.close();
}
if (combinedMapItemHandler != null) {
combinedMapItemHandler.close();
}
if (imageUploadManager != null) {
imageUploadManager.close();
}
if (imageFrameStorage != null) {
imageFrameStorage.close();
}
getServer().getConsoleSender().sendMessage(ChatColor.RED + "[ImageFrame] ImageFrame has been Disabled!");
}
@Override
public void reloadConfig() {
Config config = Config.getConfig(CONFIG_ID);
config.reload();
viaDisableSmoothAnimationForLegacyPlayers = config.getConfiguration().getBoolean("Hooks.ViaVersion.DisableSmoothAnimationForLegacyPlayers");
language = config.getConfiguration().getString("Settings.Language");
dateFormat = new SimpleDateFormat(config.getConfiguration().getString("Settings.DateFormat"));
mapItemFormat = ChatColorUtils.translateAlternateColorCodes('&', config.getConfiguration().getString("Settings.MapItemFormat"));
requireEmptyMaps = config.getConfiguration().getBoolean("Settings.RequireEmptyMaps");
mapMaxSize = config.getConfiguration().getInt("Settings.MaxSize");
restrictImageUrlEnabled = config.getConfiguration().getBoolean("Settings.RestrictImageUrl.Enabled");
restrictImageUrls = config.getConfiguration().getStringList("Settings.RestrictImageUrl.Whitelist").stream().map(s -> {
try {
return new URL(s);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
}).filter(u -> u != null).collect(Collectors.toList());
playerCreationLimit = new HashMap<>();
for (String group : config.getConfiguration().getConfigurationSection("Settings.PlayerCreationLimit").getKeys(false)) {
playerCreationLimit.put(group, config.getConfiguration().getInt("Settings.PlayerCreationLimit." + group));
}
mapMarkerLimit = config.getConfiguration().getInt("Settings.MapMarkerLimit");
maxImageFileSize = config.getConfiguration().getLong("Settings.MaxImageFileSize");
maxProcessingTime = config.getConfiguration().getInt("Settings.MaxProcessingTime");
parallelProcessingLimit = config.getConfiguration().getInt("Settings.ParallelProcessingLimit");
exemptMapIdsFromDeletion = config.getConfiguration().getList("Settings.ExemptMapIdsFromDeletion").stream().map(v -> {
try {
if (v instanceof Number) {
return IntRange.of(((Number) v).intValue());
} else {
return IntRange.of(v.toString());
}
} catch (Throwable e) {
e.printStackTrace();
return null;
}
}).filter(v -> v != null).collect(Collectors.toCollection(IntRangeList::new));
rateLimit = config.getConfiguration().getInt("Settings.MapPacketSendingRateLimit");
mapRenderersContextual = config.getConfiguration().getBoolean("Settings.MapRenderersContextual");
handleAnimatedMapsOnMainThread = config.getConfiguration().getBoolean("Settings.HandleAnimatedMapsOnMainThread");
sendAnimatedMapsOnMainThread = config.getConfiguration().getBoolean("Settings.SendAnimatedMapsOnMainThread");
cacheControlMode = ImageMapCacheControlMode.valueOf(config.getConfiguration().getString("Settings.CacheControlMode"));
tryDeleteBlankMapFiles = config.getConfiguration().getBoolean("Settings.TryDeleteBlankMapFiles");
combinedByDefault = config.getConfiguration().getBoolean("Settings.CombinedByDefault");
uploadServiceEnabled = config.getConfiguration().getBoolean("UploadService.Enabled");
uploadServiceBaseURI = URI.create(config.getConfiguration().getString("UploadService.BaseURI"));
uploadServiceMaxCacheSize = config.getConfiguration().getLong("UploadService.MaxCacheSize");
uploadServiceMaxImageWidth = config.getConfiguration().getInt("UploadService.MaxImageWidth");
uploadServiceMaxImageHeight = config.getConfiguration().getInt("UploadService.MaxImageHeight");
imageFrameClientEnabled = config.getConfiguration().getBoolean("ImageFrameClient.Enabled");
invisibleFrameMaxConversionsPerSplash = config.getConfiguration().getInt("InvisibleFrame.MaxConversionsPerSplash");
invisibleFrameGlowEmptyFrames = config.getConfiguration().getBoolean("InvisibleFrame.GlowEmptyFrames");
storageType = KeyUtils.imageFrameKey(config.getConfiguration().getString("Storage.Type"));
storageOptions = config.getConfiguration().getConfigurationSection("Storage.Options").getValues(true).entrySet().stream().collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue().toString()));
debugLogging = config.getConfiguration().getBoolean("DebugLogging");
if (updaterTask != null) {
updaterTask.cancel();
}
updaterEnabled = config.getConfiguration().getBoolean("Updater");
if (updaterEnabled) {
Bukkit.getPluginManager().registerEvents(new Updater(), this);
}
}
}