Improved UpdatePlugin
- Added branch checking - Added IsTestServer() - Restricted production updating to the master branch
This commit is contained in:
parent
bceb57c522
commit
8ad5504527
2 changed files with 75 additions and 43 deletions
|
@ -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);
|
||||||
|
|
|
@ -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.
|
||||||
*
|
*
|
||||||
|
@ -73,30 +67,41 @@ public final class TBMCCoreAPI {
|
||||||
error(sender, "Can't find plugin: " + name);
|
error(sender, "Can't find plugin: " + name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get());
|
||||||
URL url;
|
URL url;
|
||||||
File result = new File("plugins/" + correctname + ".jar_tmp");
|
File result = new File("plugins/" + correctname + ".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;
|
||||||
} else {
|
} else {
|
||||||
finalresult.delete(); // Attempt to support Windows
|
finalresult.delete(); // Attempt to support Windows
|
||||||
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!");
|
||||||
}
|
}
|
||||||
} 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) {
|
||||||
|
@ -139,6 +144,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 +179,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}.
|
||||||
*
|
*
|
||||||
|
@ -166,6 +190,7 @@ public final class TBMCCoreAPI {
|
||||||
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);
|
||||||
|
@ -180,7 +205,8 @@ public final class TBMCCoreAPI {
|
||||||
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,8 +216,7 @@ 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 +272,8 @@ public final class TBMCCoreAPI {
|
||||||
debugMessagesToSend.remove(message);
|
debugMessagesToSend.remove(message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean IsTestServer() {
|
||||||
|
return MainPlugin.Test;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue