Improved UpdatePlugin

- Made it support Windows...
- Now it returns success or not
This commit is contained in:
Norbi Peti 2016-12-22 22:32:07 +01:00
parent 8ad5504527
commit 222a1225bc

View file

@ -52,8 +52,9 @@ public final class TBMCCoreAPI {
* The command sender (if not console, messages will be printed to console as well). * The command sender (if not console, messages will be printed to console as well).
* @param branch * @param branch
* The branch to download the plugin from. * The branch to download the plugin from.
* @return Success or not
*/ */
public static void UpdatePlugin(String name, CommandSender sender, String branch) { public static boolean UpdatePlugin(String name, CommandSender sender, String branch) {
info(sender, "Checking plugin name..."); info(sender, "Checking plugin name...");
List<String> plugins = GetPluginNames(); List<String> plugins = GetPluginNames();
String correctname = null; String correctname = null;
@ -65,22 +66,24 @@ public final class TBMCCoreAPI {
} }
if (correctname == null) { if (correctname == null) {
error(sender, "Can't find plugin: " + name); error(sender, "Can't find plugin: " + name);
return; return false;
} }
info(sender, "Checking branch name..."); info(sender, "Checking branch name...");
if (TBMCCoreAPI.IsTestServer() && !branch.equalsIgnoreCase("master")) { if (TBMCCoreAPI.IsTestServer() && !branch.equalsIgnoreCase("master")) {
error(sender, "The server is in production mode, updating only allowed from master!"); error(sender, "The server is in production mode, updating only allowed from master!");
return; return false;
} }
Optional<String> correctbranch = GetPluginBranches(correctname).stream().filter(b -> b.equalsIgnoreCase(branch)) Optional<String> correctbranch = GetPluginBranches(correctname).stream().filter(b -> b.equalsIgnoreCase(branch))
.findAny(); .findAny();
if (!correctbranch.isPresent()) { if (!correctbranch.isPresent()) {
error(sender, "Can't find branch \"" + branch + "\" for plugin \"" + correctname + "\""); error(sender, "Can't find branch \"" + branch + "\" for plugin \"" + correctname + "\"");
return; return false;
} }
info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get()); info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get());
URL url; URL url;
File result = new File("plugins/" + correctname + ".jar_tmp"); final boolean isWindows = System.getProperty("os.name").contains("Windows");
File result = new File(
"plugins/" + correctname + (isWindows ? ".jar" : ".jar_tmp"));
File finalresult = new File("plugins/" + correctname + ".jar"); File finalresult = new File("plugins/" + correctname + ".jar");
try { try {
url = new URL("https://jitpack.io/com/github/TBMCPlugins/" url = new URL("https://jitpack.io/com/github/TBMCPlugins/"
@ -91,11 +94,12 @@ public final class TBMCCoreAPI {
result.delete(); result.delete();
error(sender, "The downloaded JAR for " + correctname + " from " + correctbranch.get() error(sender, "The downloaded JAR for " + correctname + " from " + correctbranch.get()
+ " is too small (smnaller than 25 bytes). Am I downloading from the right place?"); + " is too small (smnaller than 25 bytes). Am I downloading from the right place?");
return; return false;
} else { } else {
finalresult.delete(); // Attempt to support Windows if (!isWindows)
Files.move(result.toPath(), finalresult.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.move(result.toPath(), finalresult.toPath(), StandardCopyOption.REPLACE_EXISTING);
info(sender, "Updating plugin " + correctname + " from " + correctbranch.get() + " done!"); info(sender, "Updating plugin " + correctname + " from " + correctbranch.get() + " done!");
return true;
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
error(sender, error(sender,
@ -110,6 +114,7 @@ public final class TBMCCoreAPI {
e.printStackTrace(); e.printStackTrace();
error(sender, "Unknown error while updating " + correctname + ": " + e); error(sender, "Unknown error while updating " + correctname + ": " + e);
} }
return false;
} }
private static void error(CommandSender sender, String message) { private static void error(CommandSender sender, String message) {