Skip to content

Commit

Permalink
Implement title messages on first challenge/level completion (#108).
Browse files Browse the repository at this point in the history
Add settings option that can enable/disable title message and its length.
Add ability to edit these settings via admin GUI.
Add ability to use some variables in title and subtitle in translation files.
  • Loading branch information
BONNe committed May 1, 2019
1 parent f0b156d commit b24f7eb
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 5 deletions.
55 changes: 54 additions & 1 deletion src/main/java/world/bentobox/challenges/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ public class Settings implements DataObject
@ConfigEntry(path = "broadcast-messages")
private boolean broadcastMessages = true;

@ConfigComment("")
@ConfigComment("Shows a title screen for player after completion a challenge or level.")
@ConfigComment("Message can be edited via language settings.")
@ConfigEntry(path = "title.show-title")
private boolean showCompletionTitle = true;

@ConfigComment("")
@ConfigComment("Integer that represents how long title will be visible for player.")
@ConfigEntry(path = "title.title-showtime")
private int titleShowtime = 70;

@ConfigComment("")
@ConfigComment("This list stores GameModes in which Challenges addon should not work.")
@ConfigComment("To disable addon it is necessary to write its name in new line that starts with -. Example:")
Expand All @@ -161,7 +172,7 @@ public class Settings implements DataObject
* Configuration version
*/
@ConfigComment("")
private String configVersion = "v2";
private String configVersion = "v3";


// ---------------------------------------------------------------------
Expand Down Expand Up @@ -350,6 +361,48 @@ public ItemStack getLockedLevelIcon()
}


/**
* This method returns the showCompletionTitle object.
* @return the showCompletionTitle object.
*/
public boolean isShowCompletionTitle()
{
return this.showCompletionTitle;
}


/**
* This method returns the titleShowtime object.
* @return the titleShowtime object.
*/
public int getTitleShowtime()
{
return this.titleShowtime;
}


/**
* This method sets the titleShowtime object value.
* @param titleShowtime the titleShowtime object new value.
*
*/
public void setTitleShowtime(int titleShowtime)
{
this.titleShowtime = titleShowtime;
}


/**
* This method sets the showCompletionTitle object value.
* @param showCompletionTitle the showCompletionTitle object new value.
*
*/
public void setShowCompletionTitle(boolean showCompletionTitle)
{
this.showCompletionTitle = showCompletionTitle;
}


/**
* This method sets the lockedLevelIcon value.
* @param lockedLevelIcon the lockedLevelIcon new value.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,13 @@ public void build()

GuiUtils.fillBorder(panelBuilder);

panelBuilder.item(19, this.getSettingsButton(Button.RESET_CHALLENGES));
panelBuilder.item(10, this.getSettingsButton(Button.ENABLE_TITLE));

if (this.settings.isShowCompletionTitle())
{
panelBuilder.item(19, this.getSettingsButton(Button.TITLE_SHOWTIME));
}

panelBuilder.item(28, this.getSettingsButton(Button.BROADCAST));

panelBuilder.item(20, this.getSettingsButton(Button.GLOW_COMPLETED));
Expand Down Expand Up @@ -100,7 +106,8 @@ public void build()
panelBuilder.item(33, this.getSettingsButton(Button.PURGE_HISTORY));
}

panelBuilder.item(25, this.getSettingsButton(Button.STORE_MODE));
panelBuilder.item(25, this.getSettingsButton(Button.RESET_CHALLENGES));
panelBuilder.item(34, this.getSettingsButton(Button.STORE_MODE));

// Return Button
panelBuilder.item(44, this.returnButton);
Expand Down Expand Up @@ -449,6 +456,54 @@ private PanelItem getSettingsButton(Button button)
glow = false;
break;
}
case ENABLE_TITLE:
{
description = new ArrayList<>(2);
description.add(this.user.getTranslation("challenges.gui.descriptions.admin.title-enable"));
description.add(this.user.getTranslation("challenges.gui.descriptions.current-value",
"[value]",
this.settings.isShowCompletionTitle() ?
this.user.getTranslation("challenges.gui.descriptions.enabled") :
this.user.getTranslation("challenges.gui.descriptions.disabled")));
name = this.user.getTranslation("challenges.gui.buttons.admin.title-enable");
icon = new ItemStack(Material.SIGN);
clickHandler = (panel, user1, clickType, i) -> {
this.settings.setShowCompletionTitle(!this.settings.isShowCompletionTitle());

// Need to rebuild all as new buttons will show up.
this.build();
return true;
};
glow = this.settings.isShowCompletionTitle();
break;
}
case TITLE_SHOWTIME:
{
description = new ArrayList<>(2);
description.add(this.user.getTranslation("challenges.gui.descriptions.admin.title-showtime"));
description.add(this.user.getTranslation("challenges.gui.descriptions.current-value",
"[value]", Integer.toString(this.settings.getTitleShowtime())));
name = this.user.getTranslation("challenges.gui.buttons.admin.title-showtime");
icon = new ItemStack(Material.CLOCK);
clickHandler = (panel, user1, clickType, i) -> {
new NumberGUI(this.user,
this.settings.getTitleShowtime(),
0,
this.settings.getLoreLineLength(),
(status, value) -> {
if (status)
{
this.settings.setTitleShowtime(value);
}

panel.getInventory().setItem(i, this.getSettingsButton(button).getItem());
});

return true;
};
glow = false;
break;
}
default:
return new PanelItemBuilder().build();
}
Expand Down Expand Up @@ -486,7 +541,9 @@ private enum Button
PURGE_HISTORY,
STORE_MODE,
GLOW_COMPLETED,
LOCKED_LEVEL_ICON
LOCKED_LEVEL_ICON,
ENABLE_TITLE,
TITLE_SHOWTIME
}


Expand Down
69 changes: 69 additions & 0 deletions src/main/java/world/bentobox/challenges/tasks/TryToComplete.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import world.bentobox.challenges.database.object.Challenge;
import world.bentobox.challenges.database.object.Challenge.ChallengeType;
import world.bentobox.challenges.database.object.ChallengeLevel;
import world.bentobox.challenges.utils.GuiUtils;


/**
Expand Down Expand Up @@ -247,6 +248,17 @@ public ChallengeResult build()
}
}
}

// sends title to player on challenge completion
if (this.addon.getChallengesSettings().isShowCompletionTitle())
{
this.user.getPlayer().sendTitle(
this.parseChallenge(this.user.getTranslation("challenges.titles.challenge-title"), this.challenge),
this.parseChallenge(this.user.getTranslation("challenges.titles.challenge-subtitle"), this.challenge),
10,
this.addon.getChallengesSettings().getTitleShowtime(),
20);
}
}
else
{
Expand Down Expand Up @@ -322,6 +334,17 @@ public ChallengeResult build()
}

this.manager.setLevelComplete(this.user, this.world, level);

// sends title to player on level completion
if (this.addon.getChallengesSettings().isShowCompletionTitle())
{
this.user.getPlayer().sendTitle(
this.parseLevel(this.user.getTranslation("challenges.titles.level-title"), level),
this.parseLevel(this.user.getTranslation("challenges.titles.level-subtitle"), level),
10,
this.addon.getChallengesSettings().getTitleShowtime(),
20);
}
}
}
}
Expand Down Expand Up @@ -982,6 +1005,52 @@ else if (this.addon.isLevelProvided() &&
}


// ---------------------------------------------------------------------
// Section: Title parsings
// ---------------------------------------------------------------------


/**
* This method pareses input message by replacing all challenge variables in [] with their values.
* @param inputMessage inputMessage string
* @param challenge Challenge from which these values should be taken
* @return new String that replaces [VALUE] with correct value from challenge.
*/
private String parseChallenge(String inputMessage, Challenge challenge)
{
String outputMessage = inputMessage;

if (inputMessage.contains("[") && inputMessage.contains("]"))
{
outputMessage = outputMessage.replace("[friendlyName]", challenge.getFriendlyName());
outputMessage = outputMessage.replace("[level]", challenge.getLevel().isEmpty() ? "" : this.manager.getLevel(challenge.getLevel()).getFriendlyName());
outputMessage = outputMessage.replace("[rewardText]", challenge.getRewardText());
}

return outputMessage;
}


/**
* This method pareses input message by replacing all level variables in [] with their values.
* @param inputMessage inputMessage string
* @param level level from which these values should be taken
* @return new String that replaces [VALUE] with correct value from level.
*/
private String parseLevel(String inputMessage, ChallengeLevel level)
{
String outputMessage = inputMessage;

if (inputMessage.contains("[") && inputMessage.contains("]"))
{
outputMessage = outputMessage.replace("[friendlyName]", level.getFriendlyName());
outputMessage = outputMessage.replace("[rewardText]", level.getRewardText());
}

return outputMessage;
}


// ---------------------------------------------------------------------
// Section: Private classes
// ---------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ reset-challenges: true
# Broadcast 1st time challenge completion messages to all players.
# Change to false if the spam becomes too much.
broadcast-messages: true
title:
#
# Shows a title screen for player after completion a challenge or level.
# Message can be edited via language settings.
show-title: true
#
# Integer that represents how long title will be visible for player.
title-showtime: 70
#
# This list stores GameModes in which Challenges addon should not work.
# To disable addon it is necessary to write its name in new line that starts with -. Example:
Expand All @@ -109,4 +117,4 @@ disabled-gamemodes: []
#
uniqueId: config
#
configVersion: v2
configVersion: v3
16 changes: 16 additions & 0 deletions src/main/resources/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ challenges:
island-store: 'Store per Island'
default-locked-icon: 'Locked Level Icon'
input-mode: 'Switch input mode'
title-enable: 'Completion title'
title-showtime: 'Title Show Time'
next: 'Next'
previous: 'Previous'
return: 'Return'
Expand Down Expand Up @@ -242,6 +244,8 @@ challenges:
edit-text-line: '&6 Edit text message!'
add-text-line: '&6 Add new text message!'
input-mode: 'Switch between chat and anvil input modes.'
title-enable: 'Allows to enable/disable title message that will be showed when player complete challenge.'
title-showtime: 'Allows to modify how long title message will be visible for player.'
current-value: '|&6Current value: [value].'
enabled: 'Active'
disabled: 'Disabled'
Expand Down Expand Up @@ -291,6 +295,18 @@ challenges:
money-reward: '&6Money reward: $[value]'
reward-items: '&6Reward Items:'
reward-commands: '&6Reward Commands:'
titles:
# Title and subtitle my contain variable in [] that will be replaced with proper message from challenge object.
# [friendlyName] will be replaced with challenge friendly name.
# [level] will be replaced with level friendly name.
# [rewardText] will be replaced with challenge reward text.
challenge-title: 'Successfully completed'
challenge-subtitle: '[friendlyName]'
# Title and subtitle my contain variable in [] that will be replaced with proper message from level object.
# [friendlyName] will be replaced with level friendly name.
# [rewardText] will be replaced with level reward text.
level-title: 'Successfully completed'
level-subtitle: '[friendlyName]'
messages:
admin:
hit-things: 'Hit things to add them to the list of things required. Right click when done.'
Expand Down

0 comments on commit b24f7eb

Please sign in to comment.