Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.threadpoller;
- import com.google.gson.*;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.configuration.file.FileConfiguration;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import java.io.InputStreamReader;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- import java.util.concurrent.TimeUnit;
- public class ThreadPoller extends JavaPlugin implements CommandExecutor {
- private ScheduledExecutorService scheduler;
- private Set<Long> seenPosts = new HashSet<>();
- private FileConfiguration cfg;
- private Boolean bFirst;
- @Override
- public void onEnable() {
- saveDefaultConfig();
- cfg = getConfig();
- bFirst = true;
- getCommand("pollreload").setExecutor(this);
- startPolling();
- }
- @Override
- public void onDisable() {
- if (scheduler != null)
- scheduler.shutdownNow();
- }
- private void startPolling() {
- if (scheduler != null)
- scheduler.shutdownNow();
- scheduler = Executors.newSingleThreadScheduledExecutor();
- int interval = cfg.getInt("interval-seconds", 30);
- scheduler.scheduleAtFixedRate(this::pollOnce, 0, interval, TimeUnit.SECONDS);
- }
- private void pollOnce() {
- try {
- String urlStr = cfg.getString("url");
- HttpURLConnection conn = (HttpURLConnection) new URL(urlStr).openConnection();
- conn.setRequestMethod("GET");
- conn.setConnectTimeout(5000);
- conn.setReadTimeout(5000);
- conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
- + "AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36");
- conn.setRequestProperty("Accept", "application/json");
- conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9");
- conn.setRequestProperty("Referer", "https://ziemniachan.org/");
- conn.setRequestProperty("Connection", "keep-alive");
- JsonObject root = JsonParser.parseReader(
- new InputStreamReader(conn.getInputStream())).getAsJsonObject();
- JsonArray posts = root.getAsJsonArray("posts");
- String prefix = cfg.getString("prefix", "[fred na ziemniaku]");
- for (JsonElement el : posts) {
- JsonObject post = el.getAsJsonObject();
- long no = post.get("no").getAsLong();
- if (!seenPosts.contains(no)) {
- seenPosts.add(no);
- if (bFirst) {
- continue;
- }
- String raw = post.has("com") ? post.get("com").getAsString() : "";
- String noHtml = raw.replaceAll("(?i)<[^>]*>", "");
- noHtml = noHtml.replaceAll("^(>+[0-9]+)", "$1 ");
- String com = noHtml.replace(">", ">")
- .replace("<", "<")
- .replace("&", "&");
- String message = ChatColor.RED + prefix +
- ChatColor.GREEN + "<" + no + ">" +
- ChatColor.YELLOW + ": " + com;
- Bukkit.getScheduler().runTask(this, () -> Bukkit.broadcastMessage(message));
- }
- }
- bFirst = false;
- } catch (Exception e) {
- getLogger().warning("Error polling thread: " + e.getMessage());
- }
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (command.getName().equalsIgnoreCase("pollreload")) {
- reloadConfig();
- cfg = getConfig();
- seenPosts.clear();
- startPolling();
- sender.sendMessage(ChatColor.GREEN + "ThreadPoller reloaded.");
- return true;
- }
- return false;
- }
- }z
Advertisement
Add Comment
Please, Sign In to add comment