Skip to content

Commit

Permalink
feat(command): Finished commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
CarmJos committed Jan 5, 2024
1 parent 6b2199f commit 5a02339
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 84 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/artformgames/plugin/tempflight/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.artformgames.plugin.tempflight.conf.PluginMessages;
import com.artformgames.plugin.tempflight.listener.TempFlyListener;
import com.artformgames.plugin.tempflight.manager.FlightManager;
import dev.rollczi.litecommands.bukkit.LiteCommandsBukkit;
import dev.rollczi.litecommands.schematic.SchematicFormat;
import org.bstats.bukkit.Metrics;

public class Main extends EasyPlugin {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,24 +1,73 @@
package com.artformgames.plugin.tempflight.command;

import com.artformgames.core.ArtCore;
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;
import dev.rollczi.litecommands.annotations.description.Description;
import dev.rollczi.litecommands.annotations.execute.Execute;
import dev.rollczi.litecommands.annotations.optional.OptionalArg;
import dev.rollczi.litecommands.annotations.permission.Permission;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

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


@Command(name = "tempflight", aliases = {"tempfly"})
@Description("TempFlight commands")
@Permission("tempflight.admin")
public class TempFlightCommands {

void start(@Arg Player player, @Arg String duration, @OptionalArg Boolean back) {
@Execute(name = "start")
void start(@Context CommandSender sender,
@Arg Player target, @Arg String duration, @OptionalArg Boolean back) {
back = Optional.ofNullable(back).orElse(false);
FlightAccount data = ArtCore.getHandler(target, FlightAccount.class);
if (data.isTempFlying()) {
PluginMessages.COMMANDS.ALREADY_FLYING.send(sender, target.getName());
PluginMessages.FLYING.send(target, data.getRemainMillis() / 1000);
return;
}

}
FlightManager manager = Main.getFlightManager();
long cooldown = manager.getCooldownMillis(target);
if (cooldown > 0) {
PluginMessages.COMMANDS.COOLING.send(sender, target.getName(), cooldown / 1000);
PluginMessages.COOLING.send(target, cooldown / 1000);
return;
}

void stop(@Arg Player player) {
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);
} else {
PluginMessages.COMMANDS.ALREADY_FLYING.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());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,40 @@ public interface PluginMessages extends Configuration {
.defaults("&7Note: Teleport back to your current 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();


}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.artformgames.plugin.tempflight.utils;

import java.time.Duration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TimeUtils {

// Duration like [?d][?h][?m][?s]
private static final Pattern DURATION_PATTERN = Pattern.compile("^(\\d+[dhms]?)+$");


public static Duration parseDuration(String timeString) {
Matcher matcher = Pattern.compile("(\\d+)([dhms]?)").matcher(timeString);
Duration duration = Duration.ZERO;
while (matcher.find()) {
long value = Long.parseLong(matcher.group(1));
duration = switch (matcher.group(2)) {
case "d" -> duration.plusDays(value);
case "h" -> duration.plusHours(value);
case "m" -> duration.plusMinutes(value);
default -> duration.plusSeconds(value);
};
}
return duration;
}

}
11 changes: 11 additions & 0 deletions src/test/java/DurationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import com.artformgames.plugin.tempflight.utils.TimeUtils;
import org.junit.Test;

public class DurationTest {

@Test
public void testDuration() {
System.out.println(TimeUtils.parseDuration("1d2h3m4s"));
}

}

0 comments on commit 5a02339

Please sign in to comment.