Guest User

ziemnia watcher

a guest
Jul 16th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. package com.example.threadpoller;
  2.  
  3. import com.google.gson.*;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.ChatColor;
  6. import org.bukkit.command.Command;
  7. import org.bukkit.command.CommandExecutor;
  8. import org.bukkit.command.CommandSender;
  9. import org.bukkit.configuration.file.FileConfiguration;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12.  
  13. import java.io.InputStreamReader;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16. import java.util.HashSet;
  17. import java.util.Set;
  18. import java.util.concurrent.Executors;
  19. import java.util.concurrent.ScheduledExecutorService;
  20. import java.util.concurrent.TimeUnit;
  21.  
  22. public class ThreadPoller extends JavaPlugin implements CommandExecutor {
  23. private ScheduledExecutorService scheduler;
  24. private Set<Long> seenPosts = new HashSet<>();
  25. private FileConfiguration cfg;
  26. private Boolean bFirst;
  27.  
  28. @Override
  29. public void onEnable() {
  30. saveDefaultConfig();
  31. cfg = getConfig();
  32. bFirst = true;
  33. getCommand("pollreload").setExecutor(this);
  34. startPolling();
  35. }
  36.  
  37. @Override
  38. public void onDisable() {
  39. if (scheduler != null)
  40. scheduler.shutdownNow();
  41. }
  42.  
  43. private void startPolling() {
  44. if (scheduler != null)
  45. scheduler.shutdownNow();
  46. scheduler = Executors.newSingleThreadScheduledExecutor();
  47. int interval = cfg.getInt("interval-seconds", 30);
  48. scheduler.scheduleAtFixedRate(this::pollOnce, 0, interval, TimeUnit.SECONDS);
  49. }
  50.  
  51. private void pollOnce() {
  52. try {
  53. String urlStr = cfg.getString("url");
  54. HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();
  55. conn.setRequestMethod("GET");
  56. conn.setConnectTimeout(5000);
  57. conn.setReadTimeout(5000);
  58.  
  59. conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
  60. + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36");
  61. conn.setRequestProperty("Accept", "application/json");
  62. conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
  63. conn.setRequestProperty("Referer", "https://ziemniachan.org/");
  64. conn.setRequestProperty("Connection", "keep-alive");
  65.  
  66. JsonObject root = JsonParser.parseReader(
  67. new InputStreamReader(conn.getInputStream())).getAsJsonObject();
  68. JsonArray posts = root.getAsJsonArray("posts");
  69. String prefix = cfg.getString("prefix", "[fred na ziemniaku]");
  70.  
  71. for (JsonElement el : posts) {
  72. JsonObject post = el.getAsJsonObject();
  73. long no = post.get("no").getAsLong();
  74. if (!seenPosts.contains(no)) {
  75. seenPosts.add(no);
  76. if (bFirst) {
  77. continue;
  78. }
  79. String raw = post.has("com") ? post.get("com").getAsString() : "";
  80. String noHtml = raw.replaceAll("(?i)<[^>]*>", "");
  81. noHtml = noHtml.replaceAll("^(>+[0-9]+)", "$1 ");
  82. String com = noHtml.replace("&gt;", ">")
  83. .replace("&lt;", "<")
  84. .replace("&amp;", "&");
  85. String message = ChatColor.RED + prefix +
  86. ChatColor.GREEN + "<" + no + ">" +
  87. ChatColor.YELLOW + ": " + com;
  88.  
  89. Bukkit.getScheduler().runTask(this, () -> Bukkit.broadcastMessage(message));
  90. }
  91. }
  92.  
  93. bFirst = false;
  94. } catch (Exception e) {
  95. getLogger().warning("Error polling thread: " + e.getMessage());
  96. }
  97. }
  98.  
  99. @Override
  100. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  101. if (command.getName().equalsIgnoreCase("pollreload")) {
  102. reloadConfig();
  103. cfg = getConfig();
  104. seenPosts.clear();
  105. startPolling();
  106. sender.sendMessage(ChatColor.GREEN + "ThreadPoller reloaded.");
  107. return true;
  108. }
  109. return false;
  110. }
  111. }z
Advertisement
Add Comment
Please, Sign In to add comment