mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Implement Guice and Moonshine (#139)
* Initial pass - 1/? * oops * War against legacy, part 1 * Stuff & Bukkit * Sponge * Initial pass - 1/? * more guice * Separate guice module out * Fix broken rebase * Hello from Carbon! * stuff * Move init to correct location * Bukkit has moonshine! * Minor adjustments * Sponge guice part 1 * No turning back from this party * fix relocations and exclusions * Some cleanup with the Bukkit side of things, too * Add comment explaining why we pass this argument Co-authored-by: Riley Park <[email protected]> Co-authored-by: jmp <[email protected]>
This commit is contained in:
38 files changed
+908
-129
No files matched your search
@@ -5,17 +5,22 @@ import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.api.events.CarbonEventHandler;
|
||||
import net.draycia.carbon.common.command.Commander;
|
||||
import net.draycia.carbon.common.command.PlayerCommander;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import static net.kyori.adventure.text.Component.empty;
|
||||
import static net.kyori.adventure.text.Component.text;
|
||||
import static net.kyori.adventure.text.format.NamedTextColor.DARK_GRAY;
|
||||
import static net.kyori.adventure.text.format.TextDecoration.BOLD;
|
||||
|
||||
public abstract class CarbonChatCommon implements CarbonChat {
|
||||
|
||||
private final CarbonMessageService messageService;
|
||||
|
||||
protected CarbonChatCommon(final CarbonMessageService messageService) {
|
||||
this.messageService = messageService;
|
||||
}
|
||||
|
||||
private final CarbonEventHandler eventHandler = new CarbonEventHandler();
|
||||
|
||||
public final void initialize() {
|
||||
@@ -23,10 +28,9 @@ public abstract class CarbonChatCommon implements CarbonChat {
|
||||
commandManager.command(commandManager.commandBuilder("carbon")
|
||||
.handler(ctx -> {
|
||||
final Commander sender = ctx.getSender();
|
||||
sender.sendMessage(text()
|
||||
.content("Hello from ")
|
||||
.append(text("Carbon", DARK_GRAY, BOLD))
|
||||
.append(text('!')));
|
||||
|
||||
this.messageService.exampleCommandFeedback(sender, this.messageService.pluginName());
|
||||
|
||||
if (sender instanceof PlayerCommander) {
|
||||
final Component itemComponent = ((PlayerCommander) sender).carbonPlayer().createItemHoverComponent();
|
||||
if (itemComponent != empty()) {
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package net.draycia.carbon.common;
|
||||
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Provides;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.Moonshine;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageParser;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageSender;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageService;
|
||||
import net.draycia.carbon.common.messages.CarbonMessageSource;
|
||||
import net.draycia.carbon.common.messages.ComponentPlaceholderResolver;
|
||||
import net.draycia.carbon.common.messages.ServerReceiverResolver;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
public final class CarbonCommonModule extends AbstractModule {
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
public CarbonMessageService messageService(
|
||||
final @NonNull ServerReceiverResolver serverReceiverResolver,
|
||||
final @NonNull ComponentPlaceholderResolver<Audience> componentPlaceholderResolver,
|
||||
final @NonNull CarbonMessageSource carbonMessageSource,
|
||||
final @NonNull CarbonMessageParser carbonMessageParser,
|
||||
final @NonNull CarbonMessageSender carbonMessageSender
|
||||
) {
|
||||
return Moonshine.<Audience>builder()
|
||||
.receiver(serverReceiverResolver)
|
||||
.placeholder(Component.class, componentPlaceholderResolver)
|
||||
.source(carbonMessageSource)
|
||||
.parser(carbonMessageParser)
|
||||
.sender(carbonMessageSender)
|
||||
.create(CarbonMessageService.class, this.getClass().getClassLoader());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package net.draycia.carbon.common;
|
||||
|
||||
import com.google.inject.BindingAnnotation;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@BindingAnnotation
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.PARAMETER)
|
||||
public @interface ForCarbon {
|
||||
}
|
||||
@@ -1,27 +0,0 @@
|
||||
package net.draycia.carbon.common;
|
||||
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
// TODO: remove
|
||||
public final class Injector {
|
||||
|
||||
private Injector() {
|
||||
|
||||
}
|
||||
|
||||
private static final Map<Class<?>, Object> providers = new HashMap<>();
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
public static <T, S extends T> S byInject(final @NonNull Class<T> clazz) {
|
||||
return (S) providers.get(clazz);
|
||||
}
|
||||
|
||||
@Deprecated(forRemoval = true)
|
||||
public static <T> void provide(final @NonNull Class<T> clazz, final @NonNull T provider) {
|
||||
providers.put(clazz, provider);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package net.draycia.carbon.common.config;
|
||||
|
||||
import net.kyori.adventure.serializer.configurate4.ConfigurateComponentSerializer;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
import org.spongepowered.configurate.CommentedConfigurationNode;
|
||||
import org.spongepowered.configurate.ConfigurateException;
|
||||
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class ConfigLoader {
|
||||
|
||||
public CommentedConfigurationNode loadConfig(final File file) throws ConfigurateException {
|
||||
final var builder = HoconConfigurationLoader.builder().prettyPrinting(true);
|
||||
|
||||
final var loader = this.loadConfigFile(builder, file);
|
||||
final CommentedConfigurationNode node = loader.load();
|
||||
|
||||
if (!file.exists()) {
|
||||
loader.save(node);
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
private HoconConfigurationLoader loadConfigFile(final HoconConfigurationLoader.Builder builder, final File config) {
|
||||
return builder
|
||||
.defaultOptions(opts -> {
|
||||
final ConfigurateComponentSerializer serializer = ConfigurateComponentSerializer.configurate();
|
||||
|
||||
return opts.shouldCopyDefaults(true).serializers(serializerBuilder ->
|
||||
serializerBuilder.registerAll(serializer.serializers()));
|
||||
})
|
||||
.file(config)
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.message.IMessageParser;
|
||||
import com.proximyst.moonshine.message.ParsingContext;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
|
||||
@Singleton
|
||||
public class CarbonMessageParser implements IMessageParser<String, Component, Audience> {
|
||||
|
||||
private final CarbonMessageService messageService;
|
||||
|
||||
private final boolean checkForLegacy = !Boolean.getBoolean("disableLegacyCheck");
|
||||
private final String legacyChar = String.valueOf(LegacyComponentSerializer.SECTION_CHAR);
|
||||
|
||||
@Inject
|
||||
public CarbonMessageParser(final CarbonMessageService messageService) {
|
||||
this.messageService = messageService;
|
||||
}
|
||||
|
||||
public Component parse(String message, final ParsingContext<Audience> parsingContext) {
|
||||
if (this.checkForLegacy) {
|
||||
if (message.contains(this.legacyChar)) {
|
||||
message = message.replace(this.legacyChar, "");
|
||||
this.messageService.unsupportedLegacyChar(parsingContext.receiver(), message);
|
||||
}
|
||||
}
|
||||
|
||||
return MiniMessage.get().parse(message, parsingContext.placeholders());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.proximyst.moonshine.message.IMessageSender;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public class CarbonMessageSender implements IMessageSender<Audience, Component> {
|
||||
|
||||
@Override
|
||||
public void sendMessage(final Audience receiver, final Component message) {
|
||||
receiver.sendMessage(message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.proximyst.moonshine.annotation.Message;
|
||||
import com.proximyst.moonshine.annotation.Placeholder;
|
||||
import com.proximyst.moonshine.annotation.Receiver;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import net.kyori.adventure.text.Component;
|
||||
|
||||
public interface CarbonMessageService {
|
||||
|
||||
@Message("example.command.hello")
|
||||
void exampleCommandFeedback(
|
||||
@Receiver final Audience audience,
|
||||
@Placeholder final Component plugin
|
||||
);
|
||||
|
||||
@Message("unsupported.legacy")
|
||||
void unsupportedLegacyChar(
|
||||
@Receiver final Audience audience,
|
||||
@Placeholder final String message
|
||||
);
|
||||
|
||||
@Message("placeholders.plugin")
|
||||
Component pluginName();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Singleton;
|
||||
import com.proximyst.moonshine.message.IMessageSource;
|
||||
import net.draycia.carbon.api.CarbonChat;
|
||||
import net.draycia.carbon.common.ForCarbon;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.Reader;
|
||||
import java.io.Writer;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@Singleton
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class CarbonMessageSource implements IMessageSource<String, Audience> {
|
||||
|
||||
private final Properties properties;
|
||||
|
||||
@Inject
|
||||
CarbonMessageSource(final @ForCarbon Path dataDirectory) throws IOException {
|
||||
final var directoryFile = dataDirectory.toFile();
|
||||
|
||||
if (!directoryFile.exists()) {
|
||||
directoryFile.mkdirs();
|
||||
}
|
||||
|
||||
this.properties = new Properties();
|
||||
|
||||
// TODO: read file name from config, allow users to specify which file.
|
||||
// TODO: have a fallback that uses messages-en.properties in the jar
|
||||
// so that any missing entries in the user's file will be resolvable.
|
||||
final var fileName = "messages-en.properties";
|
||||
final var file = dataDirectory.resolve(fileName).toFile();
|
||||
|
||||
if (file.isFile()) {
|
||||
try (final Reader reader = new FileReader(file, StandardCharsets.UTF_8)) {
|
||||
this.properties.load(reader);
|
||||
}
|
||||
}
|
||||
|
||||
boolean write = !file.isFile();
|
||||
|
||||
try (final InputStream stream = CarbonChat.class.getResourceAsStream("/" + fileName)) {
|
||||
final Properties packaged = new Properties();
|
||||
packaged.load(stream);
|
||||
|
||||
for (final Map.Entry<Object, Object> entry : packaged.entrySet()) {
|
||||
write |= this.properties.putIfAbsent(entry.getKey(), entry.getValue()) == null;
|
||||
}
|
||||
}
|
||||
|
||||
if (write) {
|
||||
try (final Writer writer = new FileWriter(file, StandardCharsets.UTF_8)) {
|
||||
this.properties.store(writer, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String message(final String key, final Audience receiver) {
|
||||
final String value = this.properties.getProperty(key);
|
||||
if (value == null) {
|
||||
throw new IllegalStateException("No message mapping for key " + key);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.proximyst.moonshine.component.placeholder.IPlaceholderResolver;
|
||||
import com.proximyst.moonshine.component.placeholder.PlaceholderContext;
|
||||
import com.proximyst.moonshine.component.placeholder.ResolveResult;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.checkerframework.framework.qual.DefaultQualifier;
|
||||
|
||||
@DefaultQualifier(NonNull.class)
|
||||
public class ComponentPlaceholderResolver<R> implements IPlaceholderResolver<R, Component> {
|
||||
|
||||
public ResolveResult resolve(
|
||||
final String placeholderName,
|
||||
final Component value,
|
||||
final PlaceholderContext<R> ctx,
|
||||
final Multimap<String, @Nullable Object> flags
|
||||
) {
|
||||
return ResolveResult.ok(placeholderName, MiniMessage.get().serialize(value));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
//
|
||||
// ban - A punishment suite for Velocity.
|
||||
// Copyright (C) 2021 Mariell Hoversholm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
public @interface ServerReceiver {
|
||||
String permission() default "";
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//
|
||||
// ban - A punishment suite for Velocity.
|
||||
// Copyright (C) 2021 Mariell Hoversholm
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
//
|
||||
// Modified to better fit in Carbon
|
||||
//
|
||||
|
||||
package net.draycia.carbon.common.messages;
|
||||
|
||||
import com.google.common.collect.Streams;
|
||||
import com.proximyst.moonshine.component.receiver.IReceiver;
|
||||
import com.proximyst.moonshine.component.receiver.IReceiverResolver;
|
||||
import com.proximyst.moonshine.component.receiver.ReceiverContext;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
|
||||
import net.draycia.carbon.api.CarbonServer;
|
||||
import net.kyori.adventure.audience.Audience;
|
||||
import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
@Singleton
|
||||
public final class ServerReceiverResolver implements IReceiverResolver<Audience> {
|
||||
|
||||
private final @NonNull CarbonServer carbonServer;
|
||||
|
||||
@Inject
|
||||
public ServerReceiverResolver(final @NonNull CarbonServer carbonServer) {
|
||||
this.carbonServer = carbonServer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<IReceiver<Audience>> resolve(final Method method) {
|
||||
final ServerReceiver annotation = method.getAnnotation(ServerReceiver.class);
|
||||
if (annotation == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.of(new Resolver(this.carbonServer, annotation.permission()));
|
||||
}
|
||||
|
||||
private static final class Resolver implements IReceiver<Audience> {
|
||||
private final @NonNull CarbonServer carbonServer;
|
||||
private final @Nullable String permission;
|
||||
|
||||
private Resolver(final @NonNull CarbonServer carbonServer,
|
||||
final @Nullable String permission) {
|
||||
this.carbonServer = carbonServer;
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Audience find(final ReceiverContext ctx) {
|
||||
if (this.permission == null) {
|
||||
return this.carbonServer;
|
||||
}
|
||||
|
||||
return Audience.audience(Stream.concat(Stream.of(this.carbonServer.console()),
|
||||
Streams.stream(this.carbonServer.players())
|
||||
.filter(player -> player.hasPermission(this.permission)))
|
||||
.collect(Collectors.toList()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user