Reload other plugins, remove plugin updater

#84
This commit is contained in:
Norbi Peti 2020-02-19 10:47:53 +01:00
parent b9781f19f0
commit def4a846b2
3 changed files with 16 additions and 143 deletions

View file

@ -1,16 +1,22 @@
package buttondevteam.core; package buttondevteam.core;
import buttondevteam.lib.architecture.ButtonPlugin;
import buttondevteam.lib.chat.Command2; import buttondevteam.lib.chat.Command2;
import buttondevteam.lib.chat.CommandClass; import buttondevteam.lib.chat.CommandClass;
import buttondevteam.lib.chat.ICommand2MC; import buttondevteam.lib.chat.ICommand2MC;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin;
@CommandClass @CommandClass
public class ChromaCommand extends ICommand2MC { public class ChromaCommand extends ICommand2MC {
@Command2.Subcommand //TODO: Main permissions (groups) like 'mod' @Command2.Subcommand
public void reload(CommandSender sender) { public void reload(CommandSender sender, @Command2.OptionalArg Plugin plugin) {
if (MainPlugin.Instance.tryReloadConfig()) if(plugin==null)
sender.sendMessage("§bCore config reloaded."); plugin=MainPlugin.Instance;
if(!(plugin instanceof ButtonPlugin))
plugin.reloadConfig();
else if (((ButtonPlugin) plugin).tryReloadConfig())
sender.sendMessage("§b"+plugin.getName()+" config reloaded.");
else else
sender.sendMessage("§cFailed to reload config. Check console."); sender.sendMessage("§cFailed to reload config. Check console.");
} }

View file

@ -155,21 +155,6 @@ public class MainPlugin extends ButtonPlugin {
logger.info("Saving player data..."); logger.info("Saving player data...");
TBMCPlayerBase.savePlayers(); TBMCPlayerBase.savePlayers();
logger.info("Player data saved."); logger.info("Player data saved.");
new Thread(() -> {
File[] files = PluginUpdater.updatedir.listFiles();
if (files == null)
return;
logger.info("Updating " + files.length + " plugins...");
for (File file : files) {
try {
Files.move(file.toPath(), new File("plugins", file.getName()).toPath(), StandardCopyOption.REPLACE_EXISTING);
logger.info("Updated " + file.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
logger.info("Update complete!");
}).start();
} }
private boolean setupPermissions() { private boolean setupPermissions() {

View file

@ -17,19 +17,17 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
public class PluginUpdater { public class PluginUpdater {
private PluginUpdater() {
}
public static final File updatedir = new File("TBMC", "pluginupdates"); private final File updatedir = Bukkit.getServer().getUpdateFolderFile();
/** /**
* See {@link TBMCCoreAPI#UpdatePlugin(String, CommandSender, String)} * See {@link TBMCCoreAPI#UpdatePlugin(String, CommandSender, String)}
*/ */
public static boolean UpdatePlugin(String name, CommandSender sender, String branch) { public boolean UpdatePlugin(String name, CommandSender sender, String branch) {
if (!updatedir.exists() && !updatedir.mkdirs()) { if (!updatedir.exists() && !updatedir.mkdirs()) {
error(sender, "Failed to create update directory!"); error(sender, "Failed to create update directory!");
return false; return false;
} }
info(sender, "Checking plugin name..."); /*info(sender, "Checking plugin name...");
List<String> plugins = GetPluginNames(); List<String> plugins = GetPluginNames();
String correctname = null; String correctname = null;
for (String plugin : plugins) { for (String plugin : plugins) {
@ -58,61 +56,9 @@ public class PluginUpdater {
return false; return false;
} }
info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get()); info(sender, "Updating TBMC plugin: " + correctname + " from " + correctbranch.get());
return updatePluginJitPack(sender, correctname, correctbranch.get()); return updatePluginJitPack(sender, correctname, correctbranch.get());*/
} info(sender, "Not implemented");
return true;
private static boolean updatePluginJitPack(CommandSender sender, String correctname,
String correctbranch) {
/*URL url;
File result = new File(updatedir, correctname + ".jar");
try {
url = new URL("https://jitpack.io/com/github/TBMCPlugins/"
+ (correctname.equals("ButtonCore") ? "ButtonCore/ButtonCore" : correctname) + "/"
+ correctbranch + "-SNAPSHOT/" + correctname + "-" + correctbranch + "-SNAPSHOT.jar"); // ButtonCore exception required since it hosts Towny as well
FileUtils.copyURLToFile(url, result);
if (!result.exists() || result.length() < 25) {
result.delete();
error(sender, "The downloaded JAR for " + correctname + " from " + correctbranch
+ " is too small (smnaller than 25 bytes). Am I downloading from the right place?");
return false;
} else {
info(sender, "Updating plugin " + correctname + " from " + correctbranch + " done!");
return true;
}
} catch (FileNotFoundException e) {
error(sender,
"Can't find JAR for " + correctname + " from " + correctbranch
+ ", the build probably failed. Build log (scroll to bottom):" + "\n"
+ "https://jitpack.io/com/github/TBMCPlugins/" + correctname + "/" + correctbranch
+ "-SNAPSHOT/build.log\nIf you'd like to rebuild the same commit, go to:\nhttps://jitpack.io/#TBMCPlugins/"
+ correctname + "\nAnd delete the newest build.");
} catch (IOException e) {
error(sender, "IO error while updating " + correctname + "\n" + e.getMessage());
} catch (Exception e) {
e.printStackTrace();
error(sender, "Unknown error while updating " + correctname + ": " + e); - TODO: Either add Commons or don't use FileUtils
}*/
info(sender, "Plugin updating is currently not supported");
return false;
}
/**
* Checks if pom.xml is not present for the project.
*
* @param pluginname
* Does not have to match case
* @param branch
* Does not have to match case
*/
public static boolean isNotMaven(String pluginname, String branch) {
try {
return TBMCCoreAPI
.DownloadString(
"https://raw.githubusercontent.com/TBMCPlugins/" + pluginname + "/" + branch + "/pom.xml")
.equals("404: Not Found\n");
} catch (IOException e1) {
return true;
}
} }
private static void error(CommandSender sender, String message) { private static void error(CommandSender sender, String message) {
@ -126,68 +72,4 @@ public class PluginUpdater {
Bukkit.getLogger().info(message); Bukkit.getLogger().info(message);
sender.sendMessage("§b" + message); sender.sendMessage("§b" + message);
} }
/**
* Retrieves all the repository names from the GitHub organization.
*
* @return A list of names
*/
public static List<String> GetPluginNames() {
List<String> ret = new ArrayList<>();
try {
String resp = TBMCCoreAPI.DownloadString("https://api.github.com/orgs/" + "TBMCPlugins" + "/repos"); //TODO: PluginUpdater
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;
}
/**
* 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 = TBMCCoreAPI
.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 class UpdatedEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final JsonObject data;
public UpdatedEvent(JsonObject data) {
this.data = data;
}
public JsonObject getData() {
return data;
}
@Override
public HandlerList getHandlers() {
return handlers;
}
public static HandlerList getHandlerList() {
return handlers;
}
}
} }