Last active
July 18, 2019 21:49
-
-
Save WouterG/cb7891b58bc55283e015f374eac87a8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import net.menoni.rd.RuntimeDebugger; | |
import net.menoni.rd.model.Debugger; | |
import org.bukkit.Bukkit; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.conversations.ConversationContext; | |
import org.bukkit.conversations.ConversationFactory; | |
import org.bukkit.conversations.FixedSetPrompt; | |
import org.bukkit.conversations.Prompt; | |
import org.bukkit.entity.Player; | |
import org.bukkit.potion.PotionEffect; | |
import org.bukkit.potion.PotionEffectType; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class ConversationTest implements Debugger { | |
private ConversationFactory f; | |
@Override | |
public void debug(RuntimeDebugger plugin, CommandSender cs) { | |
if (!(cs instanceof Player)) { | |
cs.sendMessage("be a player"); | |
return; | |
} | |
List<Player> ps = new ArrayList<>(Bukkit.getOnlinePlayers()); | |
SPlayer[] items = new SPlayer[ps.size()]; | |
Prompt[] prompts = new Prompt[ps.size()]; | |
for (int i = 0; i < ps.size(); i++) { | |
SPlayer sp = new SPlayer(ps.get(i)); | |
items[i] = sp; | |
prompts[i] = new PlayerMenu(ps.get(i)); | |
} | |
OptionPrompt<SPlayer> playerOptionPrompt = new OptionPrompt<>("Choose player", items, prompts); | |
this.f = new ConversationFactory(plugin) | |
.withModality(true) | |
.withPrefix(context -> "[test]") | |
.withFirstPrompt(playerOptionPrompt) | |
.withEscapeSequence("end") | |
.withTimeout(10) | |
.thatExcludesNonPlayersWithMessage("No!") | |
.addConversationAbandonedListener((conversationAbandonedEvent) -> { | |
System.out.println(conversationAbandonedEvent.getContext().getAllSessionData().toString()); | |
}); | |
this.f.buildConversation((Player) cs).begin(); | |
} | |
static class SPlayer implements Stringable { | |
private Player player; | |
public SPlayer(Player player) { | |
this.player = player; | |
} | |
@Override | |
public String asString() { | |
return player.getName(); | |
} | |
@Override | |
public boolean matches(String input) { | |
return this.player.getName().equalsIgnoreCase(input); | |
} | |
@Override | |
public Runnable getAction() { | |
return null; | |
} | |
} | |
private static class PlayerOption implements Stringable { | |
private String option; | |
private Runnable action; | |
public PlayerOption(String option, Runnable action) { | |
this.option = option; | |
this.action = action; | |
} | |
@Override | |
public String asString() { | |
return this.option; | |
} | |
@Override | |
public boolean matches(String input) { | |
return this.option.equalsIgnoreCase(input); | |
} | |
@Override | |
public Runnable getAction() { | |
return this.action; | |
} | |
} | |
private static class PlayerMenu extends OptionPrompt<PlayerOption> { | |
private Player player; | |
public PlayerMenu(Player player) { | |
super("Choose an option:", new PlayerOption[]{ | |
new PlayerOption("Kick", () -> player.kickPlayer("bye")), | |
new PlayerOption("Blind", () -> player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 4, false, false, false))) | |
} | |
, new Prompt[]{ | |
Prompt.END_OF_CONVERSATION, | |
Prompt.END_OF_CONVERSATION | |
}); | |
this.player = player; | |
} | |
} | |
private static class OptionPrompt<T extends Stringable> extends FixedSetPrompt { | |
private static String[] factorKeys(Stringable[] items) { | |
String[] keys = new String[items.length]; | |
for (int i = 0; i < items.length; i++) { | |
keys[i] = items[i].asString(); | |
} | |
return keys; | |
} | |
private String prompt; | |
private T[] items; | |
private Prompt[] prompts; | |
public OptionPrompt(String prompt, T[] items, Prompt[] prompts) { | |
super(factorKeys(items)); | |
if (items.length != prompts.length) { | |
throw new RuntimeException("invalid option prompt - lenghts"); | |
} | |
this.prompts = prompts; | |
this.items = items; | |
this.prompt = prompt; | |
} | |
@Override | |
protected Prompt acceptValidatedInput(ConversationContext context, String input) { | |
for (int i = 0; i < this.items.length; i++) { | |
if (this.items[i].matches(input)) { | |
if (this.items[i].getAction() != null) { | |
this.items[i].getAction().run(); | |
} | |
return this.prompts[i]; | |
} | |
} | |
return null; | |
} | |
@Override | |
public String getPromptText(ConversationContext context) { | |
return this.prompt + " [" + String.join(", ", super.fixedSet) + "]"; | |
} | |
} | |
private interface Stringable { | |
String asString(); | |
boolean matches(String input); | |
Runnable getAction(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment