From 303c0832f17e898c30dbd8c6c3ad4e6e1ee2d9f8 Mon Sep 17 00:00:00 2001 From: carm Date: Sat, 27 Jan 2024 00:46:00 +0800 Subject: [PATCH] feat(cmd): Support more commands. --- pom.xml | 11 +- .../artformgames/plugin/tempflight/Main.java | 8 +- .../command/TempFlightCommands.java | 55 ++++----- .../plugin/tempflight/conf/PluginConfig.java | 20 ++-- .../tempflight/conf/PluginMessages.java | 109 ++++++------------ .../tempflight/manager/FlightManager.java | 42 +++---- .../plugin/tempflight/user/FlightAccount.java | 2 +- 7 files changed, 112 insertions(+), 135 deletions(-) diff --git a/pom.xml b/pom.xml index 14bc582..30b3000 100644 --- a/pom.xml +++ b/pom.xml @@ -10,8 +10,8 @@ UTF-8 UTF-8 - ${project.groupId}.plugin.${project.groupId} - 1.0.2 + ${project.groupId}.plugin.${project.artifactId} + 1.1.0 com.artformgames tempflight @@ -105,6 +105,13 @@ + + org.bstats + bstats-bukkit + 3.0.0 + compile + + com.artformgames artcore-api diff --git a/src/main/java/com/artformgames/plugin/tempflight/Main.java b/src/main/java/com/artformgames/plugin/tempflight/Main.java index b818404..74b2a55 100644 --- a/src/main/java/com/artformgames/plugin/tempflight/Main.java +++ b/src/main/java/com/artformgames/plugin/tempflight/Main.java @@ -2,6 +2,7 @@ import cc.carm.lib.easyplugin.EasyPlugin; import cc.carm.lib.mineconfiguration.bukkit.MineConfiguration; +import com.artformgames.core.ArtCore; import com.artformgames.core.utils.GHUpdateChecker; import com.artformgames.plugin.tempflight.command.TempFlightCommands; import com.artformgames.plugin.tempflight.conf.PluginConfig; @@ -10,7 +11,6 @@ import com.artformgames.plugin.tempflight.manager.FlightManager; import dev.rollczi.litecommands.LiteCommands; import dev.rollczi.litecommands.bukkit.LiteCommandsBukkit; -import dev.rollczi.litecommands.schematic.SchematicFormat; import org.bstats.bukkit.Metrics; public class Main extends EasyPlugin { @@ -44,11 +44,7 @@ protected boolean initialize() { registerListener(new TempFlyListener()); log("Register commands..."); - this.commands = LiteCommandsBukkit.builder() - .missingPermission((i, p, chain) -> PluginMessages.COMMANDS.NO_PERMISSION.send(i.sender())) - .commands(new TempFlightCommands()) - .build(); - + this.commands = ArtCore.createCommand().commands(new TempFlightCommands()).build(); if (PluginConfig.METRICS.getNotNull()) { log("Initializing bStats..."); diff --git a/src/main/java/com/artformgames/plugin/tempflight/command/TempFlightCommands.java b/src/main/java/com/artformgames/plugin/tempflight/command/TempFlightCommands.java index 9c23bf5..893f66b 100644 --- a/src/main/java/com/artformgames/plugin/tempflight/command/TempFlightCommands.java +++ b/src/main/java/com/artformgames/plugin/tempflight/command/TempFlightCommands.java @@ -1,11 +1,11 @@ package com.artformgames.plugin.tempflight.command; import com.artformgames.core.ArtCore; +import com.artformgames.core.utils.TimeStringUtils; import com.artformgames.plugin.tempflight.Main; import com.artformgames.plugin.tempflight.conf.PluginMessages; import com.artformgames.plugin.tempflight.manager.FlightManager; import com.artformgames.plugin.tempflight.user.FlightAccount; -import com.artformgames.plugin.tempflight.utils.TimeUtils; import dev.rollczi.litecommands.annotations.argument.Arg; import dev.rollczi.litecommands.annotations.command.Command; import dev.rollczi.litecommands.annotations.context.Context; @@ -17,7 +17,6 @@ import org.bukkit.entity.Player; import java.time.Duration; -import java.util.Optional; @Command(name = "tempflight", aliases = {"tempfly"}) @@ -26,48 +25,50 @@ public class TempFlightCommands { @Execute(name = "start") - void start(@Context CommandSender sender, - @Arg Player target, @Arg String duration, @OptionalArg Boolean back) { - back = Optional.ofNullable(back).orElse(false); + @Description("Start a temporary flight for a player") + void start(@Context CommandSender sender, @Arg Player target, @Arg String duration, @OptionalArg Boolean back) { + long time = TimeStringUtils.toMilliSecPlus(duration); + if (time < 0) { + PluginMessages.COMMANDS.WRONG_TIME_FORMAT.send(sender); + return; + } + FlightAccount data = ArtCore.getHandler(target, FlightAccount.class); if (data.isTempFlying()) { - PluginMessages.COMMANDS.ALREADY_FLYING.send(sender, target.getName()); + PluginMessages.COMMANDS.ALREADY_ENABLED.send(sender, target.getName()); PluginMessages.FLYING.send(target, data.getRemainMillis() / 1000); return; } - FlightManager manager = Main.getFlightManager(); - long cooldown = manager.getCooldownMillis(target); + FlightManager flightManager = Main.getFlightManager(); + long cooldown = flightManager.getCooldownMillis(target); if (cooldown > 0) { PluginMessages.COMMANDS.COOLING.send(sender, target.getName(), cooldown / 1000); PluginMessages.COOLING.send(target, cooldown / 1000); return; } - Duration time = TimeUtils.parseDuration(duration); - if (time.isZero() || time.isNegative()) { - PluginMessages.COMMANDS.TIME_USAGE.send(sender); - return; - } - - if (manager.startFly(target, time, back)) { - PluginMessages.COMMANDS.FLY_ENABLED.send(sender, target.getName(), time.toSeconds()); - PluginMessages.ENABLED.send(target, duration); - if (back) PluginMessages.WILL_BACK.send(target); + if (flightManager.startFly(target, Duration.ofMillis(time), Boolean.TRUE.equals(back))) { + PluginMessages.COMMANDS.ENABLED.send(sender, target.getName(), (time / 1000)); + PluginMessages.ENABLED.send(target, time / 1000); + if (Boolean.TRUE.equals(back)) PluginMessages.WILL_BACK.send(target); } else { - PluginMessages.COMMANDS.ALREADY_FLYING.send(sender, target.getName()); + PluginMessages.COMMANDS.ALREADY_ENABLED.send(sender, target.getName()); } + } - @Execute(name = "stop") - void stop(@Context CommandSender sender, @Arg Player player) { - FlightManager manager = Main.getFlightManager(); - if (manager.endFly(player)) { - PluginMessages.COMMANDS.FLY_DISABLED.send(sender, player.getName()); - PluginMessages.DISABLED.send(player); - } else { - PluginMessages.COMMANDS.NOT_FLYING.send(sender, player.getName()); + @Execute(name = "stop", aliases = "shut") + @Description("Stop temporary flight for a player.") + void stop(@Context CommandSender sender, @Arg Player target) { + FlightManager flightManager = Main.getFlightManager(); + FlightAccount data = ArtCore.getHandler(target, FlightAccount.class); + if (!data.isTempFlying() || !flightManager.endFly(target)) { + PluginMessages.COMMANDS.ALREADY_DISABLED.send(sender, target.getName()); + return; } + PluginMessages.COMMANDS.DISABLED.send(sender, target.getName()); + PluginMessages.DISABLED.send(target); } } diff --git a/src/main/java/com/artformgames/plugin/tempflight/conf/PluginConfig.java b/src/main/java/com/artformgames/plugin/tempflight/conf/PluginConfig.java index aab0975..4829bf6 100644 --- a/src/main/java/com/artformgames/plugin/tempflight/conf/PluginConfig.java +++ b/src/main/java/com/artformgames/plugin/tempflight/conf/PluginConfig.java @@ -2,9 +2,9 @@ import cc.carm.lib.configuration.core.Configuration; import cc.carm.lib.configuration.core.annotation.HeaderComment; -import cc.carm.lib.configuration.core.value.ConfigValue; +import cc.carm.lib.configuration.core.value.impl.ConfigValueMap; +import cc.carm.lib.configuration.core.value.type.ConfiguredMap; import cc.carm.lib.configuration.core.value.type.ConfiguredValue; -import com.artformgames.core.utils.TimeStringUtils; public interface PluginConfig extends Configuration { @@ -26,12 +26,16 @@ public interface PluginConfig extends Configuration { }) ConfiguredValue CHECK_UPDATE = ConfiguredValue.of(Boolean.class, true); - @HeaderComment("The cooldown time for the player to use the flight command, format: 1d,2h,3m,4s") - ConfiguredValue COOLDOWN = ConfigValue.builder() - .asValue(Long.class).fromString() - .parseValue((v, d) -> TimeStringUtils.toMilliSecPlus(v)) - .serializeValue(TimeStringUtils::toTimeString) - .defaults(60000L).build(); + @HeaderComment({ + "The cooldown time for the player to use the flight command of each permissions", + "format -> millisecond: permission" + }) + ConfiguredMap COOLDOWN = ConfigValueMap.builderOf(Long.class, String.class) + .asLinkedMap().fromString().parseKey(Long::parseUnsignedLong).parseValue(Object::toString) + .defaults(m -> { + m.put(60000L, "tempflight.use"); + m.put(30000L, "group.vip"); + }).build(); } diff --git a/src/main/java/com/artformgames/plugin/tempflight/conf/PluginMessages.java b/src/main/java/com/artformgames/plugin/tempflight/conf/PluginMessages.java index 45df6f4..8614cfb 100644 --- a/src/main/java/com/artformgames/plugin/tempflight/conf/PluginMessages.java +++ b/src/main/java/com/artformgames/plugin/tempflight/conf/PluginMessages.java @@ -1,102 +1,69 @@ package com.artformgames.plugin.tempflight.conf; -import cc.carm.lib.configuration.core.Configuration; -import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageListBuilder; -import cc.carm.lib.mineconfiguration.bukkit.builder.message.CraftMessageValueBuilder; -import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessage; import cc.carm.lib.mineconfiguration.bukkit.value.ConfiguredMessageList; -import de.themoep.minedown.MineDown; -import me.clip.placeholderapi.PlaceholderAPI; +import com.artformgames.core.conf.Messages; import net.md_5.bungee.api.chat.BaseComponent; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; -import java.util.function.BiFunction; +public interface PluginMessages extends Messages { -public interface PluginMessages extends Configuration { - static @NotNull CraftMessageListBuilder list() { - return ConfiguredMessageList.create(getParser()) - .whenSend((sender, message) -> message.forEach(m -> sender.spigot().sendMessage(m))); - } + interface COMMANDS extends Messages { - static @NotNull CraftMessageValueBuilder value() { - return ConfiguredMessage.create(getParser()) - .whenSend((sender, message) -> sender.spigot().sendMessage(message)); - } + ConfiguredMessageList WRONG_TIME_FORMAT = Messages.list() + .defaults("&c&lWrong format! &fPlease use &e&o1h,2m,3s &f.") + .build(); + + ConfiguredMessageList ALREADY_ENABLED = Messages.list() + .defaults("&fThe player &6%(player) &fis already enabled flying.") + .params("player") + .build(); + + ConfiguredMessageList ALREADY_DISABLED = Messages.list() + .defaults("&fThe player &6%(player) &fis not enabled flying.") + .params("player") + .build(); + + ConfiguredMessageList COOLING = Messages.list() + .defaults( + "&fThe player &6%(player) &fneed to wait for &e%(time) &fseconds before he can use the timed flight again." + ).params("player", "time").build(); + + ConfiguredMessageList ENABLED = Messages.list() + .defaults("&fSuccessfully enabled temp flight for &6%(player) &fwith &e%(time) &fseconds.") + .params("player", "time") + .build(); + ConfiguredMessageList DISABLED = Messages.list() + .defaults("&fSuccessfully disabled temp flight for &6%(player) &f.") + .params("player") + .build(); - private static @NotNull BiFunction getParser() { - return (sender, message) -> { - if (sender == null) return MineDown.parse(message); - if (sender instanceof Player player) { - return MineDown.parse(PlaceholderAPI.setPlaceholders(player, message)); - } else { - return MineDown.parse(message); - } - }; } - ConfiguredMessageList COOLING = list() + ConfiguredMessageList COOLING = Messages.list() .defaults("&fYou need to wait for &e%(time) &fseconds before you can use the timed flight again.") .params("time") .build(); - ConfiguredMessageList FLYING = list() + ConfiguredMessageList FLYING = Messages.list() .defaults("&fYou have enabled time-limited flying, and the remaining flight time is &e%(time) &fseconds.") .params("time") .build(); - ConfiguredMessageList ENABLED = list() - .defaults("&fYou &a&l&Enabled &f for timed flights, and from now on you can fly &e%(time) &fseconds.") + ConfiguredMessageList ENABLED = Messages.list() + .defaults("&fYou are &a&lEnabled &f for timed flights, and from now on you can fly &e%(time) &fseconds.") .params("time") .build(); - ConfiguredMessageList DISABLED = list() + ConfiguredMessageList DISABLED = Messages.list() .defaults("&fYour timed flight has ended and your flight status has been automatically turned off.") .build(); - ConfiguredMessageList MODE_CHANGE = list() + ConfiguredMessageList MODE_CHANGE = Messages.list() .defaults("&fYou've switched to &eCreative Mode&f, so there's no longer a time limit for flying.") .build(); - ConfiguredMessageList WILL_BACK = list() - .defaults("&7Note: Teleport back to your current location after the flight is over!") + ConfiguredMessageList WILL_BACK = Messages.list() + .defaults("&7Note: Will teleport back to your start location after the flight is over!") .build(); - interface COMMANDS extends Configuration { - - ConfiguredMessageList NO_PERMISSION = list() - .defaults("&cYou don't have permission to do this.") - .build(); - - ConfiguredMessageList ALREADY_FLYING = list().defaults( - "The player is already flying, and you can't do it again." - ).params("player").build(); - - ConfiguredMessageList NOT_FLYING = list().defaults( - "The player is not flying, and you can't do it." - ).params("player").build(); - - ConfiguredMessageList FLY_DISABLED = list().defaults( - "&fThe player &e%(player)&f is not flying, and you can't do it." - ).params("player").build(); - - - ConfiguredMessageList FLY_ENABLED = list().defaults( - "&fSuccessfully enabled temporary flight in %(time) seconds for the player &e%(player)&f." - ).params("player", "time").build(); - - ConfiguredMessageList COOLING = list().defaults( - "&fThe player %(player) need to wait for &e%(time) &fseconds before flying again." - ).params("player", "time").build(); - - // TIME USAGE - ConfiguredMessageList TIME_USAGE = list().defaults( - "&fThe time format is incorrect, the format is &e1d2h3m4s&f." - ).build(); - - - } - } diff --git a/src/main/java/com/artformgames/plugin/tempflight/manager/FlightManager.java b/src/main/java/com/artformgames/plugin/tempflight/manager/FlightManager.java index 260c05d..a0bb2a9 100644 --- a/src/main/java/com/artformgames/plugin/tempflight/manager/FlightManager.java +++ b/src/main/java/com/artformgames/plugin/tempflight/manager/FlightManager.java @@ -22,22 +22,19 @@ public class FlightManager { * Used to record the last time the user enabled an ad-hoc flight * The UUID is the user's UUID, and the time is the timestamp when the user theoretically ended the flight */ - protected final @NotNull Map lastTime = new HashMap<>(); + protected final @NotNull Map cooldownTime = new HashMap<>(); public static @NotNull BukkitRunnable repeatRunnable = new BukkitRunnable() { @Override public void run() { - Bukkit.getOnlinePlayers().forEach(Main.getFlightManager()::checkFlight); + FlightManager flightManager = Main.getFlightManager(); + Bukkit.getOnlinePlayers().forEach(flightManager::checkFlight); Main.getFlightManager().purgeCooldownData(); } }; public FlightManager(Main main) { - ArtCore.getUserManager().registerHandler( - main, FlightAccount.class, - FlightAccount::new, FlightAccount::new, - (k, a) -> lastTime.remove(k.uuid()) - ); + ArtCore.getUserManager().registerHandler(main, FlightAccount.class, FlightAccount::new, FlightAccount::new); repeatRunnable.runTaskTimerAsynchronously(Main.getInstance(), 100L, 20L); } @@ -54,7 +51,12 @@ public boolean startFly(@NotNull Player player, @NotNull Duration duration, bool if (account.isTempFlying()) return false; long end = account.start(duration, teleportBack ? player.getLocation() : null); - lastTime.put(player.getUniqueId(), end); + long cooldown = PluginConfig.COOLDOWN.entrySet().stream() + .filter(e -> player.hasPermission(e.getValue())) + .mapToLong(Map.Entry::getKey).min().orElse(-1); + if (cooldown > 0) { + cooldownTime.put(player.getUniqueId(), end + cooldown); + } player.setAllowFlight(true); player.setFlying(true); @@ -65,8 +67,15 @@ public boolean endFly(@NotNull Player player) { FlightAccount account = getAccount(player); if (!account.isTempFlying()) return false; - account.reset(); Optional.ofNullable(account.getStartLocation()).ifPresent(player::teleport); + account.reset(); + + long cooldown = PluginConfig.COOLDOWN.entrySet().stream() + .filter(e -> player.hasPermission(e.getValue())) + .mapToLong(Map.Entry::getKey).min().orElse(-1); + if (cooldown > 0) { + cooldownTime.put(player.getUniqueId(), System.currentTimeMillis() + cooldown); + } if (player.getAllowFlight()) { player.setFlying(false); @@ -93,22 +102,15 @@ public boolean isCoolingDown(@NotNull Player player) { } public long getCooldownMillis(@NotNull Player player) { - Long time = lastTime.get(player.getUniqueId()); + Long time = cooldownTime.get(player.getUniqueId()); if (time == null) return 0; - long cooldown = PluginConfig.COOLDOWN.getNotNull(); - if (cooldown <= 0) return 0; - - return cooldown - (System.currentTimeMillis() - time); + return time - System.currentTimeMillis(); } public void purgeCooldownData() { - long cooldown = PluginConfig.COOLDOWN.getNotNull(); - if (cooldown > 0) { - lastTime.entrySet().removeIf(entry -> System.currentTimeMillis() - entry.getValue() > cooldown); - } else { - lastTime.clear(); - } + long v = System.currentTimeMillis(); + cooldownTime.entrySet().removeIf(entry -> v > entry.getValue()); } } diff --git a/src/main/java/com/artformgames/plugin/tempflight/user/FlightAccount.java b/src/main/java/com/artformgames/plugin/tempflight/user/FlightAccount.java index f2f8afa..ca9c42c 100644 --- a/src/main/java/com/artformgames/plugin/tempflight/user/FlightAccount.java +++ b/src/main/java/com/artformgames/plugin/tempflight/user/FlightAccount.java @@ -38,7 +38,7 @@ public boolean isTempFlying() { public long getRemainMillis() { if (this.endMillis == null) return 0; - else return (this.endMillis) - System.currentTimeMillis(); + else return this.endMillis - System.currentTimeMillis(); }