Skip to content

Commit

Permalink
* Implemented time limit for dialogues.
Browse files Browse the repository at this point in the history
* Implemented new message for ending dialogue via escape sequence
* Cleaned up code.
  • Loading branch information
its-c10 committed Jun 3, 2022
1 parent 673d239 commit 9095259
Show file tree
Hide file tree
Showing 18 changed files with 266 additions and 176 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,59 +7,79 @@
</p>
<!-- https://www.makeareadme.com/ -->

# Dialogue

![Release](https://jitpack.io/v/nthByte-LLC/dialogue.svg)

Dialogue is a Spigot API that completely revamps the Conversation API. <b><i>This is not a plugin you put on your server</b></i>.
Dialogue is a Spigot API that completely revamps the Conversation API. <b><i>This is not a plugin you put on your
server</b></i>.

Want to know what's currently being worked on? Check out the <a href="https://calebowens.youtrack.cloud/projects/9fdf6ba0-79d6-4879-9dfc-0fcf98a7e31a">task board</a> for this project
Want to know what's currently being worked on? Check out
the <a href="https://calebowens.youtrack.cloud/projects/9fdf6ba0-79d6-4879-9dfc-0fcf98a7e31a">task board</a> for this
project

## Installation

### Maven

```xml
<repository>
<id>jitpack.io</id>
<url>https://www.jitpack.io</url>
</repository>
```

```xml
<dependency>
<groupId>com.github.nthByte-LLC</groupId>
<artifactId>dialogue</artifactId>
<version>Tag</version>
</dependency>
```
Replace "Tag" with a release tag for Dialogue. You can see the latest version <a href="https://github.com/nthByte-LLC/dialogue/releases">here</a>.

Replace "Tag" with a release tag for Dialogue. You can see the latest
version <a href="https://github.com/nthByte-LLC/dialogue/releases">here</a>.

### Gradle

```gradle
repositories {
maven { url 'https://jitpack.io' }
}
```

```gradle
dependencies {
implementation 'com.github.nthByte-LLC:dialogue:Tag'
}
```
Replace "Tag" with a release tag for Dialogue. You can see the latest version <a href="https://github.com/nthByte-LLC/dialogue/releases">here</a>.

Replace "Tag" with a release tag for Dialogue. You can see the latest
version <a href="https://github.com/nthByte-LLC/dialogue/releases">here</a>.

## Usage

Firstly, you want to hook into the API. Put this line in your main class.

```java
DialogueAPI.hook(this);
```

All this does is register the dialogue listener and initializes DialogueManager.

From this point on, you can either look at this <a href="https://github.com/nthByte-LLC/dialogue-example">example plugin</a> that uses this API or refer to the <a href="https://github.com/nthByte-LLC/dialogue/wiki">wiki</a>
From this point on, you can either look at this <a href="https://github.com/nthByte-LLC/dialogue-example">example
plugin</a> that uses this API or refer to the <a href="https://github.com/nthByte-LLC/dialogue/wiki">wiki</a>

_I strongly encourage you to look at the example plugin_. The wiki may not always be up to date, but the example plugin will always be up to date since I use it to test out the changes within this API.
_I strongly encourage you to look at the example plugin_. The wiki may not always be up to date, but the example plugin
will always be up to date since I use it to test out the changes within this API.

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

## Support

Need help or have questions? Join my <a href="https://discord.gg/ZP2xxC52An">discord</a>.


Expand Down
2 changes: 1 addition & 1 deletion 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.4.0</version>
<version>1.4.5.0</version>
<packaging>jar</packaging>

<name>Dialogue</name>
Expand Down
53 changes: 37 additions & 16 deletions src/main/java/com/nthbyte/dialogue/Dialogue.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* Object that represents dialogue between the plugin and a player.
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.4.0
* @version 1.4.5.0
*/
public class Dialogue {

Expand All @@ -34,21 +37,27 @@ public class Dialogue {
*/
private boolean repeatPrompt;

/**
* How much time the user has to complete the dialogue (in seconds).
*/
private int timeLimit;

private Dialogue(){}

private Dialogue(Dialogue.Builder builder){
private Dialogue(Dialogue.Builder builder) {
this.prompts = builder.prompts;
this.endActions = builder.endActions;
this.escapeSequences = builder.escapeSequences;
this.repeatPrompt = builder.repeatPrompt;
this.timeLimit = builder.timeLimit;
}

/**
* Gets the current prompt this dialogue is on.
*
* @return The current prompt.
*/
public Prompt getCurrentPrompt(){
public Prompt getCurrentPrompt() {
return prompts.get(currentIndexPrompt);
}

Expand All @@ -57,7 +66,7 @@ public Prompt getCurrentPrompt(){
*
* @return If the dialogue has more prompts.
*/
public boolean hasMorePrompts(){
public boolean hasMorePrompts() {
return currentIndexPrompt != prompts.size() - 1;
}

Expand All @@ -66,9 +75,9 @@ public boolean hasMorePrompts(){
*
* @param player The player we are prompting.
*/
public void nextPrompt(JavaPlugin plugin, Player player){
public void nextPrompt(JavaPlugin plugin, Player player) {
currentIndexPrompt++;
getCurrentPrompt().prompt(plugin, player);
getCurrentPrompt().prompt(plugin, this, player);
}

public Map<Action.BasePromptAction, ActionContext> getEndActions() {
Expand All @@ -87,53 +96,65 @@ public boolean shouldRepeatPrompt() {
return repeatPrompt;
}

public static class Builder{
public int getTimeLimit() {
return timeLimit;
}

public static class Builder {

private boolean repeatPrompt = true;
private String[] escapeSequences = new String[]{""};
private List<Prompt> prompts = new ArrayList<>();
private LinkedHashMap<Action.BasePromptAction, ActionContext> endActions = new LinkedHashMap<>();
private int timeLimit;

public Builder(){}
public Builder() {}

public Builder addPrompt(Prompt.Builder prompt){
public Builder addPrompt(Prompt.Builder prompt) {
this.prompts.add(prompt.build());
return this;
}

public Builder setTimeLimit(int timeLimit){
this.timeLimit = timeLimit;
return this;
}

public Builder setEscapeSequences(String... escapeSequences) {
this.escapeSequences = escapeSequences;
return this;
}

public Builder setRepeatPrompt(boolean repeatPrompt){
public Builder setRepeatPrompt(boolean repeatPrompt) {
this.repeatPrompt = repeatPrompt;
return this;
}

/**
* Using a default action.
*
* @param defaultAction A default action.
* @param context The endActionContext for the action.
* @see Action
* @param context The endActionContext for the action.
* @return The builder.
* @see Action
*/
public <U extends ActionContext> Builder addEndAction(Action.DefaultAction<U> defaultAction, U context){
public <U extends ActionContext> Builder addEndAction(Action.DefaultAction<U> defaultAction, U context) {
this.endActions.put(defaultAction, context);
return this;
}

/**
* Defines your own end action.
*
* @param action Your action.
* @return The builder.
*/
public <T extends ActionContext> Builder addEndAction(Action.EndAction<T> action){
public <T extends ActionContext> Builder addEndAction(Action.EndAction<T> action) {
this.endActions.put(action, null);
return this;
}

public Dialogue build(){
public Dialogue build() {
return new Dialogue(this);
}

Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/nthbyte/dialogue/DialogueAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* An API that completely eliminates your need for the ConversationsAPI
*
* @author <a href="linktr.ee/c10_">Caleb Owens</a>
* @version 1.4.4.0
* @version 1.4.5.0
*/
public class DialogueAPI {

Expand All @@ -23,13 +23,13 @@ public class DialogueAPI {
*
* @param hookingPlugin A plugin instance.
*/
public static void hook(JavaPlugin hookingPlugin){
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()){
if (!messagesFile.exists()) {
new File(hookingPlugin.getDataFolder() + File.separator + "dialogue").mkdirs();
hookingPlugin.saveResource("messages.yml", false);
File currentMessagesFile = new File(hookingPlugin.getDataFolder(), "messages.yml");
Expand All @@ -49,27 +49,27 @@ public static void hook(JavaPlugin hookingPlugin){
* @param player The player we are checking.
* @return If the player is having dialogue or being prompted.
*/
public static boolean isHavingDialogue(Player player){
public static boolean isHavingDialogue(Player player) {
return dialogueManager.isConversing(player);
}

/**
* Starts a new dialogue.
*
* @param player The player you wish to start a dialogue with.
* @param player The player you wish to start a dialogue with.
* @param dialogue The dialogue.
*/
public static void startDialogue(Player player, Dialogue dialogue){
public static void startDialogue(Player player, Dialogue dialogue) {
dialogueManager.startDialogue(player, dialogue);
}

/**
* Ends the player's dialogue.
*
* @param player The player that we wish to end dialogue for.
* @param cause The reason the dialogue ended.
* @param cause The reason the dialogue ended.
*/
public static void endDialogue(Player player, DialogueEndCause cause){
public static void endDialogue(Player player, DialogueEndCause cause) {
dialogueManager.endDialogue(player, cause);
}

Expand Down
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.4.0
* @version 1.4.5.0
*/
public enum DialogueEndCause {

Expand Down Expand Up @@ -33,6 +33,11 @@ public enum DialogueEndCause {
*/
RETRY_LIMIT_REACHED,

/**
* Time limit has been reached for the last prompt.
*/
TIME_LIMIT_REACHED,

/**
* The start of another dialogue has halted the current one.
*/
Expand Down
Loading

0 comments on commit 9095259

Please sign in to comment.