3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-03 14:50:30 +01:00

Merge pull request #139 from HugoDaBosss/master

Added proper update message (based on checkplys)
Dieser Commit ist enthalten in:
Myles 2016-03-05 20:40:48 +00:00
Commit eccc5091b5
4 geänderte Dateien mit 242 neuen und 1 gelöschten Zeilen

Datei anzeigen

@ -6,21 +6,29 @@ import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.SocketChannel;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import us.myles.ViaVersion.api.ViaVersion; import us.myles.ViaVersion.api.ViaVersion;
import us.myles.ViaVersion.api.ViaVersionAPI; import us.myles.ViaVersion.api.ViaVersionAPI;
import us.myles.ViaVersion.armor.ArmorListener; import us.myles.ViaVersion.armor.ArmorListener;
import us.myles.ViaVersion.commands.ViaVersionCommand; import us.myles.ViaVersion.commands.ViaVersionCommand;
import us.myles.ViaVersion.handlers.ViaVersionInitializer; import us.myles.ViaVersion.handlers.ViaVersionInitializer;
import us.myles.ViaVersion.listeners.CommandBlockListener; import us.myles.ViaVersion.listeners.CommandBlockListener;
import us.myles.ViaVersion.update.UpdateListener;
import us.myles.ViaVersion.update.UpdateUtil;
import us.myles.ViaVersion.util.ReflectionUtil; import us.myles.ViaVersion.util.ReflectionUtil;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -33,6 +41,8 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI {
private final Map<UUID, ConnectionInfo> portedPlayers = new ConcurrentHashMap<UUID, ConnectionInfo>(); private final Map<UUID, ConnectionInfo> portedPlayers = new ConcurrentHashMap<UUID, ConnectionInfo>();
private boolean debug = false; private boolean debug = false;
private FileConfiguration config;
private File configFile;
@Override @Override
public void onEnable() { public void onEnable() {
@ -51,6 +61,21 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI {
e.printStackTrace(); e.printStackTrace();
} }
this.config = getFileConfiguration();
if(!config.contains("checkforupdates")) {
config.set("checkforupdates", true);
try {
config.save(configFile);
} catch (IOException e1) {
this.getLogger().info("Unabled to write config.yml!");
e1.printStackTrace();
}
}
if(config.getBoolean("checkforupdates")) {
Bukkit.getPluginManager().registerEvents(new UpdateListener(this), this);
UpdateUtil.sendUpdateMessage(this);
}
Bukkit.getPluginManager().registerEvents(new Listener() { Bukkit.getPluginManager().registerEvents(new Listener() {
@EventHandler @EventHandler
public void onPlayerQuit(PlayerQuitEvent e) { public void onPlayerQuit(PlayerQuitEvent e) {
@ -127,6 +152,20 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI {
portedPlayers.remove(clientID); portedPlayers.remove(clientID);
} }
private FileConfiguration getFileConfiguration() {
if(!this.getDataFolder().exists())
this.getDataFolder().mkdirs();
this.configFile = new File(this.getDataFolder(), "config.yml");
if(!this.configFile.exists())
try {
this.configFile.createNewFile();
} catch (IOException e) {
this.getLogger().info("Unable to create config.yml!");
e.printStackTrace();
}
return YamlConfiguration.loadConfiguration(this.configFile);
}
public static ItemStack getHandItem(final ConnectionInfo info) { public static ItemStack getHandItem(final ConnectionInfo info) {
try { try {
return Bukkit.getScheduler().callSyncMethod(getPlugin(ViaVersionPlugin.class), new Callable<ItemStack>() { return Bukkit.getScheduler().callSyncMethod(getPlugin(ViaVersionPlugin.class), new Callable<ItemStack>() {

Datei anzeigen

@ -0,0 +1,22 @@
package us.myles.ViaVersion.update;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
public class UpdateListener implements Listener {
private Plugin plugin;
public UpdateListener(Plugin plugin) {
this.plugin = plugin;
}
@EventHandler
public void onJoin(PlayerJoinEvent e) {
if(e.getPlayer().hasPermission("viaversion.update"))
UpdateUtil.sendUpdateMessage(e.getPlayer().getUniqueId(), plugin);
}
}

Datei anzeigen

@ -0,0 +1,109 @@
package us.myles.ViaVersion.update;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.UUID;
import us.myles.ViaVersion.api.ViaVersion;
public class UpdateUtil {
private final static String URL = "https://api.spiget.org/v1/resources/";
private final static int PLUGIN = 19254;
public final static String PREFIX = ChatColor.GREEN + "" + ChatColor.BOLD + "[ViaVersion] " + ChatColor.GREEN;
public static void sendUpdateMessage(final UUID uuid, final Plugin plugin) {
new BukkitRunnable() {
@Override
public void run() {
final String message = getUpdateMessage(false);
if(message != null) {
new BukkitRunnable() {
@Override
public void run() {
Player p = Bukkit.getPlayer(uuid);
if(p != null)
p.sendMessage(PREFIX + message);
}
}.runTask(plugin);
}
}
}.runTaskAsynchronously(plugin);
}
public static void sendUpdateMessage(final Plugin plugin) {
new BukkitRunnable() {
@Override
public void run() {
final String message = getUpdateMessage(true);
if(message != null) {
new BukkitRunnable() {
@Override
public void run() {
Bukkit.getLogger().info(PREFIX + message);
}
}.runTask(plugin);
}
}
}.runTaskAsynchronously(plugin);
}
private static String getUpdateMessage(boolean console) {
Version current = new Version(ViaVersion.getInstance().getVersion());
Version newest = new Version(getNewestVersion());
if(current.compareTo(newest) < 0)
return "There is a newer version available: " + newest.toString();
else if(console) {
if(current.compareTo(newest) == 0)
return "You are running the newest version: " + current;
else
return "You are running a newer version than is released!";
}
return null;
}
private static String getNewestVersion()
{
String result = "";
try {
URL url = new URL(URL + PLUGIN);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setUseCaches(true);
connection.addRequestProperty("User-Agent", "Mozilla/4.76");
connection.setDoOutput(true);
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String input;
String content = "";
while ((input = br.readLine()) != null) {
content = content + input;
}
br.close();
JsonParser parser = new JsonParser();
JsonObject statistics = (JsonObject)parser.parse(content);
result = statistics.get("version").getAsString();
} catch(MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}

Datei anzeigen

@ -0,0 +1,71 @@
package us.myles.ViaVersion.update;
public class Version implements Comparable<Version>
{
private int[] parts;
public Version(String value)
{
if(value == null)
throw new IllegalArgumentException("Version can not be null");
if(value.matches("^[0-9]+(\\.[0-9]+)*$") == false)
throw new IllegalArgumentException("Invalid version format");
String[] split = value.split("\\.");
parts = new int[split.length];
for (int i = 0; i < split.length; i += 1)
parts[i] = Integer.parseInt(split[i]);
}
@Override
public String toString()
{
String[] split = new String[parts.length];
for (int i = 0; i < parts.length; i += 1)
split[i] = String.valueOf(parts[i]);
return String.join(".", split);
}
public static int compare(Version verA, Version verB)
{
if (verA == verB) return 0;
if (verA == null) return -1;
if (verB == null) return 1;
int max = Math.max(verA.parts.length, verB.parts.length);
for (int i = 0; i < max; i += 1)
{
int partA = i < verA.parts.length ? verA.parts[i] : 0;
int partB = i < verB.parts.length ? verB.parts[i] : 0;
if (partA < partB) return -1;
if (partA > partB) return 1;
}
return 0;
}
@Override
public int compareTo(Version that)
{
return compare(this, that);
}
public static boolean equals(Version verA, Version verB)
{
if (verA == verB) return true;
if (verA == null) return false;
if (verB == null) return false;
return compare(verA, verB) == 0;
}
@Override
public boolean equals(Object that)
{
return that instanceof Version && equals(this, (Version)that);
}
}