-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
1,256 additions
and
673 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
src/main/java/com/github/imdmk/automessage/AutoMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
src/main/java/com/github/imdmk/automessage/AutoMessagePlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
src/main/java/com/github/imdmk/automessage/command/AutoMessageCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
src/main/java/com/github/imdmk/automessage/command/AutoMessageCreateCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/github/imdmk/automessage/command/AutoMessageListCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/github/imdmk/automessage/command/AutoMessageRemoveCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.