turt2live

Untitled

Jun 29th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.16 KB | None | 0 0
  1. package com.turt2live.conversations;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.ChatColor;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.command.CommandSender;
  7. import org.bukkit.conversations.ConversationContext;
  8. import org.bukkit.conversations.ConversationFactory;
  9. import org.bukkit.conversations.ConversationPrefix;
  10. import org.bukkit.conversations.Prompt;
  11. import org.bukkit.conversations.StringPrompt;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.event.EventHandler;
  14. import org.bukkit.event.Listener;
  15. import org.bukkit.event.inventory.InventoryClickEvent;
  16. import org.bukkit.inventory.Inventory;
  17. import org.bukkit.inventory.InventoryView;
  18. import org.bukkit.inventory.ItemStack;
  19. import org.bukkit.plugin.java.JavaPlugin;
  20.  
  21. public class ConversationTest extends JavaPlugin implements Listener {
  22.  
  23.     @Override
  24.     public void onEnable(){
  25.         getServer().getPluginManager().registerEvents(this, this);
  26.     }
  27.  
  28.     @Override
  29.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args){
  30.         if(command.getName().equalsIgnoreCase("cinventory")){ // Conversation Test
  31.             if(sender instanceof Player){
  32.                 // Build conversation
  33.                 ConversationFactory factory = new ConversationFactory(this)
  34.                         .withModality(false) // No effect if true or false
  35.                         .withLocalEcho(false)
  36.                         .withFirstPrompt(new InventoryPopup(this))
  37.                         .thatExcludesNonPlayersWithMessage("Nope!")
  38.                         .withPrefix(new ConversationPrefix(){
  39.                             @Override
  40.                             public String getPrefix(ConversationContext context){
  41.                                 return ChatColor.GRAY + "[CONVO] " + ChatColor.WHITE;
  42.                             }
  43.                         })
  44.                         .withEscapeSequence("exit");
  45.                 factory.buildConversation((Player) sender).begin();
  46.             }else{
  47.                 sender.sendMessage("Run as player please");
  48.             }
  49.         }else if(command.getName().equalsIgnoreCase("dinventory")){ // Out-of-conversation Test
  50.             if(sender instanceof Player){
  51.                 // Setup
  52.                 Inventory items = getServer().createInventory(null, 45);
  53.                 Player player = (Player) sender;
  54.  
  55.                 // Open inventory
  56.                 if(player.getOpenInventory() != null)
  57.                     player.getOpenInventory().close();
  58.                 InventoryView v = player.openInventory(items);
  59.  
  60.                 // Wait for items, "null" should be replaced with something every so often
  61.                 Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new InventoryListener(v), 40, 40);
  62.             }else{
  63.                 sender.sendMessage("Run as player please");
  64.             }
  65.         }else if(command.getName().equalsIgnoreCase("killalltasks")){
  66.             getServer().getScheduler().cancelTasks(this);
  67.             sender.sendMessage("Done");
  68.         }else{
  69.             return false;
  70.         }
  71.         return true;
  72.     }
  73.  
  74.     @EventHandler
  75.     public void onClick(InventoryClickEvent event){
  76.         InventoryView inventory = event.getView();
  77.         for(ItemStack i : inventory.getTopInventory().getContents()){
  78.             System.out.println(i);
  79.         }
  80.         System.out.println("-----");
  81.     }
  82.  
  83.     public static class InventoryListener implements Runnable {
  84.  
  85.         private InventoryView inventory;
  86.  
  87.         public InventoryListener(InventoryView items){
  88.             this.inventory = items;
  89.         }
  90.  
  91.         @Override
  92.         public void run(){
  93.             for(ItemStack i : inventory.getTopInventory().getContents()){
  94.                 System.out.println(i);
  95.             }
  96.             System.out.println("-----");
  97.         }
  98.     }
  99.  
  100.     public static class InventoryPopup extends StringPrompt {
  101.  
  102.         private ConversationTest plugin;
  103.         private Inventory items;
  104.         int tid = -1;
  105.  
  106.         public InventoryPopup(ConversationTest plugin){
  107.             this.plugin = plugin;
  108.         }
  109.  
  110.         @Override
  111.         public Prompt acceptInput(ConversationContext context, String input){
  112.             if(tid != -1)
  113.                 Bukkit.getScheduler().cancelTask(tid);
  114.             return null;
  115.         }
  116.  
  117.         @Override
  118.         public String getPromptText(ConversationContext context){
  119.             // Setup
  120.             items = plugin.getServer().createInventory(null, 45, "Items to send");
  121.             Player player = (Player) context.getForWhom();
  122.  
  123.             // Open Inventory
  124.             if(player.getOpenInventory() != null)
  125.                 player.getOpenInventory().close();
  126.             InventoryView v = player.openInventory(items);
  127.  
  128.             // "Null" should be replaced with something every so often if changed
  129.             Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new InventoryListener(v), 40, 40);
  130.             return "Send any text to leave";
  131.         }
  132.  
  133.     }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment