Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package motion.motionplugin;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandExecutor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.command.PluginCommand;
- import org.bukkit.entity.Entity;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.util.Vector;
- import java.util.Arrays;
- import java.util.List;
- import java.util.UUID;
- import java.util.stream.Collectors;
- public class MotionPlugin extends JavaPlugin implements CommandExecutor {
- @Override
- public void onEnable() {
- PluginCommand command = getCommand("motion");
- if (command != null) {
- command.setExecutor(this);
- } else {
- getLogger().warning("Failed to register the motion command. The command object is null.");
- }
- }
- @Override
- public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
- if (!(sender instanceof Player)) {
- sender.sendMessage("Only players can use this command!");
- return true;
- }
- Player player = (Player) sender;
- // Check if the command has the correct number of arguments
- if (args.length < 4) {
- sender.sendMessage("Usage: /motion <selector> <dx> <dy> <dz>");
- return true;
- }
- // Parse the motion values for dx, dy, and dz using parseMotionValue method
- Vector dxVector = parseMotionValue(player, args[1]);
- Vector dyVector = parseMotionValue(player, args[2]);
- Vector dzVector = parseMotionValue(player, args[3]);
- // Combine the parsed motion values into a single motion vector
- Vector motionVector = new Vector(dxVector.getX(), dyVector.getY(), dzVector.getZ());
- // Filter entities based on the selector
- List<Entity> entities = parseSelector(player, args[0]);
- if (entities.isEmpty()) {
- sender.sendMessage("No entities found for the selector: " + args[0]);
- return true;
- }
- // Apply motion to each entity
- entities.forEach(entity -> {
- Vector currentVelocity = entity.getVelocity();
- entity.setVelocity(currentVelocity.add(motionVector));
- });
- sender.sendMessage("Motion applied successfully to " + entities.size() + " entities!");
- return true;
- }
- private List<Entity> parseSelector(Player player, String selector) {
- List<String> parts = Arrays.asList(selector.split(" "));
- String baseSelector = parts.get(0);
- String[] options = parts.size() > 1 ? parts.get(1).split(",") : new String[0];
- List<Entity> entities = player.getServer().selectEntities(player, baseSelector);
- for (String option : options) {
- if (option.startsWith("uuid=")) {
- String uuidStr = option.substring(5);
- try {
- UUID uuid = UUID.fromString(uuidStr);
- Entity entity = Bukkit.getEntity(uuid);
- if (entity != null) {
- entities.add(entity);
- }
- } catch (IllegalArgumentException ignored) {
- // Invalid UUID format, ignore
- }
- } else if (option.startsWith("distance=")) {
- try {
- double distance = Double.parseDouble(option.substring(9));
- entities = entities.stream()
- .filter(entity -> entity.getLocation().distance(player.getLocation()) <= distance)
- .collect(Collectors.toList());
- } catch (NumberFormatException e) {
- // Invalid distance format, ignore
- }
- } else if (option.startsWith("limit=")) {
- try {
- int limit = Integer.parseInt(option.substring(6));
- entities = entities.stream().limit(limit).collect(Collectors.toList());
- } catch (NumberFormatException e) {
- // Invalid limit format, ignore
- }
- } else if (option.startsWith("tag=")) {
- String tag = option.substring(4);
- entities = entities.stream()
- .filter(entity -> entity.getScoreboardTags().contains(tag))
- .collect(Collectors.toList());
- }
- }
- return entities;
- }
- private Vector parseMotionValue(Player player, String value) {
- getLogger().info("Parsing motion value: " + value);
- if (value.isEmpty()) {
- // If the value is empty, return a zero vector
- getLogger().info("Motion value is empty, returning zero vector.");
- return new Vector(0, 0, 0);
- }
- if (value.equals("~ ~ ~")) {
- // Handle ~ ~ ~ as the player's current position with an additional block upward
- Location playerLocation = player.getLocation();
- getLogger().info("Motion value is '~ ~ ~', returning player's current position: " + playerLocation);
- return playerLocation.toVector().add(new Vector(0, 1, 0));
- }
- if (value.matches("^\\^+$")) {
- // If the value contains only ^ characters, return a zero vector
- getLogger().info("Motion value consists solely of '^' characters, returning zero vector.");
- return new Vector(0, 0, 0);
- }
- if (value.startsWith("^")) {
- // Handle ^ ^ ^ for relative motion
- getLogger().info("Motion value starts with '^', parsing as relative coordinate.");
- return parseRelativeCoordinate(player.getLocation(), value);
- }
- if (value.startsWith("~")) {
- // Handle ~ ~ ~ as the player's current position
- getLogger().info("Motion value starts with '~', parsing as relative coordinate.");
- return parseRelativeCoordinate(player.getLocation(), value);
- }
- // Numeric value or mixed relative coordinates
- getLogger().info("Motion value is numeric or mixed relative coordinates, parsing components.");
- String[] components = value.split("\\s+");
- if (components.length == 3) {
- try {
- double x = Double.parseDouble(components[0]);
- double y = Double.parseDouble(components[1]);
- double z = Double.parseDouble(components[2]);
- getLogger().info("Parsed numeric motion values: x=" + x + ", y=" + y + ", z=" + z);
- return new Vector(x, y, z);
- } catch (NumberFormatException e) {
- getLogger().warning("Invalid numeric motion values: " + value);
- return new Vector(0, 0, 0);
- }
- } else {
- getLogger().warning("Invalid motion value: " + value);
- return new Vector(0, 0, 0);
- }
- }
- private Vector parseRelativeCoordinate(Location playerLocation, String value) {
- getLogger().info("Handling relative coordinate value: " + value);
- // Get player's rotation angles
- double yaw = Math.toRadians(playerLocation.getYaw());
- double pitch = Math.toRadians(playerLocation.getPitch());
- // Calculate motion offsets based on the input value
- double offsetX = 0.0, offsetY = 0.0, offsetZ = 0.0;
- if (value.equals("~")) {
- // Handle "~" as the player's current position
- getLogger().info("Handling relative coordinate '~' as the player's current position.");
- return playerLocation.toVector();
- } else if (value.equals("^")) {
- // If only "^" is provided, calculate motion along the direction player is facing
- offsetX = -Math.sin(yaw) * Math.cos(pitch);
- offsetY = Math.sin(pitch);
- offsetZ = Math.cos(yaw) * Math.cos(pitch);
- } else if (value.startsWith("^")) {
- // Handle ^ ^ ^ for relative motion
- String[] components = value.split("\\^");
- for (int i = 1; i < components.length; i++) {
- if (!components[i].isEmpty()) {
- double offset = Double.parseDouble(components[i]);
- switch (i - 1) {
- case 0:
- offsetY = offset;
- break;
- case 1:
- offsetZ = offset;
- break;
- case 2:
- offsetX = offset;
- break;
- }
- }
- }
- } else {
- getLogger().warning("Invalid relative coordinate value: " + value);
- }
- getLogger().info("Offset X: " + offsetX + ", Offset Y: " + offsetY + ", Offset Z: " + offsetZ);
- return new Vector(offsetX, offsetY, offsetZ);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement