Skip to content

Commit

Permalink
Add an option to hide undeployed challenges from challenge list (#175)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BONNe committed Sep 5, 2019
1 parent e5ec5d5 commit 643c5e8
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 6 deletions.
43 changes: 40 additions & 3 deletions src/main/java/world/bentobox/challenges/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -191,9 +202,9 @@ public class Settings implements ConfigObject
private String configVersion = "v3";


// ---------------------------------------------------------------------
// Section: Methods
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Section: Getters
// ---------------------------------------------------------------------


/**
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
}
21 changes: 21 additions & 0 deletions src/main/java/world/bentobox/challenges/config/SettingsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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<String> 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();
}
Expand Down Expand Up @@ -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
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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!'
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 643c5e8

Please sign in to comment.