Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package net.experimentalidea.quickchatcommands;
- import java.util.ArrayList;
- import java.util.List;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Sound;
- import org.bukkit.entity.Player;
- import org.bukkit.event.EventHandler;
- import org.bukkit.event.EventPriority;
- import org.bukkit.event.Listener;
- import org.bukkit.event.player.PlayerItemHeldEvent;
- import org.bukkit.event.player.PlayerQuitEvent;
- import org.bukkit.event.player.PlayerSwapHandItemsEvent;
- import org.bukkit.plugin.java.JavaPlugin;
- /** QuickChatCommandsDemo for MC/Spigot 1.3.2:
- * A demonstration on how an effective "voice command" system (like in Team Fortress 2 and CS:GO)
- * could be implemented in Minecraft via a server-side plugin with no client-side mods needed.
- * <p>
- * The code written here is to simply demonstrate this concept and doesn't necessarily follow best practices.
- * <p>
- * How it works: <br>
- * Player presses their "Swap Item In Hands" (default 'F') hotkey.
- * The server cancels the event generated by this action and sets the player's hotbar to the first slot.
- * Next a menu is sent to the player with up to 8 options - corresponding with slots 2 through 9 on their hotbar.
- * Once the player presses one of their number keys (from 2 to 9),
- * the server sends a message in chat under the player's name and plays a sound effect.
- * <p>
- * Other Notes: <br>
- * - Implementing this system would mean removing the player's ability to quickly swap an item
- * from their main hand to their off hand via the "Swap Item In Hands" (default 'F') hotkey.
- * Making this something best implemented in a mini-game where the player would typically
- * not be able to use this function anyways.
- * <p>
- * - It probably would be better to show the menu in the scoreboard rather than ruining visibility of the chat by putting it in there.
- * <p>
- * - This relies on dual wielding, i.e. Minecraft versions after 1.8. If your stuck on 1.8,
- * then you may be able to implement this on the packet level for newer Minecraft clients.
- * But players connecting with 1.8 clients wouldn't be able to benefit from this feature.
- * As much as I'd like mainstream servers to pick up on this, I doubt it'd be worth the effort until they
- * 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)
- * <p>
- *
- * I hope you found this little demo project interesting.
- * You can find me online both at YouTube and Twitter.
- * If you end up implementing this concept into your own project,
- * feel free to drop me a message/comment, I'd love to see how you use it.
- * <br> https://www.youtube.com/c/ExperimentalIdea
- * <br> https://twitter.com/ExperimentIdea
- * <p>
- * Video: https://youtu.be/DItXvV38NrM */
- public class QuickChatCommandsDemo extends JavaPlugin implements Listener {
- // Player's which have QCC active.
- private static List<Player> cmdActive = new ArrayList<Player>();
- // The menu message sent to the player when they activate QCC.
- private String cmdMenu = ChatColor.GRAY
- + "2. Help!\n"
- + "3. Thanks!\n"
- + "4. Go!\n"
- + "5. Move Up!\n"
- + "6. Dispenser Here!\n"
- + "7. Good Job!\n"
- + "8. Jeers\n"
- + "9. Negative\n";
- // The message sent by the player when they use QCC.
- private String[] cmdMsg = {"(QCC) Need Help!",
- "(QCC) Thanks Mate!",
- "(QCC) Let's Go!",
- "(QCC) Move That Gear Up!",
- "(QCC) Need A Dispenser Here!",
- "(QCC) Nicely Done!",
- "(QCC) Well This Sucks!",
- "(QCC) We are Screwed!"};
- // The sound played when the player uses QCC.
- // In my video I had a custom resource pack that replaced
- // these sounds with voice lines from The Scout in Team Fortress 2
- // I didn't include that resource pack, since it contained Valve's IP.
- // So I'll leave it up to you to change these sounds to whatever you like.
- private Sound[] cmdSound = {Sound.ENTITY_GHAST_DEATH, // 2. Help
- Sound.BLOCK_ANVIL_LAND, // 3. Thanks
- Sound.BLOCK_ANVIL_USE, // 4. Go
- Sound.ENTITY_BLAZE_DEATH, // 5. Move Up
- Sound.ENTITY_ZOMBIE_PIGMAN_DEATH, // 6. Dispenser Here!
- Sound.ENTITY_SKELETON_DEATH, // 7. Good Job
- Sound.ENTITY_BAT_DEATH, // 8. Jeers
- Sound.ENTITY_CREEPER_DEATH}; // 9. Negative
- @Override
- public void onEnable(){
- Bukkit.getPluginManager().registerEvents(this, this);
- this.getLogger().info("QuickChatCommandsDemo by Duke @ ExperimentalIdea.");
- this.getLogger().info("Youtube: https://www.youtube.com/c/ExperimentalIdea");
- this.getLogger().info("Twitter: https://twitter.com/ExperimentIdea");
- this.getLogger().info("Video: https://youtu.be/DItXvV38NrM");
- }
- @Override
- public void onDisable(){
- cmdActive.clear();
- }
- // When the player presses their "Swap Item In Hands" (default 'F') hotkey.
- // This event is fired, even when both the player's hands are empty.
- @EventHandler(priority = EventPriority.NORMAL)
- public void onPlayerSwapItems(PlayerSwapHandItemsEvent event) {
- event.setCancelled(true);
- Player player = event.getPlayer();
- if(!cmdActive.contains(player)) {
- player.sendMessage(this.cmdMenu);
- player.getInventory().setHeldItemSlot(0);
- cmdActive.add(player);
- }else {
- player.sendMessage(ChatColor.GRAY + "Quick Chat Command cancelled.");
- cmdActive.remove(player);
- }
- }
- // When the player has QCC active, listen for what command they choose.
- @EventHandler(priority = EventPriority.NORMAL)
- public void onPlayer(PlayerItemHeldEvent event) {
- Player player = event.getPlayer();
- if(cmdActive.contains(player)) {
- int slot = event.getNewSlot();
- if(slot < 9 && slot != 0) {
- int index = slot - 1;
- player.chat(this.cmdMsg[index]);
- player.getWorld().playSound(player.getLocation(), this.cmdSound[index], 1.5F, 1.0F);
- cmdActive.remove(player);
- }
- }
- }
- // Cleanup - Don't leak memory by keeping an QCC active player in the list.
- @EventHandler(priority = EventPriority.NORMAL)
- public void onPlayerQuit(PlayerQuitEvent event) {
- cmdActive.remove(event.getPlayer());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment