Skip to content

Commit

Permalink
Include update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jun 24, 2020
1 parent ceae7a2 commit fbaa954
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>xyz.jpenilla</groupId>
<artifactId>DynamicShop-Graph</artifactId>
<version>0.2.2-SNAPSHOT.{BUILD_NUMBER}</version>
<version>0.2.2</version>
<packaging>jar</packaging>

<name>DynamicShop Graph</name>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/xyz/jpenilla/dsgraph/DSGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public void onEnable() {
Metrics metrics = new Metrics(this, pluginId);
metrics.addCustomChart(new Metrics.SimplePie("amount_of_items_tracked", () -> String.valueOf(cfg.getFiles().size())));

new UpdateChecker(this, 80638).getVersion(version -> {
if (!this.getDescription().getVersion().equalsIgnoreCase(version)) {
getLogger().info("There is an update available for DynamicShop-Graph (" + version + ") at https://www.spigotmc.org/resources/dynamicshop-graph.80638/");
}
});
}

@Override
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/xyz/jpenilla/dsgraph/UpdateChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package xyz.jpenilla.dsgraph;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Consumer;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Scanner;

public class UpdateChecker {

private final Plugin plugin;
private final int resourceId;

public UpdateChecker(Plugin plugin, int resourceId) {
this.plugin = plugin;
this.resourceId = resourceId;
}

public void getVersion(final Consumer<String> consumer) {
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try (InputStream inputStream = new URL("https://api.spigotmc.org/legacy/update.php?resource=" + this.resourceId).openStream(); Scanner scanner = new Scanner(inputStream)) {
if (scanner.hasNext()) {
consumer.accept(scanner.next());
}
} catch (IOException exception) {
this.plugin.getLogger().info("Cannot look for updates: " + exception.getMessage());
}
});
}
}
2 changes: 1 addition & 1 deletion src/main/resources/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
<ul>
<li><a class="grey-text text-lighten-3" href="https://github.com/jmanpenilla/DynamicShop-Graph">GitHub</a>
</li>
<li><a class="grey-text text-lighten-3" href="#!">Spigot</a></li>
<li><a class="grey-text text-lighten-3" href="https://www.spigotmc.org/resources/dynamicshop-graph.80638/">Spigot</a></li>
</ul>
</div>
</div>
Expand Down

0 comments on commit fbaa954

Please sign in to comment.