From 643c5e8f7ca8a40fb07be6bb0a4efa11bb2d438a Mon Sep 17 00:00:00 2001 From: BONNe Date: Thu, 5 Sep 2019 15:52:14 +0300 Subject: [PATCH] Add an option to hide undeployed challenges from challenge list (#175) Added new config option "gui-settings.undeployed-view-mode" with 3 values - 'VISIBLE' - all challenges are visible - 'HIDDEN' - only deployed challenges are visible - 'TOGGLEABLE' - users will be able to choose option for themself (not implemented) Implement functionality in ChallengesGUI, where if option hidden is set, then all undeployed challenges are removed. Implement ability to edit this option via admin Settings panel. --- .../bentobox/challenges/config/Settings.java | 43 +++++++++++- .../challenges/config/SettingsUtils.java | 21 ++++++ .../panel/admin/EditSettingsGUI.java | 69 ++++++++++++++++++- .../challenges/panel/user/ChallengesGUI.java | 13 ++++ src/main/resources/config.yml | 8 +++ src/main/resources/locales/en-US.yml | 10 +++ 6 files changed, 158 insertions(+), 6 deletions(-) diff --git a/src/main/java/world/bentobox/challenges/config/Settings.java b/src/main/java/world/bentobox/challenges/config/Settings.java index 407fb097..0688599f 100644 --- a/src/main/java/world/bentobox/challenges/config/Settings.java +++ b/src/main/java/world/bentobox/challenges/config/Settings.java @@ -18,6 +18,7 @@ import world.bentobox.challenges.config.SettingsUtils.GuiMode; import world.bentobox.challenges.config.SettingsUtils.ChallengeLore; import world.bentobox.challenges.config.SettingsUtils.LevelLore; +import world.bentobox.challenges.config.SettingsUtils.VisibilityMode; import world.bentobox.challenges.database.object.adapters.ChallengeLoreAdapter; import world.bentobox.challenges.database.object.adapters.LevelLoreAdapter; @@ -82,6 +83,16 @@ public class Settings implements ConfigObject @ConfigEntry(path = "gui-settings.add-completed-glow") private boolean addCompletedGlow = true; + @ConfigComment("") + @ConfigComment("This variable allows to choose which Challenges users can see in Challenges GUI.") + @ConfigComment("Valid values are:") + @ConfigComment(" 'VISIBLE' - there will be no hidden challenges. All challenges will be viewable in GUI.") + @ConfigComment(" 'HIDDEN' - shows only deployed challenges.") + @ConfigComment(" 'TOGGLEABLE' - there will be button in GUI that allows users to switch from ALL modes.") + @ConfigComment("TOGGLEABLE - Currently not implemented.") + @ConfigEntry(path = "gui-settings.undeployed-view-mode") + private VisibilityMode visibilityMode = VisibilityMode.VISIBLE; + @ConfigComment("") @ConfigComment("This allows to change default locked level icon. This option may be") @ConfigComment("overwritten by each challenge level. If challenge level has specified") @@ -191,9 +202,9 @@ public class Settings implements ConfigObject private String configVersion = "v3"; - // --------------------------------------------------------------------- - // Section: Methods - // --------------------------------------------------------------------- +// --------------------------------------------------------------------- +// Section: Getters +// --------------------------------------------------------------------- /** @@ -400,6 +411,21 @@ public long getAutoSaveTimer() } + /** + * This method returns the visibilityMode value. + * @return the value of visibilityMode. + */ + public VisibilityMode getVisibilityMode() + { + return this.visibilityMode; + } + + +// --------------------------------------------------------------------- +// Section: Setters +// --------------------------------------------------------------------- + + /** * This method sets the autoSaveTimer object value. * @param autoSaveTimer the autoSaveTimer object new value. @@ -607,4 +633,15 @@ public void setLifeSpan(int lifeSpan) { this.lifeSpan = lifeSpan; } + + + /** + * This method sets the visibilityMode value. + * @param visibilityMode the visibilityMode new value. + * + */ + public void setVisibilityMode(VisibilityMode visibilityMode) + { + this.visibilityMode = visibilityMode; + } } diff --git a/src/main/java/world/bentobox/challenges/config/SettingsUtils.java b/src/main/java/world/bentobox/challenges/config/SettingsUtils.java index c1e2b613..c1137ed7 100644 --- a/src/main/java/world/bentobox/challenges/config/SettingsUtils.java +++ b/src/main/java/world/bentobox/challenges/config/SettingsUtils.java @@ -28,6 +28,27 @@ public enum GuiMode } + /** + * This enum holds all possible values for displaying challenges. It allows to + * enable/disable displaying un-deployed challenges. + */ + public enum VisibilityMode + { + /** + * Show all challenges regardless of their deployment status. + */ + VISIBLE, + /** + * Hide all undeployed challenges in User GUI. + */ + HIDDEN, + /** + * Allows users to switch them on/off in GUI. + */ + TOGGLEABLE + } + + /** * This enum holds all possible values for Challenge Lore Message. */ diff --git a/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java b/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java index 9171d97b..b9f44ecb 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java @@ -15,10 +15,12 @@ import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.config.Settings; import world.bentobox.challenges.config.SettingsUtils.GuiMode; +import world.bentobox.challenges.config.SettingsUtils.VisibilityMode; import world.bentobox.challenges.panel.CommonGUI; import world.bentobox.challenges.panel.util.NumberGUI; import world.bentobox.challenges.panel.util.SelectBlocksGUI; import world.bentobox.challenges.utils.GuiUtils; +import world.bentobox.challenges.utils.Utils; /** @@ -90,8 +92,9 @@ public void build() panelBuilder.item(28, this.getSettingsButton(Button.BROADCAST)); - panelBuilder.item(20, this.getSettingsButton(Button.GLOW_COMPLETED)); - panelBuilder.item(29, this.getSettingsButton(Button.REMOVE_COMPLETED)); + panelBuilder.item(11, this.getSettingsButton(Button.GLOW_COMPLETED)); + panelBuilder.item(20, this.getSettingsButton(Button.REMOVE_COMPLETED)); + panelBuilder.item(29, this.getSettingsButton(Button.VISIBILITY_MODE)); panelBuilder.item(21, this.getSettingsButton(Button.LOCKED_LEVEL_ICON)); panelBuilder.item(30, this.getSettingsButton(Button.FREE_AT_TOP)); @@ -490,6 +493,61 @@ private PanelItem getSettingsButton(Button button) glow = false; break; } + case VISIBILITY_MODE: + { + name = this.user.getTranslation("challenges.gui.buttons.admin.visibility-mode"); + + List values = new ArrayList<>(5); + values.add(this.user.getTranslation("challenges.gui.descriptions.admin.visibility-mode")); + + values.add((this.settings.getVisibilityMode().equals(VisibilityMode.VISIBLE) ? "&2" : "&c") + + this.user.getTranslation("challenges.gui.descriptions.visibility.visible")); + values.add((this.settings.getVisibilityMode().equals(VisibilityMode.HIDDEN) ? "&2" : "&c") + + this.user.getTranslation("challenges.gui.descriptions.visibility.hidden")); + values.add((this.settings.getVisibilityMode().equals(VisibilityMode.TOGGLEABLE) ? "&2" : "&c") + + this.user.getTranslation("challenges.gui.descriptions.visibility.toggleable")); + + values.add(this.user.getTranslation("challenges.gui.descriptions.current-value", + "[value]",this.settings.getVisibilityMode().name())); + + description = values; + + if (this.settings.getVisibilityMode().equals(VisibilityMode.VISIBLE)) + { + icon = new ItemStack(Material.OAK_PLANKS); + } + else if (this.settings.getVisibilityMode().equals(VisibilityMode.HIDDEN)) + { + icon = new ItemStack(Material.OAK_SLAB); + } + else + { + icon = new ItemStack(Material.OAK_BUTTON); + } + + clickHandler = (panel, user, clickType, slot) -> { + if (clickType.isRightClick()) + { + this.settings.setVisibilityMode( + Utils.getPreviousValue(VisibilityMode.values(), + this.settings.getVisibilityMode())); + } + else + { + this.settings.setVisibilityMode( + Utils.getNextValue(VisibilityMode.values(), + this.settings.getVisibilityMode())); + } + + // Rebuild just this icon + panel.getInventory().setItem(slot, + this.getSettingsButton(button).getItem()); + + return true; + }; + glow = false; + break; + } default: return new PanelItemBuilder().build(); } @@ -529,7 +587,12 @@ private enum Button GLOW_COMPLETED, LOCKED_LEVEL_ICON, ENABLE_TITLE, - TITLE_SHOWTIME + TITLE_SHOWTIME, + + /** + * This allows to switch between different challenges visibility modes. + */ + VISIBILITY_MODE } diff --git a/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java b/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java index 43219c91..cd6fc8c9 100644 --- a/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/user/ChallengesGUI.java @@ -12,6 +12,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.challenges.ChallengesAddon; import world.bentobox.challenges.ChallengesManager; +import world.bentobox.challenges.config.SettingsUtils.VisibilityMode; import world.bentobox.challenges.database.object.Challenge; import world.bentobox.challenges.panel.CommonGUI; import world.bentobox.challenges.tasks.TryToComplete; @@ -151,6 +152,12 @@ private void addFreeChallenges(PanelBuilder panelBuilder, int firstItemIndex) this.challengesManager.isChallengeComplete(this.user, this.world, challenge)); } + // Remove all undeployed challenges if VisibilityMode is set to Hidden. + if (this.addon.getChallengesSettings().getVisibilityMode().equals(VisibilityMode.HIDDEN)) + { + freeChallenges.removeIf(challenge -> !challenge.isDeployed()); + } + final int freeChallengesCount = freeChallenges.size(); if (freeChallengesCount > 18) @@ -221,6 +228,12 @@ private void addChallenges(PanelBuilder panelBuilder, int firstItemIndex) this.challengesManager.isChallengeComplete(this.user, this.world, challenge)); } + // Remove all undeployed challenges if VisibilityMode is set to Hidden. + if (this.addon.getChallengesSettings().getVisibilityMode().equals(VisibilityMode.HIDDEN)) + { + challenges.removeIf(challenge -> !challenge.isDeployed()); + } + final int challengesCount = challenges.size(); if (challengesCount > 18) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index fe3a2625..63539f14 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -43,6 +43,14 @@ gui-settings: # Add enchanted glow to completed challenges add-completed-glow: true # + # This variable allows to choose which Challenges users can see in Challenges GUI. + # Valid values are: + # 'VISIBLE' - there will be no hidden challenges. All challenges will be viewable in GUI. + # 'HIDDEN' - shows only deployed challenges. + # 'TOGGLEABLE' - there will be button in GUI that allows users to switch from ALL modes. + # TOGGLEABLE - Currently not implemented. + undeployed-view-mode: VISIBLE + # # This allows to change default locked level icon. This option may be # overwritten by each challenge level. If challenge level has specified # their locked level icon, then it will be used, instead of this one. diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 2cf9273f..31e82452 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -136,6 +136,7 @@ challenges: glow: 'Glow when completed' free-at-top: 'Free challenges first' line-length: 'Lore line length' + visibility-mode: 'Challenge Visibility Mode' toggle-user-list: 'User List' remove-selected: 'Remove Selected' add: 'Add' @@ -263,6 +264,9 @@ challenges: island-store: 'Allows to enable/disable challenges data storing per island. This means that challenges will be the same on whole team, if this is enabled.|Will NOT convert data on click. PROGRESS WILL BE LOST.' default-locked-icon: 'Allows to change default locked level icon.|This option can be overwritten by each level.' gui-mode: 'Allows to enable/disable single challenges GUI.|&2Requires server restart.' + + visibility-mode: 'Allows to switch if undeployed challenges should be displayed or not.' + click-to-edit: '&4Click here to edit input.' edit-text-line: '&6 Edit text message!' add-text-line: '&6 Add new text message!' @@ -330,6 +334,12 @@ challenges: increase-by: "&aIncrease completion count by [value]" reduce-by: "&cReduce completion count by [value]" + + visibility: + visible: "All challenges are visible for everyone" + hidden: "Only Deployed challenges are visible." + toggleable: "Allows to toggle if undeployed challenges should be displayed" + challenge-description: level: '&FLevel: [level]' completed: '&BCompleted'