Skip to content

Commit

Permalink
Add download button in Challenges Libraries panel.
Browse files Browse the repository at this point in the history
Disable access to Library panel if github api is disabled.
  • Loading branch information
BONNe committed Sep 5, 2019
1 parent a0ffacf commit 5f1ea92
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/main/java/world/bentobox/challenges/panel/admin/AdminGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.conversations.*;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.ConversationFactory;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.ValidatingPrompt;
import org.bukkit.inventory.ItemStack;
import org.eclipse.jdt.annotation.NonNull;

Expand All @@ -20,6 +24,7 @@
import world.bentobox.challenges.panel.util.ConfirmationGUI;
import world.bentobox.challenges.utils.GuiUtils;
import world.bentobox.challenges.utils.Utils;
import world.bentobox.challenges.web.WebManager;


/**
Expand Down Expand Up @@ -448,9 +453,23 @@ private PanelItem createButton(Button button)

name = this.user.getTranslation("challenges.gui.buttons.admin.library");
description = this.user.getTranslation("challenges.gui.descriptions.admin.library");
icon = new ItemStack(Material.COBWEB);

if (WebManager.isEnabled())
{
icon = new ItemStack(Material.COBWEB);
}
else
{
description += "|" + this.user.getTranslation("challenges.gui.descriptions.admin.download-disabled");
icon = new ItemStack(Material.STRUCTURE_VOID);
}

clickHandler = (panel, user, clickType, slot) -> {
ListLibraryGUI.open(this);
if (WebManager.isEnabled())
{
ListLibraryGUI.open(this);
}

return true;
};
glow = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.World;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -115,12 +116,58 @@ else if (this.pageIndex > (libraryEntries.size() / MAX_ELEMENTS))
panelBuilder.item(26, this.getButton(CommonButtons.NEXT));
}

panelBuilder.item(4, this.createDownloadNow());
panelBuilder.item(44, this.returnButton);

panelBuilder.build();
}


/**
* This creates download now button, that can skip waiting for automatic request.
* @return PanelItem button that allows to manually download libraries.
*/
private PanelItem createDownloadNow()
{
List<String> description = new ArrayList<>(2);
description.add(this.user.getTranslation("challenges.gui.descriptions.admin.download"));
description.add(this.user.getTranslation("challenges.gui.descriptions.current-value",
"[value]",
this.clearCache ?
this.user.getTranslation("challenges.gui.descriptions.enabled") :
this.user.getTranslation("challenges.gui.descriptions.disabled")));

PanelItemBuilder itemBuilder = new PanelItemBuilder().
name(this.user.getTranslation("challenges.gui.buttons.admin.download")).
description(GuiUtils.stringSplit(description, this.addon.getChallengesSettings().getLoreLineLength())).
icon(Material.HOPPER).
glow(this.clearCache);

itemBuilder.clickHandler((panel, user1, clickType, slot) ->
{
if (clickType.isRightClick())
{
this.clearCache = !this.clearCache;
panel.getInventory().setItem(slot, this.createDownloadNow().getItem());
}
else
{
this.addon.getWebManager().requestCatalogGitHubData(false);

// add some delay to rebuilding gui.
this.addon.getPlugin().getServer().getScheduler().runTaskLaterAsynchronously(
this.addon.getPlugin(),
this::build,
100L);
}

return true;
});

return itemBuilder.build();
}


/**
* This method creates button for given library entry.
* @param libraryEntry LibraryEntry which button must be created.
Expand Down Expand Up @@ -199,6 +246,10 @@ private List<String> generateEntryDescription(LibraryEntry entry)
// Section: Instance Variables
// ---------------------------------------------------------------------

/**
* Indicates if download now button should trigger cache clearing.
*/
private boolean clearCache;

/**
* This variable will protect against spam-click.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/world/bentobox/challenges/web/WebManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,16 @@ public List<LibraryEntry> getLibraryEntries()
}


/**
* This static method returns if GitHub data downloader is enabled or not.
* @return {@code true} if data downloader is enabled, {@code false} - otherwise.
*/
public static boolean isEnabled()
{
return BentoBox.getInstance().getWebManager().getGitHub().isPresent();
}


// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ challenges:
complete-wipe: 'Wipe Addon Databases'

library: 'Web Library'
download: 'Download Libraries'
next: 'Next'
previous: 'Previous'
return: 'Return'
Expand Down Expand Up @@ -277,6 +278,9 @@ challenges:
library-lang: '&aLanguage: [lang]'
library-gamemode: '&aPrimary for [gamemode]'

download: 'Allows manually update available challenges libraries. |Right click to enable cache clearing.'
download-disabled: 'GitHub data downloader is disabled in BentoBox. Without it, you cannot use Libraries!'

lore:
level: "Level string. | Represents translation 'challenges.gui.challenge-description.level'."
status: "Status string. | Represents translation 'challenges.gui.challenge-description.completed'."
Expand Down

0 comments on commit 5f1ea92

Please sign in to comment.