generated from ArtformGames/TemplateSinglePlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
128 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 0 additions & 81 deletions
81
src/main/java/com/artformgames/plugin/tempflight/command/TempFlightCommand.java
This file was deleted.
Oops, something went wrong.
55 changes: 52 additions & 3 deletions
55
src/main/java/com/artformgames/plugin/tempflight/command/TempFlightCommands.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/main/java/com/artformgames/plugin/tempflight/utils/TimeUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
|
||
} |