Skip to content

Commit

Permalink
Continuation of writing the plugin.
Browse files Browse the repository at this point in the history
  • Loading branch information
imDMK committed Aug 14, 2023
1 parent e48094b commit 0757c6c
Show file tree
Hide file tree
Showing 48 changed files with 1,256 additions and 673 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {

implementation('eu.okaeri:okaeri-configs-yaml-bukkit:5.0.0-beta.5')
implementation('eu.okaeri:okaeri-configs-serdes-commons:5.0.0-beta.5')
implementation('eu.okaeri:okaeri-configs-serdes-bukkit:5.0.0-beta.5')

implementation('net.kyori:adventure-platform-bukkit:4.3.0')
implementation('net.kyori:adventure-text-minimessage:4.14.0')
Expand All @@ -54,7 +55,7 @@ bukkit {
name = "AutoMessage"
version = "${project.version}"
apiVersion = "1.17"
main = "me.dmk.automessage.AutoMessage"
main = "com.github.imdmk.automessage.AutoMessagePlugin"
author = "DMK"
description = "Simple auto message plugin"
website = "https://github.com/imDMK/AutoMessage"
Expand Down
132 changes: 132 additions & 0 deletions src/main/java/com/github/imdmk/automessage/AutoMessage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.github.imdmk.automessage;

import com.github.imdmk.automessage.command.AutoMessageCommand;
import com.github.imdmk.automessage.command.AutoMessageCreateCommand;
import com.github.imdmk.automessage.command.AutoMessageListCommand;
import com.github.imdmk.automessage.command.AutoMessageRemoveCommand;
import com.github.imdmk.automessage.command.argument.NotificationArgument;
import com.github.imdmk.automessage.command.argument.NotificationTypeArgument;
import com.github.imdmk.automessage.command.handler.MissingPermissionHandler;
import com.github.imdmk.automessage.command.handler.NotificationHandler;
import com.github.imdmk.automessage.command.handler.UsageHandler;
import com.github.imdmk.automessage.configuration.PluginConfiguration;
import com.github.imdmk.automessage.configuration.serializer.TitleTimesSerializer;
import com.github.imdmk.automessage.notification.Notification;
import com.github.imdmk.automessage.notification.NotificationSender;
import com.github.imdmk.automessage.notification.NotificationType;
import com.github.imdmk.automessage.notification.configuration.NotificationSerializer;
import com.github.imdmk.automessage.notification.implementation.bossbar.BossBarManager;
import com.github.imdmk.automessage.notification.implementation.bossbar.BossBarTask;
import com.github.imdmk.automessage.notification.task.AutoNotificationTask;
import com.github.imdmk.automessage.scheduler.TaskScheduler;
import com.github.imdmk.automessage.scheduler.TaskSchedulerImpl;
import com.github.imdmk.automessage.util.DurationUtil;
import com.google.common.base.Stopwatch;
import dev.rollczi.litecommands.LiteCommands;
import dev.rollczi.litecommands.bukkit.adventure.platform.LiteBukkitAdventurePlatformFactory;
import dev.rollczi.litecommands.bukkit.tools.BukkitOnlyPlayerContextual;
import eu.okaeri.configs.ConfigManager;
import eu.okaeri.configs.serdes.commons.SerdesCommons;
import eu.okaeri.configs.yaml.bukkit.YamlBukkitConfigurer;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import java.io.File;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

public class AutoMessage {

private final Server server;

private final PluginConfiguration pluginConfiguration;

private final BukkitAudiences bukkitAudiences;
private final NotificationSender notificationSender;

private final BossBarManager bossBarManager;

private final TaskScheduler taskScheduler;

private LiteCommands<CommandSender> liteCommands;

public AutoMessage(Plugin plugin) {
Stopwatch stopwatch = Stopwatch.createStarted();
Logger logger = plugin.getLogger();

this.server = plugin.getServer();

/* Configuration */
this.pluginConfiguration = this.createConfiguration(plugin.getDataFolder());

/* Adventure */
this.bukkitAudiences = BukkitAudiences.create(plugin);
this.notificationSender = new NotificationSender(this.bukkitAudiences);

/* Managers */
this.bossBarManager = new BossBarManager();

/* Tasks */
this.taskScheduler = new TaskSchedulerImpl(plugin, this.server);

long delay = DurationUtil.toTicks(this.pluginConfiguration.notificationConfiguration.autoMessagesDelay);
this.taskScheduler.runTimerAsync(new AutoNotificationTask(this.pluginConfiguration.notificationConfiguration, this.notificationSender), delay, delay);
this.taskScheduler.runTimerAsync(new BossBarTask(this.bukkitAudiences, this.bossBarManager), 0L, DurationUtil.toTicks(Duration.ofSeconds(1)));

/* Commands */
if (this.pluginConfiguration.autoMessageCommandEnabled) {
this.liteCommands = this.registerLiteCommands();
}

/* Update service */

logger.info("Enabled plugin in " + stopwatch.stop().elapsed(TimeUnit.MILLISECONDS) + "ms.");
}

public void onDisable() {
this.bukkitAudiences.close();

if (this.liteCommands != null) {
this.liteCommands.getPlatform().unregisterAll();
}
}

private PluginConfiguration createConfiguration(File dataFolder) {
return ConfigManager.create(PluginConfiguration.class, (it) -> {
it.withConfigurer(new YamlBukkitConfigurer(), new SerdesCommons());
it.withSerdesPack(registry -> {
registry.register(new TitleTimesSerializer());
registry.register(new NotificationSerializer());
});
it.withBindFile(new File(dataFolder, "configuration.yml"));
it.withRemoveOrphans(true);
it.saveDefaults();
it.load(true);
});
}

private LiteCommands<CommandSender> registerLiteCommands() {
return LiteBukkitAdventurePlatformFactory.builder(this.server, "AutoMessage", false, this.bukkitAudiences, true)
.contextualBind(Player.class, new BukkitOnlyPlayerContextual<>("Command only for player"))

.argument(NotificationType.class, new NotificationTypeArgument(this.pluginConfiguration.notificationConfiguration))
.argument(Notification.class, new NotificationArgument(this.pluginConfiguration.notificationConfiguration))

.invalidUsageHandler(new UsageHandler(this.pluginConfiguration.notificationConfiguration, this.notificationSender))
.permissionHandler(new MissingPermissionHandler(this.pluginConfiguration.notificationConfiguration, this.notificationSender))
.resultHandler(Notification.class, new NotificationHandler(this.notificationSender))

.commandInstance(
new AutoMessageCommand(this.pluginConfiguration.notificationConfiguration, this.notificationSender),
new AutoMessageCreateCommand(this.pluginConfiguration.notificationConfiguration, this.notificationSender),
new AutoMessageListCommand(this.pluginConfiguration.notificationConfiguration, this.notificationSender),
new AutoMessageRemoveCommand(this.pluginConfiguration.notificationConfiguration, this.notificationSender)
)

.register();
}
}
18 changes: 18 additions & 0 deletions src/main/java/com/github/imdmk/automessage/AutoMessagePlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.github.imdmk.automessage;

import org.bukkit.plugin.java.JavaPlugin;

public class AutoMessagePlugin extends JavaPlugin {

private AutoMessage autoMessage;

@Override
public void onEnable() {
this.autoMessage = new AutoMessage(this);
}

@Override
public void onDisable() {
this.autoMessage.onDisable();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.github.imdmk.automessage.command;

import com.github.imdmk.automessage.notification.NotificationSender;
import com.github.imdmk.automessage.notification.configuration.NotificationConfiguration;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.route.Route;
import org.bukkit.command.CommandSender;

import java.time.Duration;

@Route(name = "automessage")
public class AutoMessageCommand {

private final NotificationConfiguration notificationConfiguration;
private final NotificationSender notificationSender;

public AutoMessageCommand(NotificationConfiguration notificationConfiguration, NotificationSender notificationSender) {
this.notificationConfiguration = notificationConfiguration;
this.notificationSender = notificationSender;
}

@Execute(route = "enable")
public void enable(CommandSender sender) {
this.notificationConfiguration.autoMessagesEnabled = true;

this.notificationSender.sendNotification(sender, this.notificationConfiguration.autoMessagesEnabledNotification);
}

@Execute(route = "disable")
public void disable(CommandSender sender) {
this.notificationConfiguration.autoMessagesEnabled = false;

this.notificationSender.sendNotification(sender, this.notificationConfiguration.autoMessagesDisabledNotification);
}

@Execute(route = "delay")
public void delay(CommandSender sender, Duration delay) {
this.notificationConfiguration.autoMessagesDelay = delay;

this.notificationSender.sendNotification(sender, this.notificationConfiguration.autoMessagesChangedDelayNotification);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.github.imdmk.automessage.command;

import com.github.imdmk.automessage.notification.Notification;
import com.github.imdmk.automessage.notification.NotificationFormatter;
import com.github.imdmk.automessage.notification.NotificationSender;
import com.github.imdmk.automessage.notification.configuration.NotificationConfiguration;
import com.github.imdmk.automessage.notification.implementation.ActionBarNotification;
import com.github.imdmk.automessage.notification.implementation.ChatNotification;
import com.github.imdmk.automessage.notification.implementation.TitleNotification;
import com.github.imdmk.automessage.notification.implementation.bossbar.BossBarNotification;
import dev.rollczi.litecommands.argument.Arg;
import dev.rollczi.litecommands.argument.Name;
import dev.rollczi.litecommands.argument.joiner.Joiner;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.route.Route;
import net.kyori.adventure.bossbar.BossBar;
import org.bukkit.command.CommandSender;

import java.time.Duration;
import java.util.Arrays;

@Route(name = "automessage create")
public class AutoMessageCreateCommand {

private final NotificationConfiguration notificationConfiguration;
private final NotificationSender notificationSender;

public AutoMessageCreateCommand(NotificationConfiguration notificationConfiguration, NotificationSender notificationSender) {
this.notificationConfiguration = notificationConfiguration;
this.notificationSender = notificationSender;
}

@Execute(route = "CHAT")
void createChat(CommandSender sender, @Joiner @Name("message") String message) {
ChatNotification chatNotification = new ChatNotification(message);

this.notificationConfiguration.autoMessages.add(chatNotification);

this.notificationSender.sendNotification(sender, this.createAddedNotification(chatNotification));
}

@Execute(route = "ACTIONBAR")
void createActionbar(CommandSender sender, @Joiner @Name("message") String message) {
ActionBarNotification actionBarNotification = new ActionBarNotification(message);

this.notificationConfiguration.autoMessages.add(actionBarNotification);

this.notificationSender.sendNotification(sender, this.createAddedNotification(actionBarNotification));
}

@Execute(route = "TITLE")
void createTitle(CommandSender sender, @Joiner @Name("message") String message) {
System.out.println(message);

String[] splitMessage = message.split("\\|");

System.out.println(Arrays.toString(splitMessage));

if (splitMessage[0] == null || splitMessage[1] == null) {
this.notificationSender.sendNotification(sender, this.notificationConfiguration.invalidTitleMessageNotification);
return;
}

TitleNotification titleNotification = new TitleNotification(splitMessage[0], splitMessage[1]);

this.notificationConfiguration.autoMessages.add(titleNotification);

this.notificationSender.sendNotification(sender, this.createAddedNotification(titleNotification));
}

@Execute(route = "BOSSBAR")
void createBossBar(CommandSender sender, @Arg @Name("time") Duration time, @Arg @Name("progress") float progress, @Arg @Name("color") BossBar.Color color, @Arg @Name("overlay") BossBar.Overlay overlay, @Joiner @Name("name") String name) {
BossBarNotification bossBarNotification = new BossBarNotification(name, time, progress, color, overlay);

this.notificationConfiguration.autoMessages.add(bossBarNotification);

this.notificationSender.sendNotification(sender, this.createAddedNotification(bossBarNotification));
}

private Notification createAddedNotification(Notification notification) {
return new NotificationFormatter(this.notificationConfiguration.autoMessageAddedNotification)
.placeholder("{TYPE}", notification.type().name().toUpperCase())
.placeholder("{POSITION}", this.notificationConfiguration.autoMessages.size() + 1)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.github.imdmk.automessage.command;

import com.github.imdmk.automessage.notification.Notification;
import com.github.imdmk.automessage.notification.NotificationFormatter;
import com.github.imdmk.automessage.notification.NotificationSender;
import com.github.imdmk.automessage.notification.configuration.NotificationConfiguration;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.route.Route;
import org.bukkit.command.CommandSender;

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

@Route(name = "automessage")
public class AutoMessageListCommand {

private final NotificationConfiguration notificationConfiguration;
private final NotificationSender notificationSender;

public AutoMessageListCommand(NotificationConfiguration notificationConfiguration, NotificationSender notificationSender) {
this.notificationConfiguration = notificationConfiguration;
this.notificationSender = notificationSender;
}

@Execute(route = "list")
void listAutoMessages(CommandSender sender) {
List<Notification> autoMessages = this.notificationConfiguration.autoMessages;

if (autoMessages.isEmpty()) {
this.notificationSender.sendNotification(sender, this.notificationConfiguration.autoMessagesEmptyNotification);
return;
}

AtomicInteger position = new AtomicInteger(1);

for (Notification notification : autoMessages) {
position.incrementAndGet();

Notification autoMessagesList = new NotificationFormatter(this.notificationConfiguration.autoMessagesListNotification)
.placeholder("{POSITION}", position.get())
.placeholder("{NOTIFICATION}", notification.format())
.build();

this.notificationSender.sendNotification(sender, autoMessagesList);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.github.imdmk.automessage.command;

import com.github.imdmk.automessage.notification.Notification;
import com.github.imdmk.automessage.notification.NotificationSender;
import com.github.imdmk.automessage.notification.configuration.NotificationConfiguration;
import dev.rollczi.litecommands.argument.Arg;
import dev.rollczi.litecommands.command.execute.Execute;
import dev.rollczi.litecommands.command.route.Route;
import org.bukkit.command.CommandSender;

@Route(name = "automessage")
public class AutoMessageRemoveCommand {

private final NotificationConfiguration notificationConfiguration;
private final NotificationSender notificationSender;

public AutoMessageRemoveCommand(NotificationConfiguration notificationConfiguration, NotificationSender notificationSender) {
this.notificationConfiguration = notificationConfiguration;
this.notificationSender = notificationSender;
}

@Execute(route = "remove")
void remove(CommandSender sender, @Arg Notification notification) {
this.notificationConfiguration.autoMessages.remove(notification);

this.notificationSender.sendNotification(sender, this.notificationConfiguration.autoMessageRemovedNotification);
}
}
Loading

0 comments on commit 0757c6c

Please sign in to comment.