From 93fed4d6e0cde9439fc2c2388b3887b9c29cff12 Mon Sep 17 00:00:00 2001 From: Piotr Zych <77621271+Osnixer@users.noreply.github.com> Date: Sat, 11 Mar 2023 01:58:56 +0100 Subject: [PATCH] GH-80 Refactor notification system, add new events and more (#80) * Refactor notification system, add new events and more * Resolve rollczi conversation * Resolve martin & jakub conversation * Resolve jakub conversation * Resolve Martin conversation * Remove badly usage of creating new list. * Update discord invite link --- build.gradle.kts | 7 +- .../check/CheckNotificationTask.java | 44 ++---- .../com/eternalcode/check/EternalCheck.java | 126 +++++++++------ .../check/NotificationAnnouncer.java | 37 ----- .../eternalcode/check/caller/EventCaller.java | 19 +++ .../check/command/CommandConfiguration.java | 47 ++++++ .../command/argument/CheckedUserArgument.java | 8 +- .../check/command/configurator/Command.java | 44 ++++++ .../configurator/CommandConfigurator.java | 36 +++++ .../command/configurator/SubCommand.java | 31 ++++ .../command/handler/InvalidUsageHandler.java | 37 +++++ .../command/handler/NotificationHandler.java | 21 +++ .../command/handler/PermissionHandler.java | 32 ++++ .../command/implementation/AdmitCommand.java | 27 ++-- .../implementation/CheckBanCommand.java | 64 ++++++++ .../command/implementation/CheckCommand.java | 149 ------------------ .../implementation/CheckEndCommand.java | 58 +++++++ .../implementation/CheckReloadCommand.java | 32 ++++ .../CheckSetLocationCommand.java | 39 +++++ .../implementation/CheckStartCommand.java | 93 +++++++++++ .../command/message/InvalidUseMessage.java | 36 ----- .../command/message/PermissionMessage.java | 33 ---- .../check/config/ConfigManager.java | 6 + .../config/composer/NotificationComposer.java | 52 ++++++ .../config/implementation/MessagesConfig.java | 111 +++++++------ .../config/implementation/PluginConfig.java | 33 ++-- .../check/notification/Notification.java | 63 ++++++++ .../notification/NotificationAnnouncer.java | 103 ++++++++++++ .../check/notification/NotificationType.java | 12 ++ .../check/shared/legacy/Legacy.java | 6 - .../UpdaterNotificationController.java | 49 ++++++ .../check/updater/UpdaterService.java | 34 ++++ .../check/user/CheckedUserService.java | 10 +- .../controller/CheckedUserChatController.java | 9 +- .../CheckedUserCommandController.java | 28 ++-- ...CheckedUserLogoutPunishmentController.java | 28 ++-- .../controller/CheckedUserMoveController.java | 8 +- .../user/event/CheckedUserAdmitEvent.java | 24 +++ .../check/user/event/CheckedUserBanEvent.java | 24 +++ .../check/user/event/CheckedUserEndEvent.java | 24 +++ .../check/user/event/CheckedUserEvent.java | 25 +++ .../user/event/CheckedUserLogoutEvent.java | 24 +++ .../user/event/CheckedUserStartEvent.java | 24 +++ 43 files changed, 1261 insertions(+), 456 deletions(-) delete mode 100644 src/main/java/com/eternalcode/check/NotificationAnnouncer.java create mode 100644 src/main/java/com/eternalcode/check/caller/EventCaller.java create mode 100644 src/main/java/com/eternalcode/check/command/CommandConfiguration.java create mode 100644 src/main/java/com/eternalcode/check/command/configurator/Command.java create mode 100644 src/main/java/com/eternalcode/check/command/configurator/CommandConfigurator.java create mode 100644 src/main/java/com/eternalcode/check/command/configurator/SubCommand.java create mode 100644 src/main/java/com/eternalcode/check/command/handler/InvalidUsageHandler.java create mode 100644 src/main/java/com/eternalcode/check/command/handler/NotificationHandler.java create mode 100644 src/main/java/com/eternalcode/check/command/handler/PermissionHandler.java create mode 100644 src/main/java/com/eternalcode/check/command/implementation/CheckBanCommand.java delete mode 100644 src/main/java/com/eternalcode/check/command/implementation/CheckCommand.java create mode 100644 src/main/java/com/eternalcode/check/command/implementation/CheckEndCommand.java create mode 100644 src/main/java/com/eternalcode/check/command/implementation/CheckReloadCommand.java create mode 100644 src/main/java/com/eternalcode/check/command/implementation/CheckSetLocationCommand.java create mode 100644 src/main/java/com/eternalcode/check/command/implementation/CheckStartCommand.java delete mode 100644 src/main/java/com/eternalcode/check/command/message/InvalidUseMessage.java delete mode 100644 src/main/java/com/eternalcode/check/command/message/PermissionMessage.java create mode 100644 src/main/java/com/eternalcode/check/config/composer/NotificationComposer.java create mode 100644 src/main/java/com/eternalcode/check/notification/Notification.java create mode 100644 src/main/java/com/eternalcode/check/notification/NotificationAnnouncer.java create mode 100644 src/main/java/com/eternalcode/check/notification/NotificationType.java create mode 100644 src/main/java/com/eternalcode/check/updater/UpdaterNotificationController.java create mode 100644 src/main/java/com/eternalcode/check/updater/UpdaterService.java rename src/main/java/com/eternalcode/check/{ => user}/controller/CheckedUserChatController.java (90%) rename src/main/java/com/eternalcode/check/{ => user}/controller/CheckedUserCommandController.java (67%) rename src/main/java/com/eternalcode/check/{ => user}/controller/CheckedUserLogoutPunishmentController.java (64%) rename src/main/java/com/eternalcode/check/{ => user}/controller/CheckedUserMoveController.java (85%) create mode 100644 src/main/java/com/eternalcode/check/user/event/CheckedUserAdmitEvent.java create mode 100644 src/main/java/com/eternalcode/check/user/event/CheckedUserBanEvent.java create mode 100644 src/main/java/com/eternalcode/check/user/event/CheckedUserEndEvent.java create mode 100644 src/main/java/com/eternalcode/check/user/event/CheckedUserEvent.java create mode 100644 src/main/java/com/eternalcode/check/user/event/CheckedUserLogoutEvent.java create mode 100644 src/main/java/com/eternalcode/check/user/event/CheckedUserStartEvent.java diff --git a/build.gradle.kts b/build.gradle.kts index f44c801..e934f3a 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -30,13 +30,16 @@ dependencies { implementation("net.kyori:adventure-text-minimessage:4.12.0") // LiteCommands - implementation("dev.rollczi.litecommands:bukkit:2.8.2") + implementation("dev.rollczi.litecommands:bukkit:2.8.4") // CDN - implementation("net.dzikoysk:cdn:1.14.3") + implementation("net.dzikoysk:cdn:1.14.4") // bStats implementation("org.bstats:bstats-bukkit:3.0.0") + + // gitcheck + implementation("com.eternalcode:gitcheck:1.0.0") } tasks.withType { diff --git a/src/main/java/com/eternalcode/check/CheckNotificationTask.java b/src/main/java/com/eternalcode/check/CheckNotificationTask.java index 8a49b8f..9e47a11 100644 --- a/src/main/java/com/eternalcode/check/CheckNotificationTask.java +++ b/src/main/java/com/eternalcode/check/CheckNotificationTask.java @@ -1,53 +1,43 @@ package com.eternalcode.check; import com.eternalcode.check.config.implementation.MessagesConfig; -import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; import com.eternalcode.check.user.CheckedUser; import com.eternalcode.check.user.CheckedUserService; +import org.bukkit.Server; +import org.bukkit.entity.Player; import panda.utilities.text.Formatter; -import java.time.Duration; -import java.util.UUID; - public final class CheckNotificationTask implements Runnable { - private final MessagesConfig messages; - private final PluginConfig config; private final CheckedUserService checkedUserService; private final NotificationAnnouncer announcer; + private final MessagesConfig messages; + private final Server server; - private final Duration stay, fadeOut, fadeIn; - - public CheckNotificationTask(MessagesConfig messages, PluginConfig config, CheckedUserService checkedUserService, NotificationAnnouncer announcer) { - this.messages = messages; - this.config = config; + public CheckNotificationTask(CheckedUserService checkedUserService, NotificationAnnouncer announcer, MessagesConfig messages, Server server) { this.checkedUserService = checkedUserService; this.announcer = announcer; - - this.stay = this.config.settings.title.stay; - this.fadeOut = this.config.settings.title.fadeOut; - this.fadeIn = this.config.settings.title.fadeIn; + this.messages = messages; + this.server = server; } @Override public void run() { - for (CheckedUser user : this.checkedUserService.getUsers()) { - UUID userUniqueId = user.getUniqueId(); + for (CheckedUser user : this.checkedUserService.checkedUsers()) { + Player player = this.server.getPlayer(user.getUniqueId()); + + if (player == null) { + continue; + } Formatter formatter = new Formatter() .register("{PLAYER}", user.getName()) .register("{ADMIN}", user.getChecker()); - if (this.config.settings.title.taskTitleMessageEnabled) { - this.announcer.announceTitle(userUniqueId, this.messages.check.task.title, this.messages.check.task.subTitle, this.stay, this.fadeOut, this.fadeIn); - } - - if (this.config.settings.taskActionBarEnabled) { - this.announcer.announceActionBar(userUniqueId, this.messages.check.task.actionBar); - } - - if (this.config.settings.taskMessageEnabled) { - this.messages.check.task.message.forEach(message -> this.announcer.announceMessage(userUniqueId, formatter.format(message))); + for (Notification notification : this.messages.check.taskMessages) { + this.announcer.sendAnnounce(player, notification, formatter); } } } diff --git a/src/main/java/com/eternalcode/check/EternalCheck.java b/src/main/java/com/eternalcode/check/EternalCheck.java index b8fc44f..c835429 100644 --- a/src/main/java/com/eternalcode/check/EternalCheck.java +++ b/src/main/java/com/eternalcode/check/EternalCheck.java @@ -1,25 +1,39 @@ package com.eternalcode.check; +import com.eternalcode.check.caller.EventCaller; +import com.eternalcode.check.command.CommandConfiguration; import com.eternalcode.check.command.argument.CheckedUserArgument; import com.eternalcode.check.command.argument.PlayerArgument; +import com.eternalcode.check.command.configurator.CommandConfigurator; +import com.eternalcode.check.command.handler.NotificationHandler; import com.eternalcode.check.command.implementation.AdmitCommand; -import com.eternalcode.check.command.implementation.CheckCommand; -import com.eternalcode.check.command.message.InvalidUseMessage; -import com.eternalcode.check.command.message.PermissionMessage; +import com.eternalcode.check.command.implementation.CheckBanCommand; +import com.eternalcode.check.command.implementation.CheckEndCommand; +import com.eternalcode.check.command.implementation.CheckReloadCommand; +import com.eternalcode.check.command.implementation.CheckSetLocationCommand; +import com.eternalcode.check.command.implementation.CheckStartCommand; +import com.eternalcode.check.command.handler.InvalidUsageHandler; +import com.eternalcode.check.command.handler.PermissionHandler; import com.eternalcode.check.config.ConfigManager; import com.eternalcode.check.config.implementation.CheckedUserDataConfig; import com.eternalcode.check.config.implementation.MessagesConfig; import com.eternalcode.check.config.implementation.PluginConfig; -import com.eternalcode.check.controller.CheckedUserChatController; -import com.eternalcode.check.controller.CheckedUserCommandController; -import com.eternalcode.check.controller.CheckedUserLogoutPunishmentController; -import com.eternalcode.check.controller.CheckedUserMoveController; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; +import com.eternalcode.check.updater.UpdaterNotificationController; +import com.eternalcode.check.updater.UpdaterService; +import com.eternalcode.check.user.controller.CheckedUserChatController; +import com.eternalcode.check.user.controller.CheckedUserCommandController; +import com.eternalcode.check.user.controller.CheckedUserLogoutPunishmentController; +import com.eternalcode.check.user.controller.CheckedUserMoveController; import com.eternalcode.check.shared.legacy.LegacyColorProcessor; import com.eternalcode.check.user.CheckedUser; import com.eternalcode.check.user.CheckedUserService; import dev.rollczi.litecommands.LiteCommands; import dev.rollczi.litecommands.bukkit.LiteBukkitFactory; import dev.rollczi.litecommands.bukkit.tools.BukkitOnlyPlayerContextual; +import dev.rollczi.litecommands.command.permission.RequiredPermissions; +import dev.rollczi.litecommands.schematic.Schematic; import net.kyori.adventure.platform.AudienceProvider; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -34,12 +48,11 @@ public final class EternalCheck extends JavaPlugin { - private static EternalCheck instance; - private ConfigManager configManager; - private PluginConfig config; + private CheckedUserDataConfig checkedUserDataConfig; + private CommandConfiguration commandConfig; private MessagesConfig messages; - private CheckedUserDataConfig data; + private PluginConfig config; private AudienceProvider audienceProvider; private MiniMessage miniMessage; @@ -48,65 +61,82 @@ public final class EternalCheck extends JavaPlugin { private CheckedUserService checkedUserService; + private EventCaller eventCaller; + + private UpdaterService updaterService; + private LiteCommands liteCommands; @Override public void onEnable() { - instance = this; - Server server = this.getServer(); this.configManager = new ConfigManager(this.getDataFolder()); + this.checkedUserDataConfig = new CheckedUserDataConfig(); + this.commandConfig = new CommandConfiguration(); this.config = new PluginConfig(); this.messages = new MessagesConfig(); - this.data = new CheckedUserDataConfig(); this.configManager.load(this.config); this.configManager.load(this.messages); - this.configManager.load(this.data); - - Metrics metrics = new Metrics(this, 15964); - metrics.addCustomChart(new SingleLineChart("checked_users", () -> this.data.getCheckedUsers())); + this.configManager.load(this.checkedUserDataConfig); + this.configManager.load(this.commandConfig); this.audienceProvider = BukkitAudiences.create(this); this.miniMessage = MiniMessage.builder() .postProcessor(new LegacyColorProcessor()) .build(); - this.notificationAnnouncer = new NotificationAnnouncer(this.audienceProvider, this.miniMessage); + this.notificationAnnouncer = new NotificationAnnouncer(this.audienceProvider, this.miniMessage, this.config, server); this.checkedUserService = new CheckedUserService(); + this.eventCaller = new EventCaller(server); + + this.updaterService = new UpdaterService(this.getDescription()); + this.liteCommands = LiteBukkitFactory.builder(this.getServer(), "eternalcheck") .argument(Player.class, new PlayerArgument(this.messages, server)) - .argument(CheckedUser.class, new CheckedUserArgument(this.messages, this.checkedUserService, server)) + .argument(CheckedUser.class, new CheckedUserArgument(this.checkedUserService, this.messages, server)) - .contextualBind(Player.class, new BukkitOnlyPlayerContextual("")) + .contextualBind(Player.class, new BukkitOnlyPlayerContextual<>("Only players can use this command!")) - .permissionHandler(new PermissionMessage(this.messages, this.notificationAnnouncer)) - .invalidUsageHandler(new InvalidUseMessage(this.messages, this.notificationAnnouncer)) + .resultHandler(RequiredPermissions.class, new PermissionHandler(this.notificationAnnouncer, this.messages)) + .resultHandler(Schematic.class, new InvalidUsageHandler(this.messages, this.notificationAnnouncer)) + .resultHandler(Notification.class, new NotificationHandler(this.notificationAnnouncer)) - .commandInstance(new AdmitCommand(this.messages, this.config, this.checkedUserService, server, this.notificationAnnouncer)) - .commandInstance(new CheckCommand(this.configManager, this.messages, this.config, this.checkedUserService, server, this.notificationAnnouncer, data)) + .commandInstance( + new AdmitCommand(this.messages, this.config, this.checkedUserService, server, this.notificationAnnouncer, this.eventCaller), + new CheckBanCommand(this.checkedUserService, this.notificationAnnouncer, this.messages, this.eventCaller, this.config, server), + new CheckEndCommand(this.checkedUserService, this.notificationAnnouncer, this.eventCaller, this.messages, server), + new CheckReloadCommand(this.notificationAnnouncer, this.configManager, this.messages), + new CheckSetLocationCommand(this.notificationAnnouncer, this.configManager, this.messages, this.config), + new CheckStartCommand(this.checkedUserDataConfig, this.checkedUserService, this.notificationAnnouncer, this.configManager, this.messages, this.eventCaller, this.config) + ) + + .commandGlobalEditor(new CommandConfigurator(this.commandConfig)) .register(); + Metrics metrics = new Metrics(this, 15964); + metrics.addCustomChart(new SingleLineChart("checked_users", () -> this.checkedUserDataConfig.getCheckedUsers())); + Stream.of( - new CheckedUserChatController(this.config, this.checkedUserService), - new CheckedUserCommandController(this.messages, this.config, this.checkedUserService, this.notificationAnnouncer), - new CheckedUserMoveController(this.config, this.checkedUserService), - new CheckedUserLogoutPunishmentController(this.messages, this.config, this.checkedUserService, server, this.notificationAnnouncer) + new CheckedUserChatController(this.checkedUserService, this.config), + new CheckedUserCommandController(this.checkedUserService, this.notificationAnnouncer, this.messages, this.config), + new CheckedUserMoveController(this.checkedUserService, this.config), + new CheckedUserLogoutPunishmentController(this.checkedUserService, this.notificationAnnouncer, this.messages, this.config, eventCaller, server), + new UpdaterNotificationController(this.notificationAnnouncer, this.updaterService, this.config) ).forEach(listener -> server.getPluginManager().registerEvents(listener, this)); - if (!this.config.settings.runnable.enabled) { - return; + if (this.config.settings.runnable.enabled) { + server.getScheduler().runTaskTimerAsynchronously( + this, + new CheckNotificationTask(this.checkedUserService, this.notificationAnnouncer, this.messages, server), + 20L, + 20L * this.config.settings.runnable.interval); } - - server.getScheduler().runTaskTimerAsynchronously(this, - new CheckNotificationTask(this.messages, this.config, this.checkedUserService, this.notificationAnnouncer), - 0L, 20L * this.config.settings.runnable.interval); - } @Override @@ -114,14 +144,18 @@ public void onDisable() { this.liteCommands.getPlatform().unregisterAll(); } - public static EternalCheck getInstance() { - return instance; - } - public ConfigManager getConfigManager() { return this.configManager; } + public CheckedUserDataConfig getDataConfig() { + return this.checkedUserDataConfig; + } + + public CommandConfiguration getCommandConfig() { + return this.commandConfig; + } + public PluginConfig getPluginConfig() { return this.config; } @@ -130,10 +164,6 @@ public MessagesConfig getMessagesConfig() { return this.messages; } - public CheckedUserDataConfig getDataConfig() { - return this.data; - } - public AudienceProvider getAudienceProvider() { return this.audienceProvider; } @@ -150,6 +180,14 @@ public CheckedUserService getUserService() { return this.checkedUserService; } + public EventCaller getEventCaller() { + return this.eventCaller; + } + + public UpdaterService getUpdaterService() { + return this.updaterService; + } + public LiteCommands getLiteCommands() { return this.liteCommands; } diff --git a/src/main/java/com/eternalcode/check/NotificationAnnouncer.java b/src/main/java/com/eternalcode/check/NotificationAnnouncer.java deleted file mode 100644 index 94bb2eb..0000000 --- a/src/main/java/com/eternalcode/check/NotificationAnnouncer.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.eternalcode.check; - -import net.kyori.adventure.audience.Audience; -import net.kyori.adventure.platform.AudienceProvider; -import net.kyori.adventure.text.minimessage.MiniMessage; -import net.kyori.adventure.title.Title; - -import java.time.Duration; -import java.util.UUID; - -public final class NotificationAnnouncer { - - private final AudienceProvider audienceProvider; - private final MiniMessage miniMessage; - - public NotificationAnnouncer(AudienceProvider audienceProvider, MiniMessage miniMessage) { - this.audienceProvider = audienceProvider; - this.miniMessage = miniMessage; - } - - public void announceTitle(UUID uniqueId, String title, String subTitle, Duration fadeIn, Duration stay, Duration fadeOut) { - Audience audience = this.audienceProvider.player(uniqueId); - - Title.Times titleTimes = Title.Times.times(fadeIn, stay, fadeOut); - Title titlePart = Title.title(this.miniMessage.deserialize(title), this.miniMessage.deserialize(subTitle), titleTimes); - - audience.showTitle(titlePart); - } - - public void announceActionBar(UUID uniqueId, String message) { - this.audienceProvider.player(uniqueId).sendMessage(this.miniMessage.deserialize(message)); - } - - public void announceMessage(UUID uniqueId, String message) { - this.audienceProvider.player(uniqueId).sendMessage(this.miniMessage.deserialize(message)); - } -} diff --git a/src/main/java/com/eternalcode/check/caller/EventCaller.java b/src/main/java/com/eternalcode/check/caller/EventCaller.java new file mode 100644 index 0000000..261b500 --- /dev/null +++ b/src/main/java/com/eternalcode/check/caller/EventCaller.java @@ -0,0 +1,19 @@ +package com.eternalcode.check.caller; + +import com.eternalcode.check.user.event.CheckedUserEvent; +import org.bukkit.Server; + +public final class EventCaller { + + private final Server server; + + public EventCaller(Server server) { + this.server = server; + } + + public T callEvent(T event) { + this.server.getPluginManager().callEvent(event); + + return event; + } +} diff --git a/src/main/java/com/eternalcode/check/command/CommandConfiguration.java b/src/main/java/com/eternalcode/check/command/CommandConfiguration.java new file mode 100644 index 0000000..54cef9f --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/CommandConfiguration.java @@ -0,0 +1,47 @@ +package com.eternalcode.check.command; + +import com.eternalcode.check.command.configurator.Command; +import com.eternalcode.check.command.configurator.SubCommand; +import com.eternalcode.check.config.ReloadableConfig; +import com.google.common.collect.ImmutableMap; +import net.dzikoysk.cdn.entity.Description; +import net.dzikoysk.cdn.source.Resource; +import net.dzikoysk.cdn.source.Source; + +import java.io.File; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Map; + +public class CommandConfiguration implements ReloadableConfig { + + @Description("# Set name of command, argument names and permissions:") + public Map commands = ImmutableMap.of( + "admit", + new Command( + "admit", + Arrays.asList("przyznajsie", "ff"), + new ArrayList<>() + ), + + "check", + new Command( + "check", + Collections.singletonList("sprawdz"), + Collections.singletonList("eternalcheck.check"), + Arrays.asList( + new SubCommand("start", "rozpocznij", Collections.singletonList("eternalcheck.check")), + new SubCommand("end", "koniec", Collections.singletonList("eternalcheck.check")), + new SubCommand("ban", "zbanuj", Collections.singletonList("eternalcheck.check")), + new SubCommand("setlocation", "ustawmiejsce", Collections.singletonList("eternalcheck.setlocation")), + new SubCommand("reload", "przeladuj", Collections.singletonList("eternalcheck.reload")) + ) + ) + ); + + @Override + public Resource resource(File folder) { + return Source.of(folder, "commands.yml"); + } +} diff --git a/src/main/java/com/eternalcode/check/command/argument/CheckedUserArgument.java b/src/main/java/com/eternalcode/check/command/argument/CheckedUserArgument.java index 9bfb6e0..30b937b 100644 --- a/src/main/java/com/eternalcode/check/command/argument/CheckedUserArgument.java +++ b/src/main/java/com/eternalcode/check/command/argument/CheckedUserArgument.java @@ -18,13 +18,13 @@ @ArgumentName("user") public class CheckedUserArgument implements OneArgument { - private final MessagesConfig messages; private final CheckedUserService checkedUserService; + private final MessagesConfig messages; private final Server server; - public CheckedUserArgument(MessagesConfig messages, CheckedUserService checkedUserService, Server server) { - this.messages = messages; + public CheckedUserArgument(CheckedUserService checkedUserService, MessagesConfig messages, Server server) { this.checkedUserService = checkedUserService; + this.messages = messages; this.server = server; } @@ -47,7 +47,7 @@ public CheckedUserArgument(MessagesConfig messages, CheckedUserService checkedUs @Override public List suggest(LiteInvocation invocation) { - return this.checkedUserService.getUsers() + return this.checkedUserService.checkedUsers() .stream() .map(CheckedUser::getName) .map(Suggestion::of) diff --git a/src/main/java/com/eternalcode/check/command/configurator/Command.java b/src/main/java/com/eternalcode/check/command/configurator/Command.java new file mode 100644 index 0000000..1a6ac09 --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/configurator/Command.java @@ -0,0 +1,44 @@ +package com.eternalcode.check.command.configurator; + +import java.util.ArrayList; +import java.util.List; + +public class Command { + + private String name; + private List aliases; + private List permissions; + private List subCommands = new ArrayList<>(); + + public Command(String name, List aliases, List permissions, List subCommands) { + this.name = name; + this.aliases = aliases; + this.permissions = permissions; + this.subCommands = subCommands; + } + + public Command(String name, List aliases, List permissions) { + this.name = name; + this.aliases = aliases; + this.permissions = permissions; + } + + public Command() { + } + + public String name() { + return this.name; + } + + public List aliases() { + return this.aliases; + } + + public List permissions() { + return this.permissions; + } + + public List subCommands() { + return this.subCommands; + } +} diff --git a/src/main/java/com/eternalcode/check/command/configurator/CommandConfigurator.java b/src/main/java/com/eternalcode/check/command/configurator/CommandConfigurator.java new file mode 100644 index 0000000..0a317df --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/configurator/CommandConfigurator.java @@ -0,0 +1,36 @@ +package com.eternalcode.check.command.configurator; + +import com.eternalcode.check.command.CommandConfiguration; +import dev.rollczi.litecommands.factory.CommandEditor; + +public class CommandConfigurator implements CommandEditor { + + private final CommandConfiguration commandConfiguration; + + public CommandConfigurator(CommandConfiguration commandConfiguration) { + this.commandConfiguration = commandConfiguration; + } + + @Override + public State edit(State state) { + Command command = this.commandConfiguration.commands.get(state.getName()); + + if (command == null) { + return state; + } + + if (command.subCommands().size() >= 1) { + for (SubCommand subCommand : command.subCommands()) { + state = state.editChild(subCommand.name(), editor -> editor + .name(subCommand.alias()) + .permission(subCommand.permissions(), true)); + } + } + + return state.name(command.name()) + .aliases(command.aliases(), true) + .permission(command.permissions(), true); + + } +} + diff --git a/src/main/java/com/eternalcode/check/command/configurator/SubCommand.java b/src/main/java/com/eternalcode/check/command/configurator/SubCommand.java new file mode 100644 index 0000000..ca4881f --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/configurator/SubCommand.java @@ -0,0 +1,31 @@ +package com.eternalcode.check.command.configurator; + +import java.util.List; + +public class SubCommand { + + private String name; + private String alias; + private List permissions; + + public SubCommand(String name, String alias, List permissions) { + this.name = name; + this.alias = alias; + this.permissions = permissions; + } + + public SubCommand() { + } + + public String name() { + return this.name; + } + + public String alias() { + return this.alias; + } + + public List permissions() { + return this.permissions; + } +} diff --git a/src/main/java/com/eternalcode/check/command/handler/InvalidUsageHandler.java b/src/main/java/com/eternalcode/check/command/handler/InvalidUsageHandler.java new file mode 100644 index 0000000..eb99ed7 --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/handler/InvalidUsageHandler.java @@ -0,0 +1,37 @@ +package com.eternalcode.check.command.handler; + +import com.eternalcode.check.config.implementation.MessagesConfig; +import com.eternalcode.check.notification.NotificationAnnouncer; +import dev.rollczi.litecommands.command.LiteInvocation; +import dev.rollczi.litecommands.handle.Handler; +import dev.rollczi.litecommands.schematic.Schematic; +import org.bukkit.command.CommandSender; +import panda.utilities.text.Formatter; + +public class InvalidUsageHandler implements Handler { + + private final NotificationAnnouncer announcer; + private final MessagesConfig messagesConfig; + + public InvalidUsageHandler(MessagesConfig messagesConfig, NotificationAnnouncer announcer) { + this.messagesConfig = messagesConfig; + this.announcer = announcer; + } + + @Override + public void handle(CommandSender commandSender, LiteInvocation invocation, Schematic scheme) { + if (scheme.isOnlyFirst()) { + Formatter formatter = new Formatter().register("{USAGE}", scheme.first()); + + this.announcer.sendAnnounce(commandSender, this.messagesConfig.argument.correctUsage, formatter); + + return; + } + + for (String schemematic : scheme.getSchematics()) { + Formatter formatter = new Formatter().register("{USAGE}", schemematic); + + this.announcer.sendAnnounce(commandSender, this.messagesConfig.argument.correctUsageList, formatter); + } + } +} diff --git a/src/main/java/com/eternalcode/check/command/handler/NotificationHandler.java b/src/main/java/com/eternalcode/check/command/handler/NotificationHandler.java new file mode 100644 index 0000000..85101d8 --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/handler/NotificationHandler.java @@ -0,0 +1,21 @@ +package com.eternalcode.check.command.handler; + +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; +import dev.rollczi.litecommands.command.LiteInvocation; +import dev.rollczi.litecommands.handle.Handler; +import org.bukkit.command.CommandSender; + +public class NotificationHandler implements Handler { + + private final NotificationAnnouncer announcer; + + public NotificationHandler(NotificationAnnouncer announcer) { + this.announcer = announcer; + } + + @Override + public void handle(CommandSender sender, LiteInvocation invocation, Notification value) { + this.announcer.sendAnnounce(sender, value); + } +} diff --git a/src/main/java/com/eternalcode/check/command/handler/PermissionHandler.java b/src/main/java/com/eternalcode/check/command/handler/PermissionHandler.java new file mode 100644 index 0000000..a47594a --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/handler/PermissionHandler.java @@ -0,0 +1,32 @@ +package com.eternalcode.check.command.handler; + +import com.eternalcode.check.config.implementation.MessagesConfig; +import com.eternalcode.check.notification.NotificationAnnouncer; +import dev.rollczi.litecommands.command.LiteInvocation; +import dev.rollczi.litecommands.command.permission.RequiredPermissions; +import dev.rollczi.litecommands.handle.Handler; +import org.bukkit.command.CommandSender; +import panda.utilities.text.Formatter; +import panda.utilities.text.Joiner; + +public class PermissionHandler implements Handler { + + private final NotificationAnnouncer announcer; + private final MessagesConfig messages; + + public PermissionHandler(NotificationAnnouncer announcer, MessagesConfig messages) { + this.announcer = announcer; + this.messages = messages; + } + + @Override + public void handle(CommandSender commandSender, LiteInvocation invocation, RequiredPermissions requiredPermissions) { + Formatter formatter = new Formatter() + .register("{PERMISSION}", Joiner.on(", ") + .join(requiredPermissions.getPermissions()) + .toString()); + + this.announcer.sendAnnounce(commandSender, this.messages.argument.permission, formatter); + } + +} diff --git a/src/main/java/com/eternalcode/check/command/implementation/AdmitCommand.java b/src/main/java/com/eternalcode/check/command/implementation/AdmitCommand.java index 8cf7e22..8a88588 100644 --- a/src/main/java/com/eternalcode/check/command/implementation/AdmitCommand.java +++ b/src/main/java/com/eternalcode/check/command/implementation/AdmitCommand.java @@ -1,12 +1,15 @@ package com.eternalcode.check.command.implementation; -import com.eternalcode.check.NotificationAnnouncer; +import com.eternalcode.check.caller.EventCaller; import com.eternalcode.check.config.implementation.MessagesConfig; import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; import com.eternalcode.check.user.CheckedUser; import com.eternalcode.check.user.CheckedUserService; +import com.eternalcode.check.user.event.CheckedUserAdmitEvent; import dev.rollczi.litecommands.command.execute.Execute; -import dev.rollczi.litecommands.command.section.Section; +import dev.rollczi.litecommands.command.route.Route; import org.bukkit.Server; import org.bukkit.entity.Player; import panda.utilities.StringUtils; @@ -14,21 +17,23 @@ import java.util.Optional; -@Section(route = "admit", aliases = { "przyznajsie", "ff"} ) +@Route(name = "admit") public class AdmitCommand { + private final CheckedUserService checkedUserService; + private final NotificationAnnouncer announcer; + private final EventCaller eventCaller; private final MessagesConfig messages; private final PluginConfig config; - private final CheckedUserService checkedUserService; private final Server server; - private final NotificationAnnouncer announcer; - public AdmitCommand(MessagesConfig messages, PluginConfig config, CheckedUserService checkedUserService, Server server, NotificationAnnouncer announcer) { + public AdmitCommand(MessagesConfig messages, PluginConfig config, CheckedUserService checkedUserService, Server server, NotificationAnnouncer announcer, EventCaller eventCaller) { this.messages = messages; this.config = config; this.checkedUserService = checkedUserService; this.server = server; this.announcer = announcer; + this.eventCaller = eventCaller; } @Execute @@ -36,7 +41,7 @@ public void execute(Player player) { Optional userOptional = this.checkedUserService.find(player.getUniqueId()); if (!userOptional.isPresent()) { - this.announcer.announceMessage(player.getUniqueId(), this.messages.argument.youArentChecked); + this.announcer.sendAnnounce(player, this.messages.argument.youArentChecked); return; } @@ -51,8 +56,12 @@ public void execute(Player player) { .register("{PLAYER}", user.getName()) .register("{ADMIN}", user.getChecker()); - for (Player all : this.server.getOnlinePlayers()) { - this.messages.check.broadcast.admit.forEach(message -> this.announcer.announceMessage(all.getUniqueId(), formatter.format(message))); + for (Notification notification : this.messages.check.broadcast.admit) { + this.announcer.sendAnnounceAll(notification, formatter); } + + Player admin = this.server.getPlayer(user.getChecker()); + + this.eventCaller.callEvent(new CheckedUserAdmitEvent(user, admin)); } } \ No newline at end of file diff --git a/src/main/java/com/eternalcode/check/command/implementation/CheckBanCommand.java b/src/main/java/com/eternalcode/check/command/implementation/CheckBanCommand.java new file mode 100644 index 0000000..274e139 --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/implementation/CheckBanCommand.java @@ -0,0 +1,64 @@ +package com.eternalcode.check.command.implementation; + +import com.eternalcode.check.caller.EventCaller; +import com.eternalcode.check.config.implementation.MessagesConfig; +import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; +import com.eternalcode.check.shared.position.PositionAdapter; +import com.eternalcode.check.user.CheckedUser; +import com.eternalcode.check.user.CheckedUserService; +import com.eternalcode.check.user.event.CheckedUserBanEvent; +import dev.rollczi.litecommands.argument.Arg; +import dev.rollczi.litecommands.argument.Name; +import dev.rollczi.litecommands.command.execute.Execute; +import dev.rollczi.litecommands.command.permission.Permission; +import dev.rollczi.litecommands.command.route.Route; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import panda.utilities.StringUtils; +import panda.utilities.text.Formatter; + +@Route(name = "check") +@Permission("eternalcheck.check") +public class CheckBanCommand { + + private final CheckedUserService checkedUserService; + private final NotificationAnnouncer announcer; + private final MessagesConfig messages; + private final EventCaller eventCaller; + private final PluginConfig config; + private final Server server; + + public CheckBanCommand(CheckedUserService checkedUserService, NotificationAnnouncer announcer, MessagesConfig messages, EventCaller eventCaller, PluginConfig config, Server server) { + this.checkedUserService = checkedUserService; + this.announcer = announcer; + this.messages = messages; + this.eventCaller = eventCaller; + this.config = config; + this.server = server; + } + + @Execute(route = "ban", required = 1) + void execute(Player player, @Arg @Name("player") CheckedUser user) { + Player playerArgument = this.server.getPlayer(user.getUniqueId()); + + playerArgument.teleport(PositionAdapter.convert(user.getLastPosition())); + + this.checkedUserService.unmarkChecked(user.getUniqueId()); + + Formatter formatter = new Formatter() + .register("{PLAYER}", user.getName()) + .register("{ADMIN}", player.getName()); + + this.announcer.sendAnnounce(player, this.messages.check.admin.ban, formatter); + + for (Notification notification : this.messages.check.broadcast.banCheck) { + this.announcer.sendAnnounceAll(notification, formatter); + } + + this.server.dispatchCommand(this.server.getConsoleSender(), StringUtils.replace(this.config.commands.ban, "{PLAYER}", user.getName())); + + this.eventCaller.callEvent(new CheckedUserBanEvent(user, player)); + } +} diff --git a/src/main/java/com/eternalcode/check/command/implementation/CheckCommand.java b/src/main/java/com/eternalcode/check/command/implementation/CheckCommand.java deleted file mode 100644 index 3067d28..0000000 --- a/src/main/java/com/eternalcode/check/command/implementation/CheckCommand.java +++ /dev/null @@ -1,149 +0,0 @@ -package com.eternalcode.check.command.implementation; - -import com.eternalcode.check.NotificationAnnouncer; -import com.eternalcode.check.config.ConfigManager; -import com.eternalcode.check.config.implementation.CheckedUserDataConfig; -import com.eternalcode.check.config.implementation.MessagesConfig; -import com.eternalcode.check.config.implementation.PluginConfig; -import com.eternalcode.check.shared.position.PositionAdapter; -import com.eternalcode.check.user.CheckedUser; -import com.eternalcode.check.user.CheckedUserService; -import dev.rollczi.litecommands.argument.Arg; -import dev.rollczi.litecommands.argument.Name; -import dev.rollczi.litecommands.command.execute.Execute; -import dev.rollczi.litecommands.command.permission.Permission; -import dev.rollczi.litecommands.command.section.Section; -import org.bukkit.Server; -import org.bukkit.entity.Player; -import panda.utilities.StringUtils; -import panda.utilities.text.Formatter; - -@Section(route = "check", aliases = "sprawdz") -@Permission("eternalcheck.check") -public class CheckCommand { - - private final ConfigManager configManager; - private final MessagesConfig messages; - private final PluginConfig config; - private final CheckedUserService checkedUserService; - private final Server server; - private final NotificationAnnouncer announcer; - private final CheckedUserDataConfig data; - - public CheckCommand(ConfigManager configManager, MessagesConfig messages, PluginConfig config, CheckedUserService checkedUserService, Server server, NotificationAnnouncer announcer, CheckedUserDataConfig data) { - this.configManager = configManager; - this.messages = messages; - this.config = config; - this.checkedUserService = checkedUserService; - this.server = server; - this.announcer = announcer; - this.data = data; - } - - @Execute(route = "set", aliases = "ustaw") - @Permission("eternalcheck.check.set") - public void executeSet(Player player) { - this.config.checkLocation = PositionAdapter.convert(player.getLocation().clone()); - - this.configManager.save(this.config); - - this.announcer.announceMessage(player.getUniqueId(), this.messages.check.adminLocationSet); - } - - @Execute(route = "reload", aliases = "rl") - @Permission("eternalcheck.check.reload") - public void executeReload(Player player) { - this.configManager.reload(); - - this.announcer.announceMessage(player.getUniqueId(), this.messages.check.adminReload); - } - - - @Execute(route = "start", aliases = "rozpocznij", min = 1) - public void executeStart(Player player, @Arg @Name("player") Player playerArgument) { - if (playerArgument.hasPermission("eternalcheck.bypass")) { - this.announcer.announceMessage(player.getUniqueId(), this.messages.argument.bypass); - - return; - } - - if (this.checkedUserService.find(playerArgument.getUniqueId()).isPresent()) { - this.announcer.announceMessage(player.getUniqueId(), this.messages.argument.isChecking); - - return; - } - - if (this.config.checkLocation.getWorld() == null || this.config.checkLocation == null) { - this.announcer.announceMessage(player.getUniqueId(), this.messages.argument.roomNotSet); - - return; - } - - this.checkedUserService.markChecked(playerArgument.getUniqueId(), playerArgument.getName(), player.getName(), PositionAdapter.convert(playerArgument.getLocation().clone())); - - this.data.addCheckedUser(); - this.configManager.save(this.data); - - player.teleport(PositionAdapter.convert(this.config.checkLocation)); - playerArgument.teleport(PositionAdapter.convert(this.config.checkLocation)); - - Formatter formatter = new Formatter() - .register("{PLAYER}", playerArgument.getName()) - .register("{ADMIN}", player.getName()); - - this.announcer.announceMessage(player.getUniqueId(), formatter.format(this.messages.check.adminStart)); - - for (Player all : this.server.getOnlinePlayers()) { - this.messages.check.broadcast.startCheck.forEach(message -> this.announcer.announceMessage(all.getUniqueId(), formatter.format(message))); - } - - this.messages.check.start.startMessage.forEach(message -> this.announcer.announceMessage(playerArgument.getUniqueId(), formatter.format(message))); - - if (this.config.settings.title.startTitleMessageEnabled) { - this.announcer.announceTitle(playerArgument.getUniqueId(), formatter.format(this.messages.check.start.startTitle), - formatter.format(this.messages.check.start.startSubTitle), - this.config.settings.title.fadeIn, - this.config.settings.title.stay, - this.config.settings.title.fadeOut - ); - } - } - - @Execute(route = "end", aliases = "koniec", min = 1) - public void executeEnd(Player player, @Arg @Name("player") CheckedUser user) { - Player playerArgument = this.server.getPlayer(user.getUniqueId()); - - playerArgument.teleport(PositionAdapter.convert(user.getLastPosition())); - this.checkedUserService.unmarkChecked(user.getUniqueId()); - - this.announcer.announceMessage(player.getUniqueId(), this.messages.check.adminEnd.replace("{PLAYER}", playerArgument.getName())); - - Formatter formatter = new Formatter() - .register("{PLAYER}", user.getName()) - .register("{ADMIN}", player.getName()); - - for (Player all : this.server.getOnlinePlayers()) { - this.messages.check.broadcast.endCheck.forEach(message -> this.announcer.announceMessage(all.getUniqueId(), formatter.format(message))); - } - } - - @Execute(route = "ban", aliases = "zbanuj", min = 1) - public void executeBan(Player player, @Arg @Name("player") CheckedUser user) { - Player playerArgument = this.server.getPlayer(user.getUniqueId()); - - playerArgument.teleport(PositionAdapter.convert(user.getLastPosition())); - this.checkedUserService.unmarkChecked(user.getUniqueId()); - - this.announcer.announceMessage(player.getUniqueId(), this.messages.check.adminBan.replace("{PLAYER}", playerArgument.getName())); - - Formatter formatter = new Formatter() - .register("{PLAYER}", user.getName()) - .register("{ADMIN}", player.getName()); - - for (Player all : this.server.getOnlinePlayers()) { - this.messages.check.broadcast.banCheck.forEach(message -> this.announcer.announceMessage(all.getUniqueId(), formatter.format(message))); - } - - this.server.dispatchCommand(this.server.getConsoleSender(), StringUtils.replace(this.config.commands.ban, "{PLAYER}", user.getName())); - } -} \ No newline at end of file diff --git a/src/main/java/com/eternalcode/check/command/implementation/CheckEndCommand.java b/src/main/java/com/eternalcode/check/command/implementation/CheckEndCommand.java new file mode 100644 index 0000000..0d74190 --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/implementation/CheckEndCommand.java @@ -0,0 +1,58 @@ +package com.eternalcode.check.command.implementation; + +import com.eternalcode.check.caller.EventCaller; +import com.eternalcode.check.config.implementation.MessagesConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; +import com.eternalcode.check.shared.position.PositionAdapter; +import com.eternalcode.check.user.CheckedUser; +import com.eternalcode.check.user.CheckedUserService; +import com.eternalcode.check.user.event.CheckedUserEndEvent; +import dev.rollczi.litecommands.argument.Arg; +import dev.rollczi.litecommands.argument.Name; +import dev.rollczi.litecommands.command.execute.Execute; +import dev.rollczi.litecommands.command.permission.Permission; +import dev.rollczi.litecommands.command.route.Route; +import org.bukkit.Server; +import org.bukkit.entity.Player; +import panda.utilities.text.Formatter; + +@Route(name = "check") +@Permission("eternalcheck.check") +public class CheckEndCommand { + + private final CheckedUserService checkedUserService; + private final NotificationAnnouncer announcer; + private final EventCaller eventCaller; + private final MessagesConfig messages; + private final Server server; + + public CheckEndCommand(CheckedUserService checkedUserService, NotificationAnnouncer announcer, EventCaller eventCaller, MessagesConfig messages, Server server) { + this.checkedUserService = checkedUserService; + this.announcer = announcer; + this.eventCaller = eventCaller; + this.messages = messages; + this.server = server; + } + + @Execute(route = "end", required = 1) + void execute(Player player, @Arg @Name("player") CheckedUser user) { + Player playerArgument = this.server.getPlayer(user.getUniqueId()); + + playerArgument.teleport(PositionAdapter.convert(user.getLastPosition())); + + this.checkedUserService.unmarkChecked(user.getUniqueId()); + + Formatter formatter = new Formatter() + .register("{PLAYER}", user.getName()) + .register("{ADMIN}", player.getName()); + + this.announcer.sendAnnounce(player, this.messages.check.admin.end, formatter); + + for (Notification notification : this.messages.check.broadcast.endCheck) { + this.announcer.sendAnnounceAll(notification, formatter); + } + + this.eventCaller.callEvent(new CheckedUserEndEvent(user, player)); + } +} diff --git a/src/main/java/com/eternalcode/check/command/implementation/CheckReloadCommand.java b/src/main/java/com/eternalcode/check/command/implementation/CheckReloadCommand.java new file mode 100644 index 0000000..17a1685 --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/implementation/CheckReloadCommand.java @@ -0,0 +1,32 @@ +package com.eternalcode.check.command.implementation; + +import com.eternalcode.check.config.ConfigManager; +import com.eternalcode.check.config.implementation.MessagesConfig; +import com.eternalcode.check.notification.NotificationAnnouncer; +import dev.rollczi.litecommands.command.execute.Execute; +import dev.rollczi.litecommands.command.permission.Permission; +import dev.rollczi.litecommands.command.route.Route; +import org.bukkit.entity.Player; + +@Route(name = "check") +@Permission("eternalcheck.reload") +public class CheckReloadCommand { + + private final NotificationAnnouncer announcer; + private final ConfigManager configManager; + private final MessagesConfig messages; + + public CheckReloadCommand(NotificationAnnouncer announcer, ConfigManager configManager, MessagesConfig messages) { + this.announcer = announcer; + this.configManager = configManager; + this.messages = messages; + } + + @Execute(route = "reload") + @Permission("eternalcheck.check.reload") + void execute(Player player) { + this.configManager.reload(); + + this.announcer.sendAnnounce(player, this.messages.check.admin.reload); + } +} diff --git a/src/main/java/com/eternalcode/check/command/implementation/CheckSetLocationCommand.java b/src/main/java/com/eternalcode/check/command/implementation/CheckSetLocationCommand.java new file mode 100644 index 0000000..3f1da95 --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/implementation/CheckSetLocationCommand.java @@ -0,0 +1,39 @@ +package com.eternalcode.check.command.implementation; + +import com.eternalcode.check.config.ConfigManager; +import com.eternalcode.check.config.implementation.MessagesConfig; +import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.NotificationAnnouncer; +import com.eternalcode.check.shared.position.PositionAdapter; +import dev.rollczi.litecommands.command.execute.Execute; +import dev.rollczi.litecommands.command.permission.Permission; +import dev.rollczi.litecommands.command.route.Route; +import org.bukkit.entity.Player; + +@Route(name = "check") +@Permission("eternalcheck.setlocation") +public class CheckSetLocationCommand { + + private final NotificationAnnouncer announcer; + private final ConfigManager configManager; + private final MessagesConfig messages; + private final PluginConfig config; + + public CheckSetLocationCommand(NotificationAnnouncer announcer, ConfigManager configManager, MessagesConfig messages, PluginConfig config) { + this.announcer = announcer; + this.configManager = configManager; + this.messages = messages; + this.config = config; + } + + @Execute(route = "setlocation") + @Permission("eternalcheck.setlocation") + void execute(Player player) { + this.config.checkLocation = PositionAdapter.convert(player.getLocation().clone()); + + this.configManager.save(this.config); + + this.announcer.sendAnnounce(player, this.messages.check.admin.locationSet); + } + +} diff --git a/src/main/java/com/eternalcode/check/command/implementation/CheckStartCommand.java b/src/main/java/com/eternalcode/check/command/implementation/CheckStartCommand.java new file mode 100644 index 0000000..2eff9ee --- /dev/null +++ b/src/main/java/com/eternalcode/check/command/implementation/CheckStartCommand.java @@ -0,0 +1,93 @@ +package com.eternalcode.check.command.implementation; + +import com.eternalcode.check.caller.EventCaller; +import com.eternalcode.check.config.ConfigManager; +import com.eternalcode.check.config.implementation.CheckedUserDataConfig; +import com.eternalcode.check.config.implementation.MessagesConfig; +import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; +import com.eternalcode.check.shared.position.PositionAdapter; +import com.eternalcode.check.user.CheckedUser; +import com.eternalcode.check.user.CheckedUserService; +import com.eternalcode.check.user.event.CheckedUserStartEvent; +import dev.rollczi.litecommands.argument.Arg; +import dev.rollczi.litecommands.argument.Name; +import dev.rollczi.litecommands.command.execute.Execute; +import dev.rollczi.litecommands.command.permission.Permission; +import dev.rollczi.litecommands.command.route.Route; +import org.bukkit.entity.Player; +import panda.utilities.text.Formatter; + +@Route(name = "check") +@Permission("eternalcheck.check") +public class CheckStartCommand { + + private final CheckedUserDataConfig checkedUserDataConfig; + private final CheckedUserService checkedUserService; + private final NotificationAnnouncer announcer; + private final ConfigManager configManager; + private final MessagesConfig messages; + private final EventCaller eventCaller; + private final PluginConfig config; + + public CheckStartCommand(CheckedUserDataConfig checkedUserDataConfig, CheckedUserService checkedUserService, NotificationAnnouncer announcer, ConfigManager configManager, MessagesConfig messages, EventCaller eventCaller, PluginConfig config) { + this.checkedUserDataConfig = checkedUserDataConfig; + this.checkedUserService = checkedUserService; + this.announcer = announcer; + this.configManager = configManager; + this.messages = messages; + this.eventCaller = eventCaller; + this.config = config; + } + + @Execute(route = "start", required = 1) + void execute(Player player, @Arg @Name("player") Player playerArgument) { + if (playerArgument.hasPermission("eternalcheck.bypass")) { + this.announcer.sendAnnounce(player, this.messages.argument.bypass); + + return; + } + + if (this.checkedUserService.find(playerArgument.getUniqueId()).isPresent()) { + this.announcer.sendAnnounce(player, this.messages.argument.isChecking); + + return; + } + + if (this.config.checkLocation.getWorld() == null || this.config.checkLocation == null) { + this.announcer.sendAnnounce(player, this.messages.argument.roomNotSet); + + return; + } + + CheckedUser checkedUser = this.checkedUserService.markChecked( + playerArgument.getUniqueId(), + playerArgument.getName(), + player.getName(), + PositionAdapter.convert(playerArgument.getLocation().clone()) + ); + + this.checkedUserDataConfig.addCheckedUser(); + this.configManager.save(this.checkedUserDataConfig); + + player.teleport(PositionAdapter.convert(this.config.checkLocation)); + playerArgument.teleport(PositionAdapter.convert(this.config.checkLocation)); + + Formatter formatter = new Formatter() + .register("{PLAYER}", playerArgument.getName()) + .register("{ADMIN}", player.getName()); + + this.announcer.sendAnnounce(player, this.messages.check.admin.start, formatter); + + for (Notification notification : this.messages.check.startMessages) { + this.announcer.sendAnnounce(playerArgument, notification, formatter); + } + + for (Notification notification : this.messages.check.broadcast.startCheck) { + this.announcer.sendAnnounceAll(notification, formatter); + } + + this.eventCaller.callEvent(new CheckedUserStartEvent(checkedUser, player)); + } +} diff --git a/src/main/java/com/eternalcode/check/command/message/InvalidUseMessage.java b/src/main/java/com/eternalcode/check/command/message/InvalidUseMessage.java deleted file mode 100644 index 56588a7..0000000 --- a/src/main/java/com/eternalcode/check/command/message/InvalidUseMessage.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.eternalcode.check.command.message; - -import com.eternalcode.check.NotificationAnnouncer; -import com.eternalcode.check.config.implementation.MessagesConfig; -import dev.rollczi.litecommands.command.LiteInvocation; -import dev.rollczi.litecommands.handle.InvalidUsageHandler; -import dev.rollczi.litecommands.schematic.Schematic; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -public class InvalidUseMessage implements InvalidUsageHandler { - - private final MessagesConfig messagesConfig; - private final NotificationAnnouncer announcer; - - public InvalidUseMessage(MessagesConfig messagesConfig, NotificationAnnouncer announcer) { - this.messagesConfig = messagesConfig; - this.announcer = announcer; - } - - @Override - public void handle(CommandSender commandSender, LiteInvocation invocation, Schematic scheme) { - if (commandSender instanceof Player) { - Player player = (Player) commandSender; - - if (scheme.getSchematics().size() > 1) { - for (String schem : scheme.getSchematics()) { - this.announcer.announceMessage(player.getUniqueId(), this.messagesConfig.argument.correctUsageList.replace("{USAGE}", schem)); - } - } - else { - this.announcer.announceMessage(player.getUniqueId(), this.messagesConfig.argument.correctUsage.replace("{USAGE}", scheme.getSchematics().get(0))); - } - } - } -} diff --git a/src/main/java/com/eternalcode/check/command/message/PermissionMessage.java b/src/main/java/com/eternalcode/check/command/message/PermissionMessage.java deleted file mode 100644 index 9654bbc..0000000 --- a/src/main/java/com/eternalcode/check/command/message/PermissionMessage.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.eternalcode.check.command.message; - -import com.eternalcode.check.NotificationAnnouncer; -import com.eternalcode.check.config.implementation.MessagesConfig; -import dev.rollczi.litecommands.command.LiteInvocation; -import dev.rollczi.litecommands.command.permission.RequiredPermissions; -import dev.rollczi.litecommands.handle.PermissionHandler; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; -import panda.utilities.text.Joiner; - -public class PermissionMessage implements PermissionHandler { - - private final MessagesConfig messages; - private final NotificationAnnouncer announcer; - - public PermissionMessage(MessagesConfig messages, NotificationAnnouncer announcer) { - this.messages = messages; - this.announcer = announcer; - } - - @Override - public void handle(CommandSender commandSender, LiteInvocation invocation, RequiredPermissions requiredPermissions) { - if (commandSender instanceof Player) { - Player player = (Player) commandSender; - - this.announcer.announceMessage(player.getUniqueId(), this.messages.argument.permission.replace("{PERMISSION}", Joiner.on(", ") - .join(requiredPermissions.getPermissions()) - .toString())); - } - } - -} diff --git a/src/main/java/com/eternalcode/check/config/ConfigManager.java b/src/main/java/com/eternalcode/check/config/ConfigManager.java index 5ac3f9b..31a94b3 100644 --- a/src/main/java/com/eternalcode/check/config/ConfigManager.java +++ b/src/main/java/com/eternalcode/check/config/ConfigManager.java @@ -1,10 +1,14 @@ package com.eternalcode.check.config; import com.eternalcode.check.config.composer.DurationComposer; +import com.eternalcode.check.config.composer.NotificationComposer; import com.eternalcode.check.config.composer.PositionComposer; +import com.eternalcode.check.notification.Notification; import com.eternalcode.check.shared.position.Position; import net.dzikoysk.cdn.Cdn; import net.dzikoysk.cdn.CdnFactory; +import net.dzikoysk.cdn.reflect.MemberResolver; +import net.dzikoysk.cdn.reflect.Visibility; import java.io.File; import java.time.Duration; @@ -18,6 +22,8 @@ public class ConfigManager { .getSettings() .withComposer(Position.class, new PositionComposer()) .withComposer(Duration.class, new DurationComposer()) + .withComposer(Notification.class, new NotificationComposer()) + .withMemberResolver(Visibility.PRIVATE) .build(); private final Set configs = new HashSet<>(); diff --git a/src/main/java/com/eternalcode/check/config/composer/NotificationComposer.java b/src/main/java/com/eternalcode/check/config/composer/NotificationComposer.java new file mode 100644 index 0000000..7efc9d4 --- /dev/null +++ b/src/main/java/com/eternalcode/check/config/composer/NotificationComposer.java @@ -0,0 +1,52 @@ +package com.eternalcode.check.config.composer; + +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationType; +import com.google.common.base.Joiner; +import net.dzikoysk.cdn.serdes.Composer; +import net.dzikoysk.cdn.serdes.SimpleDeserializer; +import net.dzikoysk.cdn.serdes.SimpleSerializer; +import panda.std.Result; + +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class NotificationComposer implements Composer, SimpleDeserializer, SimpleSerializer { + + private static final String SERIALIZE_FORMAT = "[%s]%s"; + private static final Pattern DESERIALIZE_PATTERN = Pattern.compile("\\[([^]]*)](.*)"); + + @Override + public Result deserialize(String source) { + Matcher matcher = DESERIALIZE_PATTERN.matcher(source); + + if (!matcher.matches()) { + return Result.error(new IllegalStateException("Can not parse \"" + source + "\" to Notification. Correct format: \"[CHAT]Text to send\"")); + } + + String types = matcher.group(1); + String message = matcher.group(2); + List notificationTypes = new ArrayList<>(); + + for (String type : types.replace(" ", "").split(",")) { + try { + NotificationType notificationType = NotificationType.valueOf(type); + + notificationTypes.add(notificationType); + } + catch (IllegalArgumentException exception) { + return Result.error(new IllegalStateException("Value '" + type + "' is not a notification type! Available types: " + Joiner.on(", ").join(NotificationType.values()))); + } + } + + return Result.ok(Notification.of(message, notificationTypes)); + } + + @Override + public Result serialize(Notification notification) { + return Result.ok(String.format(SERIALIZE_FORMAT, Joiner.on(", ").join(notification.types()), notification.message())); + } +} + diff --git a/src/main/java/com/eternalcode/check/config/implementation/MessagesConfig.java b/src/main/java/com/eternalcode/check/config/implementation/MessagesConfig.java index 712bbc9..91d10b7 100644 --- a/src/main/java/com/eternalcode/check/config/implementation/MessagesConfig.java +++ b/src/main/java/com/eternalcode/check/config/implementation/MessagesConfig.java @@ -1,6 +1,8 @@ package com.eternalcode.check.config.implementation; import com.eternalcode.check.config.ReloadableConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationType; import net.dzikoysk.cdn.entity.Contextual; import net.dzikoysk.cdn.entity.Description; import net.dzikoysk.cdn.source.Resource; @@ -18,12 +20,25 @@ public Resource resource(File folder) { return Source.of(folder, "messages.yml"); } - @Description({ "# ", + @Description({ + "# ", "# EternalCheck - A simple plugin for checking suspicious players.", "# ", - "# Discord: https://dc.eternalcode.pl/", - "# Website: https://eternalcode.pl/", " " }) - + "# Discord: https://discord.gg/TBgbN3fruj", + "# Website: https://eternalcode.pl/", + "#", + "# Notification types:", + "# CHAT, ACTIONBAR, TITLE, SUBTITLE, NONE", + "#", + "# Usage: [CHAT, ACTIONBAR]test", + "# in this case, the message will be sent to the player's on chat and actionbar!", + "# ", + "# Usage: [NONE]test", + "# in this case, the message wont be sent!", + "# ", + "# We are supporting MiniMessage: https://docs.adventure.kyori.net/minimessage.html", + " " + }) public Argument argument = new Argument(); @Description(" ") @@ -31,106 +46,98 @@ public Resource resource(File folder) { @Contextual public static class Argument { - public String permission = "&cYou don't have permission! &7({PERMISSIONS})"; + public Notification permission = Notification.chat("&cYou don't have permission! &7({PERMISSIONS})"); @Description(" ") - public String correctUsage = "&eCorrect usage: &7{USAGE}"; + public Notification correctUsage = Notification.chat("&eCorrect usage: &7{USAGE}"); @Description(" ") - public String correctUsageList = "&7{USAGE}"; + public Notification correctUsageList = Notification.chat("&7{USAGE}"); @Description(" ") - public String offlinePlayer = "&cThat player is offline!"; + public Notification offlinePlayer = Notification.chat("&cThat player is offline!"); @Description(" ") - public String roomNotSet = "&cUse &e/check set &cto set check location!"; + public Notification roomNotSet = Notification.chat("&cUse &e/check setlocation &cto set check location!"); @Description(" ") - public String notChecking = "&cThis player wasn't checking"; + public Notification notChecking = Notification.chat("&cThis player wasn't checking"); @Description(" ") - public String isChecking = "&cThis player is checking now!"; + public Notification isChecking = Notification.chat("&cThis player is checking now!"); @Description(" ") - public String youArentChecked = "&cYou aren't being checked right now!"; + public Notification youArentChecked = Notification.chat("&cYou aren't being checked right now!"); @Description(" ") - public String cantUseCommand = "&cYou are being checked! You can use only: &e/helpop, &e/admit"; + public Notification cantUseCommand = Notification.chat("&cYou are being checked! You can use only: &e/helpop, &e/admit"); @Description(" ") - public String bypass = "&cThis player have bypass permission!"; + public Notification bypass = Notification.chat("&cThis player have bypass permission!"); } @Contextual public static class Check { - @Description(" ") - public String adminStart = "&aYou started checking {PLAYER}"; + @Description("# Available variables: {PLAYER}, {ADMIN}") - @Description(" ") - public String adminEnd = "&aYou ended checking {PLAYER}"; + public List startMessages = Arrays.asList( + Notification.chat("&cYou are checking by: {ADMIN}"), + Notification.title("&c{PLAYER}, You are checking now!"), + Notification.subtitle("&7By: {ADMIN}!") + ); - @Description(" ") - public String adminBan = "&aYou banned {PLAYER} for cheats"; + @Description("# Available variables: {PLAYER}, {ADMIN}") - @Description(" ") - public String adminLocationSet = "&aLocation check set!"; + public List taskMessages = Arrays.asList( + Notification.chat("&cYou are checking by: {ADMIN}"), + Notification.actionbar("&cYou are checking by: {ADMIN}"), + Notification.title("&cYou are checking!"), + Notification.subtitle("&7More info on chat!") + ); @Description(" ") - public String adminReload = "&aConfiguration reloaded!"; + public Admin admin = new Admin(); @Description(" ") public Broadcast broadcast = new Broadcast(); - @Description(" ") - public Start start = new Start(); - - @Description(" ") - public Task task = new Task(); - @Contextual - public static class Start { - - @Description("# Available variables: {PLAYER}, {ADMIN}") - - public String startTitle = "&c{PLAYER}, You are checking now!"; + public static class Admin { - public String startSubTitle = "&7By: {ADMIN}!"; - - public List startMessage = Collections.singletonList("&cYou are checking by: {ADMIN}"); - } - - @Contextual - public static class Task { + @Description(" ") + public Notification start = Notification.chat("&aYou started checking {PLAYER}"); - @Description("# Available variables: {PLAYER}, {ADMIN}") + @Description(" ") + public Notification end = Notification.chat("&aYou ended checking {PLAYER}"); - public String actionBar = "&cYou are checking!"; + @Description(" ") + public Notification ban = Notification.chat("&aYou banned {PLAYER} for cheats"); - public String title = "&cYou are checking!"; + @Description(" ") + public Notification locationSet = Notification.chat("&aLocation check set!"); - public String subTitle = "&7More info on chat!"; + @Description(" ") + public Notification reload = Notification.chat("&aConfiguration reloaded!"); - public List message = Collections.singletonList("&cYou are checking by: {ADMIN}"); } - @Contextual public static class Broadcast { @Description("# Available variables: {PLAYER}, {ADMIN}") - public List admit = Collections.singletonList("&cPlayer &e{PLAYER} &cconfessed to cheats!"); + public List admit = Collections.singletonList(Notification.chat("&cPlayer &e{PLAYER} &cconfessed to cheats!")); @Description(" ") - public List startCheck = Collections.singletonList("&cPlayer &e{PLAYER} &cis checking by &e{ADMIN}&c!"); + public List startCheck = Collections.singletonList(Notification.chat("&cPlayer &e{PLAYER} &cis checking by &e{ADMIN}&c!")); @Description(" ") - public List endCheck = Collections.singletonList("&cPlayer &e{PLAYER} &cwas checked by &e{ADMIN}&c!"); + public List endCheck = Collections.singletonList(Notification.chat("&cPlayer &e{PLAYER} &cwas checked by &e{ADMIN}&c!")); @Description(" ") - public List banCheck = Collections.singletonList("&cPlayer &e{PLAYER} &cwas checked and banned by &e{ADMIN}&c!"); + public List banCheck = Collections.singletonList(Notification.chat("&cPlayer &e{PLAYER} &cwas checked and banned by &e{ADMIN}&c!")); @Description(" ") - public List logoutCheck = Collections.singletonList("&cPlayer &e{PLAYER} &clogged out while checking!"); + public List logoutCheck = Collections.singletonList(Notification.chat("&cPlayer &e{PLAYER} &clogged out while checking!")); } } } diff --git a/src/main/java/com/eternalcode/check/config/implementation/PluginConfig.java b/src/main/java/com/eternalcode/check/config/implementation/PluginConfig.java index 1d8073c..95ab994 100644 --- a/src/main/java/com/eternalcode/check/config/implementation/PluginConfig.java +++ b/src/main/java/com/eternalcode/check/config/implementation/PluginConfig.java @@ -14,15 +14,10 @@ public class PluginConfig implements ReloadableConfig { - @Override - public Resource resource(File folder) { - return Source.of(folder, "config.yml"); - } - @Description({ "# ", "# EternalCheck - A simple plugin for checking suspicious players", "# ", - "# Discord: https://dc.eternalcode.pl/", + "# Discord: https://discord.gg/TBgbN3fruj", "# Website: https://eternalcode.pl/", " " }) @Description("# Settings") @@ -34,6 +29,10 @@ public Resource resource(File folder) { @Contextual public static class Settings { + @Description("# Whether the player after entering the server should receive information about the new version of the plugin?") + public boolean receiveUpdates = true; + + @Description(" ") public Runnable runnable = new Runnable(); @Description(" ") @@ -55,21 +54,8 @@ public static class Title { public Duration stay = Duration.ofSeconds(1); public Duration fadeIn = Duration.ofSeconds(2); public Duration fadeOut = Duration.ofSeconds(1); - - @Description({ " ", "# Whether the title at the start of checking should be enabled?" }) - public boolean startTitleMessageEnabled = true; - - @Description({ " ", "# Whether the title in the task should be enabled?" }) - public boolean taskTitleMessageEnabled = true; - } - @Description({ " ", "# Whether the message in the task should be enabled?" }) - public boolean taskMessageEnabled = true; - - @Description({ " ", "# Whether the actionbar in the task should be enabled?" }) - public boolean taskActionBarEnabled = true; - @Description({ " ", "# Whether the checked player is to see only the messages of the checking admin?" }) public boolean onlyMessageFromAdmin = true; @@ -80,7 +66,7 @@ public static class Title { public boolean canUseCommand = true; @Description({ " ", "# Available commands when checking,", - "# if canUseCommand is set to true" }) + "# if canUseCommand is set to false" }) public List availableCommands = Arrays.asList("/admit", "/helpop", "/przyznajsie"); } @@ -97,6 +83,11 @@ public static class Commands { public String ban = "tempban {PLAYER} 14d Having cheats!"; } - @Description({ " ", "# Check location - use \"/check set\" in game!" }) + @Description({ " ", "# Check location - use \"/check setlocation\" in game!" }) public Position checkLocation = new Position(0, 100, 0, 0.f, 0.f, "world"); + + @Override + public Resource resource(File folder) { + return Source.of(folder, "config.yml"); + } } diff --git a/src/main/java/com/eternalcode/check/notification/Notification.java b/src/main/java/com/eternalcode/check/notification/Notification.java new file mode 100644 index 0000000..ac5d16d --- /dev/null +++ b/src/main/java/com/eternalcode/check/notification/Notification.java @@ -0,0 +1,63 @@ +package com.eternalcode.check.notification; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class Notification { + + protected final Set types = new HashSet<>(); + protected final String message; + + protected Notification(String message, NotificationType... types) { + this.message = message; + this.types.addAll(Arrays.asList(types)); + } + + protected Notification(String message, Collection types) { + this.message = message; + this.types.addAll(types); + } + + public Set types() { + return Collections.unmodifiableSet(this.types); + } + + public String message() { + return this.message; + } + + public static Notification of(String message, NotificationType... types) { + return new Notification(message, types); + } + + public static Notification of(String message, Collection types) { + return new Notification(message, types); + } + + public static Notification actionbar(String message) { + return new Notification(message, NotificationType.ACTIONBAR); + } + + public static Notification chat(String message) { + return new Notification(message, NotificationType.CHAT); + } + + public static Notification title(String message) { + return new Notification(message, NotificationType.TITLE); + } + + public static Notification subtitle(String message) { + return new Notification(message, NotificationType.SUBTITLE); + } + + public static Notification bossbar(String message) { + return new Notification(message, NotificationType.BOSSBAR); + } + + public static Notification none() { + return new Notification("none", NotificationType.NONE); + } +} diff --git a/src/main/java/com/eternalcode/check/notification/NotificationAnnouncer.java b/src/main/java/com/eternalcode/check/notification/NotificationAnnouncer.java new file mode 100644 index 0000000..5f4878f --- /dev/null +++ b/src/main/java/com/eternalcode/check/notification/NotificationAnnouncer.java @@ -0,0 +1,103 @@ +package com.eternalcode.check.notification; + +import com.eternalcode.check.config.implementation.PluginConfig; +import net.kyori.adventure.audience.Audience; +import net.kyori.adventure.platform.AudienceProvider; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.title.Title; +import org.bukkit.Server; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import panda.utilities.text.Formatter; + +public class NotificationAnnouncer { + + private static final Formatter EMPTY_FORMATTER = new Formatter(); + + private final AudienceProvider audienceProvider; + private final MiniMessage miniMessage; + private final PluginConfig config; + private Title.Times titleTimes; + private final Server server; + + public NotificationAnnouncer(AudienceProvider audienceProvider, MiniMessage miniMessage, PluginConfig config, Server server) { + this.audienceProvider = audienceProvider; + this.miniMessage = miniMessage; + this.config = config; + this.server = server; + } + + public void sendAnnounce(CommandSender sender, Notification notification) { + this.sendAnnounce(sender, notification, EMPTY_FORMATTER); + } + + public void sendAnnounce(CommandSender sender, Notification notification, Formatter formatter) { + Audience audience = this.toAudience(sender); + + String message = formatter.format(notification.message()); + + for (NotificationType notificationType : notification.types()) { + switch (notificationType) { + case TITLE: { + Title title = Title.title(this.miniMessage.deserialize(message), Component.empty(), this.times()); + + audience.showTitle(title); + + break; + } + + case SUBTITLE: { + Title title = Title.title(Component.empty(), this.miniMessage.deserialize(message), this.times()); + + audience.showTitle(title); + + break; + } + + case ACTIONBAR: { + audience.sendActionBar(this.miniMessage.deserialize(message)); + + break; + } + + case CHAT: { + audience.sendMessage(this.miniMessage.deserialize(message)); + + break; + } + + case NONE: { + break; + } + } + } + + } + + public void sendAnnounceAll(Notification notification, Formatter formatter) { + this.server.getOnlinePlayers().forEach(player -> this.sendAnnounce(player, notification, formatter)); + } + + private Audience toAudience(CommandSender sender) { + if (sender instanceof Player) { + Player player = (Player) sender; + + return this.audienceProvider.player(player.getUniqueId()); + } + + return this.audienceProvider.console(); + } + + private Title.Times times() { + if (this.titleTimes == null) { + this.titleTimes = Title.Times.times( + this.config.settings.title.fadeIn, + this.config.settings.title.stay, + this.config.settings.title.fadeOut + ); + } + + return this.titleTimes; + } +} diff --git a/src/main/java/com/eternalcode/check/notification/NotificationType.java b/src/main/java/com/eternalcode/check/notification/NotificationType.java new file mode 100644 index 0000000..79db213 --- /dev/null +++ b/src/main/java/com/eternalcode/check/notification/NotificationType.java @@ -0,0 +1,12 @@ +package com.eternalcode.check.notification; + +public enum NotificationType { + + CHAT, + ACTIONBAR, + TITLE, + SUBTITLE, + BOSSBAR, + NONE + +} diff --git a/src/main/java/com/eternalcode/check/shared/legacy/Legacy.java b/src/main/java/com/eternalcode/check/shared/legacy/Legacy.java index 52fa842..0ebe3d5 100644 --- a/src/main/java/com/eternalcode/check/shared/legacy/Legacy.java +++ b/src/main/java/com/eternalcode/check/shared/legacy/Legacy.java @@ -17,10 +17,4 @@ public final class Legacy { public static Component component(String text) { return AMPERSAND_SERIALIZER.deserialize(text); } - - public static List component(List texts) { - return texts.stream() - .map(Legacy::component) - .collect(Collectors.toList()); - } } diff --git a/src/main/java/com/eternalcode/check/updater/UpdaterNotificationController.java b/src/main/java/com/eternalcode/check/updater/UpdaterNotificationController.java new file mode 100644 index 0000000..b81f1e2 --- /dev/null +++ b/src/main/java/com/eternalcode/check/updater/UpdaterNotificationController.java @@ -0,0 +1,49 @@ +package com.eternalcode.check.updater; + +import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; + +public class UpdaterNotificationController implements Listener { + + private static final Notification NEW_VERSION_AVAILABLE = Notification.chat("EternalCheck: New version of EternalCheck is available, please update!"); + + private final NotificationAnnouncer announcer; + private final UpdaterService updaterService; + private final PluginConfig pluginConfig; + + public UpdaterNotificationController(NotificationAnnouncer announcer, UpdaterService updaterService, PluginConfig pluginConfig) { + this.announcer = announcer; + this.updaterService = updaterService; + this.pluginConfig = pluginConfig; + } + + @EventHandler + void onJoin(PlayerJoinEvent event) { + Player player = event.getPlayer(); + + if (!this.pluginConfig.settings.receiveUpdates) { + return; + } + + if (!player.hasPermission("eternalcheck.receiveupdates")) { + return; + } + + this.updaterService.isUpToDate().whenComplete((isUpToDate, throwable) -> { + if (throwable != null) { + throwable.printStackTrace(); + + return; + } + + if (!isUpToDate) { + this.announcer.sendAnnounce(player, NEW_VERSION_AVAILABLE); + } + }); + } +} diff --git a/src/main/java/com/eternalcode/check/updater/UpdaterService.java b/src/main/java/com/eternalcode/check/updater/UpdaterService.java new file mode 100644 index 0000000..aec616a --- /dev/null +++ b/src/main/java/com/eternalcode/check/updater/UpdaterService.java @@ -0,0 +1,34 @@ +package com.eternalcode.check.updater; + +import com.eternalcode.gitcheck.GitCheck; +import com.eternalcode.gitcheck.GitCheckResult; +import com.eternalcode.gitcheck.git.GitRepository; +import com.eternalcode.gitcheck.git.GitTag; +import org.bukkit.plugin.PluginDescriptionFile; +import panda.std.Lazy; + +import java.util.concurrent.CompletableFuture; + +public class UpdaterService { + + private static final GitRepository GIT_REPOSITORY = GitRepository.of("EternalCodeTeam", "EternalCheck"); + + private final GitCheck gitCheck = new GitCheck(); + private final Lazy gitCheckResult; + + public UpdaterService(PluginDescriptionFile descriptionFile) { + this.gitCheckResult = new Lazy<>(() -> { + String version = descriptionFile.getVersion(); + + return this.gitCheck.checkRelease(GIT_REPOSITORY, GitTag.of("v" + version)); + }); + } + + public CompletableFuture isUpToDate() { + return CompletableFuture.supplyAsync(() -> { + GitCheckResult result = this.gitCheckResult.get(); + + return result.isUpToDate(); + }); + } +} diff --git a/src/main/java/com/eternalcode/check/user/CheckedUserService.java b/src/main/java/com/eternalcode/check/user/CheckedUserService.java index fa7415c..e4152fe 100644 --- a/src/main/java/com/eternalcode/check/user/CheckedUserService.java +++ b/src/main/java/com/eternalcode/check/user/CheckedUserService.java @@ -13,8 +13,12 @@ public class CheckedUserService { private final Map checkedUsers = new HashMap<>(); - public void markChecked(UUID uniqueId, String name, String admin, Position lastPosition) { - this.checkedUsers.put(uniqueId, new CheckedUser(uniqueId, name, admin, lastPosition)); + public CheckedUser markChecked(UUID uniqueId, String name, String admin, Position lastPosition) { + CheckedUser checkedUser = new CheckedUser(uniqueId, name, admin, lastPosition); + + this.checkedUsers.put(uniqueId, checkedUser); + + return checkedUser; } public Optional find(UUID uniqueId) { @@ -25,7 +29,7 @@ public void unmarkChecked(UUID uniqueId) { this.checkedUsers.remove(uniqueId); } - public Collection getUsers() { + public Collection checkedUsers() { return Collections.unmodifiableCollection(this.checkedUsers.values()); } } diff --git a/src/main/java/com/eternalcode/check/controller/CheckedUserChatController.java b/src/main/java/com/eternalcode/check/user/controller/CheckedUserChatController.java similarity index 90% rename from src/main/java/com/eternalcode/check/controller/CheckedUserChatController.java rename to src/main/java/com/eternalcode/check/user/controller/CheckedUserChatController.java index 80c18f3..c79ee39 100644 --- a/src/main/java/com/eternalcode/check/controller/CheckedUserChatController.java +++ b/src/main/java/com/eternalcode/check/user/controller/CheckedUserChatController.java @@ -1,4 +1,4 @@ -package com.eternalcode.check.controller; +package com.eternalcode.check.user.controller; import com.eternalcode.check.config.implementation.PluginConfig; import com.eternalcode.check.user.CheckedUser; @@ -13,12 +13,12 @@ public class CheckedUserChatController implements Listener { - private final PluginConfig config; private final CheckedUserService checkedUserService; + private final PluginConfig config; - public CheckedUserChatController(PluginConfig config, CheckedUserService checkedUserService) { - this.config = config; + public CheckedUserChatController(CheckedUserService checkedUserService, PluginConfig config) { this.checkedUserService = checkedUserService; + this.config = config; } @EventHandler @@ -47,7 +47,6 @@ public void onChat(AsyncPlayerChatEvent event) { } event.getRecipients().remove(recipient); - } } } diff --git a/src/main/java/com/eternalcode/check/controller/CheckedUserCommandController.java b/src/main/java/com/eternalcode/check/user/controller/CheckedUserCommandController.java similarity index 67% rename from src/main/java/com/eternalcode/check/controller/CheckedUserCommandController.java rename to src/main/java/com/eternalcode/check/user/controller/CheckedUserCommandController.java index ddadaa7..bccd557 100644 --- a/src/main/java/com/eternalcode/check/controller/CheckedUserCommandController.java +++ b/src/main/java/com/eternalcode/check/user/controller/CheckedUserCommandController.java @@ -1,8 +1,8 @@ -package com.eternalcode.check.controller; +package com.eternalcode.check.user.controller; -import com.eternalcode.check.NotificationAnnouncer; import com.eternalcode.check.config.implementation.MessagesConfig; import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.NotificationAnnouncer; import com.eternalcode.check.user.CheckedUserService; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -13,17 +13,16 @@ public class CheckedUserCommandController implements Listener { - private final MessagesConfig messages; - private final PluginConfig config; private final CheckedUserService checkedUserService; private final NotificationAnnouncer announcer; + private final MessagesConfig messages; + private final PluginConfig config; - - public CheckedUserCommandController(MessagesConfig messages, PluginConfig config, CheckedUserService checkedUserService, NotificationAnnouncer announcer) { - this.messages = messages; - this.config = config; + public CheckedUserCommandController(CheckedUserService checkedUserService, NotificationAnnouncer announcer, MessagesConfig messages, PluginConfig config) { this.checkedUserService = checkedUserService; this.announcer = announcer; + this.messages = messages; + this.config = config; } @EventHandler @@ -37,13 +36,14 @@ public void onCommand(PlayerCommandPreprocessEvent event) { String command = event.getMessage().split(" ")[0]; this.checkedUserService.find(uniqueId).ifPresent(user -> { - if (this.config.settings.availableCommands.contains(command)) { - return; - } - - event.setCancelled(true); - this.announcer.announceMessage(uniqueId, this.messages.argument.cantUseCommand); + for (String allowedCommand : this.config.settings.availableCommands) { + if (command.startsWith(allowedCommand)) { + return; + } + event.setCancelled(true); + this.announcer.sendAnnounce(player, this.messages.argument.cantUseCommand); + } }); } } diff --git a/src/main/java/com/eternalcode/check/controller/CheckedUserLogoutPunishmentController.java b/src/main/java/com/eternalcode/check/user/controller/CheckedUserLogoutPunishmentController.java similarity index 64% rename from src/main/java/com/eternalcode/check/controller/CheckedUserLogoutPunishmentController.java rename to src/main/java/com/eternalcode/check/user/controller/CheckedUserLogoutPunishmentController.java index 6179b75..767e6fb 100644 --- a/src/main/java/com/eternalcode/check/controller/CheckedUserLogoutPunishmentController.java +++ b/src/main/java/com/eternalcode/check/user/controller/CheckedUserLogoutPunishmentController.java @@ -1,9 +1,12 @@ -package com.eternalcode.check.controller; +package com.eternalcode.check.user.controller; -import com.eternalcode.check.NotificationAnnouncer; +import com.eternalcode.check.caller.EventCaller; import com.eternalcode.check.config.implementation.MessagesConfig; import com.eternalcode.check.config.implementation.PluginConfig; +import com.eternalcode.check.notification.Notification; +import com.eternalcode.check.notification.NotificationAnnouncer; import com.eternalcode.check.user.CheckedUserService; +import com.eternalcode.check.user.event.CheckedUserLogoutEvent; import org.bukkit.Server; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -16,18 +19,20 @@ public class CheckedUserLogoutPunishmentController implements Listener { + private final CheckedUserService checkedUserService; + private final NotificationAnnouncer announcer; + private final EventCaller eventCaller; private final MessagesConfig messages; private final PluginConfig config; - private final CheckedUserService checkedUserService; private final Server server; - private final NotificationAnnouncer announcer; - public CheckedUserLogoutPunishmentController(MessagesConfig messages, PluginConfig config, CheckedUserService checkedUserService, Server server, NotificationAnnouncer announcer) { + public CheckedUserLogoutPunishmentController(CheckedUserService checkedUserService, NotificationAnnouncer announcer, MessagesConfig messages, PluginConfig config, EventCaller eventCaller, Server server) { + this.checkedUserService = checkedUserService; + this.announcer = announcer; this.messages = messages; this.config = config; - this.checkedUserService = checkedUserService; + this.eventCaller = eventCaller; this.server = server; - this.announcer = announcer; } @EventHandler @@ -36,18 +41,21 @@ public void onQuit(PlayerQuitEvent event) { UUID uniqueId = player.getUniqueId(); this.checkedUserService.find(uniqueId).ifPresent(user -> { - Formatter formatter = new Formatter() .register("{PLAYER}", user.getName()) .register("{ADMIN}", player.getName()); - for (Player all : this.server.getOnlinePlayers()) { - this.messages.check.broadcast.logoutCheck.forEach(message -> this.announcer.announceMessage(all.getUniqueId(), formatter.format(message))); + for (Notification notification : this.messages.check.broadcast.logoutCheck) { + this.announcer.sendAnnounce(player, notification, formatter); } this.server.dispatchCommand(this.server.getConsoleSender(), StringUtils.replace(this.config.commands.logout, "{PLAYER}", player.getName())); this.checkedUserService.unmarkChecked(uniqueId); + + Player admin = this.server.getPlayer(user.getChecker()); + + this.eventCaller.callEvent(new CheckedUserLogoutEvent(user, admin)); }); } } \ No newline at end of file diff --git a/src/main/java/com/eternalcode/check/controller/CheckedUserMoveController.java b/src/main/java/com/eternalcode/check/user/controller/CheckedUserMoveController.java similarity index 85% rename from src/main/java/com/eternalcode/check/controller/CheckedUserMoveController.java rename to src/main/java/com/eternalcode/check/user/controller/CheckedUserMoveController.java index baa9418..a35ce09 100644 --- a/src/main/java/com/eternalcode/check/controller/CheckedUserMoveController.java +++ b/src/main/java/com/eternalcode/check/user/controller/CheckedUserMoveController.java @@ -1,4 +1,4 @@ -package com.eternalcode.check.controller; +package com.eternalcode.check.user.controller; import com.eternalcode.check.config.implementation.PluginConfig; import com.eternalcode.check.user.CheckedUserService; @@ -11,12 +11,12 @@ public class CheckedUserMoveController implements Listener { - private final PluginConfig config; private final CheckedUserService checkedUserService; + private final PluginConfig config; - public CheckedUserMoveController(PluginConfig config, CheckedUserService checkedUserService) { - this.config = config; + public CheckedUserMoveController(CheckedUserService checkedUserService, PluginConfig config) { this.checkedUserService = checkedUserService; + this.config = config; } @EventHandler diff --git a/src/main/java/com/eternalcode/check/user/event/CheckedUserAdmitEvent.java b/src/main/java/com/eternalcode/check/user/event/CheckedUserAdmitEvent.java new file mode 100644 index 0000000..2b5fa57 --- /dev/null +++ b/src/main/java/com/eternalcode/check/user/event/CheckedUserAdmitEvent.java @@ -0,0 +1,24 @@ +package com.eternalcode.check.user.event; + +import com.eternalcode.check.user.CheckedUser; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class CheckedUserAdmitEvent extends CheckedUserEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + public CheckedUserAdmitEvent(CheckedUser checkedUser, Player admin) { + super(checkedUser, admin); + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static @NotNull HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/eternalcode/check/user/event/CheckedUserBanEvent.java b/src/main/java/com/eternalcode/check/user/event/CheckedUserBanEvent.java new file mode 100644 index 0000000..6683c0c --- /dev/null +++ b/src/main/java/com/eternalcode/check/user/event/CheckedUserBanEvent.java @@ -0,0 +1,24 @@ +package com.eternalcode.check.user.event; + +import com.eternalcode.check.user.CheckedUser; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class CheckedUserBanEvent extends CheckedUserEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + public CheckedUserBanEvent(CheckedUser checkedUser, Player admin) { + super(checkedUser, admin); + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static @NotNull HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/eternalcode/check/user/event/CheckedUserEndEvent.java b/src/main/java/com/eternalcode/check/user/event/CheckedUserEndEvent.java new file mode 100644 index 0000000..3f0f94e --- /dev/null +++ b/src/main/java/com/eternalcode/check/user/event/CheckedUserEndEvent.java @@ -0,0 +1,24 @@ +package com.eternalcode.check.user.event; + +import com.eternalcode.check.user.CheckedUser; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class CheckedUserEndEvent extends CheckedUserEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + public CheckedUserEndEvent(CheckedUser checkedUser, Player admin) { + super(checkedUser, admin); + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static @NotNull HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/eternalcode/check/user/event/CheckedUserEvent.java b/src/main/java/com/eternalcode/check/user/event/CheckedUserEvent.java new file mode 100644 index 0000000..cb3e94c --- /dev/null +++ b/src/main/java/com/eternalcode/check/user/event/CheckedUserEvent.java @@ -0,0 +1,25 @@ +package com.eternalcode.check.user.event; + +import com.eternalcode.check.user.CheckedUser; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; + +public abstract class CheckedUserEvent extends Event { + + private final CheckedUser checkedUser; + private final Player admin; + + protected CheckedUserEvent(CheckedUser checkedUser, Player admin) { + this.checkedUser = checkedUser; + this.admin = admin; + } + + public CheckedUser getCheckedUser() { + return this.checkedUser; + } + + public Player getAdmin() { + return this.admin; + } +} diff --git a/src/main/java/com/eternalcode/check/user/event/CheckedUserLogoutEvent.java b/src/main/java/com/eternalcode/check/user/event/CheckedUserLogoutEvent.java new file mode 100644 index 0000000..44b63b5 --- /dev/null +++ b/src/main/java/com/eternalcode/check/user/event/CheckedUserLogoutEvent.java @@ -0,0 +1,24 @@ +package com.eternalcode.check.user.event; + +import com.eternalcode.check.user.CheckedUser; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class CheckedUserLogoutEvent extends CheckedUserEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + public CheckedUserLogoutEvent(CheckedUser checkedUser, Player admin) { + super(checkedUser, admin); + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static @NotNull HandlerList getHandlerList() { + return HANDLER_LIST; + } +} diff --git a/src/main/java/com/eternalcode/check/user/event/CheckedUserStartEvent.java b/src/main/java/com/eternalcode/check/user/event/CheckedUserStartEvent.java new file mode 100644 index 0000000..f084a6b --- /dev/null +++ b/src/main/java/com/eternalcode/check/user/event/CheckedUserStartEvent.java @@ -0,0 +1,24 @@ +package com.eternalcode.check.user.event; + +import com.eternalcode.check.user.CheckedUser; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class CheckedUserStartEvent extends CheckedUserEvent { + + private static final HandlerList HANDLER_LIST = new HandlerList(); + + public CheckedUserStartEvent(CheckedUser checkedUser, Player admin) { + super(checkedUser, admin); + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLER_LIST; + } + + public static @NotNull HandlerList getHandlerList() { + return HANDLER_LIST; + } +}