Java Help Needed [updater]

Discussion in 'Plugin Development' started by xYourFreindx, Jul 6, 2014.

Thread Status:
Not open for further replies.
  1. I know I'm missing something obvious.
    Well, obvious to those who have taken java classes.
    I'm self taught, and haven't had a teacher or colleague to tell me "no you can't do that".
    So any assistance you could provide in locating my problem would be lovely. Thank you.

    Here's as far as I can tell where my issue is coming from:​
    Code:java
    1. package com.gmail.justisroot.cameramode;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6.  
    7.  
    8. import java.util.UUID;
    9.  
    10. import org.bukkit.Location;
    11. import org.bukkit.entity.Entity;
    12. import org.bukkit.plugin.PluginDescriptionFile;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.potion.PotionEffect;
    15. import org.bukkit.util.Vector;
    16.  
    17. import com.gmail.justisroot.cameramode.Updater.UpdateType;
    18.  
    19. public class CameraMode extends JavaPlugin {
    20.  
    21. //##########################//
    22. //######- Data Store -######//
    23. //##########################//
    24.  
    25. public ArrayList<String> flyplayers = new ArrayList<String>();
    26. public HashMap<String, Integer> fireticks = new HashMap<String, Integer>();
    27. public HashMap<String, Integer> breath = new HashMap<String, Integer>();
    28. public ArrayList<String> pause = new ArrayList<String>();
    29. public List<String> allowedcmds = getConfig().getStringList("CameraMode.PlayersInCM.AllowedCommands");
    30. public HashMap<String, Location> locations = new HashMap<String, Location>();
    31. public ArrayList<String> tre = new ArrayList<String>();
    32. public HashMap<String, List<PotionEffect>> effects = new HashMap<String, List<PotionEffect>>();
    33. public HashMap<String, Vector> vel = new HashMap<String, Vector>();
    34. public HashMap<String, Entity> mobs = new HashMap<String, Entity>();
    35. public HashMap<String, Integer> pvpTimer = new HashMap<String, Integer>();
    36. public HashMap<String, Float> falldistance = new HashMap<String, Float>();
    37. public HashMap<UUID, Integer> coolDown = new HashMap<UUID, Integer>();
    38.  
    39. //###### - Objects - ######//
    40.  
    41.  
    42. String reason = "You are in CameraMode!";
    43. UpdateType bifff;
    44. UpdateType biff = this.getUpdater();
    45.  
    46. public ArrayList<String> commands = new ArrayList<String>();
    47.  
    48.  
    49. //############################//
    50. //#########- Start - #########//
    51. //############################//
    52.  
    53. public void onEnable() {
    54. getConfig().options().copyHeader(true);
    55. getConfig().options().copyDefaults(true);
    56. saveConfig();
    57. if (getConfig().getBoolean("CameraMode.Enabled") == false) {
    58. getLogger().info("Plugin Disable Setting Detected...");
    59. getServer().getPluginManager().disablePlugin(this);
    60. }
    61. getServer().getPluginManager().registerEvents(new Events(this), this);
    62. PluginDescriptionFile pdfFile = this.getDescription();
    63. commands.add("camera");
    64. commands.add("cameramode");
    65. for (String commands : this.commands){
    66. getCommand(commands).setExecutor(new Commands(this));
    67. }
    68. getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been enabled");
    69. if (biff != null) {
    70. @SuppressWarnings("unused")
    71. Updater updater = new Updater(this, 80542, getFile(), biff, true);
    72. }
    73. }
    74. public void onDisable(){
    75. reloadConfig();
    76. PluginDescriptionFile pdfFile = this.getDescription();
    77. getLogger().info(pdfFile.getName() + " v" + pdfFile.getVersion() + " has been disabled");
    78.  
    79. }
    80.  
    81. //#############################//
    82. //########- Booleans -#########//
    83. //#############################//
    84.  
    85. public boolean commandIsWhitelisted(String playerCmd) {
    86. playerCmd = playerCmd.replace("/", "");
    87. for(String allowedCmd : this.allowedcmds) {
    88. allowedCmd = allowedCmd.replace("/", "");
    89. if(playerCmd.startsWith(allowedCmd)) return true;
    90. }
    91. return false;
    92. }
    93. public boolean isInt(String s) {
    94. try {
    95. Integer.parseInt(s);
    96. } catch (NumberFormatException nfe) {
    97. return false;
    98. }
    99. return true;
    100. }
    101. //####################################################################################
    102. //####### THIS ##############################################################
    103. // VVVVVV
    104.  
    105. @SuppressWarnings("unused")
    106. public UpdateType getUpdater(){
    107. if (getConfig().getBoolean("CameraMode.Updates.AutoUpdate") == true){
    108. UpdateType bifff = Updater.UpdateType.DEFAULT;
    109. }else if (getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true){
    110. UpdateType bifff = Updater.UpdateType.NO_DOWNLOAD;
    111. }else{
    112. UpdateType bifff = null;
    113. }
    114. return bifff;
    115. }
    116. }

    And...​
    Code:java
    1.  
    2. @EventHandler
    3. public void onPlayerJoin(PlayerJoinEvent e){
    4. if (main.biff.equals(Updater.UpdateResult.UPDATE_AVAILABLE)) {
    5. if (updater.getLatestType().toString().equalsIgnoreCase("release") && (main.getConfig().getBoolean("CameraMode.Updates.NotifyOps") == true)) {
    6. if (e.getPlayer().hasPermission("cameramode.update")){
    7. e.getPlayer().sendMessage(ChatColor.DARK_AQUA + "CameraMode: " + ChatColor.AQUA + "New Update Available!");
    8. e.getPlayer().sendMessage(ChatColor.GRAY + updater.getLatestFileLink().toString());
    9. }
    10. }
    11. }
    12. }

    Perhaps I'm just getting the information incorrectly... But I suspect it's something to do with the system I've made for determining update availability in the main class...

    I'd rather someone help me find the problem and help me to understand why it's not working rather than just be pointed to an answer that works.
    Thank you for your time. ​
     
  2. Offline

    Gater12

    Last edited by a moderator: Nov 18, 2016

  3. Of course... Sorry.
    I'm using the latest of one of dev.bukkit's provided updater classes.
    Code:java
    1. package com.gmail.justisroot.cameramode;
    2.  
    3.  
    4. import java.io.*;
    5. import java.net.MalformedURLException;
    6. import java.net.URL;
    7. import java.net.URLConnection;
    8. import java.util.Enumeration;
    9. import java.util.logging.Level;
    10. import java.util.zip.ZipEntry;
    11. import java.util.zip.ZipFile;
    12.  
    13. import org.bukkit.configuration.file.YamlConfiguration;
    14. import org.bukkit.plugin.Plugin;
    15. import org.json.simple.JSONArray;
    16. import org.json.simple.JSONObject;
    17. import org.json.simple.JSONValue;
    18.  
    19. /**
    20. * Check dev.bukkit.org to find updates for a given plugin, and download the updates if needed.
    21. * <p/>
    22. * <b>VERY, VERY IMPORTANT</b>: Because there are no standards for adding auto-update toggles in your plugin's config, this system provides NO CHECK WITH YOUR CONFIG to make sure the user has allowed auto-updating.
    23. * <br>
    24. * It is a <b>BUKKIT POLICY</b> that you include a boolean value in your config that prevents the auto-updater from running <b>AT ALL</b>.
    25. * <br>
    26. * If you fail to include this option in your config, your plugin will be <b>REJECTED</b> when you attempt to submit it to dev.bukkit.org.
    27. * <p/>
    28. * An example of a good configuration option would be something similar to 'auto-update: true' - if this value is set to false you may NOT run the auto-updater.
    29. * <br>
    30. * If you are unsure about these rules, please read the plugin submission guidelines: [url]http://goo.gl/8iU5l[/url]
    31. *
    32. * @author Gravity
    33. * @version 2.1
    34. */
    35.  
    36. public class Updater{
    37.  
    38. private Plugin plugin;
    39. private UpdateType type;
    40. private String versionName;
    41. private String versionLink;
    42. private String versionType;
    43. private String versionGameVersion;
    44.  
    45. private boolean announce; // Whether to announce file downloads
    46.  
    47. private URL url; // Connecting to RSS
    48. public File file; // The plugin's file
    49. private Thread thread; // Updater thread
    50.  
    51. private int id = -1; // Project's Curse ID
    52. private String apiKey = null; // BukkitDev ServerMods API key
    53. private static final String TITLE_VALUE = "name"; // Gets remote file's title
    54. private static final String LINK_VALUE = "downloadUrl"; // Gets remote file's download link
    55. private static final String TYPE_VALUE = "releaseType"; // Gets remote file's release type
    56. private static final String VERSION_VALUE = "gameVersion"; // Gets remote file's build version
    57. private static final String QUERY = "/servermods/files?projectIds="; // Path to GET
    58. private static final String HOST = "[url]https://api.curseforge.com[/url]"; // Slugs will be appended to this to get to the project's RSS feed
    59.  
    60. private static final String USER_AGENT = "Updater (by Gravity)";
    61. private static final String delimiter = "^v|[\\s_-]v"; // Used for locating version numbers in file names
    62. private static final String[] NO_UPDATE_TAG = { "-DEV", "-PRE", "-SNAPSHOT" }; // If the version number contains one of these, don't update.
    63. private static final int BYTE_SIZE = 1024; // Used for downloading files
    64. private final YamlConfiguration config = new YamlConfiguration(); // Config file
    65. public String updateFolder;// The folder that downloads will be placed in
    66. private Updater.UpdateResult result = Updater.UpdateResult.SUCCESS; // Used for determining the outcome of the update process
    67.  
    68.  
    69. /**
    70.   * Gives the developer the result of the update process. Can be obtained by called {@link #getResult()}
    71.   */
    72. public enum UpdateResult {
    73. /**
    74.   * The updater found an update, and has readied it to be loaded the next time the server restarts/reloads.
    75.   */
    76. SUCCESS,
    77. /**
    78.   * The updater did not find an update, and nothing was downloaded.
    79.   */
    80. NO_UPDATE,
    81. /**
    82.   * The server administrator has disabled the updating system.
    83.   */
    84. DISABLED,
    85. /**
    86.   * The updater found an update, but was unable to download it.
    87.   */
    88. FAIL_DOWNLOAD,
    89. /**
    90.   * For some reason, the updater was unable to contact dev.bukkit.org to download the file.
    91.   */
    92. FAIL_DBO,
    93. /**
    94.   * When running the version check, the file on DBO did not contain a recognizable version.
    95.   */
    96. FAIL_NOVERSION,
    97. /**
    98.   * The id provided by the plugin running the updater was invalid and doesn't exist on DBO.
    99.   */
    100. FAIL_BADID,
    101. /**
    102.   * The server administrator has improperly configured their API key in the configuration.
    103.   */
    104. FAIL_APIKEY,
    105. /**
    106.   * The updater found an update, but because of the UpdateType being set to NO_DOWNLOAD, it wasn't downloaded.
    107.   */
    108. UPDATE_AVAILABLE
    109. }
    110.  
    111. /**
    112.   * Allows the developer to specify the type of update that will be run.
    113.   */
    114. public enum UpdateType {
    115. /**
    116.   * Run a version check, and then if the file is out of date, download the newest version.
    117.   */
    118. DEFAULT,
    119. /**
    120.   * Don't run a version check, just find the latest update and download it.
    121.   */
    122. NO_VERSION_CHECK,
    123. /**
    124.   * Get information about the version and the download size, but don't actually download anything.
    125.   */
    126. NO_DOWNLOAD
    127. }
    128.  
    129. /**
    130.   * Represents the various release types of a file on BukkitDev.
    131.   */
    132. public enum ReleaseType {
    133. /**
    134.   * An "alpha" file.
    135.   */
    136. ALPHA,
    137. /**
    138.   * A "beta" file.
    139.   */
    140. BETA,
    141. /**
    142.   * A "release" file.
    143.   */
    144. RELEASE
    145. }
    146.  
    147. /**
    148.   * Initialize the updater.
    149.   *
    150.   * @param plugin The plugin that is checking for an update.
    151.   * @param id The dev.bukkit.org id of the project.
    152.   * @param file The file that the plugin is running from, get this by doing this.getFile() from within your main class.
    153.   * @param type Specify the type of update this will be. See {@link UpdateType}
    154.   * @param announce True if the program should announce the progress of new updates in console.
    155.   */
    156. public Updater(Plugin plugin, int id, File file, UpdateType type, boolean announce) {
    157. this.plugin = plugin;
    158. this.type = type;
    159. this.announce = announce;
    160. this.file = file;
    161. this.id = id;
    162. this.updateFolder = plugin.getServer().getUpdateFolder();
    163.  
    164. final File pluginFile = plugin.getDataFolder().getParentFile();
    165. final File updaterFile = new File(pluginFile, "Updater");
    166. final File updaterConfigFile = new File(updaterFile, "config.yml");
    167.  
    168. this.config.options().header("This configuration file affects all plugins using the Updater system (version 2+ - [url]http://forums.bukkit.org/threads/96681/[/url] )" + '\n'
    169. + "If you wish to use your API key, read [url]http://wiki.bukkit.org/ServerMods_API[/url] and place it below." + '\n'
    170. + "Some updating systems will not adhere to the disabled value, but these may be turned off in their plugin's configuration.");
    171. this.config.addDefault("api-key", "PUT_API_KEY_HERE");
    172. this.config.addDefault("disable", false);
    173.  
    174. if (!updaterFile.exists()) {
    175. updaterFile.mkdir();
    176. }
    177.  
    178. boolean createFile = !updaterConfigFile.exists();
    179. try {
    180. if (createFile) {
    181. updaterConfigFile.createNewFile();
    182. this.config.options().copyDefaults(true);
    183. this.config.save(updaterConfigFile);
    184. } else {
    185. this.config.load(updaterConfigFile);
    186. }
    187. } catch (final Exception e) {
    188. if (createFile) {
    189. plugin.getLogger().severe("The updater could not create configuration at " + updaterFile.getAbsolutePath());
    190. } else {
    191. plugin.getLogger().severe("The updater could not load configuration at " + updaterFile.getAbsolutePath());
    192. }
    193. plugin.getLogger().log(Level.SEVERE, null, e);
    194. }
    195.  
    196. if (this.config.getBoolean("disable")) {
    197. this.result = UpdateResult.DISABLED;
    198. return;
    199. }
    200.  
    201. String key = this.config.getString("api-key");
    202. if (key.equalsIgnoreCase("PUT_API_KEY_HERE") || key.equals("")) {
    203. key = null;
    204. }
    205.  
    206. this.apiKey = key;
    207.  
    208. try {
    209. this.url = new URL(Updater.HOST + Updater.QUERY + id);
    210. } catch (final MalformedURLException e) {
    211. plugin.getLogger().log(Level.SEVERE, "The project ID provided for updating, " + id + " is invalid.", e);
    212. this.result = UpdateResult.FAIL_BADID;
    213. }
    214.  
    215. this.thread = new Thread(new UpdateRunnable());
    216. this.thread.start();
    217. }
    218.  
    219. /**
    220.   * Get the result of the update process.
    221.   *
    222.   * @return result of the update process.
    223.   * @see UpdateResult
    224.   */
    225. public Updater.UpdateResult getResult() {
    226. this.waitForThread();
    227. return this.result;
    228. }
    229.  
    230. /**
    231.   * Get the latest version's release type.
    232.   *
    233.   * @return latest version's release type.
    234.   * @see ReleaseType
    235.   */
    236. public ReleaseType getLatestType() {
    237. this.waitForThread();
    238. if (this.versionType != null) {
    239. for (ReleaseType type : ReleaseType.values()) {
    240. if (this.versionType.equals(type.name().toLowerCase())) {
    241. return type;
    242. }
    243. }
    244. }
    245. return null;
    246. }
    247.  
    248. /**
    249.   * Get the latest version's game version (such as "CB 1.2.5-R1.0").
    250.   *
    251.   * @return latest version's game version.
    252.   */
    253. public String getLatestGameVersion() {
    254. this.waitForThread();
    255. return this.versionGameVersion;
    256. }
    257.  
    258. /**
    259.   * Get the latest version's name (such as "Project v1.0").
    260.   *
    261.   * @return latest version's name.
    262.   */
    263. public String getLatestName() {
    264. this.waitForThread();
    265. return this.versionName;
    266. }
    267.  
    268. /**
    269.   * Get the latest version's direct file link.
    270.   *
    271.   * @return latest version's file link.
    272.   */
    273. public String getLatestFileLink() {
    274. this.waitForThread();
    275. return this.versionLink;
    276. }
    277.  
    278. /**
    279.   * As the result of Updater output depends on the thread's completion, it is necessary to wait for the thread to finish
    280.   * before allowing anyone to check the result.
    281.   */
    282. private void waitForThread() {
    283. if ((this.thread != null) && this.thread.isAlive()) {
    284. try {
    285. this.thread.join();
    286. } catch (final InterruptedException e) {
    287. plugin.getLogger().log(Level.SEVERE, null, e);
    288. }
    289. }
    290. }
    291.  
    292. /**
    293.   * Save an update from dev.bukkit.org into the server's update folder.
    294.   *
    295.   * @param folder the updates folder location.
    296.   * @param file the name of the file to save it as.
    297.   * @param link the url of the file.
    298.   */
    299. public void saveFile(File folder, String file, String link) {
    300. if (!folder.exists()) {
    301. folder.mkdir();
    302. }
    303. FileOutputStream fout = null;
    304. try {
    305. // Download the file
    306. final URL url = new URL(link);
    307. final int fileLength = url.openConnection().getContentLength();
    308. in = new BufferedInputStream(url.openStream());
    309. fout = new FileOutputStream(folder.getAbsolutePath() + File.separator + file);
    310.  
    311. final byte[] data = new byte[Updater.BYTE_SIZE];
    312. int count;
    313. if (this.announce) {
    314. this.plugin.getLogger().info("About to download a new update: " + this.versionName);
    315. }
    316. long downloaded = 0;
    317. while ((count = in.read(data, 0, Updater.BYTE_SIZE)) != -1) {
    318. downloaded += count;
    319. fout.write(data, 0, count);
    320. final int percent = (int) ((downloaded * 100) / fileLength);
    321. if (this.announce && ((percent % 10) == 0)) {
    322. this.plugin.getLogger().info("Downloading update: " + percent + "% of " + fileLength + " bytes.");
    323. }
    324. }
    325. //Just a quick check to make sure we didn't leave any files from last time...
    326. for (final File xFile : new File(this.plugin.getDataFolder().getParent(), this.updateFolder).listFiles()) {
    327. if (xFile.getName().endsWith(".zip")) {
    328. xFile.delete();
    329. }
    330. }
    331. // Check to see if it's a zip file, if it is, unzip it.
    332. final File dFile = new File(folder.getAbsolutePath() + File.separator + file);
    333. if (dFile.getName().endsWith(".zip")) {
    334. // Unzip
    335. this.unzip(dFile.getCanonicalPath());
    336. }
    337. if (this.announce) {
    338. this.plugin.getLogger().info("Finished updating.");
    339. }
    340. } catch (final Exception ex) {
    341. this.plugin.getLogger().warning("The auto-updater tried to download a new update, but was unsuccessful.");
    342. this.result = Updater.UpdateResult.FAIL_DOWNLOAD;
    343. } finally {
    344. try {
    345. if (in != null) {
    346. in.close();
    347. }
    348. if (fout != null) {
    349. fout.close();
    350. }
    351. } catch (final Exception ex) {
    352. }
    353. }
    354. }
    355.  
    356. /**
    357.   * Part of Zip-File-Extractor, modified by Gravity for use with Updater.
    358.   *
    359.   * @param file the location of the file to extract.
    360.   */
    361. private void unzip(String file) {
    362. try {
    363. final File fSourceZip = new File(file);
    364. final String zipPath = file.substring(0, file.length() - 4);
    365. ZipFile zipFile = new ZipFile(fSourceZip);
    366. Enumeration<? extends ZipEntry> e = zipFile.entries();
    367. while (e.hasMoreElements()) {
    368. ZipEntry entry = e.nextElement();
    369. File destinationFilePath = new File(zipPath, entry.getName());
    370. destinationFilePath.getParentFile().mkdirs();
    371. if (entry.isDirectory()) {
    372. continue;
    373. } else {
    374. final BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
    375. int b;
    376. final byte buffer[] = new byte[Updater.BYTE_SIZE];
    377. final FileOutputStream fos = new FileOutputStream(destinationFilePath);
    378. final BufferedOutputStream bos = new BufferedOutputStream(fos, Updater.BYTE_SIZE);
    379. while ((b = bis.read(buffer, 0, Updater.BYTE_SIZE)) != -1) {
    380. bos.write(buffer, 0, b);
    381. }
    382. bos.flush();
    383. bos.close();
    384. bis.close();
    385. final String name = destinationFilePath.getName();
    386. if (name.endsWith(".jar") && this.pluginFile(name)) {
    387. destinationFilePath.renameTo(new File(this.plugin.getDataFolder().getParent(), this.updateFolder + File.separator + name));
    388. }
    389. }
    390. entry = null;
    391. destinationFilePath = null;
    392. }
    393. e = null;
    394. zipFile.close();
    395. zipFile = null;
    396.  
    397. // Move any plugin data folders that were included to the right place, Bukkit won't do this for us.
    398. for (final File dFile : new File(zipPath).listFiles()) {
    399. if (dFile.isDirectory()) {
    400. if (this.pluginFile(dFile.getName())) {
    401. final File oFile = new File(this.plugin.getDataFolder().getParent(), dFile.getName()); // Get current dir
    402. final File[] contents = oFile.listFiles(); // List of existing files in the current dir
    403. for (final File cFile : dFile.listFiles()) // Loop through all the files in the new dir
    404. {
    405. boolean found = false;
    406. for (final File xFile : contents) // Loop through contents to see if it exists
    407. {
    408. if (xFile.getName().equals(cFile.getName())) {
    409. found = true;
    410. break;
    411. }
    412. }
    413. if (!found) {
    414. // Move the new file into the current dir
    415. cFile.renameTo(new File(oFile.getCanonicalFile() + File.separator + cFile.getName()));
    416. } else {
    417. // This file already exists, so we don't need it anymore.
    418. cFile.delete();
    419. }
    420. }
    421. }
    422. }
    423. dFile.delete();
    424. }
    425. new File(zipPath).delete();
    426. fSourceZip.delete();
    427. } catch (final IOException e) {
    428. this.plugin.getLogger().log(Level.SEVERE, "The auto-updater tried to unzip a new update file, but was unsuccessful.", e);
    429. this.result = Updater.UpdateResult.FAIL_DOWNLOAD;
    430. }
    431. new File(file).delete();
    432. }
    433.  
    434.  
    435. /**
    436.   * Check if the name of a jar is one of the plugins currently installed, used for extracting the correct files out of a zip.
    437.   *
    438.   * @param name a name to check for inside the plugins folder.
    439.   * @return true if a file inside the plugins folder is named this.
    440.   */
    441. private boolean pluginFile(String name) {
    442. for (final File file : new File("plugins").listFiles()) {
    443. if (file.getName().equals(name)) {
    444. return true;
    445. }
    446. }
    447. return false;
    448. }
    449. /**
    450.   * Check to see if the program should continue by evaluating whether the plugin is already updated, or shouldn't be updated.
    451.   *
    452.   * @param title the plugin's title.
    453.   * @return true if the version was located and is not the same as the remote's newest.
    454.   */
    455. private boolean versionCheck(String title) {
    456. if (this.type != UpdateType.NO_VERSION_CHECK) {
    457. final String localVersion = this.plugin.getDescription().getVersion();
    458. if (title.split(delimiter).length == 2) {
    459. final String remoteVersion = title.split(delimiter)[1].split(" ")[0]; // Get the newest file's version number
    460.  
    461. if (this.hasTag(localVersion) || !this.shouldUpdate(localVersion, remoteVersion)) {
    462. // We already have the latest version, or this build is tagged for no-update
    463. this.result = Updater.UpdateResult.NO_UPDATE;
    464. return false;
    465. }
    466. } else {
    467. // The file's name did not contain the string 'vVersion'
    468. final String authorInfo = this.plugin.getDescription().getAuthors().size() == 0 ? "" : " (" + this.plugin.getDescription().getAuthors().get(0) + ")";
    469. this.plugin.getLogger().warning("The author of this plugin" + authorInfo + " has misconfigured their Auto Update system");
    470. this.plugin.getLogger().warning("File versions should follow the format 'PluginName vVERSION'");
    471. this.plugin.getLogger().warning("Please notify the author of this error.");
    472. this.result = Updater.UpdateResult.FAIL_NOVERSION;
    473. return false;
    474. }
    475. }
    476. return true;
    477. }
    478. /**
    479.   * <b>If you wish to run mathematical versioning checks, edit this method.</b>
    480.   * <p>
    481.   * With default behavior, Updater will NOT verify that a remote version available on BukkitDev
    482.   * which is not this version is indeed an "update".
    483.   * If a version is present on BukkitDev that is not the version that is currently running,
    484.   * Updater will assume that it is a newer version.
    485.   * This is because there is no standard versioning scheme, and creating a calculation that can
    486.   * determine whether a new update is actually an update is sometimes extremely complicated.
    487.   * </p>
    488.   * <p>
    489.   * Updater will call this method from {@link #versionCheck(String)} before deciding whether
    490.   * the remote version is actually an update.
    491.   * If you have a specific versioning scheme with which a mathematical determination can
    492.   * be reliably made to decide whether one version is higher than another, you may
    493.   * revise this method, using the local and remote version parameters, to execute the
    494.   * appropriate check.
    495.   * </p>
    496.   * <p>
    497.   * Returning a value of <b>false</b> will tell the update process that this is NOT a new version.
    498.   * Without revision, this method will always consider a remote version at all different from
    499.   * that of the local version a new update.
    500.   * </p>
    501.   * @param localVersion the current version
    502.   * @param remoteVersion the remote version
    503.   * @return true if Updater should consider the remote version an update, false if not.
    504.   */
    505. public boolean shouldUpdate(String localVersion, String remoteVersion) {
    506. return !localVersion.equalsIgnoreCase(remoteVersion);
    507. }
    508. /**
    509.   * Evaluate whether the version number is marked showing that it should not be updated by this program.
    510.   *
    511.   * @param version a version number to check for tags in.
    512.   * @return true if updating should be disabled.
    513.   */
    514. private boolean hasTag(String version) {
    515. for (final String string : Updater.NO_UPDATE_TAG) {
    516. if (version.contains(string)) {
    517. return true;
    518. }
    519. }
    520. return false;
    521. }
    522. /**
    523.   * Make a connection to the BukkitDev API and request the newest file's details.
    524.   *
    525.   * @return true if successful.
    526.   */
    527. private boolean read() {
    528. try {
    529. final URLConnection conn = this.url.openConnection();
    530. conn.setConnectTimeout(5000);
    531. if (this.apiKey != null) {
    532. conn.addRequestProperty("X-API-Key", this.apiKey);
    533. }
    534. conn.addRequestProperty("User-Agent", Updater.USER_AGENT);
    535. conn.setDoOutput(true);
    536. final BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    537. final String response = reader.readLine();
    538. final JSONArray array = (JSONArray) JSONValue.parse(response);
    539. if (array.size() == 0) {
    540. this.plugin.getLogger().warning("The updater could not find any files for the project id " + this.id);
    541. this.result = UpdateResult.FAIL_BADID;
    542. return false;
    543. }
    544. this.versionName = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TITLE_VALUE);
    545. this.versionLink = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.LINK_VALUE);
    546. this.versionType = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.TYPE_VALUE);
    547. this.versionGameVersion = (String) ((JSONObject) array.get(array.size() - 1)).get(Updater.VERSION_VALUE);
    548. return true;
    549. } catch (final IOException e) {
    550. if (e.getMessage().contains("HTTP response code: 403")) {
    551. this.plugin.getLogger().severe("dev.bukkit.org rejected the API key provided in plugins/Updater/config.yml");
    552. this.plugin.getLogger().severe("Please double-check your configuration to ensure it is correct.");
    553. this.result = UpdateResult.FAIL_APIKEY;
    554. } else {
    555. this.plugin.getLogger().severe("The updater could not contact dev.bukkit.org for updating.");
    556. this.plugin.getLogger().severe("If you have not recently modified your configuration and this is the first time you are seeing this message, the site may be experiencing temporary downtime.");
    557. this.result = UpdateResult.FAIL_DBO;
    558. }
    559. return false;
    560. }
    561. }
    562. private class UpdateRunnable implements Runnable {
    563. @Override
    564. public void run() {
    565. if (Updater.this.url != null) {
    566. // Obtain the results of the project's file feed
    567. if (Updater.this.read()) {
    568. if (Updater.this.versionCheck(Updater.this.versionName)) {
    569. if ((Updater.this.versionLink != null) && (Updater.this.type != UpdateType.NO_DOWNLOAD)) {
    570. String name = Updater.this.file.getName();
    571. // If it's a zip file, it shouldn't be downloaded as the plugin's name
    572. if (Updater.this.versionLink.endsWith(".zip")) {
    573. final String[] split = Updater.this.versionLink.split("/");
    574. name = split[split.length - 1];
    575. }
    576. Updater.this.saveFile(new File(Updater.this.plugin.getDataFolder().getParent(), Updater.this.updateFolder), name, Updater.this.versionLink);
    577. } else {
    578. Updater.this.result = UpdateResult.UPDATE_AVAILABLE;
    579. }
    580. }
    581. }
    582. }
    583. }
    584. }
    585. }
    586.  

     
  4. Offline

    fireblast709

    xYourFreindx lines 29 and 44 should be moved to onEnable. You cannot use the config before onEnable (could be onLoad, not entirely sure)
     
  5. Gater12 Nonono... I know how to use it. I've used it before. But I can't just use one line if I want it to be accepted by the bukkit staff for upload to dev.bukkit.

    If you hadn't noticed, I've got three options.
    You can...
    Have the plugin autoUpdate,
    Have the plugin notify you that there is an update available.
    Or have it do nothing.

    That's basically what I'm trying to do here.
    I had it working on an earlier version. But it still instantiated the updater class (even though it didn't do anything), and it was rejected. This is what I'm trying to remedy here. And my question was... Why is my system not working?
     
  6. Offline

    1Rogue

    If you want it to meet standards there just needs to be a way to disable it
     
  7. 1Rogue
    I submitted the plugin with the option to disable updating and notifying for updates, but it still checked (in the background). It was rejected.

    An option to disable it completely is what I'm trying to do here. That's the purpose.
     
  8. Offline

    DevRosemberg

    xYourFreindx Not sure on why you sent me the PM as im not fully aware of your error, is there any Stack Trace or something that is failing?
     
  9. Offline

    mythbusterma

    If all you need to do is make the auto-update configurable:

    Before the "check update" line (whatever that code actually is), you can do:

    Code:java
    1. if(getConfig().getBoolean("autoupdate") {
    2. // check for updates
    3. }


    Off-topic: seriously, the amount of publics in that class hurts my eyes, please Google and read about why we encapsulate data. Furthermore, learn about Upper and lowerCamelCase, and what they identify and why they are used in code.
     
    xYourFreindx likes this.
  10. DevRosemberg Not really.
    It just throws a NPE whenever someone joins.

    As I said in my original post, I think it might be my system for determining the updater settings, but I'm not sure how to improve it.
     
Thread Status:
Not open for further replies.

Share This Page