Solved Vectors and getting direction

Discussion in 'Plugin Development' started by Xp10d3, Oct 8, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Xp10d3

    Hello. I'm making a very, VERY simple plugin that launches a player a certain direction with a certain force. Basically simulating a player hitting them. However, I'm making this plugin to practice block clutches and want to return a player's location and the direction their looking at. So the code I currently have is this:
    Code:java
    1.  
    2. player.setVelocity(player.getEyeLocation().getDirection().multiply(launchMultiplier));
    3.  

    I know that player.getEyeLocation() returns a location, but how would I get a specific direction? Ex. North, South, East, West, etc.
    Code:
    Code:java
    1.  
    2. package eltik.endran.launch;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.scheduler.BukkitRunnable;
    11.  
    12. public class Commands implements CommandExecutor {
    13.  
    14. // Get the main class
    15. private Core core;
    16.  
    17. // Constructor. Register all commands
    18. public Commands(Core core) {
    19. this.core = core;
    20. Bukkit.getPluginCommand("launch").setExecutor(this);
    21. Bukkit.getPluginCommand("launchdir").setExecutor(this);
    22. }
    23.  
    24. @Override
    25. public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
    26. // If the sender of a command is NOT a player...
    27. if (!(sender instanceof Player)) {
    28. sender.sendMessage("You must be a player to access this command!");
    29. return false;
    30. }
    31.  
    32. // Get the player
    33. Player player = (Player) sender;
    34.  
    35. // When the player sends a command to check whether a player has KA...
    36. if (cmd.getName().equalsIgnoreCase("launch") && player.hasPermission("launch.launch")) {
    37. if (args.length == 3 || args.length == 4) {
    38. int time = Integer.parseInt(args[1]);
    39. int launchMultiplier = Integer.parseInt(args[2]);
    40. if (args[4].isEmpty()) {
    41. core.otherLog("Player " + player.getName() + " have launched themselves. Arguments:");
    42. core.otherLog("args[0]: " + args[0]);
    43. core.otherLog("args[1]: " + args[1]);
    44. core.otherLog("args[2]: " + args[2]);
    45. core.otherLog("Time waited: " + time);
    46. core.otherLog("Launch Multiplier: " + launchMultiplier);
    47. new BukkitRunnable() {
    48. @Override
    49. public void run() {
    50. player.setVelocity(player.getEyeLocation().getDirection().multiply(launchMultiplier));
    51. }
    52. }.runTaskLater(core, time * 20);
    53. } else {
    54. if (player.hasPermission("launch.launchothers")) {
    55. Player target = Bukkit.getServer().getPlayer(args[3]);
    56. if (target == null) {
    57. player.sendMessage(ChatColor.RED + "That player isn't online!");
    58. core.otherLog("Player tried to launch another player, but they weren't online. args[3]: " + args[3]);
    59. } else {
    60. core.otherLog("Player " + player.getName() + " have launched another player. Arguments:");
    61. core.otherLog("args[0]: " + args[0]);
    62. core.otherLog("args[1]: " + args[1]);
    63. core.otherLog("args[2]: " + args[2]);
    64. core.otherLog("args[3]: " + args[3]);
    65. core.otherLog("Time waited: " + time);
    66. core.otherLog("Launch Multiplier: " + launchMultiplier);
    67. core.otherLog("Target: " + target.getName());
    68. new BukkitRunnable() {
    69. @Override
    70. public void run() {
    71. player.setVelocity(target.getEyeLocation().getDirection().multiply(launchMultiplier));
    72. }
    73. }.runTaskLater(core, time * 20);
    74. }
    75. } else if (!player.hasPermission("launch.launchothers")) {
    76. player.sendMessage(ChatColor.RED + "You don't have permission!");
    77. }
    78. }
    79. } else if (args.length < 1){
    80. player.sendMessage(ChatColor.RED + "Not enough arguments! Correct usage: /launch <seconds> <multiplier> [player]");
    81. } else if (args.length > 2) {
    82. player.sendMessage(ChatColor.RED + "Too many arguments! Correct usage: /launch <seconds> <multiplier> [player]");
    83. }
    84. } else if (!player.hasPermission("launch.launch")) {
    85. player.sendMessage(ChatColor.RED + "You don't have permission!");
    86. }
    87.  
    88. return false;
    89. }
    90. }
    91.  
    92.  


    EDIT: Never mind, figured it out. New code:
    Code:java
    1.  
    2. package eltik.endran.launch;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.command.Command;
    7. import org.bukkit.command.CommandExecutor;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.scheduler.BukkitRunnable;
    11. import org.bukkit.util.Vector;
    12.  
    13. public class Commands implements CommandExecutor {
    14.  
    15. // Get the main class
    16. private Core core;
    17.  
    18. // Constructor. Register all commands
    19. public Commands(Core core) {
    20. this.core = core;
    21. Bukkit.getPluginCommand("launch").setExecutor(this);
    22. Bukkit.getPluginCommand("launchdir").setExecutor(this);
    23. }
    24.  
    25. @Override
    26. public boolean onCommand(CommandSender sender, Command cmd, String lable, String[] args) {
    27. // If the sender of a command is NOT a player...
    28. /*
    29.   if (!(sender instanceof Player)) {
    30.   sender.sendMessage("You must be a player to access this command!");
    31.   return false;
    32.   }
    33.   */
    34.  
    35. // Get the player
    36. Player player = (Player) sender;
    37.  
    38. // When the player sends a command to check whether a player has KA...
    39. if (cmd.getName().equalsIgnoreCase("launch") && player.hasPermission("launch.launch")) {
    40.  
    41. if (args.length == 3 || args.length == 4) {
    42. int time = Integer.parseInt(args[1]);
    43. int launchMultiplier = Integer.parseInt(args[2]);
    44.  
    45. if (args[4].isEmpty()) {
    46. double pitch = ((90 - player.getLocation().getPitch()) * Math.PI) / 180;
    47. double yaw = ((player.getLocation().getYaw() + 90 + 180) * Math.PI) / 180;
    48. double x = Math.sin(pitch) * Math.cos(yaw);
    49. double y = Math.sin(pitch) * Math.sin(yaw);
    50. double z = Math.cos(pitch);
    51. Vector vector = new Vector(x, z, y);
    52.  
    53. core.otherLog("Player " + player.getName() + " have launched themselves. Arguments:");
    54. core.otherLog("args[0]: " + args[0]);
    55. core.otherLog("args[1]: " + args[1]);
    56. core.otherLog("args[2]: " + args[2]);
    57. core.otherLog("Time waited: " + time);
    58. core.otherLog("Launch Multiplier: " + launchMultiplier);
    59. new BukkitRunnable() {
    60. @Override
    61. public void run() {
    62. player.setVelocity(vector.multiply(launchMultiplier));
    63. }
    64. }.runTaskLater(core, time * 20);
    65. } else {
    66. if (player.hasPermission("launch.launchothers")) {
    67. Player target = Bukkit.getServer().getPlayer(args[4]);
    68.  
    69. double pitch = ((90 - target.getLocation().getPitch()) * Math.PI) / 180;
    70. double yaw = ((target.getLocation().getYaw() + 90 + 180) * Math.PI) / 180;
    71. double x = Math.sin(pitch) * Math.cos(yaw);
    72. double y = Math.sin(pitch) * Math.sin(yaw);
    73. double z = Math.cos(pitch);
    74. Vector vector = new Vector(x, z, y);
    75.  
    76. core.otherLog("Player " + player.getName() + " have launched another player. Arguments:");
    77. core.otherLog("args[0]: " + args[0]);
    78. core.otherLog("args[1]: " + args[1]);
    79. core.otherLog("args[2]: " + args[2]);
    80. core.otherLog("args[3]: " + args[3]);
    81. core.otherLog("Time waited: " + time);
    82. core.otherLog("Launch Multiplier: " + launchMultiplier);
    83. core.otherLog("Target: " + target.getName());
    84. new BukkitRunnable() {
    85. @Override
    86. public void run() {
    87. player.setVelocity(vector.multiply(launchMultiplier));
    88. }
    89. }.runTaskLater(core, time * 20);
    90. } else if (!player.hasPermission("launch.launchothers")) {
    91. player.sendMessage(ChatColor.RED + "You don't have permission!");
    92. }
    93. }
    94. } else if (args.length < 1){
    95. player.sendMessage(ChatColor.RED + "Not enough arguments! Correct usage: /launch <seconds> <multiplier> [player]");
    96. } else if (args.length > 2) {
    97. player.sendMessage(ChatColor.RED + "Too many arguments! Correct usage: /launch <seconds> <multiplier> [player]");
    98. }
    99. } else if (!player.hasPermission("launch.launch")) {
    100. player.sendMessage(ChatColor.RED + "You don't have permission!");
    101. }
    102.  
    103. return false;
    104. }
    105. }
    106.  
     
    Last edited: Oct 8, 2020
Thread Status:
Not open for further replies.

Share This Page