Skip to content

Commit

Permalink
feat(cmd): Support more commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Jan 26, 2024
1 parent 4b7f52e commit 303c083
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 135 deletions.
11 changes: 9 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>

<project.package>${project.groupId}.plugin.${project.groupId}</project.package>
<deps.core.version>1.0.2</deps.core.version>
<project.package>${project.groupId}.plugin.${project.artifactId}</project.package>
<deps.core.version>1.1.0</deps.core.version>
</properties>
<groupId>com.artformgames</groupId>
<artifactId>tempflight</artifactId>
Expand Down Expand Up @@ -105,6 +105,13 @@

<dependencies>

<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.0</version>
<scope>compile</scope>
</dependency>

<dependency>
<groupId>com.artformgames</groupId>
<artifactId>artcore-api</artifactId>
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/com/artformgames/plugin/tempflight/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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...");
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,7 +17,6 @@
import org.bukkit.entity.Player;

import java.time.Duration;
import java.util.Optional;


@Command(name = "tempflight", aliases = {"tempfly"})
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -26,12 +26,16 @@ public interface PluginConfig extends Configuration {
})
ConfiguredValue<Boolean> 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<Long> 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<Long, String> 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();


}
Original file line number Diff line number Diff line change
@@ -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<BaseComponent[]> list() {
return ConfiguredMessageList.create(getParser())
.whenSend((sender, message) -> message.forEach(m -> sender.spigot().sendMessage(m)));
}
interface COMMANDS extends Messages {

static @NotNull CraftMessageValueBuilder<BaseComponent[]> value() {
return ConfiguredMessage.create(getParser())
.whenSend((sender, message) -> sender.spigot().sendMessage(message));
}
ConfiguredMessageList<BaseComponent[]> WRONG_TIME_FORMAT = Messages.list()
.defaults("&c&lWrong format! &fPlease use &e&o1h,2m,3s &f.")
.build();

ConfiguredMessageList<BaseComponent[]> ALREADY_ENABLED = Messages.list()
.defaults("&fThe player &6%(player) &fis already enabled flying.")
.params("player")
.build();

ConfiguredMessageList<BaseComponent[]> ALREADY_DISABLED = Messages.list()
.defaults("&fThe player &6%(player) &fis not enabled flying.")
.params("player")
.build();

ConfiguredMessageList<BaseComponent[]> 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<BaseComponent[]> ENABLED = Messages.list()
.defaults("&fSuccessfully enabled temp flight for &6%(player) &fwith &e%(time) &fseconds.")
.params("player", "time")
.build();
ConfiguredMessageList<BaseComponent[]> DISABLED = Messages.list()
.defaults("&fSuccessfully disabled temp flight for &6%(player) &f.")
.params("player")
.build();

private static @NotNull BiFunction<CommandSender, String, BaseComponent[]> 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<BaseComponent[]> COOLING = list()
ConfiguredMessageList<BaseComponent[]> COOLING = Messages.list()
.defaults("&fYou need to wait for &e%(time) &fseconds before you can use the timed flight again.")
.params("time")
.build();

ConfiguredMessageList<BaseComponent[]> FLYING = list()
ConfiguredMessageList<BaseComponent[]> FLYING = Messages.list()
.defaults("&fYou have enabled time-limited flying, and the remaining flight time is &e%(time) &fseconds.")
.params("time")
.build();

ConfiguredMessageList<BaseComponent[]> ENABLED = list()
.defaults("&fYou &a&l&Enabled &f for timed flights, and from now on you can fly &e%(time) &fseconds.")
ConfiguredMessageList<BaseComponent[]> 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<BaseComponent[]> DISABLED = list()
ConfiguredMessageList<BaseComponent[]> DISABLED = Messages.list()
.defaults("&fYour timed flight has ended and your flight status has been automatically turned off.")
.build();

ConfiguredMessageList<BaseComponent[]> MODE_CHANGE = list()
ConfiguredMessageList<BaseComponent[]> MODE_CHANGE = Messages.list()
.defaults("&fYou've switched to &eCreative Mode&f, so there's no longer a time limit for flying.")
.build();

ConfiguredMessageList<BaseComponent[]> WILL_BACK = list()
.defaults("&7Note: Teleport back to your current location after the flight is over!")
ConfiguredMessageList<BaseComponent[]> WILL_BACK = Messages.list()
.defaults("&7Note: Will teleport back to your start location after the flight is over!")
.build();

interface COMMANDS extends Configuration {

ConfiguredMessageList<BaseComponent[]> NO_PERMISSION = list()
.defaults("&cYou don't have permission to do this.")
.build();

ConfiguredMessageList<BaseComponent[]> ALREADY_FLYING = list().defaults(
"The player is already flying, and you can't do it again."
).params("player").build();

ConfiguredMessageList<BaseComponent[]> NOT_FLYING = list().defaults(
"The player is not flying, and you can't do it."
).params("player").build();

ConfiguredMessageList<BaseComponent[]> FLY_DISABLED = list().defaults(
"&fThe player &e%(player)&f is not flying, and you can't do it."
).params("player").build();


ConfiguredMessageList<BaseComponent[]> FLY_ENABLED = list().defaults(
"&fSuccessfully enabled temporary flight in %(time) seconds for the player &e%(player)&f."
).params("player", "time").build();

ConfiguredMessageList<BaseComponent[]> COOLING = list().defaults(
"&fThe player %(player) need to wait for &e%(time) &fseconds before flying again."
).params("player", "time").build();

// TIME USAGE
ConfiguredMessageList<BaseComponent[]> TIME_USAGE = list().defaults(
"&fThe time format is incorrect, the format is &e1d2h3m4s&f."
).build();


}


}
Loading

0 comments on commit 303c083

Please sign in to comment.