Skip to content

Commit

Permalink
move announcements to the main configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
fabianmakila committed Nov 10, 2024
1 parent 57683f2 commit 527b852
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 50 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
public final class ConfigManager {
private final Logger logger;
private final ConfigurationHelper<ProxyChatConfig> mainConfigHelper;
private final ConfigurationHelper<AnnouncementsConfig> announcementsConfigHelper;
private final ConfigurationHelper<ChannelsConfig> channelsConfigHelper;

private volatile ProxyChatConfig mainConfigData;
private volatile AnnouncementsConfig announcementsConfigData;
private volatile ChannelsConfig channelsConfigData;

public ConfigManager(ProxyChat proxyChat) {
Expand Down Expand Up @@ -52,16 +50,6 @@ public ConfigManager(ProxyChat proxyChat) {
)
);

this.announcementsConfigHelper = new ConfigurationHelper<>(
dataDirectory,
"announcements.yml",
SnakeYamlConfigurationFactory.create(
AnnouncementsConfig.class,
ConfigurationOptions.defaults(),
yamlOptions
)
);

this.channelsConfigHelper = new ConfigurationHelper<>(
dataDirectory,
"channels.yml",
Expand All @@ -76,14 +64,12 @@ public ConfigManager(ProxyChat proxyChat) {
public void reload() {
try {
this.mainConfigData = this.mainConfigHelper.reloadConfigData();
this.announcementsConfigData = this.announcementsConfigHelper.reloadConfigData();
this.channelsConfigData = this.channelsConfigHelper.reloadConfigData();
} catch (IOException ex) {
throw new UncheckedIOException(ex);

} catch (ConfigFormatSyntaxException ex) {
this.mainConfigData = this.mainConfigHelper.getFactory().loadDefaults();
this.announcementsConfigData = this.announcementsConfigHelper.getFactory().loadDefaults();
this.channelsConfigData = this.channelsConfigHelper.getFactory().loadDefaults();
this.logger.error(
"The yaml syntax in your configuration is invalid. " +
Expand All @@ -92,7 +78,6 @@ public void reload() {
);
} catch (InvalidConfigException ex) {
this.mainConfigData = this.mainConfigHelper.getFactory().loadDefaults();
this.announcementsConfigData = this.announcementsConfigHelper.getFactory().loadDefaults();
this.channelsConfigData = this.channelsConfigHelper.getFactory().loadDefaults();
this.logger.error(
"One of the values in your configuration is not valid. " +
Expand All @@ -110,14 +95,6 @@ public ProxyChatConfig mainConfig() {
return configData;
}

public AnnouncementsConfig announcementsConfig() {
AnnouncementsConfig configData = this.announcementsConfigData;
if (configData == null) {
throw new IllegalStateException("Configuration has not been loaded yet");
}
return configData;
}

public ChannelsConfig channelsConfig() {
ChannelsConfig configData = this.channelsConfigData;
if (configData == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface ProxyChatConfig {
@ConfDefault.DefaultBoolean(false)
boolean globalChat();

AnnouncementsSection announcements();

@SubSection
interface FormatSection {
@ConfDefault.DefaultString("<gold>[<green>Broadcast</green>]</gold> <message>")
Expand All @@ -34,4 +36,19 @@ interface FormatSection {
@ConfDefault.DefaultString("<gold>[<aqua><sender> -> <receiver></aqua>]</gold> <gray><message>")
String msgSpy();
}

@SubSection
interface AnnouncementsSection {
@ConfDefault.DefaultStrings({})
List<String> messages();

@ConfDefault.DefaultInteger(30)
int interval();

@ConfDefault.DefaultBoolean(false)
boolean random();

@ConfDefault.DefaultString("<gray>[<green>!</green>]</gray> <message>")
String format();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package fi.fabianadrian.proxychat.common.service;

import fi.fabianadrian.proxychat.common.ProxyChat;
import fi.fabianadrian.proxychat.common.config.AnnouncementsConfig;
import fi.fabianadrian.proxychat.common.config.ProxyChatConfig;
import fi.fabianadrian.proxychat.common.user.User;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;

import java.util.concurrent.*;

Expand All @@ -13,20 +14,20 @@ public final class AnnouncementService {
private final ProxyChat proxyChat;
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private final MiniMessage miniMessage = MiniMessage.miniMessage();
private AnnouncementsConfig config;
private ProxyChatConfig.AnnouncementsSection config;
private int index = 0;
private ScheduledFuture<?> scheduledTask;

public AnnouncementService(ProxyChat proxyChat) {
this.proxyChat = proxyChat;
this.config = proxyChat.configManager().announcementsConfig();
this.config = proxyChat.configManager().mainConfig().announcements();
}

public void reload() {
this.config = this.proxyChat.configManager().announcementsConfig();
this.config = this.proxyChat.configManager().mainConfig().announcements();

// No need to run the task if there are no announcements
if (config.announcements().isEmpty()) {
if (config.messages().isEmpty()) {
if (this.scheduledTask != null) {
this.scheduledTask.cancel(false);
}
Expand All @@ -41,10 +42,14 @@ public void reload() {

public void sendAnnouncement() {
this.index = this.config.random() ?
ThreadLocalRandom.current().nextInt(this.config.announcements().size()) :
(this.index >= this.config.announcements().size() - 1) ? 0 : this.index + 1;
ThreadLocalRandom.current().nextInt(this.config.messages().size()) :
(this.index >= this.config.messages().size() - 1) ? 0 : this.index + 1;

Component announcement = this.miniMessage.deserialize(
this.config.format(),
Placeholder.parsed("message", this.config.messages().get(this.index))
);

Component announcement = this.miniMessage.deserialize(this.config.prefix() + this.config.announcements().get(this.index));
for (User user : this.proxyChat.userManager().users()) {
if (user.messageSettings().announcements()) {
user.sendMessage(announcement);
Expand Down

0 comments on commit 527b852

Please sign in to comment.