Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.turt2live.conversations;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.conversations.ConversationContext;
- import org.bukkit.conversations.ConversationFactory;
- import org.bukkit.conversations.ConversationPrefix;
- import org.bukkit.conversations.Prompt;
- import org.bukkit.conversations.StringPrompt;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.Listener;
- import org.bukkit.event.inventory.InventoryClickEvent;
- import org.bukkit.inventory.Inventory;
- import org.bukkit.inventory.InventoryView;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.plugin.java.JavaPlugin;
- public class ConversationTest extends JavaPlugin implements Listener {
- @Override
- public void onEnable(){
- getServer().getPluginManager().registerEvents(this, this);
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
- if(command.getName().equalsIgnoreCase("cinventory")){ // Conversation Test
- if(sender instanceof Player){
- // Build conversation
- ConversationFactory factory = new ConversationFactory(this)
- .withModality(false) // No effect if true or false
- .withLocalEcho(false)
- .withFirstPrompt(new InventoryPopup(this))
- .thatExcludesNonPlayersWithMessage("Nope!")
- .withPrefix(new ConversationPrefix(){
- @Override
- public String getPrefix(ConversationContext context){
- return ChatColor.GRAY + "[CONVO] " + ChatColor.WHITE;
- }
- })
- .withEscapeSequence("exit");
- factory.buildConversation((Player) sender).begin();
- }else{
- sender.sendMessage("Run as player please");
- }
- }else if(command.getName().equalsIgnoreCase("dinventory")){ // Out-of-conversation Test
- if(sender instanceof Player){
- // Setup
- Inventory items = getServer().createInventory(null, 45);
- Player player = (Player) sender;
- // Open inventory
- if(player.getOpenInventory() != null)
- player.getOpenInventory().close();
- InventoryView v = player.openInventory(items);
- // Wait for items, "null" should be replaced with something every so often
- Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new InventoryListener(v), 40, 40);
- }else{
- sender.sendMessage("Run as player please");
- }
- }else if(command.getName().equalsIgnoreCase("killalltasks")){
- getServer().getScheduler().cancelTasks(this);
- sender.sendMessage("Done");
- }else{
- return false;
- }
- return true;
- }
- @EventHandler
- public void onClick(InventoryClickEvent event){
- InventoryView inventory = event.getView();
- for(ItemStack i : inventory.getTopInventory().getContents()){
- System.out.println(i);
- }
- System.out.println("-----");
- }
- public static class InventoryListener implements Runnable {
- private InventoryView inventory;
- public InventoryListener(InventoryView items){
- this.inventory = items;
- }
- @Override
- public void run(){
- for(ItemStack i : inventory.getTopInventory().getContents()){
- System.out.println(i);
- }
- System.out.println("-----");
- }
- }
- public static class InventoryPopup extends StringPrompt {
- private ConversationTest plugin;
- private Inventory items;
- int tid = -1;
- public InventoryPopup(ConversationTest plugin){
- this.plugin = plugin;
- }
- @Override
- public Prompt acceptInput(ConversationContext context, String input){
- if(tid != -1)
- Bukkit.getScheduler().cancelTask(tid);
- return null;
- }
- @Override
- public String getPromptText(ConversationContext context){
- // Setup
- items = plugin.getServer().createInventory(null, 45, "Items to send");
- Player player = (Player) context.getForWhom();
- // Open Inventory
- if(player.getOpenInventory() != null)
- player.getOpenInventory().close();
- InventoryView v = player.openInventory(items);
- // "Null" should be replaced with something every so often if changed
- Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new InventoryListener(v), 40, 40);
- return "Send any text to leave";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment