Finished implementation of the Catalog Panel

It requires some polishing, but it is working.
This commit is contained in:
Florian CUNY 2019-04-21 10:28:48 +02:00
parent 8fcbdc716e
commit a728f5e531
5 changed files with 182 additions and 17 deletions

View File

@ -189,7 +189,6 @@ public class BentoBox extends JavaPlugin {
hooksManager.registerHook(new DynmapHook());
webManager = new WebManager(this);
webManager.requestGitHubData();
// Show banner
User.getInstance(Bukkit.getConsoleSender()).sendMessage("successfully-loaded",
@ -438,6 +437,14 @@ public class BentoBox extends JavaPlugin {
return Optional.ofNullable(metrics);
}
/**
* @return the {@link WebManager}.
* @since 1.5.0
*/
public WebManager getWebManager() {
return webManager;
}
// Overriding default JavaPlugin methods
/* (non-Javadoc)

View File

@ -25,7 +25,7 @@ public class BentoBoxCatalogCommand extends CompositeCommand {
@Override
public boolean execute(User user, String label, List<String> args) {
CatalogPanel.openPanel(user);
CatalogPanel.openPanel(user, CatalogPanel.View.GAMEMODES);
return true;
}
}

View File

@ -1,7 +1,17 @@
package world.bentobox.bentobox.managers;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.Settings;
import world.bentobox.githubapi4java.GitHub;
import world.bentobox.githubapi4java.objects.GitHubGist;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* Handles web-related stuff.
@ -12,26 +22,75 @@ import world.bentobox.bentobox.BentoBox;
public class WebManager {
private @NonNull BentoBox plugin;
private @Nullable GitHub gitHub;
private @NonNull List<JsonObject> addonsCatalog;
private @NonNull List<JsonObject> gamemodesCatalog;
public WebManager(@NonNull BentoBox plugin) {
this.plugin = plugin;
this.addonsCatalog = new ArrayList<>();
this.gamemodesCatalog = new ArrayList<>();
// Setup the GitHub connection
if (plugin.getSettings().isGithubDownloadData()) {
setupAddonsGitHubConnectors();
this.gitHub = new GitHub();
long connectionInterval = plugin.getSettings().getGithubConnectionInterval() * 20L * 60L;
if (connectionInterval == 0) {
plugin.getServer().getScheduler().runTaskLaterAsynchronously(plugin, () -> requestGitHubData(true), 20L);
} else {
plugin.getServer().getScheduler().runTaskTimerAsynchronously(plugin, () -> requestGitHubData(true), 20L, connectionInterval);
}
}
}
/**
* Setups the {@code GitHubConnector} instances for each addons.
*/
private void setupAddonsGitHubConnectors() {
plugin.getAddonsManager().getEnabledAddons().stream()
.filter(addon -> !addon.getDescription().getRepository().isEmpty());
public void requestGitHubData(boolean clearCache) {
getGitHub().ifPresent(gh -> {
if (clearCache) {
gh.clearCache(); // TODO might be better to not clear cache, check
this.addonsCatalog.clear();
this.gamemodesCatalog.clear();
}
plugin.log("Updating data from GitHub...");
try {
String catalogContent = new GitHubGist(gh, "bccabc20bce17f358d0f94bbbe83babd").getRawResponseAsJson()
.getAsJsonObject().getAsJsonObject("files").getAsJsonObject("catalog.json").get("content").getAsString()
.replace("\\", "");
JsonObject catalog = new JsonParser().parse(catalogContent).getAsJsonObject();
catalog.getAsJsonArray("gamemodes").forEach(gamemode -> gamemodesCatalog.add(gamemode.getAsJsonObject()));
catalog.getAsJsonArray("addons").forEach(addon -> addonsCatalog.add(addon.getAsJsonObject()));
} catch (Exception e) {
e.printStackTrace();
}
});
}
/**
* Returns the contents of the addons catalog (may be an empty list).
* @return the contents of the addons catalog.
*/
public void requestGitHubData() {
if (plugin.getSettings().isGithubDownloadData()) {
}
@NonNull
public List<JsonObject> getAddonsCatalog() {
return addonsCatalog;
}
/**
* Returns the contents of the gamemodes catalog (may be an empty list).
* @return the contents of the gamemodes catalog.
*/
@NonNull
public List<JsonObject> getGamemodesCatalog() {
return gamemodesCatalog;
}
/**
* Returns an optional that may contain the {@link GitHub} instance only and only if {@link Settings#isGithubDownloadData()} is {@code true}.
* @return the GitHub instance.
*/
@NonNull
public Optional<GitHub> getGitHub() {
return Optional.ofNullable(gitHub);
}
}

View File

@ -1,12 +1,17 @@
package world.bentobox.bentobox.panels;
import com.google.gson.JsonObject;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.eclipse.jdt.annotation.NonNull;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.panels.PanelItem;
import world.bentobox.bentobox.api.panels.builders.PanelBuilder;
import world.bentobox.bentobox.api.panels.builders.PanelItemBuilder;
import world.bentobox.bentobox.api.user.User;
import java.util.List;
/**
* @since 1.5.0
* @author Poslovitch
@ -14,22 +19,94 @@ import world.bentobox.bentobox.api.user.User;
public class CatalogPanel {
private static final String LOCALE_REF = "catalog.panel.";
private static final int[] PANES = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53};
private static final int[] PANES = {0, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,};
private CatalogPanel() {}
public static void openPanel(@NonNull User user) {
public static void openPanel(@NonNull User user, @NonNull View view) {
BentoBox plugin = BentoBox.getInstance();
PanelBuilder builder = new PanelBuilder()
.name(user.getTranslation(LOCALE_REF + "title"))
.size(54);
.name(user.getTranslation(LOCALE_REF + view.name() + ".title"))
.size(45);
// Setup header and corners
for (int i : PANES) {
builder.item(i, new PanelItemBuilder().icon(Material.LIGHT_BLUE_STAINED_GLASS_PANE).name(" ").build());
}
PanelItemBuilder gamemodesButton = new PanelItemBuilder()
.icon(Material.COMMAND_BLOCK)
.name(user.getTranslation(LOCALE_REF + "views.gamemodes.name"))
.description(user.getTranslation(LOCALE_REF + "views.gamemodes.description"));
PanelItemBuilder addonsButton = new PanelItemBuilder()
.icon(Material.BOOK)
.name(user.getTranslation(LOCALE_REF + "views.addons.name"))
.description(user.getTranslation(LOCALE_REF + "views.addons.description"));
List<JsonObject> catalog;
if (view == View.GAMEMODES) {
catalog = plugin.getWebManager().getGamemodesCatalog();
// Make the gamemodes button glow
gamemodesButton.glow(true);
// Make the addons button move to the addons view
addonsButton.clickHandler((panel, user1, clickType, slot) -> {
openPanel(user, View.ADDONS);
return true;
});
} else {
catalog = plugin.getWebManager().getAddonsCatalog();
// Make the addons button glow
addonsButton.glow(true);
// Make the gamemodes button move to the gamemodes view
gamemodesButton.clickHandler((panel, user1, clickType, slot) -> {
openPanel(user, View.GAMEMODES);
return true;
});
}
builder.item(1, gamemodesButton.build());
builder.item(2, addonsButton.build());
// Populate with the addons from the catalog we're actually viewing.
if (catalog.isEmpty()) {
looksEmpty(builder, user);
} else {
for (JsonObject addon : catalog) {
PanelItemBuilder itemBuilder = new PanelItemBuilder();
Material icon = Material.getMaterial(addon.get("icon").getAsString());
if (icon == null) {
icon = Material.PAPER;
}
String name = addon.get("name").getAsString();
itemBuilder.icon(icon)
.name(ChatColor.WHITE + name);
// If the addon is already installed, then tell the user it's already installed
plugin.getAddonsManager().getAddonByName(name).ifPresent(addon1 -> itemBuilder.glow(true).description(user.getTranslation(LOCALE_REF + "already-installed")));
builder.item(addon.get("slot").getAsInt(), itemBuilder.build());
}
}
builder.build().open(user);
}
private static void looksEmpty(@NonNull PanelBuilder builder, @NonNull User user) {
PanelItem emptyHere = new PanelItemBuilder()
.icon(Material.STRUCTURE_VOID)
.name(user.getTranslation(LOCALE_REF + "empty-here.name"))
.description(user.getTranslation(LOCALE_REF + "empty-here.description"))
.build();
builder.item(22, emptyHere);
}
public enum View {
GAMEMODES,
ADDONS
}
}

View File

@ -1024,7 +1024,29 @@ management:
catalog:
panel:
title: "Addons Catalog"
GAMEMODES:
title: "Gamemodes Catalog"
ADDONS:
title: "Addons Catalog"
views:
gamemodes:
name: "&6Gamemodes"
description: |+
&eClick &ato browse through the
&aavailable official Gamemodes.
addons:
name: "&6Addons"
description: |+
&eClick &ato browse through the
&aavailable official Addons.
already-installed: "&aAlready installed!"
empty-here:
name: "&bThis looks empty here..."
description: |+
&cBentoBox could not connect to GitHub.
&aAllow BentoBox to connect to GitHub in
&athe configuration or try again later.
successfully-loaded: |