Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/com/nthbyte/dialogue/Prompt.java
  • Loading branch information
its-c10 committed May 21, 2022
2 parents 55c7baf + c3bcdb5 commit 673d239
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 31 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
Third number I will change whenever I add a new feature.
Fourth number I will change for bug fixes.
-->
<version>1.4.2.0</version>
<version>1.4.4.0</version>
<packaging>jar</packaging>

<name>Dialogue</name>
Expand Down Expand Up @@ -53,7 +53,7 @@
</plugins>
<resources>
<resource>
<directory>../Personal Plugins/Dialogue/src/main/resources</directory>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nthbyte/dialogue/Dialogue.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Object that represents dialogue between the plugin and a player.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class Dialogue {

Expand Down
26 changes: 25 additions & 1 deletion src/main/java/com/nthbyte/dialogue/DialogueAPI.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.nthbyte.dialogue;

import com.google.common.io.Files;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

/**
* An API that completely eliminates your need for the ConversationsAPI
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class DialogueAPI {

private static MessagesConfig messagesConfig;
private static DialogueManager dialogueManager;

/**
Expand All @@ -19,8 +24,23 @@ public class DialogueAPI {
* @param hookingPlugin A plugin instance.
*/
public static void hook(JavaPlugin hookingPlugin){

dialogueManager = new DialogueManager(hookingPlugin);
hookingPlugin.getServer().getPluginManager().registerEvents(new DialogueListener(hookingPlugin, dialogueManager), hookingPlugin);

File messagesFile = new File(hookingPlugin.getDataFolder() + File.separator + "dialogue" + File.separator + "messages.yml");
if(!messagesFile.exists()){
new File(hookingPlugin.getDataFolder() + File.separator + "dialogue").mkdirs();
hookingPlugin.saveResource("messages.yml", false);
File currentMessagesFile = new File(hookingPlugin.getDataFolder(), "messages.yml");
try {
Files.move(currentMessagesFile, messagesFile);
} catch (IOException e) {
e.printStackTrace();
}
}
messagesConfig = new MessagesConfig(hookingPlugin);

}

/**
Expand Down Expand Up @@ -53,4 +73,8 @@ public static void endDialogue(Player player, DialogueEndCause cause){
dialogueManager.endDialogue(player, cause);
}

public static MessagesConfig getMessagesConfig() {
return messagesConfig;
}

}
7 changes: 6 additions & 1 deletion src/main/java/com/nthbyte/dialogue/DialogueEndCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Reasons that the dialogue ended.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public enum DialogueEndCause {

Expand All @@ -28,6 +28,11 @@ public enum DialogueEndCause {
*/
OTHER,

/**
* You have reached the retry limit.
*/
RETRY_LIMIT_REACHED,

/**
* The start of another dialogue has halted the current one.
*/
Expand Down
49 changes: 33 additions & 16 deletions src/main/java/com/nthbyte/dialogue/DialogueListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* The listener for all input and dialogue.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class DialogueListener implements Listener {

Expand Down Expand Up @@ -123,7 +123,9 @@ private void fireReceiveInputEvent(Player player, Dialogue dialogue, String inpu

boolean shouldRepeatPrompt = dialogue.shouldRepeatPrompt();
if(!InputFormatValidator.isValidFormat(inputType, input)){
player.sendMessage(Utils.tr("&cThe input is not in the valid format! The input type should be " + inputType));
String rawMsg = DialogueAPI.getMessagesConfig().INVALID_INPUT.replace("%inputType%", inputType.toString());
String msg = rawMsg.replace("%inputType%", inputType.toString());
player.sendMessage(Utils.tr(msg));
if(shouldRepeatPrompt){
prompt.prompt(hookedPlugin, player);
}
Expand All @@ -134,27 +136,42 @@ private void fireReceiveInputEvent(Player player, Dialogue dialogue, String inpu
Bukkit.getPluginManager().callEvent(validationEvent);

Function<String, Boolean> validationAction = prompt.getOnValidateInputAction();
if(!validationEvent.isValidInput() || (validationAction != null && !validationAction.apply(input))){
if(shouldRepeatPrompt){
prompt.prompt(hookedPlugin, player);
}
return;

if(prompt.getRetryLimit() != -1){
prompt.incrementRetries();
}

Bukkit.getPluginManager().callEvent(new ReceiveInputEvent(player, prompt, input));
boolean isValidInput = validationEvent.isValidInput() && (validationAction == null || validationAction.apply(input));
if(isValidInput){
Bukkit.getPluginManager().callEvent(new ReceiveInputEvent(player, prompt, input));
}

if(dialogue.hasMorePrompts()){
boolean atRetryLimit = prompt.isAtRetryLimit();
if(!isValidInput && shouldRepeatPrompt && !atRetryLimit) {
prompt.prompt(hookedPlugin, player);
}else if(!isValidInput && atRetryLimit){
player.sendMessage(Utils.tr(DialogueAPI.getMessagesConfig().REACHED_RETRY_LIMIT));
if(prompt.willStopDialougeOnFailure()){
endDialogue(dialogue, player);
}else{
dialogue.nextPrompt(hookedPlugin, player);
}
} else if(dialogue.hasMorePrompts()){
dialogue.nextPrompt(hookedPlugin, player);
}else{
Map<String, String> inputStorage = DialogueManager.getInputStoragePerPlayer().get(player);
for(ActionContext context : dialogue.getEndActions().values()){
if(context != null){
context.setInputStorage(inputStorage);
}
}
dialogueManager.endDialogue(player, DialogueEndCause.NO_MORE_PROMPTS);
endDialogue(dialogue, player);
}

}

private void endDialogue(Dialogue dialogue, Player player){
Map<String, String> inputStorage = DialogueManager.getInputStoragePerPlayer().get(player);
for(ActionContext context : dialogue.getEndActions().values()){
if(context != null){
context.setInputStorage(inputStorage);
}
}
dialogueManager.endDialogue(player, DialogueEndCause.NO_MORE_PROMPTS);
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/nthbyte/dialogue/DialogueManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* The manager for all dialogue.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class DialogueManager {

Expand Down
27 changes: 27 additions & 0 deletions src/main/java/com/nthbyte/dialogue/MessagesConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.nthbyte.dialogue;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;

public class MessagesConfig {

public final String INVALID_INPUT;
public final String REACHED_RETRY_LIMIT;

private FileConfiguration config;

public MessagesConfig(JavaPlugin hookingPlugin){
loadConfig(hookingPlugin);
INVALID_INPUT = config.getString("invalid-input");
REACHED_RETRY_LIMIT = config.getString("reached-retry-limit");
}

private void loadConfig(JavaPlugin hookingPlugin){
File file = new File(hookingPlugin.getDataFolder() + File.separator + "dialogue" + File.separator + "messages.yml");
this.config = YamlConfiguration.loadConfiguration(file);
}

}
45 changes: 44 additions & 1 deletion src/main/java/com/nthbyte/dialogue/Prompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* Represents a question or a request.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class Prompt {

Expand Down Expand Up @@ -53,6 +53,19 @@ public class Prompt {
*/
private int delay;

/**
* How many times a player can try to answer a prompt.
*/
private int retryLimit;

/**
* Whether the dialogue stops when a player fails (you have reached the retry limit) to enter valid input for a prompt.
* This value only matters if the retry limit is not -1.
*/
private boolean stopUponFailure;

private int numRetries = 0;

private int timeLimit;

private Prompt(Prompt.Builder builder){
Expand All @@ -63,6 +76,8 @@ private Prompt(Prompt.Builder builder){
this.onValidateInputAction = builder.onValidateInputAction;
this.delay = builder.delay;
this.timeLimit = builder.timeLimit;
this.retryLimit = builder.retryLimit;
this.stopUponFailure = builder.stopUponFailure;
}

public String getId() {
Expand Down Expand Up @@ -93,6 +108,22 @@ public void prompt(JavaPlugin plugin, Player player){
}, delay);
}

public void incrementRetries(){
numRetries++;
}

public boolean isAtRetryLimit(){
return numRetries == retryLimit;
}

public int getRetryLimit() {
return retryLimit;
}

public boolean willStopDialougeOnFailure(){
return stopUponFailure;
}

public int getTimeLimit() {
return timeLimit;
}
Expand All @@ -105,6 +136,8 @@ public static class Builder{
private LinkedHashMap<Action.BasePromptAction, ActionContext> receiveInputActions = new LinkedHashMap<>();
private int delay;
private int timeLimit;
private int retryLimit = -1;
private boolean stopUponFailure;

// Prompt validator returns true by default.
private Function<String, Boolean> onValidateInputAction = s -> true;
Expand Down Expand Up @@ -171,6 +204,16 @@ public Builder setDelay(int delay) {
return this;
}

public Builder setRetryLimit(int retries){
this.retryLimit = retries;
return this;
}

public Builder stopDialogueUponFailure(){
this.stopUponFailure = true;
return this;
}

public Prompt build(){
return new Prompt(this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nthbyte/dialogue/PromptInputType.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* The different types of input for prompts.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public enum PromptInputType {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nthbyte/dialogue/action/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Default actions that you can use for different stages of the prompt.
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public final class Action {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Container that provides a given action with specific information/data it needs to run successfully.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class ActionContext<T> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Context that provides the given action with a location.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class LocationContext extends ActionContext<Location> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* Event fired when you receive input from a player post-validation.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class ReceiveInputEvent extends Event {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* The event that fires just before the prompt uses its own validation check (If there is any). Use this if you wish to do your own validation.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class ValidateInputEvent extends Event {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Validates the format of input.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class InputFormatValidator {

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/nthbyte/dialogue/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Utility class.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.2.0
* @version 1.4.4.0
*/
public class Utils {

Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/messages.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
invalid-input: "&cThe input is not in the valid format! The input type should be %inputType%"
reached-retry-limit: "&cYou've reached the retry limit!"

0 comments on commit 673d239

Please sign in to comment.