OneKey-Update feature

Former-commit-id: a8b951102beabdbb960fb096726ea46c529aa65c [formerly 30c676d0b4a1ba08f493e064777c9a8e4a5619e9]
Former-commit-id: b67a3640ef89274c0f48bf27904fa027314a292c
This commit is contained in:
Ghost_chu committed 2019-07-06 13:51:27 +08:00
1 parent 69297a92e8
commit 65e3dd20eb
3 files changed
+58 -2

No files matched your search

@@ -74,6 +74,9 @@ public class CommandManager implements TabCompleter, CommandExecutor {
registerCmd(CommandContainer.builder().prefix("create").permission("quickshop.create.cmd")
.executor(new SubCommand_Create())
.build());
registerCmd(CommandContainer.builder().prefix("silentupdate").hidden(true).permission("quickshop.alert")
.executor(new SubCommand_SilentUpdate())
.build());
}
private void registerCmd(CommandContainer container) {
@@ -0,0 +1,53 @@
package org.maxgamer.quickshop.Command.SubCommands;
import java.io.IOException;
import java.util.List;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.*;
import org.maxgamer.quickshop.Command.CommandProcesser;
import org.maxgamer.quickshop.QuickShop;
import org.maxgamer.quickshop.Util.Updater;
public class SubCommand_SilentUpdate implements CommandProcesser {
QuickShop plugin = QuickShop.instance;
@Override
public void onCommand(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
if (!Updater.checkUpdate().getVersion().equals(plugin.getDescription().getVersion())) {
sender.sendMessage(ChatColor.GREEN + "No updates can update now.");
return;
}
byte[] pluginBin;
try {
pluginBin = Updater.downloadUpdatedJar();
} catch (IOException e) {
sender.sendMessage(ChatColor.RED + "Update failed, get details to look the console.");
plugin.getSentryErrorReporter().ignoreThrow();
e.printStackTrace();
return;
}
if (pluginBin == null || pluginBin.length < 1) {
sender.sendMessage(ChatColor.RED + "Download failed, check your connection before contact the author.");
return;
}
try {
Updater.replaceTheJar(pluginBin);
} catch (IOException ioe) {
sender.sendMessage(ChatColor.RED + "Update failed, get details to look the console.");
plugin.getSentryErrorReporter().ignoreThrow();
ioe.printStackTrace();
return;
} catch (RuntimeException re) {
sender.sendMessage(ChatColor.RED + "Update failed, " + re.getMessage());
return;
}
sender.sendMessage(ChatColor.GREEN + "Update successfully, restart your server to apply the changes!");
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull String commandLabel, @NotNull String[] cmdArg) {
return null;
}
}
@@ -42,7 +42,7 @@ public class Updater {
}
}
public void replaceTheJar(byte[] data) throws RuntimeException, IOException {
public static void replaceTheJar(byte[] data) throws RuntimeException, IOException {
File pluginFolder = new File("plugins");
if (!pluginFolder.exists())
throw new RuntimeException("Can't find the plugins folder.");
@@ -71,7 +71,7 @@ public class Updater {
outputStream.close();
}
public byte[] downloadUpdatedJar() throws IOException {
public static byte[] downloadUpdatedJar() throws IOException {
final String uurl = "https://api.spiget.org/v2/resources/62575/versions/latest/download";
URL url = new URL(uurl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();