This commit is contained in:
BradBot_1 2023-10-07 23:26:02 +01:00
commit c81cd4459f
8 changed files with 435 additions and 0 deletions

61
pom.xml Normal file
View file

@ -0,0 +1,61 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>fun.bb1.spigot</groupId>
<artifactId>fly</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Fly</name>
<description>A simple flying plugin</description>
<packaging>jar</packaging>
<url>https://git.bb1.fun/Spigot/Fly</url>
<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<defaultGoal>clean package</defaultGoal>
<finalName>Fly</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>repo</id>
<url>https://repo.bb1.fun/releases</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20.1-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.3</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View file

@ -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);
}
}

View file

@ -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<String> 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;
}
}

View file

@ -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<String> 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;
}
}

View file

@ -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"));
}
}

View file

@ -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;
};
}
}

View file

@ -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

View file

@ -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 <player(s)>"
flyspeed:
aliases: [ "fspeed", "fs" ]
description: "Set your flight speed"
permission: flyspeed.toggle.self
usage: "/flyspeed [amount] <player(s)>"
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"