Merge pull request #26 from TBMCPlugins/dev

Improved UpdatePlugin and added IsTestServer
This commit is contained in:
Norbi Peti 2016-12-22 23:08:04 +01:00 committed by GitHub
commit 48edb00d75
2 changed files with 86 additions and 49 deletions

View file

@ -16,6 +16,7 @@ import net.milkbowl.vault.permission.Permission;
public class MainPlugin extends JavaPlugin { public class MainPlugin extends JavaPlugin {
public static MainPlugin Instance; public static MainPlugin Instance;
public static Permission permission; public static Permission permission;
public static boolean Test;
private PluginDescriptionFile pdfFile; private PluginDescriptionFile pdfFile;
private Logger logger; private Logger logger;
@ -27,6 +28,8 @@ public class MainPlugin extends JavaPlugin {
pdfFile = getDescription(); pdfFile = getDescription();
logger = getLogger(); logger = getLogger();
setupPermissions(); setupPermissions();
Test = getConfig().getBoolean("test", true);
saveConfig();
TBMCChatAPI.AddCommand(this, UpdatePluginCommand.class); TBMCChatAPI.AddCommand(this, UpdatePluginCommand.class);
TBMCChatAPI.AddCommand(this, ScheduledRestartCommand.class); TBMCChatAPI.AddCommand(this, ScheduledRestartCommand.class);
TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this); TBMCCoreAPI.RegisterEventsForExceptions(new PlayerListener(), this);

View file

@ -1,17 +1,10 @@
package buttondevteam.lib; package buttondevteam.lib;
import java.io.File; import java.io.*;
import java.io.FileNotFoundException; import java.net.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
@ -22,10 +15,9 @@ import org.bukkit.entity.Player;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import com.google.gson.JsonArray; import com.google.gson.*;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import buttondevteam.core.MainPlugin;
import com.google.gson.JsonParser;
public final class TBMCCoreAPI { public final class TBMCCoreAPI {
static List<String> coders = new ArrayList<String>() { static List<String> coders = new ArrayList<String>() {
@ -36,7 +28,9 @@ public final class TBMCCoreAPI {
add("iie"); add("iie");
add("thewindmillman"); add("thewindmillman");
add("mayskam1995"); add("mayskam1995");
}}; }
};
/** /**
* Updates or installs the specified plugin. The plugin must use Maven. * Updates or installs the specified plugin. The plugin must use Maven.
* *
@ -58,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;
@ -71,32 +66,46 @@ 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, "Updating TBMC plugin: " + correctname + " from " + branch); info(sender, "Checking branch name...");
if (!TBMCCoreAPI.IsTestServer() && !branch.equalsIgnoreCase("master")) {
error(sender, "The server is in production mode, updating only allowed from master!");
return false;
}
Optional<String> correctbranch = GetPluginBranches(correctname).stream().filter(b -> b.equalsIgnoreCase(branch))
.findAny();
if (!correctbranch.isPresent()) {
error(sender, "Can't find branch \"" + branch + "\" for plugin \"" + correctname + "\"");
return false;
}
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/"
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/" + branch + (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/"
+ "-SNAPSHOT/" + correctname + "-" + branch + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well + correctbranch.get() + "-SNAPSHOT/" + correctname + "-" + correctbranch.get() + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
FileUtils.copyURLToFile(url, result); FileUtils.copyURLToFile(url, result);
if (!result.exists() || result.length() < 25) { if (!result.exists() || result.length() < 25) {
result.delete(); result.delete();
error(sender, "The downloaded JAR for " + correctname + " from " + branch 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 " + branch + " done!"); info(sender, "Updating plugin " + correctname + " from " + correctbranch.get() + " done!");
return true;
} }
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
error(sender, error(sender,
"Can't find JAR for " + correctname + " from " + branch "Can't find JAR for " + correctname + " from " + correctbranch.get()
+ ", the build probably failed. Build log (scroll to bottom):" + "\n" + ", the build probably failed. Build log (scroll to bottom):" + "\n"
+ "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + branch + "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + correctbranch.get()
+ "-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/" + "-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/"
+ correctname + "\nAnd delete the newest build."); + correctname + "\nAnd delete the newest build.");
} catch (IOException e) { } catch (IOException e) {
@ -105,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) {
@ -139,6 +149,26 @@ public final class TBMCCoreAPI {
return ret; return ret;
} }
/**
* Retrieves all the branches from the plugin repository.
*
* @return A list of names
*/
public static List<String> GetPluginBranches(String plugin) {
List<String> ret = new ArrayList<>();
try {
String resp = DownloadString("https://api.github.com/repos/TBMCPlugins/" + plugin + "/branches");
JsonArray arr = new JsonParser().parse(resp).getAsJsonArray();
for (JsonElement obj : arr) {
JsonObject jobj = obj.getAsJsonObject();
ret.add(jobj.get("name").getAsString());
}
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
public static String DownloadString(String urlstr) throws MalformedURLException, IOException { public static String DownloadString(String urlstr) throws MalformedURLException, IOException {
URL url = new URL(urlstr); URL url = new URL(urlstr);
URLConnection con = url.openConnection(); URLConnection con = url.openConnection();
@ -154,7 +184,6 @@ public final class TBMCCoreAPI {
private static HashMap<String, Throwable> exceptionsToSend = new HashMap<>(); private static HashMap<String, Throwable> exceptionsToSend = new HashMap<>();
private static List<String> debugMessagesToSend = new ArrayList<>(); private static List<String> debugMessagesToSend = new ArrayList<>();
/** /**
* Send exception to the {@link TBMCExceptionEvent}. * Send exception to the {@link TBMCExceptionEvent}.
* *
@ -163,9 +192,10 @@ public final class TBMCCoreAPI {
* @param e * @param e
* The exception to send * The exception to send
*/ */
public static void SendException(String sourcemsg, Throwable e){ public static void SendException(String sourcemsg, Throwable e) {
SendException(sourcemsg, e, false); SendException(sourcemsg, e, false);
} }
public static void SendException(String sourcemsg, Throwable e, boolean debugPotato) { public static void SendException(String sourcemsg, Throwable e, boolean debugPotato) {
SendUnsentExceptions(); SendUnsentExceptions();
TBMCExceptionEvent event = new TBMCExceptionEvent(sourcemsg, e); TBMCExceptionEvent event = new TBMCExceptionEvent(sourcemsg, e);
@ -174,13 +204,14 @@ public final class TBMCCoreAPI {
exceptionsToSend.put(sourcemsg, e); exceptionsToSend.put(sourcemsg, e);
Bukkit.getLogger().warning(sourcemsg); Bukkit.getLogger().warning(sourcemsg);
e.printStackTrace(); e.printStackTrace();
if (debugPotato){ if (debugPotato) {
List<Player> devsOnline = new ArrayList<Player>(); List<Player> devsOnline = new ArrayList<Player>();
for (Player player : Bukkit.getOnlinePlayers()){ for (Player player : Bukkit.getOnlinePlayers()) {
if (coders.contains(player.getName())){ if (coders.contains(player.getName())) {
devsOnline.add(player); devsOnline.add(player);
} }
}; }
;
if (!devsOnline.isEmpty()) { if (!devsOnline.isEmpty()) {
DebugPotato potato = new DebugPotato() DebugPotato potato = new DebugPotato()
.setMessage(new String[] { // .setMessage(new String[] { //
@ -190,9 +221,8 @@ public final class TBMCCoreAPI {
.setType(e instanceof IOException ? "Throwable Potato" .setType(e instanceof IOException ? "Throwable Potato"
: e instanceof ClassCastException ? "Squished Potato" : e instanceof ClassCastException ? "Squished Potato"
: e instanceof NullPointerException ? "Plain Potato" : e instanceof NullPointerException ? "Plain Potato"
: e instanceof StackOverflowError ? "Chips" : e instanceof StackOverflowError ? "Chips" : "Error Potato");
: "Error Potato"); for (Player dev : devsOnline) {
for (Player dev : devsOnline){
potato.Send(dev); potato.Send(dev);
} }
} }
@ -247,4 +277,8 @@ public final class TBMCCoreAPI {
debugMessagesToSend.remove(message); debugMessagesToSend.remove(message);
} }
} }
public static boolean IsTestServer() {
return MainPlugin.Test;
}
} }