From e1a6b5e58776e2f700feced4aa9cf115f098ff94 Mon Sep 17 00:00:00 2001 From: EnZaXD <60033407+FlorianMichael@users.noreply.github.com> Date: Tue, 1 Aug 2023 16:15:46 +0200 Subject: [PATCH] Implemented Fuckoff command from TFM back (#16) * Implemented Fuckoff command back from TFM * Actual use the radius field * Change default radius to 15 * Renamed Fuckoff#pushPlayers * Use command API properly --- .../java/me/totalfreedom/datura/Datura.java | 14 ++++- .../datura/cmd/FuckoffCommand.java | 62 +++++++++++++++++++ .../totalfreedom/datura/features/Fuckoff.java | 59 ++++++++++++++++++ 3 files changed, 134 insertions(+), 1 deletion(-) create mode 100644 Datura/src/main/java/me/totalfreedom/datura/cmd/FuckoffCommand.java create mode 100644 Datura/src/main/java/me/totalfreedom/datura/features/Fuckoff.java diff --git a/Datura/src/main/java/me/totalfreedom/datura/Datura.java b/Datura/src/main/java/me/totalfreedom/datura/Datura.java index 60e39a1..57097cb 100644 --- a/Datura/src/main/java/me/totalfreedom/datura/Datura.java +++ b/Datura/src/main/java/me/totalfreedom/datura/Datura.java @@ -1,6 +1,7 @@ package me.totalfreedom.datura; import me.totalfreedom.base.Patchwork; +import me.totalfreedom.datura.features.Fuckoff; import me.totalfreedom.datura.features.CommandSpy; import me.totalfreedom.datura.punishment.Cager; import me.totalfreedom.datura.punishment.Halter; @@ -21,6 +22,7 @@ public class Datura extends JavaPlugin // Features private final CommandSpy commandSpy = new CommandSpy(); + private final Fuckoff fuckoff = new Fuckoff(); @Override public void onEnable() @@ -38,6 +40,10 @@ public class Datura extends JavaPlugin .getRegistrations() .getServiceTaskRegistry() .registerService(SubscriptionProvider.syncService(this, cager)); + Patchwork.getInstance() + .getRegistrations() + .getServiceTaskRegistry() + .registerService(SubscriptionProvider.syncService(this, fuckoff)); Bukkit.getPluginManager() .registerEvents(halter, this); @@ -65,7 +71,13 @@ public class Datura extends JavaPlugin return cager; } - public CommandSpy getCommandSpy() { + public CommandSpy getCommandSpy() + { return commandSpy; } + + public Fuckoff getFuckoff() + { + return fuckoff; + } } diff --git a/Datura/src/main/java/me/totalfreedom/datura/cmd/FuckoffCommand.java b/Datura/src/main/java/me/totalfreedom/datura/cmd/FuckoffCommand.java new file mode 100644 index 0000000..5b41ef4 --- /dev/null +++ b/Datura/src/main/java/me/totalfreedom/datura/cmd/FuckoffCommand.java @@ -0,0 +1,62 @@ +package me.totalfreedom.datura.cmd; + +import me.totalfreedom.base.Shortcuts; +import me.totalfreedom.command.Commander; +import me.totalfreedom.command.annotation.*; +import me.totalfreedom.datura.Datura; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; + +@Info(name = "fuckoff", description = "You'll never even see it coming - repeatedly push players away from you until command is untoggled.", usage = "/fuckoff [radius]") +@Permissive(perm = "datura.fuckoff", onlyPlayers = true) +@Completion(args = {"on", "off"}, index = 0) +@Completion(args = {"[radius]"}, index = 1) +public class FuckoffCommand extends Commander +{ + private final Datura plugin = Shortcuts.provideModule(Datura.class); + + /** + * Initializes this command object. The provided {@link JavaPlugin} should be the plugin which contains the + * command. + *

+ * This constructor will automatically register all subcommands and completions for this command. It will also + * automatically infer all required information from the provided {@link Info} and {@link Permissive} annotations. + * + * @param plugin The plugin which contains this command. + */ + public FuckoffCommand(@NotNull JavaPlugin plugin) + { + super(plugin); + } + + @Subcommand(permission = "datura.fuckoff", args = {String.class}) + public void fuckOff(final Player sender, final String toggle) + { + execute(sender, toggle, 15); + } + + @Subcommand(permission = "datura.fuckoff", args = {String.class, Integer.class}) + public void fuckOff(final Player sender, final String toggle, final Integer radius) + { + execute(sender, toggle, radius); + } + + private void execute(final Player sender, final String toggle, final int radius) + { + if (toggle.equalsIgnoreCase("on")) + { + plugin.getFuckoff(). + add(sender, radius); + + sender.sendPlainMessage("FuckOff enabled."); + } else if (toggle.equalsIgnoreCase("off")) + { + plugin.getFuckoff(). + remove(sender); + + sender.sendPlainMessage("FuckOff disabled."); + } + } +} diff --git a/Datura/src/main/java/me/totalfreedom/datura/features/Fuckoff.java b/Datura/src/main/java/me/totalfreedom/datura/features/Fuckoff.java new file mode 100644 index 0000000..dd20579 --- /dev/null +++ b/Datura/src/main/java/me/totalfreedom/datura/features/Fuckoff.java @@ -0,0 +1,59 @@ +package me.totalfreedom.datura.features; + +import me.totalfreedom.service.Service; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +public class Fuckoff extends Service +{ + private final Map players = new ConcurrentHashMap<>(); + + public Fuckoff() + { + super("fuckoff-service"); + } + + public void add(final Player player, final int radius) + { + players.put(player.getUniqueId(), radius); + } + + public void remove(final Player player) + { + players.remove(player.getUniqueId()); + } + + @Override + public void tick() + { + for (Map.Entry entry : players.entrySet()) + { + final var player = Bukkit.getPlayer(entry.getKey()); + + if (player == null) + { + players.remove(entry.getKey()); + continue; + } + + pushPlayers(player, entry.getValue()); + } + } + + private void pushPlayers(@NotNull final Player player, final int radius) + { + Bukkit.getOnlinePlayers() + .stream() + .filter(onlinePlayer -> onlinePlayer.getLocation().distanceSquared(player.getLocation()) < (radius * radius)) + .forEach(onlinePlayer -> { + onlinePlayer.setVelocity( + player.getLocation().toVector() + .add(onlinePlayer.getLocation().toVector()).normalize().multiply(radius) + ); + }); + } +}