From 8ce2d6256eff4b878f95a178905b9d402b4ef9cd Mon Sep 17 00:00:00 2001 From: james <75051061+notsceptor@users.noreply.github.com> Date: Wed, 29 May 2024 01:01:38 +0100 Subject: [PATCH] Implemented the /expel command --- .../dev/plex/extras/command/ExpelCommand.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/main/java/dev/plex/extras/command/ExpelCommand.java diff --git a/src/main/java/dev/plex/extras/command/ExpelCommand.java b/src/main/java/dev/plex/extras/command/ExpelCommand.java new file mode 100644 index 0000000..e685315 --- /dev/null +++ b/src/main/java/dev/plex/extras/command/ExpelCommand.java @@ -0,0 +1,100 @@ +package dev.plex.extras.command; + +import dev.plex.command.PlexCommand; +import dev.plex.command.annotation.CommandParameters; +import dev.plex.command.annotation.CommandPermissions; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import org.bukkit.Location; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +@CommandParameters(name = "expel", description = "Pushes away nearby players", usage = "/expel ", aliases = "push") +@CommandPermissions(permission = "plex.tfmextras.expel") +public class ExpelCommand extends PlexCommand +{ + @Override + protected Component execute(@NotNull CommandSender sender, @Nullable Player player, @NotNull String[] args) + { + double radius = 20.0; + double strength = 5.0; + + if (args.length > 0) + { + try + { + radius = Double.parseDouble(args[0]); + } + catch (NumberFormatException ignored) + { + return usage(); + } + } + + if (args.length > 1) + { + try + { + strength = Double.parseDouble(args[1]); + } + catch (NumberFormatException ignored) + { + return usage(); + } + } + + List pushedPlayers = new ArrayList<>(); + + final Vector senderPos = player.getLocation().toVector(); + final List players = player.getWorld().getPlayers(); + + for (final Player target : players) + { + if (target.equals(player)) + { + continue; + } + + final Location targetPos = target.getLocation(); + final Vector targetPosVec = targetPos.toVector(); + + if (targetPosVec.distanceSquared(senderPos) < (radius * radius)) + { + target.setFlying(false); + + target.getWorld().createExplosion(targetPos, 0.0f, false); + target.setVelocity(targetPosVec.subtract(senderPos).normalize().multiply(strength)); + + pushedPlayers.add(target.getName()); + } + } + + if (!pushedPlayers.isEmpty()) + { + return MiniMessage.miniMessage().deserialize("Pushed away players: " + String.join(", ", pushedPlayers)); + } + + return null; + } + + @Override + public @NotNull List smartTabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) + { + if (args.length == 1) + { + return Collections.singletonList(""); + } + else if (args.length == 2) + { + return Collections.singletonList(""); + } + return Collections.emptyList(); + } +}