Added update checking for LibsDisguises

This commit is contained in:
Andrew 2013-05-31 20:55:55 +12:00
parent fa97bf43a5
commit 20a75f947a
3 changed files with 96 additions and 1 deletions

4
config.yml Normal file
View File

@ -0,0 +1,4 @@
# Shall I notify people of a LibsDisguises update?
NotifyUpdate: true
# Whats the permission to get the notification?
Permission: 'libsdisguises.update'

View File

@ -12,11 +12,15 @@ import me.libraryaddict.disguise.DisguiseTypes.PlayerDisguise;
import net.minecraft.server.v1_5_R3.WatchableObject; import net.minecraft.server.v1_5_R3.WatchableObject;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Arrow; import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb; import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Item; import org.bukkit.entity.Item;
import org.bukkit.entity.Player; 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.bukkit.plugin.java.JavaPlugin;
import com.comphenix.protocol.Packets; import com.comphenix.protocol.Packets;
@ -28,7 +32,10 @@ import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
public class LibsDisguises extends JavaPlugin { public class LibsDisguises extends JavaPlugin implements Listener {
private String latestVersion;
private String currentVersion;
private String permission;
@Override @Override
public void onEnable() { public void onEnable() {
@ -124,5 +131,39 @@ public class LibsDisguises extends JavaPlugin {
getCommand("undisguise").setExecutor(new UndisguiseCommand()); getCommand("undisguise").setExecutor(new UndisguiseCommand());
getCommand("disguiseplayer").setExecutor(new DisguisePlayerCommand()); getCommand("disguiseplayer").setExecutor(new DisguisePlayerCommand());
getCommand("undisguiseplayer").setExecutor(new UndisguisePlayerCommand()); getCommand("undisguiseplayer").setExecutor(new UndisguisePlayerCommand());
saveDefaultConfig();
permission = getConfig().getString("Permission");
if (getConfig().getBoolean("NotifyUpdate"))
Bukkit.getScheduler().scheduleAsyncDelayedTask(this, new Runnable() {
public void run() {
try {
UpdateChecker updateChecker = new UpdateChecker();
updateChecker.checkUpdate("v"
+ Bukkit.getPluginManager().getPlugin("LibsDisguises").getDescription().getVersion());
latestVersion = updateChecker.getLatestVersion();
if (latestVersion != null) {
latestVersion = "v" + latestVersion;
for (Player p : Bukkit.getOnlinePlayers())
if (p.hasPermission(permission))
p.sendMessage(String.format(ChatColor.GOLD + "[LibsDisguises] " + ChatColor.DARK_GREEN
+ "There is a update ready to be downloaded! You are using " + ChatColor.GREEN + "%s"
+ ChatColor.DARK_GREEN + ", the new version is " + ChatColor.GREEN + "%s"
+ ChatColor.DARK_GREEN + "!", currentVersion, latestVersion));
}
} catch (Exception ex) {
System.out.print(String.format("[LibsDisguises] Failed to check for update: %s", ex.getMessage()));
}
}
});
}
@EventHandler
public void onJoin(PlayerJoinEvent event) {
Player p = event.getPlayer();
if (latestVersion != null && p.hasPermission(permission))
p.sendMessage(String.format(ChatColor.GOLD + "[LibsDisguises] " + ChatColor.DARK_GREEN
+ "There is a update ready to be downloaded! You are using " + ChatColor.GREEN + "%s" + ChatColor.DARK_GREEN
+ ", the new version is " + ChatColor.GREEN + "%s" + ChatColor.DARK_GREEN + "!", currentVersion,
latestVersion));
} }
} }

View File

@ -0,0 +1,50 @@
package me.libraryaddict.disguise;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Pattern;
public class UpdateChecker {
private String latestVersion;
private boolean checkHigher(String currentVersion, String newVersion) {
String current = toReadable(currentVersion);
String newVers = toReadable(newVersion);
return current.compareTo(newVers) < 0;
}
public void checkUpdate(String currentVersion) throws Exception {
String version = getVersion("98BE0FE67F88AB82B4C197FAF1DC3B69206EFDCC4D3B80FC83A00037510B99B4", 55);
if (checkHigher(currentVersion, version))
latestVersion = version;
}
public String getLatestVersion() {
return latestVersion;
}
private String getVersion(String key, int resourceId) {
String version = null;
try {
HttpURLConnection con = (HttpURLConnection) new URL("http://www.spigotmc.org/api/general.php").openConnection();
con.setDoOutput(true);
con.setRequestMethod("POST");
con.getOutputStream().write(("key=" + key + "&resource=" + resourceId).getBytes("UTF-8"));
version = new BufferedReader(new InputStreamReader(con.getInputStream())).readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return version;
}
public String toReadable(String version) {
String[] split = Pattern.compile(".", Pattern.LITERAL).split(version.replace("v", ""));
version = "";
for (String s : split)
version += String.format("%4s", s);
return version;
}
}