Skip to content
This repository has been archived by the owner on Oct 2, 2024. It is now read-only.

Commit

Permalink
GH-80 Refactor notification system, add new events and more (#80)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
P1otrulla authored Mar 11, 2023
1 parent 31bfd8d commit 93fed4d
Show file tree
Hide file tree
Showing 43 changed files with 1,261 additions and 456 deletions.
7 changes: 5 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaCompile> {
Expand Down
44 changes: 17 additions & 27 deletions src/main/java/com/eternalcode/check/CheckNotificationTask.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Expand Down
126 changes: 82 additions & 44 deletions src/main/java/com/eternalcode/check/EternalCheck.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -48,80 +61,101 @@ public final class EternalCheck extends JavaPlugin {

private CheckedUserService checkedUserService;

private EventCaller eventCaller;

private UpdaterService updaterService;

private LiteCommands<CommandSender> 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
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;
}
Expand All @@ -130,10 +164,6 @@ public MessagesConfig getMessagesConfig() {
return this.messages;
}

public CheckedUserDataConfig getDataConfig() {
return this.data;
}

public AudienceProvider getAudienceProvider() {
return this.audienceProvider;
}
Expand All @@ -150,6 +180,14 @@ public CheckedUserService getUserService() {
return this.checkedUserService;
}

public EventCaller getEventCaller() {
return this.eventCaller;
}

public UpdaterService getUpdaterService() {
return this.updaterService;
}

public LiteCommands<CommandSender> getLiteCommands() {
return this.liteCommands;
}
Expand Down
37 changes: 0 additions & 37 deletions src/main/java/com/eternalcode/check/NotificationAnnouncer.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/com/eternalcode/check/caller/EventCaller.java
Original file line number Diff line number Diff line change
@@ -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 extends CheckedUserEvent> T callEvent(T event) {
this.server.getPluginManager().callEvent(event);

return event;
}
}
Loading

0 comments on commit 93fed4d

Please sign in to comment.