|
1 | 1 | package me.rothes.protocolstringreplacer; |
2 | 2 |
|
3 | | -import com.google.gson.JsonElement; |
4 | | -import com.google.gson.JsonObject; |
5 | | -import com.google.gson.JsonParser; |
6 | 3 | import com.sk89q.protocolstringreplacer.PsrDisguisePlugin; |
7 | 4 | import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion; |
8 | 5 | import me.rothes.protocolstringreplacer.api.configuration.CommentYamlConfiguration; |
9 | | -import me.rothes.protocolstringreplacer.api.replacer.ReplacerConfig; |
10 | 6 | import me.rothes.protocolstringreplacer.api.user.PsrUser; |
11 | 7 | import me.rothes.protocolstringreplacer.api.user.PsrUserManager; |
12 | 8 | import me.rothes.protocolstringreplacer.commands.CommandHandler; |
|
16 | 12 | import me.rothes.protocolstringreplacer.listeners.PlayerJoinListener; |
17 | 13 | import me.rothes.protocolstringreplacer.listeners.PlayerQuitListener; |
18 | 14 | import me.rothes.protocolstringreplacer.packetlisteners.PacketListenerManager; |
19 | | -import me.rothes.protocolstringreplacer.replacer.ReplaceMode; |
20 | 15 | import me.rothes.protocolstringreplacer.replacer.ReplacerManager; |
21 | 16 | import me.rothes.protocolstringreplacer.upgrades.AbstractUpgradeHandler; |
22 | 17 | import me.rothes.protocolstringreplacer.upgrades.UpgradeEnum; |
23 | 18 | import me.rothes.protocolstringreplacer.utils.FileUtils; |
24 | 19 | import org.apache.commons.lang.Validate; |
25 | | -import org.bstats.bukkit.Metrics; |
26 | | -import org.bstats.charts.DrilldownPie; |
27 | 20 | import org.bukkit.Bukkit; |
28 | 21 | import org.bukkit.configuration.ConfigurationSection; |
29 | 22 | import org.bukkit.entity.Player; |
|
33 | 26 | import org.jetbrains.annotations.NotNull; |
34 | 27 |
|
35 | 28 | import javax.annotation.Nonnull; |
36 | | -import java.io.BufferedReader; |
37 | 29 | import java.io.File; |
38 | 30 | import java.io.IOException; |
39 | | -import java.io.InputStream; |
40 | | -import java.io.InputStreamReader; |
41 | 31 | import java.lang.reflect.Field; |
42 | 32 | import java.lang.reflect.InvocationTargetException; |
43 | | -import java.net.URL; |
44 | | -import java.nio.charset.StandardCharsets; |
45 | 33 | import java.util.HashMap; |
46 | 34 | import java.util.LinkedList; |
47 | | -import java.util.Map; |
48 | 35 | import java.util.logging.Level; |
49 | 36 | import java.util.logging.Logger; |
50 | 37 | import java.util.regex.Pattern; |
51 | 38 |
|
52 | 39 | public class ProtocolStringReplacer extends JavaPlugin { |
53 | | - |
54 | | - public static final String VERSION_CHANNEL = "Stable"; |
55 | | - public static final int VERSION_NUMBER = 103; |
56 | 40 | private static ProtocolStringReplacer instance; |
57 | 41 | private static Logger logger; |
58 | | - private final HashMap<String, Integer> msgTimes = new HashMap<>(); |
59 | 42 | private CommentYamlConfiguration config; |
60 | 43 | private File configFile; |
61 | 44 | private ReplacerManager replacerManager; |
@@ -190,6 +173,7 @@ public void onEnable() { |
190 | 173 | if (!checkDepends("PlaceholderAPI", "ProtocolLib")) { |
191 | 174 | initialize(); |
192 | 175 | } |
| 176 | + new Updater(this).start(); |
193 | 177 | } |
194 | 178 |
|
195 | 179 | @Override |
@@ -259,46 +243,6 @@ private void initialize() { |
259 | 243 | userManager.loadUser(player); |
260 | 244 | player.updateInventory(); |
261 | 245 | } |
262 | | - initMetrics(); |
263 | | - Bukkit.getScheduler().runTaskTimerAsynchronously(instance, () -> { |
264 | | - if (!checkPluginVersion()) { |
265 | | - Bukkit.getPluginManager().disablePlugin(instance); |
266 | | - } |
267 | | - }, 0L, 72000L); |
268 | | - } |
269 | | - |
270 | | - private void initMetrics() { |
271 | | - Metrics metrics = new Metrics(this, 11740); |
272 | | - metrics.addCustomChart(new DrilldownPie("Replaces_Count", () -> { |
273 | | - int configs = 0; |
274 | | - int replaces = 0; |
275 | | - for (ReplacerConfig replacerConfig : replacerManager.getReplacerConfigList()) { |
276 | | - configs++; |
277 | | - for (ReplaceMode mode : ReplaceMode.values()) { |
278 | | - replaces += replacerConfig.getReplaces(mode).size(); |
279 | | - } |
280 | | - } |
281 | | - Map<String, Map<String, Integer>> map = new HashMap<>(); |
282 | | - Map<String, Integer> entry = new HashMap<>(); |
283 | | - entry.put(replaces + (replaces >= 1 ? " Replaces" : " Replace"), 1); |
284 | | - map.put(configs + (configs >= 1 ? " Configs" : " Config"), entry); |
285 | | - return map; |
286 | | - })); |
287 | | - metrics.addCustomChart(new DrilldownPie("Blocks_Count", () -> { |
288 | | - int configs = 0; |
289 | | - int blocks = 0; |
290 | | - for (ReplacerConfig replacerConfig : replacerManager.getReplacerConfigList()) { |
291 | | - configs++; |
292 | | - for (ReplaceMode mode : ReplaceMode.values()) { |
293 | | - blocks += replacerConfig.getBlocks(mode).size(); |
294 | | - } |
295 | | - } |
296 | | - Map<String, Map<String, Integer>> map = new HashMap<>(); |
297 | | - Map<String, Integer> entry = new HashMap<>(); |
298 | | - entry.put(blocks + (blocks >= 1 ? " Blocks" : " Block"), 1); |
299 | | - map.put(configs + (configs >= 1 ? " Configs" : " Config"), entry); |
300 | | - return map; |
301 | | - })); |
302 | 246 | } |
303 | 247 |
|
304 | 248 | private boolean checkDepends(String... depends) { |
@@ -418,90 +362,6 @@ public void checkConfigsVersion() { |
418 | 362 | } |
419 | 363 | } |
420 | 364 |
|
421 | | - /** |
422 | | - * @return false if plugin doesn't pass the check. |
423 | | - * @since 2.0.0 |
424 | | - */ |
425 | | - public boolean checkPluginVersion() { |
426 | | - try { |
427 | | - final URL url = new URL("https://" + getConfigManager().gitRawHost + "/Rothes/ProtocolStringReplacer/master/Version%20Infos.json"); |
428 | | - final InputStream stream = url.openStream(); |
429 | | - final BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)); |
430 | | - final StringBuilder jsonBuilder = new StringBuilder(); |
431 | | - for (String line = reader.readLine(); line != null; line = reader.readLine()) { |
432 | | - jsonBuilder.append(line).append("\n"); |
433 | | - } |
434 | | - stream.close(); |
435 | | - reader.close(); |
436 | | - try { |
437 | | - final JsonElement element = new JsonParser().parse(jsonBuilder.toString()); |
438 | | - final JsonObject root = element.getAsJsonObject(); |
439 | | - JsonObject channel = root.getAsJsonObject("Version_Channels").getAsJsonObject(VERSION_CHANNEL); |
440 | | - if (channel == null) { |
441 | | - warn(PsrLocalization.getLocaledMessage("Console-Sender.Messages.Updater.Invalid-Channel")); |
442 | | - } else if (channel.has("Message") |
443 | | - && Integer.parseInt(channel.getAsJsonPrimitive("Latest_Version_Number").getAsString()) |
444 | | - > VERSION_NUMBER) { |
445 | | - for (String s : getLocaledJsonMessage(channel.getAsJsonObject("Message")).split("\n")) { |
446 | | - warn(s); |
447 | | - } |
448 | | - } |
449 | | - |
450 | | - boolean prohibit = false; |
451 | | - for (Map.Entry<String, JsonElement> entry : root.getAsJsonObject("Version_Actions").entrySet()) { |
452 | | - String[] split = entry.getKey().split("-"); |
453 | | - if (Integer.parseInt(split[1]) > VERSION_NUMBER |
454 | | - && VERSION_NUMBER > Integer.parseInt(split[0])) { |
455 | | - JsonObject json = (JsonObject) entry.getValue(); |
456 | | - if (json.has("Message")) { |
457 | | - JsonElement temp = json.get("Message_Times"); |
458 | | - final int msgTimes = temp == null ? -1 : temp.getAsInt(); |
459 | | - final int curTimes = this.msgTimes.get(entry.getKey()) == null ? 0 : this.msgTimes.get(entry.getKey()); |
460 | | - if (msgTimes == -1 || curTimes < msgTimes) { |
461 | | - temp = json.get("Log_Level"); |
462 | | - for (String s : getLocaledJsonMessage(json.getAsJsonObject("Message")).split("\n")) { |
463 | | - switch (temp == null ? "default maybe" : temp.getAsString()) { |
464 | | - case "Error": |
465 | | - error(s); |
466 | | - break; |
467 | | - case "Warn": |
468 | | - warn(s); |
469 | | - break; |
470 | | - case "Info": |
471 | | - default: |
472 | | - info(s); |
473 | | - break; |
474 | | - } |
475 | | - } |
476 | | - this.msgTimes.put(entry.getKey(), curTimes + 1); |
477 | | - } |
478 | | - } |
479 | | - for (JsonElement action : json.getAsJsonArray("Actions")) { |
480 | | - prohibit = prohibit || action.getAsString().equals("Prohibit"); |
481 | | - } |
482 | | - } |
483 | | - } |
484 | | - return !prohibit; |
485 | | - } catch (IllegalStateException | NullPointerException e) { |
486 | | - error(PsrLocalization.getLocaledMessage("Console-Sender.Messages.Updater.Error-Parsing-Json", e.toString())); |
487 | | - e.printStackTrace(); |
488 | | - } |
489 | | - } catch (IOException e) { |
490 | | - // error(PsrLocalization.getLocaledMessage("Console-Sender.Messages.Updater.Error-Checking-Version", e.toString())); |
491 | | - } |
492 | | - return true; |
493 | | - } |
494 | | - |
495 | | - public static String getLocaledJsonMessage(@NotNull JsonObject messageJson) { |
496 | | - String msg; |
497 | | - if (messageJson.has(PsrLocalization.getLocale())) { |
498 | | - msg = messageJson.get(PsrLocalization.getLocale()).getAsString(); |
499 | | - } else { |
500 | | - msg = messageJson.get("en-US").getAsString(); |
501 | | - } |
502 | | - return msg; |
503 | | - } |
504 | | - |
505 | 365 | public void reload(@Nonnull PsrUser user) { |
506 | 366 | reloading = true; |
507 | 367 | Validate.notNull(user, "user cannot be null"); |
|
0 commit comments