Fixed NPE when the WebManager fails to gather data from GitHub API

Fixes https://github.com/BentoBoxWorld/BentoBox/issues/760
This commit is contained in:
Florian CUNY 2019-06-14 10:22:26 +02:00
parent 2e84860aa0
commit d064ed71ec

View File

@ -1,5 +1,6 @@
package world.bentobox.bentobox.managers; package world.bentobox.bentobox.managers;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.NonNull;
@ -50,23 +51,25 @@ public class WebManager {
public void requestGitHubData(boolean clearCache) { public void requestGitHubData(boolean clearCache) {
getGitHub().ifPresent(gh -> { getGitHub().ifPresent(gh -> {
if (clearCache) {
gh.clearCache(); // TODO might be better to not clear cache, check
this.addonsCatalog.clear();
this.gamemodesCatalog.clear();
}
if (plugin.getSettings().isLogGithubDownloadData()) { if (plugin.getSettings().isLogGithubDownloadData()) {
plugin.log("Downloading data from GitHub..."); plugin.log("Downloading data from GitHub...");
} }
try { try {
String catalogContent = new GitHubGist(gh, "bccabc20bce17f358d0f94bbbe83babd").getRawResponseAsJson() JsonElement gistContent = new GitHubGist(gh, "bccabc20bce17f358d0f94bbbe83babd").getRawResponseAsJson();
.getAsJsonObject().getAsJsonObject("files").getAsJsonObject("catalog.json").get("content").getAsString() if (gistContent != null) {
.replace("\n", "").replace("\\", ""); if (clearCache) {
gh.clearCache();
this.addonsCatalog.clear();
this.gamemodesCatalog.clear();
}
JsonObject catalog = new JsonParser().parse(catalogContent).getAsJsonObject(); String catalogContent = gistContent.getAsJsonObject().getAsJsonObject("files").getAsJsonObject("catalog.json")
catalog.getAsJsonArray("gamemodes").forEach(gamemode -> gamemodesCatalog.add(new CatalogEntry(gamemode.getAsJsonObject()))); .get("content").getAsString().replace("\n", "").replace("\\", "");
catalog.getAsJsonArray("addons").forEach(addon -> addonsCatalog.add(new CatalogEntry(addon.getAsJsonObject())));
JsonObject catalog = new JsonParser().parse(catalogContent).getAsJsonObject();
catalog.getAsJsonArray("gamemodes").forEach(gamemode -> gamemodesCatalog.add(new CatalogEntry(gamemode.getAsJsonObject())));
catalog.getAsJsonArray("addons").forEach(addon -> addonsCatalog.add(new CatalogEntry(addon.getAsJsonObject())));
}
} catch (Exception e) { } catch (Exception e) {
plugin.logError("An error occurred when downloading or parsing data from GitHub..."); plugin.logError("An error occurred when downloading or parsing data from GitHub...");
plugin.logStacktrace(e); plugin.logStacktrace(e);