Skip to content

Commit

Permalink
added and implemented UpdateChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
efekurbann committed Apr 14, 2022
1 parent 5fafb20 commit cdc98d3
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import io.github.efekurbann.synccommands.messaging.impl.socket.SocketImpl;
import io.github.efekurbann.synccommands.objects.Server;
import io.github.efekurbann.synccommands.scheduler.Scheduler;
import io.github.efekurbann.synccommands.util.UpdateChecker;
import net.md_5.bungee.api.ProxyServer;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import org.bstats.bungeecord.Metrics;
Expand All @@ -16,15 +18,16 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public final class SyncCommandsBungee extends Plugin {

private Messaging messaging;
private final Config config = new Config(this, "config.yml");
private final ConsoleExecutor consoleExecutor = new BungeeExecutor();
private final Scheduler scheduler = new BungeeScheduler(this);
private final Map<String, Server> servers = new HashMap<>();
private Server server;
private Messaging messaging;

@Override
public void onEnable() {
Expand All @@ -37,9 +40,10 @@ public void onEnable() {
this.getConfig().getString("connection.password"),
this.getConfig().getBoolean("connection.secure"));

if (this.getConfig().getString("connection.type").equalsIgnoreCase("socket"))
String type = this.getConfig().getString("connection.type", "socket");
if (type.equalsIgnoreCase("socket"))
this.messaging = new SocketImpl(server, consoleExecutor, this.getLogger(), scheduler);
else if (this.getConfig().getString("connection.type").equalsIgnoreCase("redis"))
else if (type.equalsIgnoreCase("redis"))
this.messaging = new Redis(server, consoleExecutor, this.getLogger(), scheduler);

this.messaging.connect(
Expand All @@ -64,6 +68,12 @@ else if (this.getConfig().getString("connection.type").equalsIgnoreCase("redis")
this.getProxy().getPluginManager().registerCommand(this, new BSyncCommand(this));

new Metrics(this, 14139);

// some forks sends ugly messages on initialization
// so we will check updates after 3 seconds
ProxyServer.getInstance().getScheduler().schedule(this,
()-> new UpdateChecker(this.getDescription().getVersion(), this.getLogger(), scheduler).checkUpdates(),
3, TimeUnit.SECONDS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.github.efekurbann.synccommands.util;

import io.github.efekurbann.synccommands.scheduler.Scheduler;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.logging.Logger;

public class UpdateChecker {

private static final String RESOURCE_ID = "99596";
private static final String URL = "https://api.spigotmc.org/legacy/update.php?resource=" + RESOURCE_ID;

private final Logger logger;
private final Scheduler scheduler;
private final String version;
private boolean upToDate;

public UpdateChecker(String version, Logger logger, Scheduler scheduler) {
this.logger = logger;
this.scheduler = scheduler;
this.version = version;
}

public void checkUpdates() {
this.scheduler.runAsync(()-> {
try {
HttpsURLConnection con = (HttpsURLConnection) new URL(URL).openConnection();
InputStreamReader reader = new InputStreamReader(con.getInputStream());
String latestVersion = (new BufferedReader(reader)).readLine();
this.upToDate = latestVersion.equals(version);

if (!this.upToDate) {
logger.info("An update was found for SyncCommands!");
logger.info("Download from: https://www.spigotmc.org/resources/" + RESOURCE_ID);
} else
logger.info("Plugin is up to date, no update found.");
} catch (IOException exception) {
this.logger.info("Could not check for updates: " + exception.getMessage());
}
});
}

public boolean isUpToDate() {
return upToDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
import io.github.efekurbann.synccommands.messaging.impl.redis.Redis;
import io.github.efekurbann.synccommands.messaging.impl.socket.SocketImpl;
import io.github.efekurbann.synccommands.objects.Server;
import io.github.efekurbann.synccommands.util.UpdateChecker;
import org.bstats.bukkit.Metrics;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import io.github.efekurbann.synccommands.executor.ConsoleExecutor;
Expand All @@ -23,6 +29,7 @@ public final class SyncCommandsSpigot extends JavaPlugin {
private final ConsoleExecutor consoleExecutor = new BukkitExecutor(this);
private final Map<String, Server> servers = new HashMap<>();
private final Scheduler scheduler = new BukkitScheduler(this);
private UpdateChecker updateChecker;
private Messaging messaging;
private Server server;

Expand All @@ -37,9 +44,10 @@ public void onEnable() {
this.getConfig().getString("connection.password"),
this.getConfig().getBoolean("connection.secure"));

if (this.getConfig().getString("connection.type").equalsIgnoreCase("socket"))
String type = this.getConfig().getString("connection.type", "socket");
if (type.equalsIgnoreCase("socket"))
this.messaging = new SocketImpl(server, consoleExecutor, this.getLogger(), scheduler);
else if (this.getConfig().getString("connection.type").equalsIgnoreCase("redis"))
else if (type.equalsIgnoreCase("redis"))
this.messaging = new Redis(server, consoleExecutor, this.getLogger(), scheduler);

this.messaging.connect(
Expand All @@ -64,6 +72,23 @@ else if (this.getConfig().getString("connection.type").equalsIgnoreCase("redis")
this.getCommand("sync").setExecutor(new SyncCommand(this));

new Metrics(this, 14138);

(updateChecker = new UpdateChecker(this.getDescription().getVersion(), this.getLogger(), scheduler)).checkUpdates();

this.getServer().getPluginManager().registerEvents(new Listener() {
@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();

if (!player.hasPermission("synccommands.admin")) return;

if (!updateChecker.isUpToDate()) {
player.sendMessage(ChatColor.GOLD + "[SyncCommands]" + ChatColor.YELLOW + " An update was found!");
player.sendMessage(ChatColor.GOLD + "[SyncCommands]" + ChatColor.YELLOW +
" Download from: https://www.spigotmc.org/resources/99596");
}
}
}, this);
}

@Override
Expand Down Expand Up @@ -93,4 +118,8 @@ public Messaging getMessaging() {
public Scheduler getScheduler() {
return scheduler;
}

public UpdateChecker getUpdateChecker() {
return updateChecker;
}
}

0 comments on commit cdc98d3

Please sign in to comment.