Skip to content

Commit

Permalink
Code cleanup.
Browse files Browse the repository at this point in the history
Moved classes.
Removed async parameter in opening guis.
From now on, when resetting the global spent time, we do not remove users from the database.
  • Loading branch information
imDMK committed Aug 9, 2023
1 parent c84232b commit abee558
Show file tree
Hide file tree
Showing 17 changed files with 91 additions and 127 deletions.
7 changes: 2 additions & 5 deletions src/main/java/com/github/imdmk/spenttime/SpentTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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;
Expand All @@ -13,8 +12,8 @@
import com.github.imdmk.spenttime.configuration.PluginConfiguration;
import com.github.imdmk.spenttime.configuration.serializer.pack.SpentTimePack;
import com.github.imdmk.spenttime.database.DatabaseManager;
import com.github.imdmk.spenttime.gui.top.TopSpentTimeGui;
import com.github.imdmk.spenttime.gui.top.TopSpentTimePaginatedGui;
import com.github.imdmk.spenttime.gui.implementation.top.TopSpentTimeGui;
import com.github.imdmk.spenttime.gui.implementation.top.TopSpentTimePaginatedGui;
import com.github.imdmk.spenttime.notification.Notification;
import com.github.imdmk.spenttime.notification.NotificationSender;
import com.github.imdmk.spenttime.placeholder.PlaceholderRegistry;
Expand Down Expand Up @@ -194,6 +193,4 @@ private LiteCommands<CommandSender> registerLiteCommands() {

.register();
}


}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package com.github.imdmk.spenttime.command;

import com.github.imdmk.spenttime.configuration.MessageConfiguration;
import com.github.imdmk.spenttime.gui.ConfirmGui;
import com.github.imdmk.spenttime.gui.implementation.ConfirmGui;
import com.github.imdmk.spenttime.notification.Notification;
import com.github.imdmk.spenttime.notification.NotificationSender;
import com.github.imdmk.spenttime.scheduler.TaskScheduler;
import com.github.imdmk.spenttime.user.UserManager;
import com.github.imdmk.spenttime.user.repository.UserRepository;
import com.github.imdmk.spenttime.util.ComponentUtil;
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;
Expand Down Expand Up @@ -40,15 +40,15 @@ public SpentTimeResetCommand(Server server, MessageConfiguration messageConfigur
@Execute(route = "reset", required = 1)
void resetTime(CommandSender sender, @Arg @Name("target") Player target) {
if (sender instanceof Player player) {
new ConfirmGui(this.taskScheduler, "<red>Reset " + target.getName() + " player spent time?")
new ConfirmGui(this.taskScheduler, ComponentUtil.createItalic("<red>Reset " + target.getName() + " player spent time?"))
.afterConfirm(event -> {
player.closeInventory();

this.taskScheduler.runAsync(() -> this.resetSpentTime(target));
this.sendTargetResetNotification(sender, target);
})
.afterCancel(event -> player.closeInventory())
.open(player, false);
.open(player);
return;
}

Expand All @@ -59,15 +59,15 @@ void resetTime(CommandSender sender, @Arg @Name("target") Player target) {
@Execute(route = "reset-all")
void resetTimeAll(CommandSender sender) {
if (sender instanceof Player player) {
new ConfirmGui(this.taskScheduler, "<red>Reset spent time of all users?")
new ConfirmGui(this.taskScheduler, ComponentUtil.createItalic("<red>Reset spent time of all users?"))
.afterConfirm(event -> {
player.closeInventory();

this.taskScheduler.runAsync(this::resetTimeAll);
this.notificationSender.sendMessage(player, this.messageConfiguration.resetSpentTimeForAllUsersNotification);
})
.afterCancel(event -> player.closeInventory())
.open(player, false);
.open(player);
return;
}

Expand All @@ -87,8 +87,7 @@ private void resetSpentTime(Player player) {
}

private void resetTimeAll() {
this.userRepository.dropTable();
this.userRepository.createTable();
this.userRepository.resetGlobalSpentTime();

for (OfflinePlayer offlinePlayer : this.server.getOfflinePlayers()) {
offlinePlayer.setStatistic(Statistic.PLAY_ONE_MINUTE, 0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.github.imdmk.spenttime.command;

import com.github.imdmk.spenttime.configuration.GuiConfiguration;
import com.github.imdmk.spenttime.configuration.MessageConfiguration;
import com.github.imdmk.spenttime.gui.top.TopSpentTimeGui;
import com.github.imdmk.spenttime.gui.top.TopSpentTimePaginatedGui;
import com.github.imdmk.spenttime.gui.GuiConfiguration;
import com.github.imdmk.spenttime.gui.implementation.top.TopSpentTimeGui;
import com.github.imdmk.spenttime.gui.implementation.top.TopSpentTimePaginatedGui;
import com.github.imdmk.spenttime.notification.Notification;
import com.github.imdmk.spenttime.notification.NotificationSender;
import com.github.imdmk.spenttime.user.User;
Expand Down Expand Up @@ -47,22 +47,24 @@ void showTopSpentTime(Player player) {
}

if (this.guiConfiguration.enabled) {
switch (this.guiConfiguration.guiType) {
case STANDARD -> this.topSpentTimeGui.open(player, topUsers, true);
case PAGINATED -> this.topSpentTimePaginatedGui.open(player, topUsers, true);
default -> throw new IllegalStateException("Unexpected gui type value: " + this.guiConfiguration.guiType);
switch (this.guiConfiguration.type) {
case STANDARD -> this.topSpentTimeGui.open(player, topUsers);
case PAGINATED -> this.topSpentTimePaginatedGui.open(player, topUsers);
default -> throw new IllegalStateException("Unexpected gui type value: " + this.guiConfiguration.type);
}
return;
}

this.notificationSender.sendMessage(player, this.messageConfiguration.topSpentTimeListFirstNotification);

AtomicInteger position = new AtomicInteger(1);
AtomicInteger position = new AtomicInteger(0);

for (User user : topUsers) {
position.incrementAndGet();

Notification notification = Notification.builder()
.fromNotification(this.messageConfiguration.topSpentTimeListNotification)
.placeholder("{POSITION}", position.getAndIncrement())
.placeholder("{POSITION}", position.get())
.placeholder("{PLAYER}", user.getName())
.placeholder("{TIME}", DurationUtil.toHumanReadable(user.getSpentTimeDuration()))
.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.imdmk.spenttime.configuration;

import com.github.imdmk.spenttime.database.DatabaseConfiguration;
import com.github.imdmk.spenttime.gui.GuiConfiguration;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.github.imdmk.spenttime.configuration.serializer.ComponentSerializer;
import com.github.imdmk.spenttime.configuration.serializer.ItemMetaSerializer;
import com.github.imdmk.spenttime.configuration.serializer.ItemStackSerializer;
import com.github.imdmk.spenttime.configuration.serializer.NotificationSerializer;
import com.github.imdmk.spenttime.notification.NotificationSerializer;
import eu.okaeri.configs.serdes.OkaeriSerdesPack;
import eu.okaeri.configs.serdes.SerdesRegistry;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.imdmk.spenttime.configuration;
package com.github.imdmk.spenttime.database;

import com.github.imdmk.spenttime.database.DatabaseMode;
import eu.okaeri.configs.OkaeriConfig;
import eu.okaeri.configs.annotation.Comment;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.imdmk.spenttime.database;

import com.github.imdmk.spenttime.configuration.DatabaseConfiguration;
import com.j256.ormlite.jdbc.DataSourceConnectionSource;
import com.j256.ormlite.support.ConnectionSource;
import com.zaxxer.hikari.HikariDataSource;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.imdmk.spenttime.configuration;
package com.github.imdmk.spenttime.gui;

import com.github.imdmk.spenttime.util.ComponentUtil;
import dev.triumphteam.gui.builder.item.ItemBuilder;
Expand All @@ -12,36 +12,38 @@

public class GuiConfiguration extends OkaeriConfig {

public boolean enabled = true;

@Comment({
"# This determines how many players are to be displayed in the Player top",
"# This determines how many players are to be displayed in the player top",
"# WARNING: Increasing this value may increase the consumption of database server resources",
"# recommended value: 10"
"# Recommended value: 10"
})
public int querySize = 10;

@Comment({
"# Should the gui be enabled?",
"# If you disable the gui, a list of top players in the chat will be displayed"
})
public boolean enabled = true;

@Comment({
"# The type of gui",
"# Available types: ",
"# STANDARD - Standard, basic gui",
"# PAGINATED - Gui with pages; Useful when you want to display several pages of tops"
})
public GuiType guiType = GuiType.STANDARD;
public GuiType type = GuiType.STANDARD;

@Comment("# The title of spent time gui")
public Component title = ComponentUtil.createItalic("<red>Top spent time<dark_gray>:");

@Comment({
"# The title of head item in spent time gui",
"# The head item in top spent time gui",
"# {PLAYER} - The player name",
"# {POSITION} - The player position",
"# {PLAYER} - The player name"
"# {TIME} - The player time"
})
public Component headItemTitle = ComponentUtil.createItalic("<red>{POSITION}. <gray>Player <red>{PLAYER}");

@Comment({
"# The lore of head item in spent time gui",
"# {TIME} - The spent time"
})
public List<Component> headItemLore = List.of(
ComponentUtil.createItalic(""),
ComponentUtil.createItalic("<green>The player has spent <red>{TIME} <green>on the server<dark_gray>."),
Expand All @@ -56,7 +58,7 @@ public class GuiConfiguration extends OkaeriConfig {
@Comment({
"# Exit item slot",
"# Set to -1 to disable",
"# USEFUL: https://i.imgur.com/yuNaucx.png"
"# USEFUL: https://i.imgur.com/gK9plGo.png"
})
public int exitItemSlot = 49;
public ItemStack exitItem = ItemBuilder.from(Material.ACACIA_BUTTON)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.github.imdmk.spenttime.gui;
package com.github.imdmk.spenttime.gui.implementation;

import com.github.imdmk.spenttime.scheduler.TaskScheduler;
import com.github.imdmk.spenttime.util.ComponentUtil;
import dev.triumphteam.gui.builder.item.ItemBuilder;
import dev.triumphteam.gui.components.GuiAction;
import dev.triumphteam.gui.guis.Gui;
import dev.triumphteam.gui.guis.GuiItem;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryClickEvent;
Expand All @@ -18,10 +19,10 @@ public class ConfirmGui {
private GuiAction<InventoryClickEvent> actionAfterConfirm;
private GuiAction<InventoryClickEvent> actionAfterCancel;

public ConfirmGui(TaskScheduler taskScheduler, String title) {
public ConfirmGui(TaskScheduler taskScheduler, Component title) {
this.taskScheduler = taskScheduler;
this.gui = Gui.gui()
.title(ComponentUtil.createItalic(title))
.title(title)
.rows(6)
.disableAllInteractions()
.create();
Expand All @@ -37,7 +38,7 @@ public ConfirmGui afterCancel(GuiAction<InventoryClickEvent> actionAfterCancel)
return this;
}

public void open(Player player, boolean async) {
public void open(Player player) {
GuiItem cancelItem = ItemBuilder.from(Material.RED_CONCRETE)
.name(ComponentUtil.createItalic("<red>Cancel"))
.asGuiItem();
Expand All @@ -62,11 +63,6 @@ public void open(Player player, boolean async) {
this.gui.setItem(23, cancelItem);
this.gui.setItem(32, cancelItem);

if (async) { //opening inventory cannot be async
this.taskScheduler.runLater(() -> this.gui.open(player));
}
else {
this.gui.open(player);
}
this.taskScheduler.runSync(() -> this.gui.open(player));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.imdmk.spenttime.gui.top;
package com.github.imdmk.spenttime.gui.implementation.top;

import com.github.imdmk.spenttime.configuration.GuiConfiguration;
import com.github.imdmk.spenttime.gui.GuiConfiguration;
import com.github.imdmk.spenttime.scheduler.TaskScheduler;
import com.github.imdmk.spenttime.user.User;
import com.github.imdmk.spenttime.util.DurationUtil;
Expand All @@ -12,7 +12,6 @@
import org.bukkit.Server;
import org.bukkit.entity.Player;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -28,7 +27,7 @@ public TopSpentTimeGui(Server server, GuiConfiguration guiConfiguration, TaskSch
this.taskScheduler = taskScheduler;
}

public void open(Player player, List<User> topUsers, boolean async) {
public void open(Player player, List<User> topUsers) {
Gui gui = Gui.gui()
.title(this.guiConfiguration.title)
.rows(6)
Expand All @@ -46,28 +45,26 @@ public void open(Player player, List<User> topUsers, boolean async) {

gui.setItem(this.guiConfiguration.exitItemSlot, exitGuiItem);

AtomicInteger position = new AtomicInteger(1);
AtomicInteger position = new AtomicInteger(0);

for (User user : topUsers) {
Duration userTimeSpent = user.getSpentTimeDuration();
position.incrementAndGet();

String userTimeSpent = DurationUtil.toHumanReadable(user.getSpentTimeDuration());
OfflinePlayer offlinePlayer = this.server.getOfflinePlayer(user.getUuid());

Component headItemTitle = this.guiConfiguration.headItemTitle
.replaceText(builder -> builder
.matchLiteral("{PLAYER}")
.replacement(player.getName())
)
.replaceText(builder -> builder
.matchLiteral("{POSITION}")
.replacement(String.valueOf(position.getAndIncrement()))
);
.replaceText(builder -> builder.matchLiteral("{POSITION}").replacement(String.valueOf(position.get())))
.replaceText(builder -> builder.matchLiteral("{PLAYER}").replacement(player.getName()))
.replaceText(builder -> builder.matchLiteral("{TIME}").replacement(userTimeSpent));

List<Component> headItemLore = this.guiConfiguration.headItemLore
.stream()
.map(component -> component.replaceText(builder -> builder
.matchLiteral("{TIME}")
.replacement(DurationUtil.toHumanReadable(userTimeSpent))
))
.map(component -> component
.replaceText(builder -> builder.matchLiteral("{POSITION}").replacement(String.valueOf(position.get())))
.replaceText(builder -> builder.matchLiteral("{PLAYER}").replacement(player.getName()))
.replaceText(builder -> builder.matchLiteral("{TIME}").replacement(userTimeSpent))
)
.toList();

GuiItem guiItem = ItemBuilder.skull()
Expand All @@ -79,11 +76,6 @@ public void open(Player player, List<User> topUsers, boolean async) {
gui.addItem(guiItem);
}

if (async) { //Opening gui cannot be asynchronous
this.taskScheduler.runLater(() -> gui.open(player));
}
else {
gui.open(player);
}
this.taskScheduler.runSync(() -> gui.open(player));
}
}
Loading

0 comments on commit abee558

Please sign in to comment.