From 16fc04966057b54598da2f380401f006db9acedf Mon Sep 17 00:00:00 2001 From: rockyhawk64 Date: Tue, 8 Dec 2020 11:38:03 +1100 Subject: [PATCH] v3.14.2.0 --- resource/config.yml | 3 + resource/plugin.yml | 4 +- .../commandpanels/CommandPanels.java | 6 ++ .../commands/Commandpanelversion.java | 35 +++++--- .../openwithitem/UtilsOpenWithItem.java | 3 + .../commandpanels/updater/Updater.java | 86 ++++++++++++++++++- 6 files changed, 123 insertions(+), 14 deletions(-) diff --git a/resource/config.yml b/resource/config.yml index 6512a87..c565b29 100644 --- a/resource/config.yml +++ b/resource/config.yml @@ -20,6 +20,9 @@ config: input-message: - '%cp-tag%&aEnter Input for Command' - '&cType &4%cp-args% &cto Cancel the command' + updater: + auto-update: true + minor-updates-only: true format: tag: '&6[&bCommandPanels&6]' perms: '&cNo permission.' diff --git a/resource/plugin.yml b/resource/plugin.yml index c633d87..fce3248 100644 --- a/resource/plugin.yml +++ b/resource/plugin.yml @@ -1,4 +1,4 @@ -version: 3.14.1.5 +version: 3.14.2.0 main: me.rockyhawk.commandpanels.CommandPanels name: CommandPanels author: RockyHawk @@ -69,6 +69,8 @@ permissions: default: op commandpanel.version: default: true + commandpanel.update: + default: op commandpanel.addons: default: true commandpanel.editor: diff --git a/src/me/rockyhawk/commandpanels/CommandPanels.java b/src/me/rockyhawk/commandpanels/CommandPanels.java index 7e02a32..82bf22d 100644 --- a/src/me/rockyhawk/commandpanels/CommandPanels.java +++ b/src/me/rockyhawk/commandpanels/CommandPanels.java @@ -213,6 +213,9 @@ public class CommandPanels extends JavaPlugin { } public void onDisable() { + if (Objects.requireNonNull(this.config.getString("config.updater.auto-update")).equalsIgnoreCase("true")) { + updater.autoUpdatePlugin(this.getFile().getName()); + } Bukkit.getLogger().info("RockyHawk's CommandPanels Plugin Disabled, aww man."); } @@ -565,6 +568,9 @@ public class CommandPanels extends JavaPlugin { if (p.hasPermission("commandpanel.version")) { p.sendMessage(ChatColor.GOLD + "/cpv " + ChatColor.WHITE + "Display the current version."); } + if (p.hasPermission("commandpanel.update")) { + p.sendMessage(ChatColor.GOLD + "/cpv [version:latest:cancel]" + ChatColor.WHITE + "Download an update upon server reload/restart."); + } if (p.hasPermission("commandpanel.edit")) { p.sendMessage(ChatColor.GOLD + "/cpe [panel] " + ChatColor.WHITE + "Edit a panel with the Panel Editor."); } diff --git a/src/me/rockyhawk/commandpanels/commands/Commandpanelversion.java b/src/me/rockyhawk/commandpanels/commands/Commandpanelversion.java index 1cd1718..58e6bf0 100644 --- a/src/me/rockyhawk/commandpanels/commands/Commandpanelversion.java +++ b/src/me/rockyhawk/commandpanels/commands/Commandpanelversion.java @@ -15,20 +15,35 @@ public class Commandpanelversion implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if (label.equalsIgnoreCase("cpv") || label.equalsIgnoreCase("commandpanelversion") || label.equalsIgnoreCase("cpanelv")) { - if (sender.hasPermission("commandpanel.version")) { - //version command - sender.sendMessage(plugin.papi(plugin.tag)); - sender.sendMessage(ChatColor.GREEN + "This Version " + ChatColor.GRAY + plugin.getDescription().getVersion()); - sender.sendMessage(ChatColor.GREEN + "Latest Version " + ChatColor.GRAY + plugin.updater.githubNewUpdate(false)); - sender.sendMessage(ChatColor.GRAY + "-------------------"); - sender.sendMessage(ChatColor.GREEN + "Developer " + ChatColor.GRAY + "RockyHawk"); - sender.sendMessage(ChatColor.GREEN + "Command " + ChatColor.GRAY + "/cp"); + if(args.length == 0) { + if (sender.hasPermission("commandpanel.version")) { + //version command + sender.sendMessage(plugin.papi(plugin.tag)); + sender.sendMessage(ChatColor.GREEN + "This Version " + ChatColor.GRAY + plugin.getDescription().getVersion()); + sender.sendMessage(ChatColor.GREEN + "Latest Version " + ChatColor.GRAY + plugin.updater.githubNewUpdate(false)); + sender.sendMessage(ChatColor.GRAY + "-------------------"); + sender.sendMessage(ChatColor.GREEN + "Developer " + ChatColor.GRAY + "RockyHawk"); + sender.sendMessage(ChatColor.GREEN + "Command " + ChatColor.GRAY + "/cp"); + } else { + sender.sendMessage(plugin.papi(plugin.tag + plugin.config.getString("config.format.perms"))); + } + }else if(args.length == 1){ + if (sender.hasPermission("commandpanel.update")) { + if (args[0].equals("cancel")) { + plugin.updater.downloadVersionManually = null; + sender.sendMessage(plugin.papi(plugin.tag + ChatColor.GREEN + "Will not download a new version on reload or restart.")); + } else { + plugin.updater.downloadVersionManually = args[0]; + sender.sendMessage(plugin.papi(plugin.tag + ChatColor.GREEN + "Downloading version " + ChatColor.GRAY + args[0] + ChatColor.GREEN + " upon server reload or restart.")); + } + }else{ + sender.sendMessage(plugin.papi(plugin.tag + plugin.config.getString("config.format.perms"))); + } }else{ - sender.sendMessage(plugin.papi(plugin.tag + plugin.config.getString("config.format.perms"))); + sender.sendMessage(plugin.papi(plugin.tag + ChatColor.RED + "Usage: /cpv [update]")); } return true; } - sender.sendMessage(plugin.papi(plugin.tag + ChatColor.RED + "Usage: /cpv")); return true; } } diff --git a/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java b/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java index aa444ac..f0dba9e 100644 --- a/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java +++ b/src/me/rockyhawk/commandpanels/openwithitem/UtilsOpenWithItem.java @@ -13,6 +13,7 @@ import org.bukkit.event.block.Action; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; @@ -36,6 +37,8 @@ public class UtilsOpenWithItem implements Listener { Player p = (Player)e.getWhoClicked(); //get the item clicked, then loop through panel names after action isn't nothing if(e.getAction() == InventoryAction.NOTHING){return;} + if(e.getSlot() == -999){return;} + if(e.getClickedInventory().getType() != InventoryType.PLAYER){return;} if(plugin.hotbar.stationaryExecute(e.getSlot(),p,true)){ e.setCancelled(true); p.updateInventory(); diff --git a/src/me/rockyhawk/commandpanels/updater/Updater.java b/src/me/rockyhawk/commandpanels/updater/Updater.java index 8cd3687..c0b2d3a 100644 --- a/src/me/rockyhawk/commandpanels/updater/Updater.java +++ b/src/me/rockyhawk/commandpanels/updater/Updater.java @@ -4,11 +4,11 @@ import me.rockyhawk.commandpanels.CommandPanels; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Objects; +import java.util.logging.Level; public class Updater { CommandPanels plugin; @@ -16,6 +16,10 @@ public class Updater { this.plugin = pl; } + //if this is set to something, it will download that version on restart + //can be a version number, 'latest' or 'cancel' + public String downloadVersionManually = null; + public String githubNewUpdate(boolean sendMessages){ HttpURLConnection connection; String gitVersion; @@ -60,4 +64,80 @@ public class Updater { } return null; } + + //the pluginFileName can only be obtained from the main class + public void autoUpdatePlugin(String pluginFileName){ + String latestVersion = githubNewUpdate(false); + String thisVersion = plugin.getDescription().getVersion(); + + if(downloadVersionManually != null) { + if (downloadVersionManually.equals("latest")) { + downloadFile(latestVersion, pluginFileName); + }else{ + downloadFile(downloadVersionManually, pluginFileName); + } + return; + } + + if(latestVersion.equals(thisVersion)){ + //no need to update + return; + } + if(Objects.equals(plugin.config.getString("config.updater.minor-updates-only"), "true")){ + //only update versions that will not break + if(thisVersion.split("\\.")[1].equals(latestVersion.split("\\.")[1]) && thisVersion.split("\\.")[0].equals(latestVersion.split("\\.")[0])){ + //the first and second number of the version is the same, updates: [major.major.minor.minor] + downloadFile(latestVersion,pluginFileName); + } + }else{ + downloadFile(latestVersion,pluginFileName); + } + } + + private void downloadFile(String latestVersion, String pluginFileName) { + BufferedInputStream in = null; + FileOutputStream fout = null; + + try { + this.plugin.getLogger().info("Downloading new update: " + "v" + latestVersion); + URL fileUrl = new URL("https://github.com/rockyhawk64/CommandPanels/releases/download/" + latestVersion + "/Command.Panels.jar"); + int fileLength = fileUrl.openConnection().getContentLength(); + in = new BufferedInputStream(fileUrl.openStream()); + fout = new FileOutputStream(new File(new File(".").getAbsolutePath() + "/plugins/", pluginFileName)); + byte[] data = new byte[1024]; + + long downloaded = 0L; + + int count; + while((count = in.read(data, 0, 1024)) != -1) { + downloaded += count; + fout.write(data, 0, count); + int percent = (int)(downloaded * 100L / (long)fileLength); + if (percent % 10 == 0) { + this.plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes."); + } + } + this.plugin.getLogger().info("Finished updating."); + } catch (Exception var22) { + this.plugin.getLogger().log(Level.WARNING, "Could not download update.", var22); + } finally { + try { + if (in != null) { + in.close(); + } + } catch (IOException var21) { + this.plugin.getLogger().log(Level.SEVERE, null, var21); + } + + try { + if (fout != null) { + fout.close(); + } + } catch (IOException var20) { + this.plugin.getLogger().log(Level.SEVERE, null, var20); + } + + } + + } }