Guest User

QuickChatCommandsDemo for MC/Spigot 1.3.2

a guest
Apr 27th, 2019
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.21 KB | None | 0 0
  1. package net.experimentalidea.quickchatcommands;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import org.bukkit.Bukkit;
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.Sound;
  9. import org.bukkit.entity.Player;
  10. import org.bukkit.event.EventHandler;
  11. import org.bukkit.event.EventPriority;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.player.PlayerItemHeldEvent;
  14. import org.bukkit.event.player.PlayerQuitEvent;
  15. import org.bukkit.event.player.PlayerSwapHandItemsEvent;
  16. import org.bukkit.plugin.java.JavaPlugin;
  17.  
  18. /** QuickChatCommandsDemo for MC/Spigot 1.3.2:
  19.  * A demonstration on how an effective "voice command" system (like in Team Fortress 2 and CS:GO)
  20.  * could be implemented in Minecraft via a server-side plugin with no client-side mods needed.
  21.  * <p>
  22.  * The code written here is to simply demonstrate this concept and doesn't necessarily follow best practices.
  23.  * <p>
  24.  * How it works: <br>
  25.  * Player presses their "Swap Item In Hands" (default 'F') hotkey.
  26.  * The server cancels the event generated by this action and sets the player's hotbar to the first slot.
  27.  * Next a menu is sent to the player with up to 8 options - corresponding with slots 2 through 9 on their hotbar.
  28.  * Once the player presses one of their number keys (from 2 to 9),
  29.  * the server sends a message in chat under the player's name and plays a sound effect.
  30.  * <p>
  31.  * Other Notes: <br>
  32.  * - Implementing this system would mean removing the player's ability to quickly swap an item
  33.  *   from their main hand to their off hand via the "Swap Item In Hands" (default 'F') hotkey.
  34.  *   Making this something best implemented in a mini-game where the player would typically
  35.  *   not be able to use this function anyways.
  36.  *  <p>
  37.  * - It probably would be better to show the menu in the scoreboard rather than ruining visibility of the chat by putting it in there.
  38.  * <p>
  39.  * - This relies on dual wielding, i.e. Minecraft versions after 1.8. If your stuck on 1.8,
  40.  *   then you may be able to implement this on the packet level for newer Minecraft clients.
  41.  *   But players connecting with 1.8 clients wouldn't be able to benefit from this feature.
  42.  *   As much as I'd like mainstream servers to pick up on this, I doubt it'd be worth the effort until they
  43.  *   update to the latest version of the game. (Ha. Fat chance I know - with 1.8 being the go-to PVP version of the game)
  44.  * <p>
  45.  *
  46.  * I hope you found this little demo project interesting.
  47.  * You can find me online both at YouTube and Twitter.
  48.  * If you end up implementing this concept into your own project,
  49.  * feel free to drop me a message/comment, I'd love to see how you use it.
  50.  * <br> https://www.youtube.com/c/ExperimentalIdea
  51.  * <br> https://twitter.com/ExperimentIdea
  52.  * <p>
  53.  * Video: https://youtu.be/DItXvV38NrM */
  54. public class QuickChatCommandsDemo extends JavaPlugin implements Listener {
  55.  
  56.     // Player's which have QCC active.
  57.     private static List<Player> cmdActive = new ArrayList<Player>();
  58.    
  59.     // The menu message sent to the player when they activate QCC.
  60.     private String cmdMenu = ChatColor.GRAY
  61.                              + "2. Help!\n"
  62.                              + "3. Thanks!\n"
  63.                              + "4. Go!\n"
  64.                              + "5. Move Up!\n"
  65.                              + "6. Dispenser Here!\n"
  66.                              + "7. Good Job!\n"
  67.                              + "8. Jeers\n"
  68.                              + "9. Negative\n";
  69.    
  70.     // The message sent by the player when they use QCC.
  71.     private String[] cmdMsg =  {"(QCC) Need Help!",
  72.                                 "(QCC) Thanks Mate!",
  73.                                 "(QCC) Let's Go!",
  74.                                 "(QCC) Move That Gear Up!",
  75.                                 "(QCC) Need A Dispenser Here!",
  76.                                 "(QCC) Nicely Done!",
  77.                                 "(QCC) Well This Sucks!",
  78.                                 "(QCC) We are Screwed!"};
  79.    
  80.     // The sound played when the player uses QCC.
  81.     // In my video I had a custom resource pack that replaced
  82.     // these sounds with voice lines from The Scout in Team Fortress 2
  83.     // I didn't include that resource pack, since it contained Valve's IP.
  84.     // So I'll leave it up to you to change these sounds to whatever you like.
  85.     private Sound[] cmdSound = {Sound.ENTITY_GHAST_DEATH, // 2. Help
  86.                                 Sound.BLOCK_ANVIL_LAND, // 3. Thanks
  87.                                 Sound.BLOCK_ANVIL_USE, // 4. Go
  88.                                 Sound.ENTITY_BLAZE_DEATH, // 5. Move Up
  89.                                 Sound.ENTITY_ZOMBIE_PIGMAN_DEATH, // 6. Dispenser Here!
  90.                                 Sound.ENTITY_SKELETON_DEATH, // 7. Good Job
  91.                                 Sound.ENTITY_BAT_DEATH, // 8. Jeers
  92.                                 Sound.ENTITY_CREEPER_DEATH}; // 9. Negative
  93.    
  94.     @Override
  95.     public void onEnable(){
  96.         Bukkit.getPluginManager().registerEvents(this, this);
  97.         this.getLogger().info("QuickChatCommandsDemo by Duke @ ExperimentalIdea.");
  98.         this.getLogger().info("Youtube: https://www.youtube.com/c/ExperimentalIdea");
  99.         this.getLogger().info("Twitter: https://twitter.com/ExperimentIdea");
  100.         this.getLogger().info("Video: https://youtu.be/DItXvV38NrM");
  101.     }
  102.    
  103.     @Override
  104.     public void onDisable(){
  105.         cmdActive.clear();
  106.     }
  107.    
  108.     // When the player presses their "Swap Item In Hands" (default 'F') hotkey.
  109.     // This event is fired, even when both the player's hands are empty.
  110.     @EventHandler(priority = EventPriority.NORMAL)
  111.     public void onPlayerSwapItems(PlayerSwapHandItemsEvent event) {
  112.         event.setCancelled(true);
  113.        
  114.         Player player = event.getPlayer();
  115.        
  116.         if(!cmdActive.contains(player)) {
  117.             player.sendMessage(this.cmdMenu);
  118.             player.getInventory().setHeldItemSlot(0);
  119.             cmdActive.add(player);         
  120.         }else {
  121.             player.sendMessage(ChatColor.GRAY + "Quick Chat Command cancelled.");
  122.             cmdActive.remove(player);
  123.         }
  124.     }
  125.    
  126.     // When the player has QCC active, listen for what command they choose.
  127.     @EventHandler(priority = EventPriority.NORMAL)
  128.     public void onPlayer(PlayerItemHeldEvent event) {
  129.         Player player = event.getPlayer();
  130.        
  131.         if(cmdActive.contains(player)) {
  132.             int slot = event.getNewSlot();
  133.             if(slot < 9 && slot != 0) {
  134.                 int index = slot - 1;
  135.                 player.chat(this.cmdMsg[index]);
  136.                 player.getWorld().playSound(player.getLocation(), this.cmdSound[index], 1.5F, 1.0F);
  137.                 cmdActive.remove(player);
  138.             }
  139.         }
  140.     }
  141.    
  142.     // Cleanup - Don't leak memory by keeping an QCC active player in the list.
  143.     @EventHandler(priority = EventPriority.NORMAL)
  144.     public void onPlayerQuit(PlayerQuitEvent event) {
  145.         cmdActive.remove(event.getPlayer());
  146.     }
  147.    
  148. }
Advertisement
Add Comment
Please, Sign In to add comment