Skip to content

Commit

Permalink
Changed the way the plugin handles input storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
its-c10 committed Apr 10, 2022
1 parent 51ff655 commit ee93d4a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
7 changes: 3 additions & 4 deletions src/main/java/com/nthbyte/dialogue/DialogueListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
public class DialogueListener implements Listener {

private Map<Player, Map<String, String>> inputStoragePerPlayer = new HashMap<>();
private DialogueManager dialogueManager;
private JavaPlugin hookedPlugin;

Expand Down Expand Up @@ -88,7 +87,7 @@ public void onReceiveInput(ReceiveInputEvent e){
Player player = e.getPlayer();
if(onReceiveInputAction != null){

Map<String, String> inputStorage = inputStoragePerPlayer.get(player);
Map<String, String> inputStorage = DialogueManager.getInputStoragePerPlayer().get(player);
if(inputStorage == null) {
inputStorage = new HashMap<>();
}
Expand All @@ -103,7 +102,7 @@ public void onReceiveInput(ReceiveInputEvent e){
onReceiveInputAction.accept(context, input);

if(!inputStorage.isEmpty()){
inputStoragePerPlayer.put(player, inputStorage);
DialogueManager.getInputStoragePerPlayer().put(player, inputStorage);
}

}
Expand Down Expand Up @@ -147,7 +146,7 @@ private void fireReceiveInputEvent(Player player, Dialogue dialogue, String inpu
if(dialogue.hasMorePrompts()){
dialogue.nextPrompt(player);
}else{
Map<String, String> inputStorage = inputStoragePerPlayer.remove(player);
Map<String, String> inputStorage = DialogueManager.getInputStoragePerPlayer().get(player);
for(ActionContext context : dialogue.getEndActions().values()){
if(context != null){
context.setInputStorage(inputStorage);
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/nthbyte/dialogue/DialogueManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
public class DialogueManager {

private static Map<Player, Map<String, String>> inputStoragePerPlayer = new HashMap<>();

/**
* The players current in dialogue.
*/
Expand Down Expand Up @@ -54,10 +56,11 @@ public void endDialogue(Player player, DialogueEndCause cause){
Dialogue endedDialogue = playersInDialogue.remove(player.getUniqueId());
if(endedDialogue == null) return;

for(Map.Entry<Action.BasePromptAction, ActionContext> pair : endedDialogue.getEndActions().entrySet()){
ActionContext context = pair.getValue();
Map<String, String> inputStorage = (context == null || !context.hasStoredInputs()) ? new HashMap<>() : context.getInputStorage();
Action.BasePromptAction endAction = pair.getKey();
for(Map.Entry<Action.BasePromptAction, ActionContext> entry : endedDialogue.getEndActions().entrySet()){

ActionContext<?> context = entry.getValue();
Map<String, String> inputStorage = inputStoragePerPlayer.remove(player);
Action.BasePromptAction endAction = entry.getKey();
// They are defining their own action.
if(endAction instanceof Action.EndAction || context == null){
// Will be null if they are defining their own action (and not using a default one).
Expand All @@ -77,13 +80,17 @@ public void endDialogue(Player player, DialogueEndCause cause){
endAction.accept(context, "");
}
}
}

}

}

public Dialogue getCurrentDialogue(Player player){
return playersInDialogue.get(player.getUniqueId());
}

public static Map<Player, Map<String, String>> getInputStoragePerPlayer() {
return inputStoragePerPlayer;
}

}
1 change: 1 addition & 0 deletions src/main/java/com/nthbyte/dialogue/Prompt.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.nthbyte.dialogue.action.Action;
import com.nthbyte.dialogue.action.context.ActionContext;
import com.nthbyte.dialogue.util.Utils;
import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;
import org.bukkit.entity.Player;

import java.util.*;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/nthbyte/dialogue/action/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public final class Action {

/**
* Base prompt action. Can run when prompt ends, or on input receive.
* @param <T> The type of action context.
* @param <U> Another useful piece of information. Could be any data type.
*/
public interface BasePromptAction<T extends ActionContext, U> extends BiConsumer<T, U> {}

Expand Down

0 comments on commit ee93d4a

Please sign in to comment.