From b24f7ebcb03992fe876878eed349feb8c70aa02c Mon Sep 17 00:00:00 2001 From: BONNe Date: Wed, 1 May 2019 22:57:24 +0300 Subject: [PATCH] Implement title messages on first challenge/level completion (#108). 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. --- .../world/bentobox/challenges/Settings.java | 55 ++++++++++++++- .../panel/admin/EditSettingsGUI.java | 63 ++++++++++++++++- .../challenges/tasks/TryToComplete.java | 69 +++++++++++++++++++ src/main/resources/config.yml | 10 ++- src/main/resources/locales/en-US.yml | 16 +++++ 5 files changed, 208 insertions(+), 5 deletions(-) diff --git a/src/main/java/world/bentobox/challenges/Settings.java b/src/main/java/world/bentobox/challenges/Settings.java index f86276c..3666ca7 100644 --- a/src/main/java/world/bentobox/challenges/Settings.java +++ b/src/main/java/world/bentobox/challenges/Settings.java @@ -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:") @@ -161,7 +172,7 @@ public class Settings implements DataObject * Configuration version */ @ConfigComment("") - private String configVersion = "v2"; + private String configVersion = "v3"; // --------------------------------------------------------------------- @@ -350,6 +361,48 @@ public class Settings implements DataObject } + /** + * 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. 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 a6ed710..42864e5 100644 --- a/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java +++ b/src/main/java/world/bentobox/challenges/panel/admin/EditSettingsGUI.java @@ -72,7 +72,13 @@ public class EditSettingsGUI extends CommonGUI 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)); @@ -100,7 +106,8 @@ public class EditSettingsGUI extends CommonGUI 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); @@ -449,6 +456,54 @@ public class EditSettingsGUI extends CommonGUI 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(); } @@ -486,7 +541,9 @@ public class EditSettingsGUI extends CommonGUI PURGE_HISTORY, STORE_MODE, GLOW_COMPLETED, - LOCKED_LEVEL_ICON + LOCKED_LEVEL_ICON, + ENABLE_TITLE, + TITLE_SHOWTIME } diff --git a/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java b/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java index 314304d..b680240 100644 --- a/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java +++ b/src/main/java/world/bentobox/challenges/tasks/TryToComplete.java @@ -26,6 +26,7 @@ import world.bentobox.challenges.ChallengesManager; 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; /** @@ -247,6 +248,17 @@ public class TryToComplete } } } + + // 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 { @@ -322,6 +334,17 @@ public class TryToComplete } 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); + } } } } @@ -982,6 +1005,52 @@ public class TryToComplete } +// --------------------------------------------------------------------- +// 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 // --------------------------------------------------------------------- diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 70a4725..6056a2b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -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: @@ -109,4 +117,4 @@ disabled-gamemodes: [] # uniqueId: config # -configVersion: v2 +configVersion: v3 diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 845f249..215568c 100755 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -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' @@ -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' @@ -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.'