Skip to content

Commit

Permalink
Changed Player to User argument in spent time command.
Browse files Browse the repository at this point in the history
Moved catch git exception to UpdateService.
  • Loading branch information
imDMK committed Aug 8, 2023
1 parent 5843ae1 commit 6a0a34b
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 26 deletions.
21 changes: 10 additions & 11 deletions src/main/java/com/github/imdmk/spenttime/SpentTime.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.github.imdmk.spenttime;

import com.eternalcode.gitcheck.git.GitException;
import com.github.imdmk.spenttime.command.SpentTimeCommand;
import com.github.imdmk.spenttime.command.SpentTimeResetCommand;
import com.github.imdmk.spenttime.command.SpentTimeTopCommand;

import com.github.imdmk.spenttime.command.argument.PlayerArgument;
import com.github.imdmk.spenttime.command.argument.UserArgument;
import com.github.imdmk.spenttime.command.editor.SpentTimeCommandEditor;
import com.github.imdmk.spenttime.command.handler.MissingPermissionHandler;
import com.github.imdmk.spenttime.command.handler.NotificationHandler;
Expand All @@ -20,14 +21,14 @@
import com.github.imdmk.spenttime.scheduler.TaskScheduler;
import com.github.imdmk.spenttime.scheduler.TaskSchedulerImpl;
import com.github.imdmk.spenttime.update.UpdateService;
import com.github.imdmk.spenttime.user.User;
import com.github.imdmk.spenttime.user.UserManager;
import com.github.imdmk.spenttime.user.listener.UserCreateListener;
import com.github.imdmk.spenttime.user.listener.UserSaveListener;
import com.github.imdmk.spenttime.user.repository.UserRepository;
import com.github.imdmk.spenttime.user.repository.impl.UserEmptyRepositoryImpl;
import com.github.imdmk.spenttime.user.repository.impl.UserRepositoryImpl;
import com.github.imdmk.spenttime.user.task.UserTimeSaveTask;
import com.github.imdmk.spenttime.util.AnsiColor;
import com.github.imdmk.spenttime.util.DurationUtil;
import dev.rollczi.litecommands.LiteCommands;
import dev.rollczi.litecommands.bukkit.adventure.platform.LiteBukkitAdventurePlatformFactory;
Expand Down Expand Up @@ -127,14 +128,9 @@ public SpentTime(Plugin plugin) {

/* Update check */
if (this.pluginConfiguration.checkForUpdate) {
try {
UpdateService updateService = new UpdateService(plugin.getDescription(), this.logger);

this.taskScheduler.runLaterAsync(updateService::check, DurationUtil.toTicks(Duration.ofSeconds(5)));
}
catch (GitException gitException) {
this.logger.info(AnsiColor.RED + "An error occurred while checking for update: " + gitException.getMessage() + AnsiColor.RESET);
}
UpdateService updateService = new UpdateService(plugin.getDescription(), this.logger);

this.taskScheduler.runLaterAsync(updateService::check, DurationUtil.toTicks(Duration.ofSeconds(5)));
}

/* PlaceholderAPI */
Expand Down Expand Up @@ -180,6 +176,7 @@ private PluginConfiguration createConfiguration(File dataFolder) {
private LiteCommands<CommandSender> registerLiteCommands() {
return LiteBukkitAdventurePlatformFactory.builder(this.server, "SpentTime", false, this.bukkitAudiences, true)
.argument(Player.class, new PlayerArgument(this.server, this.pluginConfiguration.messageConfiguration))
.argument(User.class, new UserArgument(this.pluginConfiguration.messageConfiguration, this.userManager))

.contextualBind(Player.class, new BukkitOnlyPlayerContextual<>("Only player can use this command."))

Expand All @@ -188,7 +185,7 @@ private LiteCommands<CommandSender> registerLiteCommands() {
.invalidUsageHandler(new UsageHandler(this.pluginConfiguration.messageConfiguration, this.notificationSender))

.commandInstance(
new SpentTimeCommand(this.pluginConfiguration.messageConfiguration, this.notificationSender),
new SpentTimeCommand(this.server, this.pluginConfiguration.messageConfiguration, this.notificationSender),
new SpentTimeResetCommand(this.server, this.pluginConfiguration.messageConfiguration, this.userRepository, this.userManager, this.notificationSender, this.taskScheduler),
new SpentTimeTopCommand(this.pluginConfiguration.guiConfiguration, this.pluginConfiguration.messageConfiguration, this.userRepository, this.notificationSender, this.topSpentTimeGui, this.topSpentTimePaginatedGui)
)
Expand All @@ -197,4 +194,6 @@ private LiteCommands<CommandSender> registerLiteCommands() {

.register();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import com.github.imdmk.spenttime.configuration.MessageConfiguration;
import com.github.imdmk.spenttime.notification.Notification;
import com.github.imdmk.spenttime.notification.NotificationSender;
import com.github.imdmk.spenttime.user.User;
import com.github.imdmk.spenttime.util.DurationUtil;
import com.github.imdmk.spenttime.util.PlayerUtil;
import dev.rollczi.litecommands.argument.Arg;
import dev.rollczi.litecommands.argument.Name;
import dev.rollczi.litecommands.command.async.Async;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.route.Route;
import org.bukkit.OfflinePlayer;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

Expand All @@ -18,10 +20,12 @@
@Route(name = "spent-time")
public class SpentTimeCommand {

private final Server server;
private final MessageConfiguration messageConfiguration;
private final NotificationSender notificationSender;

public SpentTimeCommand(MessageConfiguration messageConfiguration, NotificationSender notificationSender) {
public SpentTimeCommand(Server server, MessageConfiguration messageConfiguration, NotificationSender notificationSender) {
this.server = server;
this.messageConfiguration = messageConfiguration;
this.notificationSender = notificationSender;
}
Expand All @@ -41,8 +45,9 @@ void showSelfSpentTime(Player player) {

@Async
@Execute(required = 1)
void showTargetSpentTime(CommandSender sender, @Arg @Name("target") Player target) {
Duration targetSpentTime = PlayerUtil.getSpentTimeDuration(target);
void showTargetSpentTime(CommandSender sender, @Arg User target) {
OfflinePlayer targetPlayer = this.server.getOfflinePlayer(target.getUuid());
Duration targetSpentTime = PlayerUtil.getSpentTimeDuration(targetPlayer);

Notification notification = Notification.builder()
.fromNotification(this.messageConfiguration.targetSpentTimeNotification)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.github.imdmk.spenttime.command.argument;

import com.github.imdmk.spenttime.configuration.MessageConfiguration;
import com.github.imdmk.spenttime.user.User;
import com.github.imdmk.spenttime.user.UserManager;
import dev.rollczi.litecommands.argument.simple.OneArgument;
import dev.rollczi.litecommands.command.LiteInvocation;
import dev.rollczi.litecommands.suggestion.Suggestion;
import panda.std.Result;

import java.util.List;
import java.util.Optional;

public class UserArgument implements OneArgument<User> {

private final MessageConfiguration messageConfiguration;
private final UserManager userManager;

public UserArgument(MessageConfiguration messageConfiguration, UserManager userManager) {
this.messageConfiguration = messageConfiguration;
this.userManager = userManager;
}

@Override
public Result<User, ?> parse(LiteInvocation liteInvocation, String argument) {
Optional<User> userOptional = this.userManager.getOrFindUser(argument);

return userOptional.map(Result::ok)
.orElseGet(() -> Result.error(this.messageConfiguration.playerNotFoundNotification));
}

@Override
public List<Suggestion> suggest(LiteInvocation invocation) {
return Suggestion.of(this.userManager.getNameUserCache());
}
}
30 changes: 19 additions & 11 deletions src/main/java/com/github/imdmk/spenttime/update/UpdateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,42 @@
import com.github.imdmk.spenttime.util.AnsiColor;
import org.bukkit.plugin.PluginDescriptionFile;

import java.util.logging.Level;
import java.util.logging.Logger;

public class UpdateService {

private static final GitRepository GIT_REPOSITORY = GitRepository.of("imDMK", "SpentTime");

private final GitCheck gitCheck;
private final PluginDescriptionFile pluginDescriptionFile;
private final Logger logger;

public UpdateService(PluginDescriptionFile pluginDescriptionFile, Logger logger) {
this.gitCheck = new GitCheck();
this.pluginDescriptionFile = pluginDescriptionFile;
this.logger = logger;
}

public void check() throws GitException {
GitCheck gitCheck = new GitCheck();
public void check() {
this.logger.info("Checking for update...");

GitTag gitTag = GitTag.of("v" + this.pluginDescriptionFile.getVersion());
GitCheckResult checkResult = gitCheck.checkRelease(GIT_REPOSITORY, gitTag);
try {
GitTag gitTag = GitTag.of("v" + this.pluginDescriptionFile.getVersion());
GitCheckResult checkResult = this.gitCheck.checkRelease(GIT_REPOSITORY, gitTag);

if (checkResult.isUpToDate()) {
this.logger.info(AnsiColor.GREEN + "You are using latest version. Thank you." + AnsiColor.RESET);
}
else {
GitRelease latestRelease = checkResult.getLatestRelease();
if (checkResult.isUpToDate()) {
this.logger.info(AnsiColor.GREEN + "You are using latest version. Thank you." + AnsiColor.RESET);
}
else {
GitRelease latestRelease = checkResult.getLatestRelease();

this.logger.info(AnsiColor.YELLOW + "A new version is available: " + latestRelease.getTag() + AnsiColor.RESET);
this.logger.info(AnsiColor.YELLOW + "Download it here: " + latestRelease.getPageUrl() + AnsiColor.RESET);
this.logger.info(AnsiColor.YELLOW + "A new version is available: " + latestRelease.getTag() + AnsiColor.RESET);
this.logger.info(AnsiColor.YELLOW + "Download it here: " + latestRelease.getPageUrl() + AnsiColor.RESET);
}
}
catch (GitException gitException) {
this.logger.log(Level.SEVERE, AnsiColor.RED + "An error occurred while checking for update: " + gitException.getMessage() + AnsiColor.RESET);
}
}
}

0 comments on commit 6a0a34b

Please sign in to comment.