mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-02 14:38:49 +00:00
refactor how reloads are handled
also, translations are now reloadable
This commit is contained in:
6 files changed
+83
-36
No files matched your search
@@ -50,6 +50,7 @@ import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.events.CarbonReloadEvent;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.kyori.adventure.audience.MessageType;
|
||||
import net.kyori.adventure.identity.Identity;
|
||||
@@ -114,6 +115,10 @@ public class CarbonChannelRegistry implements ChannelRegistry, DefaultedRegistry
|
||||
this.messageService = messageService;
|
||||
this.basicChannel = basicChannel;
|
||||
this.carbonChat = carbonChat;
|
||||
|
||||
carbonChat.eventHandler().subscribe(CarbonReloadEvent.class, event -> {
|
||||
this.reloadRegisteredConfigChannels();
|
||||
});
|
||||
}
|
||||
|
||||
public void reloadRegisteredConfigChannels() {
|
||||
|
||||
@@ -22,13 +22,11 @@ package net.draycia.carbon.common.command.commands;
|
||||
import cloud.commandframework.CommandManager;
|
||||
import cloud.commandframework.minecraft.extras.MinecraftExtrasMetaKeys;
|
||||
import com.google.inject.Inject;
|
||||
import net.draycia.carbon.common.channels.CarbonChannelRegistry;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.config.PrimaryConfig;
|
||||
import net.draycia.carbon.common.events.CarbonReloadEvent;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
@@ -36,9 +34,8 @@ public class ReloadCommand {
|
||||
|
||||
@Inject
|
||||
public ReloadCommand(
|
||||
final CarbonChat carbonChat,
|
||||
final CommandManager<Commander> commandManager,
|
||||
final ConfigFactory configFactory,
|
||||
final CarbonChannelRegistry channelRegistry,
|
||||
final CarbonMessageService messageService
|
||||
) {
|
||||
final var command = commandManager.commandBuilder("creload", "carbonreload")
|
||||
@@ -46,16 +43,9 @@ public class ReloadCommand {
|
||||
.senderType(Commander.class)
|
||||
.meta(MinecraftExtrasMetaKeys.DESCRIPTION, messageService.commandReloadDescription().component())
|
||||
.handler(handler -> {
|
||||
channelRegistry.reloadRegisteredConfigChannels();
|
||||
|
||||
final @Nullable PrimaryConfig config = configFactory.reloadPrimaryConfig();
|
||||
|
||||
if (config != null) {
|
||||
messageService.configReloaded(handler.getSender());
|
||||
} else {
|
||||
messageService.configReloadFailed(handler.getSender());
|
||||
}
|
||||
|
||||
// TODO: Check if all listeners succeeded
|
||||
carbonChat.eventHandler().emit(new CarbonReloadEvent());
|
||||
messageService.configReloaded(handler.getSender());
|
||||
})
|
||||
.build();
|
||||
|
||||
|
||||
@@ -24,7 +24,9 @@ import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Locale;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.common.events.CarbonReloadEvent;
|
||||
import net.draycia.carbon.common.serialisation.gson.LocaleSerializerConfigurate;
|
||||
import net.kyori.adventure.serializer.configurate4.ConfigurateComponentSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
@@ -43,11 +45,16 @@ public class ConfigFactory {
|
||||
|
||||
@Inject
|
||||
public ConfigFactory(
|
||||
final CarbonChat carbonChat,
|
||||
@ForCarbon final Path dataDirectory,
|
||||
final LocaleSerializerConfigurate locale
|
||||
) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.locale = locale;
|
||||
|
||||
carbonChat.eventHandler().subscribe(CarbonReloadEvent.class, event -> {
|
||||
this.reloadPrimaryConfig();
|
||||
});
|
||||
}
|
||||
|
||||
public @Nullable PrimaryConfig reloadPrimaryConfig() {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* CarbonChat
|
||||
*
|
||||
* Copyright (c) 2021 Josua Parks (Vicarious)
|
||||
* Contributors
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package net.draycia.carbon.common.events;
|
||||
|
||||
import net.draycia.carbon.api.events.CarbonEvent;
|
||||
|
||||
public class CarbonReloadEvent implements CarbonEvent {
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -42,10 +42,12 @@ import java.util.Objects;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Stream;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.users.CarbonPlayer;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.config.ConfigFactory;
|
||||
import net.draycia.carbon.common.events.CarbonReloadEvent;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.translation.Translator;
|
||||
import net.kyori.moonshine.message.IMessageSource;
|
||||
@@ -62,19 +64,47 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
|
||||
private final Map<Locale, Properties> locales = new HashMap<>();
|
||||
private final Path pluginJar;
|
||||
private final Logger logger;
|
||||
private final Path dataDirectory;
|
||||
|
||||
@Inject
|
||||
private CarbonMessageSource(
|
||||
final CarbonChat carbonChat,
|
||||
final @ForCarbon Path dataDirectory,
|
||||
final ConfigFactory configFactory,
|
||||
final Logger logger
|
||||
) throws IOException {
|
||||
this.dataDirectory = dataDirectory;
|
||||
this.pluginJar = pluginJar();
|
||||
this.logger = logger;
|
||||
|
||||
this.defaultLocale = Objects.requireNonNull(configFactory.primaryConfig()).defaultLocale();
|
||||
|
||||
final Path localeDirectory = dataDirectory.resolve("locale");
|
||||
this.reloadTranslations();
|
||||
|
||||
carbonChat.eventHandler().subscribe(CarbonReloadEvent.class, event -> {
|
||||
this.reloadTranslations();
|
||||
});
|
||||
}
|
||||
|
||||
private static @NonNull Path pluginJar() {
|
||||
try {
|
||||
URL sourceUrl = CarbonMessageSource.class.getProtectionDomain().getCodeSource().getLocation();
|
||||
// Some class loaders give the full url to the class, some give the URL to its jar.
|
||||
// We want the containing jar, so we will unwrap jar-schema code sources.
|
||||
if (sourceUrl.getProtocol().equals("jar")) {
|
||||
final int exclamationIdx = sourceUrl.getPath().lastIndexOf('!');
|
||||
if (exclamationIdx != -1) {
|
||||
sourceUrl = new URL(sourceUrl.getPath().substring(0, exclamationIdx));
|
||||
}
|
||||
}
|
||||
return Paths.get(sourceUrl.toURI());
|
||||
} catch (final URISyntaxException | MalformedURLException ex) {
|
||||
throw new RuntimeException("Could not locate plugin jar", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void reloadTranslations() throws IOException {
|
||||
final Path localeDirectory = this.dataDirectory.resolve("locale");
|
||||
|
||||
// Create locale directory
|
||||
if (!Files.exists(localeDirectory)) {
|
||||
@@ -112,23 +142,6 @@ public final class CarbonMessageSource implements IMessageSource<Audience, Strin
|
||||
}));
|
||||
}
|
||||
|
||||
private static @NonNull Path pluginJar() {
|
||||
try {
|
||||
URL sourceUrl = CarbonMessageSource.class.getProtectionDomain().getCodeSource().getLocation();
|
||||
// Some class loaders give the full url to the class, some give the URL to its jar.
|
||||
// We want the containing jar, so we will unwrap jar-schema code sources.
|
||||
if (sourceUrl.getProtocol().equals("jar")) {
|
||||
final int exclamationIdx = sourceUrl.getPath().lastIndexOf('!');
|
||||
if (exclamationIdx != -1) {
|
||||
sourceUrl = new URL(sourceUrl.getPath().substring(0, exclamationIdx));
|
||||
}
|
||||
}
|
||||
return Paths.get(sourceUrl.toURI());
|
||||
} catch (final URISyntaxException | MalformedURLException ex) {
|
||||
throw new RuntimeException("Could not locate plugin jar", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String messageOf(final Audience receiver, final String messageKey) {
|
||||
Audience audience = receiver;
|
||||
|
||||
Reference in new issue
Block a user