Advertisement
Guest User

motion plugin

a guest
Feb 27th, 2024
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.96 KB | None | 0 0
  1. package motion.motionplugin;
  2.  
  3. import org.bukkit.Bukkit;
  4. import org.bukkit.Location;
  5. import org.bukkit.command.Command;
  6. import org.bukkit.command.CommandExecutor;
  7. import org.bukkit.command.CommandSender;
  8. import org.bukkit.command.PluginCommand;
  9. import org.bukkit.entity.Entity;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.plugin.java.JavaPlugin;
  12. import org.bukkit.util.Vector;
  13.  
  14. import java.util.Arrays;
  15. import java.util.List;
  16. import java.util.UUID;
  17. import java.util.stream.Collectors;
  18.  
  19. public class MotionPlugin extends JavaPlugin implements CommandExecutor {
  20.  
  21. @Override
  22. public void onEnable() {
  23. PluginCommand command = getCommand("motion");
  24. if (command != null) {
  25. command.setExecutor(this);
  26. } else {
  27. getLogger().warning("Failed to register the motion command. The command object is null.");
  28. }
  29. }
  30.  
  31. @Override
  32. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  33. if (!(sender instanceof Player)) {
  34. sender.sendMessage("Only players can use this command!");
  35. return true;
  36. }
  37.  
  38. Player player = (Player) sender;
  39.  
  40. // Check if the command has the correct number of arguments
  41. if (args.length < 4) {
  42. sender.sendMessage("Usage: /motion <selector> <dx> <dy> <dz>");
  43. return true;
  44. }
  45.  
  46. // Parse the motion values for dx, dy, and dz using parseMotionValue method
  47. Vector dxVector = parseMotionValue(player, args[1]);
  48. Vector dyVector = parseMotionValue(player, args[2]);
  49. Vector dzVector = parseMotionValue(player, args[3]);
  50.  
  51. // Combine the parsed motion values into a single motion vector
  52. Vector motionVector = new Vector(dxVector.getX(), dyVector.getY(), dzVector.getZ());
  53.  
  54. // Filter entities based on the selector
  55. List<Entity> entities = parseSelector(player, args[0]);
  56.  
  57. if (entities.isEmpty()) {
  58. sender.sendMessage("No entities found for the selector: " + args[0]);
  59. return true;
  60. }
  61.  
  62. // Apply motion to each entity
  63. entities.forEach(entity -> {
  64. Vector currentVelocity = entity.getVelocity();
  65. entity.setVelocity(currentVelocity.add(motionVector));
  66. });
  67. sender.sendMessage("Motion applied successfully to " + entities.size() + " entities!");
  68. return true;
  69. }
  70.  
  71. private List<Entity> parseSelector(Player player, String selector) {
  72. List<String> parts = Arrays.asList(selector.split(" "));
  73. String baseSelector = parts.get(0);
  74. String[] options = parts.size() > 1 ? parts.get(1).split(",") : new String[0];
  75.  
  76. List<Entity> entities = player.getServer().selectEntities(player, baseSelector);
  77. for (String option : options) {
  78. if (option.startsWith("uuid=")) {
  79. String uuidStr = option.substring(5);
  80. try {
  81. UUID uuid = UUID.fromString(uuidStr);
  82. Entity entity = Bukkit.getEntity(uuid);
  83. if (entity != null) {
  84. entities.add(entity);
  85. }
  86. } catch (IllegalArgumentException ignored) {
  87. // Invalid UUID format, ignore
  88. }
  89. } else if (option.startsWith("distance=")) {
  90. try {
  91. double distance = Double.parseDouble(option.substring(9));
  92. entities = entities.stream()
  93. .filter(entity -> entity.getLocation().distance(player.getLocation()) <= distance)
  94. .collect(Collectors.toList());
  95. } catch (NumberFormatException e) {
  96. // Invalid distance format, ignore
  97. }
  98. } else if (option.startsWith("limit=")) {
  99. try {
  100. int limit = Integer.parseInt(option.substring(6));
  101. entities = entities.stream().limit(limit).collect(Collectors.toList());
  102. } catch (NumberFormatException e) {
  103. // Invalid limit format, ignore
  104. }
  105. } else if (option.startsWith("tag=")) {
  106. String tag = option.substring(4);
  107. entities = entities.stream()
  108. .filter(entity -> entity.getScoreboardTags().contains(tag))
  109. .collect(Collectors.toList());
  110. }
  111. }
  112.  
  113. return entities;
  114. }
  115.  
  116. private Vector parseMotionValue(Player player, String value) {
  117. getLogger().info("Parsing motion value: " + value);
  118. if (value.isEmpty()) {
  119. // If the value is empty, return a zero vector
  120. getLogger().info("Motion value is empty, returning zero vector.");
  121. return new Vector(0, 0, 0);
  122. }
  123. if (value.equals("~ ~ ~")) {
  124. // Handle ~ ~ ~ as the player's current position with an additional block upward
  125. Location playerLocation = player.getLocation();
  126. getLogger().info("Motion value is '~ ~ ~', returning player's current position: " + playerLocation);
  127. return playerLocation.toVector().add(new Vector(0, 1, 0));
  128. }
  129. if (value.matches("^\\^+$")) {
  130. // If the value contains only ^ characters, return a zero vector
  131. getLogger().info("Motion value consists solely of '^' characters, returning zero vector.");
  132. return new Vector(0, 0, 0);
  133. }
  134. if (value.startsWith("^")) {
  135. // Handle ^ ^ ^ for relative motion
  136. getLogger().info("Motion value starts with '^', parsing as relative coordinate.");
  137. return parseRelativeCoordinate(player.getLocation(), value);
  138. }
  139. if (value.startsWith("~")) {
  140. // Handle ~ ~ ~ as the player's current position
  141. getLogger().info("Motion value starts with '~', parsing as relative coordinate.");
  142. return parseRelativeCoordinate(player.getLocation(), value);
  143. }
  144. // Numeric value or mixed relative coordinates
  145. getLogger().info("Motion value is numeric or mixed relative coordinates, parsing components.");
  146. String[] components = value.split("\\s+");
  147. if (components.length == 3) {
  148. try {
  149. double x = Double.parseDouble(components[0]);
  150. double y = Double.parseDouble(components[1]);
  151. double z = Double.parseDouble(components[2]);
  152. getLogger().info("Parsed numeric motion values: x=" + x + ", y=" + y + ", z=" + z);
  153. return new Vector(x, y, z);
  154. } catch (NumberFormatException e) {
  155. getLogger().warning("Invalid numeric motion values: " + value);
  156. return new Vector(0, 0, 0);
  157. }
  158. } else {
  159. getLogger().warning("Invalid motion value: " + value);
  160. return new Vector(0, 0, 0);
  161. }
  162. }
  163.  
  164. private Vector parseRelativeCoordinate(Location playerLocation, String value) {
  165. getLogger().info("Handling relative coordinate value: " + value);
  166.  
  167. // Get player's rotation angles
  168. double yaw = Math.toRadians(playerLocation.getYaw());
  169. double pitch = Math.toRadians(playerLocation.getPitch());
  170.  
  171. // Calculate motion offsets based on the input value
  172. double offsetX = 0.0, offsetY = 0.0, offsetZ = 0.0;
  173.  
  174. if (value.equals("~")) {
  175. // Handle "~" as the player's current position
  176. getLogger().info("Handling relative coordinate '~' as the player's current position.");
  177. return playerLocation.toVector();
  178. } else if (value.equals("^")) {
  179. // If only "^" is provided, calculate motion along the direction player is facing
  180. offsetX = -Math.sin(yaw) * Math.cos(pitch);
  181. offsetY = Math.sin(pitch);
  182. offsetZ = Math.cos(yaw) * Math.cos(pitch);
  183. } else if (value.startsWith("^")) {
  184. // Handle ^ ^ ^ for relative motion
  185. String[] components = value.split("\\^");
  186. for (int i = 1; i < components.length; i++) {
  187. if (!components[i].isEmpty()) {
  188. double offset = Double.parseDouble(components[i]);
  189. switch (i - 1) {
  190. case 0:
  191. offsetY = offset;
  192. break;
  193. case 1:
  194. offsetZ = offset;
  195. break;
  196. case 2:
  197. offsetX = offset;
  198. break;
  199. }
  200. }
  201. }
  202. } else {
  203. getLogger().warning("Invalid relative coordinate value: " + value);
  204. }
  205.  
  206. getLogger().info("Offset X: " + offsetX + ", Offset Y: " + offsetY + ", Offset Z: " + offsetZ);
  207.  
  208. return new Vector(offsetX, offsetY, offsetZ);
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement