mirror of
https://github.com/JEFF-Media-GbR/ChestSort.git
synced 2024-11-26 20:48:12 +01:00
Merge pull request #16 from JEFF-Media-GbR/async-updatecheck
async updatechecker
This commit is contained in:
commit
eebf3f2129
1
pom.xml
1
pom.xml
@ -58,4 +58,5 @@
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
<description>Automatically sorts your chests!</description>
|
||||
</project>
|
||||
|
@ -62,7 +62,8 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
boolean usingMatchingConfig = true;
|
||||
boolean debug = false;
|
||||
boolean verbose = true;
|
||||
private long updateCheckInterval = 86400; // in seconds. We check on startup and every 24 hours (if you never restart your server)
|
||||
private long updateCheckInterval = 60; // in seconds. We check on startup and every 24 hours (if you never
|
||||
// restart your server)
|
||||
|
||||
// Public API method to sort any given inventory
|
||||
public void sortInventory(Inventory inv) {
|
||||
@ -71,19 +72,25 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
|
||||
// Public API method to sort any given inventory inbetween startSlot and endSlot
|
||||
public void sortInventory(Inventory inv, int startSlot, int endSlot) {
|
||||
this.organizer.sortInventory(inv,startSlot,endSlot);
|
||||
this.organizer.sortInventory(inv, startSlot, endSlot);
|
||||
}
|
||||
|
||||
// Creates the default configuration file
|
||||
// Also checks the config-version of an already existing file. If the existing config is too
|
||||
// old (generated prior to ChestSort 2.0.0), we rename it to config.old.yml so that users
|
||||
// can start off with a new config file that includes all new options. However, on most
|
||||
// updates, the file will not be touched, even if new config options were added. You will instead
|
||||
// get a warning in the console that you should consider adding the options manually. If you do
|
||||
// Also checks the config-version of an already existing file. If the existing
|
||||
// config is too
|
||||
// old (generated prior to ChestSort 2.0.0), we rename it to config.old.yml so
|
||||
// that users
|
||||
// can start off with a new config file that includes all new options. However,
|
||||
// on most
|
||||
// updates, the file will not be touched, even if new config options were added.
|
||||
// You will instead
|
||||
// get a warning in the console that you should consider adding the options
|
||||
// manually. If you do
|
||||
// not add them, the default values will be used for any unset values.
|
||||
void createConfig() {
|
||||
|
||||
// This saves the config.yml included in the .jar file, but it will not overwrite an existing config.yml
|
||||
|
||||
// This saves the config.yml included in the .jar file, but it will not
|
||||
// overwrite an existing config.yml
|
||||
this.saveDefaultConfig();
|
||||
|
||||
// Config version prior to 5? Then it must have been generated by ChestSort 1.x
|
||||
@ -110,8 +117,9 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
getLogger().warning("Could not load freshly generated config file!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Using old config version, but it's no problem. We just print a warning and use the default values later on
|
||||
|
||||
// Using old config version, but it's no problem. We just print a warning and
|
||||
// use the default values later on
|
||||
} else if (getConfig().getInt("config-version", 0) != currentConfigVersion) {
|
||||
getLogger().warning("========================================================");
|
||||
getLogger().warning("YOU ARE USING AN OLD CONFIG FILE!");
|
||||
@ -130,16 +138,18 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
if (!playerDataFolder.getAbsoluteFile().exists()) {
|
||||
playerDataFolder.mkdir();
|
||||
}
|
||||
|
||||
// Create a categories folder that contains text files. ChestSort includes default category files,
|
||||
|
||||
// Create a categories folder that contains text files. ChestSort includes
|
||||
// default category files,
|
||||
// but you can also create your own
|
||||
File categoriesFolder = new File(getDataFolder().getPath() + File.separator + "categories");
|
||||
if (!categoriesFolder.getAbsoluteFile().exists()) {
|
||||
categoriesFolder.mkdir();
|
||||
}
|
||||
|
||||
// If you use an old config file with missing options, the following default values will be used instead
|
||||
// for every missing option.
|
||||
// If you use an old config file with missing options, the following default
|
||||
// values will be used instead
|
||||
// for every missing option.
|
||||
// By default, sorting is disabled. Every player has to run /chestsort once
|
||||
getConfig().addDefault("sorting-enabled-by-default", false);
|
||||
getConfig().addDefault("show-message-when-using-chest", true);
|
||||
@ -151,7 +161,8 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
getConfig().addDefault("auto-generate-category-files", true);
|
||||
getConfig().addDefault("verbose", true); // Prints some information in onEnable()
|
||||
|
||||
// Load disabled-worlds. If it does not exist in the config, it returns null. That's no problem
|
||||
// Load disabled-worlds. If it does not exist in the config, it returns null.
|
||||
// That's no problem
|
||||
disabledWorlds = (ArrayList<String>) getConfig().getStringList("disabled-worlds");
|
||||
}
|
||||
|
||||
@ -165,42 +176,46 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Create the config file, including checks for old config versions, and load the default values for unset options
|
||||
// Create the config file, including checks for old config versions, and load
|
||||
// the default values for unset options
|
||||
createConfig();
|
||||
|
||||
|
||||
// Save default sorting category files when enabled in the config (default=true)
|
||||
saveDefaultCategories();
|
||||
|
||||
|
||||
verbose = getConfig().getBoolean("verbose");
|
||||
|
||||
|
||||
// Create all needed instances of our classes
|
||||
|
||||
// Messages class will load messages from config, fallback to hardcoded default messages
|
||||
|
||||
// Messages class will load messages from config, fallback to hardcoded default
|
||||
// messages
|
||||
messages = new JeffChestSortMessages(this);
|
||||
|
||||
|
||||
// Organizer will load all category files and will be ready to sort stuff
|
||||
organizer = new JeffChestSortOrganizer(this);
|
||||
|
||||
// UpdateChecker will check on startup and every 24 hours for new updates (when enabled)
|
||||
|
||||
// UpdateChecker will check on startup and every 24 hours for new updates (when
|
||||
// enabled)
|
||||
updateChecker = new JeffChestSortUpdateChecker(this);
|
||||
|
||||
|
||||
// The listener will register joining (and unregister leaving) players, and call
|
||||
// the Organizer to sort inventories when a player closes a chest, shulkerbox or barrel inventory
|
||||
// the Organizer to sort inventories when a player closes a chest, shulkerbox or
|
||||
// barrel inventory
|
||||
listener = new JeffChestSortListener(this);
|
||||
|
||||
|
||||
// The sorting method will determine how stuff is sorted
|
||||
sortingMethod = getConfig().getString("sorting-method");
|
||||
|
||||
sortingMethod = getConfig().getString("sorting-method");
|
||||
|
||||
// Register the events for our Listener
|
||||
getServer().getPluginManager().registerEvents(listener, this);
|
||||
|
||||
|
||||
// Register the /chestsort command and associate it to a new CommandExecutor
|
||||
JeffChestSortCommandExecutor commandExecutor = new JeffChestSortCommandExecutor(this);
|
||||
this.getCommand("chestsort").setExecutor(commandExecutor);
|
||||
|
||||
|
||||
// Register the /invsort command
|
||||
this.getCommand("invsort").setExecutor(commandExecutor);
|
||||
|
||||
|
||||
// Does anyone actually need this?
|
||||
if (verbose) {
|
||||
getLogger().info("Current sorting method: " + sortingMethod);
|
||||
@ -208,27 +223,25 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
getLogger().info("Auto generate category files: " + getConfig().getBoolean("auto-generate-category-files"));
|
||||
getLogger().info("Check for updates: " + getConfig().getString("check-for-updates"));
|
||||
}
|
||||
|
||||
|
||||
// Check for updates (async, of course)
|
||||
|
||||
// When set to true, we check for updates right now, and every 24 hours (see updateCheckInterval)
|
||||
|
||||
// When set to true, we check for updates right now, and every 24 hours (see
|
||||
// updateCheckInterval)
|
||||
if (getConfig().getString("check-for-updates", "true").equalsIgnoreCase("true")) {
|
||||
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
|
||||
public void run() {
|
||||
updateChecker.checkForUpdate();
|
||||
}
|
||||
}, 0L, updateCheckInterval * 20);
|
||||
// When set to on-startup, we check right now (delay 0)
|
||||
} else if (getConfig().getString("check-for-updates", "true").equalsIgnoreCase("on-startup")) {
|
||||
getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
updateChecker.checkForUpdate();
|
||||
}
|
||||
}, 0L);
|
||||
|
||||
} // When set to on-startup, we check right now (delay 0)
|
||||
else if (getConfig().getString("check-for-updates", "true").equalsIgnoreCase("on-startup")) {
|
||||
updateChecker.checkForUpdate();
|
||||
}
|
||||
|
||||
// Metrics will need json-simple with 1.14 API. See pom.xml, it is already included and commented out
|
||||
// Metrics will need json-simple with 1.14 API. See pom.xml, it is already
|
||||
// included and commented out
|
||||
Metrics metrics = new Metrics(this);
|
||||
metrics.addCustomChart(new Metrics.SimplePie("sorting_method", () -> sortingMethod));
|
||||
metrics.addCustomChart(new Metrics.SimplePie("config_version",
|
||||
@ -237,8 +250,8 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
new Metrics.SimplePie("check_for_updates", () -> getConfig().getString("check-for-updates", "true")));
|
||||
metrics.addCustomChart(new Metrics.SimplePie("show_message_when_using_chest",
|
||||
() -> Boolean.toString(getConfig().getBoolean("show-message-when-using-chest"))));
|
||||
metrics.addCustomChart(new Metrics.SimplePie("show_message_when_using_chest_and_sorting_is_enabl",
|
||||
() -> Boolean.toString(getConfig().getBoolean("show-message-when-using-chest-and-sorting-is-enabled"))));
|
||||
metrics.addCustomChart(new Metrics.SimplePie("show_message_when_using_chest_and_sorting_is_enabl", () -> Boolean
|
||||
.toString(getConfig().getBoolean("show-message-when-using-chest-and-sorting-is-enabled"))));
|
||||
metrics.addCustomChart(new Metrics.SimplePie("show_message_again_after_logout",
|
||||
() -> Boolean.toString(getConfig().getBoolean("show-message-again-after-logout"))));
|
||||
metrics.addCustomChart(new Metrics.SimplePie("sorting_enabled_by_default",
|
||||
@ -250,12 +263,12 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
|
||||
// Saves default category files, when enabled in the config
|
||||
private void saveDefaultCategories() {
|
||||
|
||||
|
||||
// Abort when auto-generate-category-files is set to false in config.yml
|
||||
if (getConfig().getBoolean("auto-generate-category-files", true) != true) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// Isn't there a smarter way to find all the 9** files in the .jar?
|
||||
String[] defaultCategories = { "900-tools", "910-valuables", "920-combat", "930-brewing", "940-food",
|
||||
"950-redstone", "960-wood", "970-stone", "980-plants", "981-corals" };
|
||||
@ -266,8 +279,7 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
File fileDefault;
|
||||
|
||||
try {
|
||||
InputStream in = getClass()
|
||||
.getResourceAsStream("/categories/" + category + ".default.txt");
|
||||
InputStream in = getClass().getResourceAsStream("/categories/" + category + ".default.txt");
|
||||
|
||||
fileDefault = new File(getDataFolder().getAbsolutePath() + File.separator + "categories"
|
||||
+ File.separator + category + ".txt");
|
||||
@ -297,13 +309,16 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
// Check whether sorting is enabled for a player. Public because it can be accessed as part of the API
|
||||
// Check whether sorting is enabled for a player. Public because it can be
|
||||
// accessed as part of the API
|
||||
public boolean sortingEnabled(Player p) {
|
||||
|
||||
// The following is for all the lazy server admins who use /reload instead of properly restarting their
|
||||
// server ;) I am sometimes getting stacktraces although it is clearly stated that /reload is NOT
|
||||
// The following is for all the lazy server admins who use /reload instead of
|
||||
// properly restarting their
|
||||
// server ;) I am sometimes getting stacktraces although it is clearly stated
|
||||
// that /reload is NOT
|
||||
// supported. So, here is a quick fix
|
||||
if(PerPlayerSettings == null) {
|
||||
if (PerPlayerSettings == null) {
|
||||
PerPlayerSettings = new HashMap<String, JeffChestSortPlayerSetting>();
|
||||
}
|
||||
listener.registerPlayerIfNeeded(p);
|
||||
@ -314,10 +329,12 @@ public class JeffChestSortPlugin extends JavaPlugin {
|
||||
|
||||
// Unregister a player and save their settings in the playerdata folder
|
||||
void unregisterPlayer(Player p) {
|
||||
// File will be named by the player's uuid. This will prevent problems on player name changes.
|
||||
// File will be named by the player's uuid. This will prevent problems on player
|
||||
// name changes.
|
||||
UUID uniqueId = p.getUniqueId();
|
||||
|
||||
// When using /reload or some other obscure features, it can happen that players are online
|
||||
|
||||
// When using /reload or some other obscure features, it can happen that players
|
||||
// are online
|
||||
// but not registered. So, we only continue when the player has been registered
|
||||
if (PerPlayerSettings.containsKey(uniqueId.toString())) {
|
||||
JeffChestSortPlayerSetting setting = PerPlayerSettings.get(p.getUniqueId().toString());
|
||||
|
@ -5,16 +5,19 @@ import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class JeffChestSortUpdateChecker {
|
||||
|
||||
// This checks for updates. A txt file is downloaded. If the txt file contains a string that is
|
||||
// unequal to the currently used plugin's version, a message is printed in the console.
|
||||
// The listener will also ensure that OPs will be notified on join.
|
||||
// When the update checker could not complete the request, e.g. when the JEFF Media GbR API server is
|
||||
// offline, or if you have no internet connection, a warning will be printed in the console.
|
||||
|
||||
// This checks for updates. A txt file is downloaded. If the txt file contains a
|
||||
// string that is unequal to the currently used plugin's version, a message is
|
||||
// printed in the console.
|
||||
// The listener will also ensure that OPs will be notified on join. When the
|
||||
// update checker could not complete the request, e.g. when the JEFF
|
||||
// Media GbR API server is offline, or if you have no internet connection, a
|
||||
// warning will be printed in the console.
|
||||
|
||||
private JeffChestSortPlugin plugin;
|
||||
|
||||
@ -24,47 +27,54 @@ public class JeffChestSortUpdateChecker {
|
||||
|
||||
// This text file always contains a string with the latest version, e.g. 3.7.1
|
||||
String latestVersionLink = "https://api.jeff-media.de/chestsort/chestsort-latest-version.txt";
|
||||
|
||||
|
||||
String downloadLink = "https://www.chestsort.de";
|
||||
private String currentVersion = "undefined";
|
||||
private String latestVersion = "undefined";
|
||||
|
||||
void sendUpdateMessage(Player p) {
|
||||
if(!latestVersion.equals("undefined")) {
|
||||
if (!currentVersion.equals(latestVersion)) {
|
||||
p.sendMessage(ChatColor.GRAY + "There is a new version of " + ChatColor.GOLD + "ChestSort" + ChatColor.GRAY
|
||||
+ " available.");
|
||||
p.sendMessage(ChatColor.GRAY + "Please download at " + downloadLink);
|
||||
}
|
||||
if (!latestVersion.equals("undefined")) {
|
||||
if (!currentVersion.equals(latestVersion)) {
|
||||
p.sendMessage(ChatColor.GRAY + "There is a new version of " + ChatColor.GOLD + "ChestSort"
|
||||
+ ChatColor.GRAY + " available.");
|
||||
p.sendMessage(ChatColor.GRAY + "Please download at " + downloadLink);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void checkForUpdate() {
|
||||
plugin.getLogger().info("Checking for available updates...");
|
||||
try {
|
||||
HttpURLConnection httpcon = (HttpURLConnection) new URL(latestVersionLink).openConnection();
|
||||
httpcon.addRequestProperty("User-Agent", "ChestSort/"+plugin.getDescription().getVersion());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
|
||||
String inputLine = reader.readLine().trim();
|
||||
latestVersion = inputLine;
|
||||
currentVersion = plugin.getDescription().getVersion().trim();
|
||||
|
||||
if (latestVersion.equals(currentVersion)) {
|
||||
plugin.getLogger().info("You are using the latest version of ChestSort.");
|
||||
} else {
|
||||
plugin.getLogger().warning("========================================================");
|
||||
plugin.getLogger().warning("There is a new version of ChestSort available!");
|
||||
plugin.getLogger().warning("Latest : " + inputLine);
|
||||
plugin.getLogger().warning("Current: " + currentVersion);
|
||||
plugin.getLogger().warning("Please update to the newest version. Download:");
|
||||
plugin.getLogger().warning(downloadLink);
|
||||
plugin.getLogger().warning("========================================================");
|
||||
}
|
||||
void checkForUpdate() {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("Could not check for updates.");
|
||||
}
|
||||
plugin.getLogger().info("Checking for available updates...");
|
||||
try {
|
||||
HttpURLConnection httpcon = (HttpURLConnection) new URL(latestVersionLink).openConnection();
|
||||
httpcon.addRequestProperty("User-Agent", "ChestSort/" + plugin.getDescription().getVersion());
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(httpcon.getInputStream()));
|
||||
String inputLine = reader.readLine().trim();
|
||||
latestVersion = inputLine;
|
||||
currentVersion = plugin.getDescription().getVersion().trim();
|
||||
|
||||
if (latestVersion.equals(currentVersion)) {
|
||||
plugin.getLogger().info("You are using the latest version of ChestSort.");
|
||||
} else {
|
||||
plugin.getLogger().warning("========================================================");
|
||||
plugin.getLogger().warning("There is a new version of ChestSort available!");
|
||||
plugin.getLogger().warning("Latest : " + inputLine);
|
||||
plugin.getLogger().warning("Current: " + currentVersion);
|
||||
plugin.getLogger().warning("Please update to the newest version. Download:");
|
||||
plugin.getLogger().warning(downloadLink);
|
||||
plugin.getLogger().warning("========================================================");
|
||||
}
|
||||
|
||||
reader.close();
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().warning("Could not check for updates.");
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user