commit c81cd4459f92b825314678f99a1ec7a7eb6df31b Author: BradBot_1 Date: Sat Oct 7 23:26:02 2023 +0100 init diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..cb25006 --- /dev/null +++ b/pom.xml @@ -0,0 +1,61 @@ + + 4.0.0 + fun.bb1.spigot + fly + 0.0.1-SNAPSHOT + Fly + A simple flying plugin + jar + https://git.bb1.fun/Spigot/Fly + + + 17 + UTF-8 + + + + clean package + Fly + + + org.apache.maven.plugins + maven-compiler-plugin + 3.11.0 + + 17 + 17 + + + + + + + + jitpack.io + https://jitpack.io + + + repo + https://repo.bb1.fun/releases + + + placeholderapi + https://repo.extendedclip.com/content/repositories/placeholderapi/ + + + + + + org.spigotmc + spigot-api + 1.20.1-R0.1-SNAPSHOT + provided + + + me.clip + placeholderapi + 2.11.3 + provided + + + \ No newline at end of file diff --git a/src/main/java/fun/bb1/spigot/fly/EntryPoint.java b/src/main/java/fun/bb1/spigot/fly/EntryPoint.java new file mode 100644 index 0000000..cf9db4f --- /dev/null +++ b/src/main/java/fun/bb1/spigot/fly/EntryPoint.java @@ -0,0 +1,28 @@ +package fun.bb1.spigot.fly; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.java.JavaPlugin; + +import fun.bb1.spigot.fly.config.Config; +import fun.bb1.spigot.fly.connect.PlaceHolderApiSupport; + +public class EntryPoint extends JavaPlugin { + + @Override + public void onLoad() { + this.saveDefaultConfig(); + super.onLoad(); + } + + @Override + public void onEnable() { + this.getLogger().info("Thank you for using flight!"); + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { + new PlaceHolderApiSupport(this); + } + final Config config = new Config(this); + new FlyCommand(this, config); + new FlySpeedCommand(this, config); + } + +} diff --git a/src/main/java/fun/bb1/spigot/fly/FlyCommand.java b/src/main/java/fun/bb1/spigot/fly/FlyCommand.java new file mode 100644 index 0000000..7afdfc5 --- /dev/null +++ b/src/main/java/fun/bb1/spigot/fly/FlyCommand.java @@ -0,0 +1,66 @@ +package fun.bb1.spigot.fly; + +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import fun.bb1.spigot.fly.config.Config; + +final class FlyCommand implements TabExecutor { + + private @NotNull final JavaPlugin plugin; + private @NotNull final Config config; + + FlyCommand(@NotNull final JavaPlugin plugin, @NotNull final Config config) { + this.plugin = plugin; + this.config = config; + final PluginCommand command = this.plugin.getCommand("fly"); + command.setExecutor(this); + command.setTabCompleter(this); + } + + @Override + public final List onTabComplete(CommandSender sender, Command command, String alias, String[] arguments) { + if (sender.hasPermission("fly.toggle.other")) return Bukkit.getOnlinePlayers().stream().map(Player::getName).toList(); + return List.of(); + } + + @Override + public final boolean onCommand(CommandSender sender, Command command, String alias, String[] arguments) { + if (arguments.length != 0) { + if (!sender.hasPermission("fly.toggle.other")) { + this.config.sendToggleOtherFailPermissionMessage(sender); + return true; + } + for (final String playerName : arguments) { + @Nullable final Player player = Bukkit.getPlayer(playerName); + if (player == null) { + this.config.sendToggleOtherFailPlayerMessage(sender); + continue; + } + player.setAllowFlight(!player.getAllowFlight()); + player.setFlying(player.getAllowFlight()); + this.config.sendToggleMessage(player); + this.config.sendToggleOtherMessage(sender, player); + } + return true; + } + if (!(sender instanceof Player player)) { + sender.sendMessage("Cannot toggle flight for non-player"); + return true; + } + player.setAllowFlight(!player.getAllowFlight()); + player.setFlying(player.getAllowFlight()); + this.config.sendToggleMessage(player); + return true; + } + +} diff --git a/src/main/java/fun/bb1/spigot/fly/FlySpeedCommand.java b/src/main/java/fun/bb1/spigot/fly/FlySpeedCommand.java new file mode 100644 index 0000000..f7fc1bf --- /dev/null +++ b/src/main/java/fun/bb1/spigot/fly/FlySpeedCommand.java @@ -0,0 +1,80 @@ +package fun.bb1.spigot.fly; + +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.command.TabExecutor; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import fun.bb1.spigot.fly.config.Config; + +final class FlySpeedCommand implements TabExecutor { + + private @NotNull final JavaPlugin plugin; + private @NotNull final Config config; + + FlySpeedCommand(@NotNull final JavaPlugin plugin, @NotNull final Config config) { + this.plugin = plugin; + this.config = config; + final PluginCommand command = this.plugin.getCommand("flyspeed"); + command.setExecutor(this); + command.setTabCompleter(this); + } + + @Override + public final List onTabComplete(CommandSender sender, Command command, String alias, String[] arguments) { + if (arguments.length <= 1) return List.of("1", "3", "5", "10"); + if (sender.hasPermission("flyspeed.toggle.other")) return Bukkit.getOnlinePlayers().stream().map(Player::getName).toList(); + return List.of(); + } + + @Override + public final boolean onCommand(CommandSender sender, Command command, String alias, String[] arguments) { + if (arguments.length == 0) { + this.config.sendSetSpeedFailValueMessage(sender); + return true; + } + final int value; + try { + value = Integer.parseInt(arguments[0]); + } catch (NumberFormatException e) { + this.config.sendSetSpeedFailValueMessage(sender); + return true; + } + if (value < 0 || value > 10) { + this.config.sendSetSpeedFailValueMessage(sender); + return true; + } + if (arguments.length > 1) { + if (!sender.hasPermission("flyspeed.toggle.other")) { + this.config.sendSetSpeedOtherFailPermissionMessage(sender); + return true; + } + for (int i = 1; i < arguments.length; i++) { + @Nullable final Player player = Bukkit.getPlayer(arguments[i]); + if (player == null) { + this.config.sendSetSpeedOtherFailPlayerMessage(sender); + continue; + } + player.setFlySpeed(value / 10f); + this.config.sendSetSpeedMessage(player); + this.config.sendSetSpeedOtherMessage(sender, player); + } + return true; + } + if (!(sender instanceof Player player)) { + sender.sendMessage("Cannot toggle flight speed for non-player"); + return true; + } + player.setFlySpeed(value / 10f); + this.config.sendSetSpeedMessage(player); + return true; + } + +} diff --git a/src/main/java/fun/bb1/spigot/fly/config/Config.java b/src/main/java/fun/bb1/spigot/fly/config/Config.java new file mode 100644 index 0000000..971b425 --- /dev/null +++ b/src/main/java/fun/bb1/spigot/fly/config/Config.java @@ -0,0 +1,75 @@ +package fun.bb1.spigot.fly.config; + +import org.bukkit.Bukkit; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.ApiStatus.Internal; +import org.jetbrains.annotations.NotNull; + +import me.clip.placeholderapi.PlaceholderAPI; +import net.md_5.bungee.api.ChatMessageType; +import net.md_5.bungee.api.chat.TextComponent; + +@Internal +public final class Config { + + private final @NotNull Plugin plugin; + + @Internal + public Config(final @NotNull Plugin plugin) { + this.plugin = plugin; + } + + private final void sendMessage(@NotNull final CommandSender sender, @NotNull String message) { + if (sender instanceof Player player && this.plugin.getConfig().getBoolean("actionBar", false)) { + player.spigot().sendMessage(ChatMessageType.ACTION_BAR, TextComponent.fromLegacyText(message)); + return; + } + sender.sendMessage(message); + } + + private final @NotNull String placeholder(@NotNull final Player player, @NotNull final String message) { + if (Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) { + return PlaceholderAPI.setPlaceholders(player, message); + } + return message; + } + + public final void sendToggleMessage(@NotNull final Player sender) { + this.sendMessage(sender, this.placeholder(sender, this.plugin.getConfig().getString(sender.getAllowFlight() ? "fly.self.enabled" : "fly.self.disabled", "§0[§a✓§0]§r You have toggled flight"))); + } + + public final void sendToggleOtherMessage(@NotNull final CommandSender sender, @NotNull final Player recipient) { + this.sendMessage(sender, this.placeholder(recipient, this.plugin.getConfig().getString(recipient.getAllowFlight() ? "fly.other.enabled" : "fly.other.disabled", "§0[§a✓§0]§r You have toggled their flight"))); + } + + public final void sendToggleOtherFailPermissionMessage(@NotNull final CommandSender sender) { + this.sendMessage(sender, this.plugin.getConfig().getString("fly.other.nopermission", "§0[§c!§0]§r You don't have permission to do that")); + } + + public final void sendToggleOtherFailPlayerMessage(@NotNull final CommandSender sender) { + this.sendMessage(sender, this.plugin.getConfig().getString("fly.other.notfound", "§0[§c!§0]§r Invalid player name")); + } + + public final void sendSetSpeedMessage(@NotNull final Player sender) { + this.sendMessage(sender, this.placeholder(sender, this.plugin.getConfig().getString("flyspeed.self.set", "§0[§a✓§0]§r Your flight speed hsa been changed"))); + } + + public final void sendSetSpeedOtherMessage(@NotNull final CommandSender sender, @NotNull final Player recipient) { + this.sendMessage(sender, this.placeholder(recipient, this.plugin.getConfig().getString("flyspeed.other.set", "§0[§a✓§0]§r Their flight speed has been set"))); + } + + public final void sendSetSpeedOtherFailPermissionMessage(@NotNull final CommandSender sender) { + this.sendMessage(sender, this.plugin.getConfig().getString("flyspeed.other.nopermission", "§0[§c!§0]§r You don't have permission to do that")); + } + + public final void sendSetSpeedOtherFailPlayerMessage(@NotNull final CommandSender sender) { + this.sendMessage(sender, this.plugin.getConfig().getString("flyspeed.other.notfound", "§0[§c!§0]§r Invalid player name")); + } + + public final void sendSetSpeedFailValueMessage(@NotNull final CommandSender sender) { + this.sendMessage(sender, this.plugin.getConfig().getString("flyspeed.noValue", "§0[§c!§0]§r Invalid player name")); + } + +} diff --git a/src/main/java/fun/bb1/spigot/fly/connect/PlaceHolderApiSupport.java b/src/main/java/fun/bb1/spigot/fly/connect/PlaceHolderApiSupport.java new file mode 100644 index 0000000..a5656db --- /dev/null +++ b/src/main/java/fun/bb1/spigot/fly/connect/PlaceHolderApiSupport.java @@ -0,0 +1,53 @@ +package fun.bb1.spigot.fly.connect; + +import org.bukkit.OfflinePlayer; +import org.bukkit.entity.Player; +import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.ApiStatus.Internal; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import me.clip.placeholderapi.expansion.PlaceholderExpansion; + +@Internal +public final class PlaceHolderApiSupport extends PlaceholderExpansion { + + private final @NotNull JavaPlugin plugin; + + @Internal + public PlaceHolderApiSupport(final @NotNull JavaPlugin plugin) { + this.plugin = plugin; + plugin.getLogger().info("Placeholder detected adding support!"); + } + + @Override + public final @NotNull String getIdentifier() { + return "fly"; + } + + @Override + public final @NotNull String getAuthor() { + return "BradBot_1#2042"; + } + + @Override + public final @NotNull String getVersion() { + return "1.0.0"; + } + + @Override + public final @Nullable String onPlaceholderRequest(final Player player, final @NotNull String params) { + return super.onRequest(player, params); + } + + @Override + public final @Nullable String onRequest(final OfflinePlayer player, final @NotNull String params) { + return switch(params.toLowerCase()) { + case "active", "on" -> Boolean.toString(player.isOnline() && ((Player)player).getAllowFlight()); + case "inactive", "off" -> Boolean.toString(!player.isOnline() || !((Player)player).getAllowFlight()); + case "speed" -> Float.toString(player.isOnline() ? 0 : ((Player)player).getFlySpeed()); + default -> null; + }; + } + +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml new file mode 100644 index 0000000..21ac790 --- /dev/null +++ b/src/main/resources/config.yml @@ -0,0 +1,37 @@ +# +# Thank you for using Fly! +# Make sure you have the most upto date version from modrinth: +# https://modrinth.com/plugin/fly +# +# Placeholders are supported! +# + +fly: + # The message sent to the player who's flight's been toggled + self: + enabled: §0[§a✓§0]§r You can now fly + disabled: §0[§c!§0]§r You can no longer fly + # The message sent to the player who toggles another players flight + other: + enabled: §0[§a✓§0]§r Their flight has now been enabled + disabled: §0[§a✓§0]§r Their flight has not been enabled + # Sent when the player doesn't have fly.toggle.other + nopermission: §0[§c!§0]§r You don't have permission to do that + # Sent when the player doesn't exist + notfound: §0[§c!§0]§r Unkown player + +flyspeed: + self: + # If PlayerHolderApi is installed you can use %fly_speed% to insert the players fly speed + set: §0[§a✓§0]§r Your flight speed has been changed + other: + set: §0[§a✓§0]§r Their flight speed has been changed + # Sent when the player doesn't have flyspeed.toggle.other + nopermission: §0[§c!§0]§r You don't have permission to do that + # Sent when the player doesn't exist + notfound: §0[§c!§0]§r Unkown player + noValue: §0[§c!§0]§r Please enter a valid value to set the flight speed to (0-10) + +# If to display all messages in the actionbar rather than in the chat +# Console commands will naturally still appear in console +actionBar: false \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml new file mode 100644 index 0000000..3108412 --- /dev/null +++ b/src/main/resources/plugin.yml @@ -0,0 +1,35 @@ +main: fun.bb1.spigot.fly.Entrypoint +name: Fly +version: '1.0.0' +description: 'A simple flying plugin' +api-version: '1.20' +api: '1.20' +authors: [ BradBot_1#2042 ] +prefix: FLY +website: https://modrinth.com/plugin/fly +load: POSTWORLD +softdepend: [ PlaceholderAPI ] +commands: + fly: + aliases: [ "flight" ] + description: "Toggles flight" + permission: fly.toggle.self + usage: "/fly " + flyspeed: + aliases: [ "fspeed", "fs" ] + description: "Set your flight speed" + permission: flyspeed.toggle.self + usage: "/flyspeed [amount] " +permissions: + fly.toggle.self: + default: op + description: "If the player can access /fly" + fly.toggle.other: + default: op + description: "Toggle other players ability to fly with /fly" + flyspeed.toggle.self: + default: op + description: "If the player can access /flyspeed" + flyspeed.toggle.other: + default: op + description: "Toggle other players flight speed" \ No newline at end of file