diff --git a/build.gradle.kts b/build.gradle.kts index bfa43ff..3d528fd 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,43 +1,44 @@ +import com.github.jengelman.gradle.plugins.shadow.ShadowPlugin +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar +import org.gradle.kotlin.dsl.support.uppercaseFirstChar + plugins { java alias(libs.plugins.shadow) } -allprojects { +subprojects { apply() -} + apply() + + val projectName = project.name.replace("titleannouncer-", "").uppercaseFirstChar() -subprojects { repositories { - mavenCentral() maven("https://papermc.io/repo/repository/maven-public/") } java.toolchain.languageVersion.set(JavaLanguageVersion.of(17)) - tasks.withType { - options.encoding = Charsets.UTF_8.name() - options.release.set(17) - } -} - -tasks { - shadowJar { - archiveFileName.set("TitleAnnouncer-${project.version}.jar") - archiveClassifier.set("") - doLast { - copy { - from(archiveFile) - into("${rootProject.projectDir}/build") + tasks { + withType { + options.encoding = Charsets.UTF_8.name() + options.release.set(17) + } + if (projectName != "Common") { + withType { + archiveFileName.set("TitleAnnouncer-${projectName}-${project.version}.jar") + archiveClassifier.set("") + doLast { + copy { + from(archiveFile) + into("${rootProject.projectDir}/build") + } + } + duplicatesStrategy = DuplicatesStrategy.EXCLUDE } } - duplicatesStrategy = DuplicatesStrategy.EXCLUDE - } - build { - dependsOn(shadowJar) + build { + dependsOn(shadowJar) + } } } - -tasks.withType() { - options.encoding = "UTF-8" -} diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncer.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncer.java index 439028e..ab87bf4 100644 --- a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncer.java +++ b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncer.java @@ -1,6 +1,27 @@ package io.github._4drian3d.titleannouncer.common; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; +import io.github._4drian3d.titleannouncer.common.commands.AnnouncerCommand; -public interface TitleAnnouncer { - void init(); +import java.util.Set; + +public abstract class TitleAnnouncer { + private final Injector injector; + + protected TitleAnnouncer( + Injector injector + ) { + this.injector = injector; + } + + public void init() { + var commands = injector.getInstance(Key.get(new TypeLiteral>() {})); + for (final AnnouncerCommand command : commands) { + command.register(); + } + } + + public abstract void stop(); } diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncerMainModule.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncerMainModule.java index 2d5f95e..68734bb 100644 --- a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncerMainModule.java +++ b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/TitleAnnouncerMainModule.java @@ -2,8 +2,12 @@ import com.google.inject.AbstractModule; import com.google.inject.Provides; +import com.google.inject.Scopes; import com.google.inject.Singleton; +import com.google.inject.multibindings.Multibinder; import io.github._4drian3d.titleannouncer.common.annotations.DataFolder; +import io.github._4drian3d.titleannouncer.common.commands.AnnouncerCommand; +import io.github._4drian3d.titleannouncer.common.commands.TitleCommand; import io.github._4drian3d.titleannouncer.common.configuration.Configuration; import io.github._4drian3d.titleannouncer.common.configuration.ConfigurationContainer; import io.github._4drian3d.titleannouncer.common.configuration.Messages; @@ -42,4 +46,10 @@ public Formatter formatter() { return new Formatter(); } } + + @Override + protected void configure() { + final Multibinder commandBinder = Multibinder.newSetBinder(binder(), AnnouncerCommand.class); + commandBinder.addBinding().to(TitleCommand.class).in(Scopes.SINGLETON); + } } diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/adapter/CommandAdapter.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/adapter/CommandAdapter.java deleted file mode 100644 index 56f014b..0000000 --- a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/adapter/CommandAdapter.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.github._4drian3d.titleannouncer.common.adapter; - -import net.kyori.adventure.audience.Audience; -import org.jetbrains.annotations.NotNull; - -import java.util.Collection; -import java.util.Optional; - -public interface CommandAdapter { - @NotNull Audience getGlobalAudience(); - - @NotNull Optional stringToAudience(final @NotNull String string); - - @NotNull Collection playerSuggestions(); -} diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/adapter/PlatformAdapter.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/adapter/PlatformAdapter.java new file mode 100644 index 0000000..e1f9faa --- /dev/null +++ b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/adapter/PlatformAdapter.java @@ -0,0 +1,29 @@ +package io.github._4drian3d.titleannouncer.common.adapter; + +import net.kyori.adventure.audience.Audience; +import org.jetbrains.annotations.NotNull; + +import java.util.Collection; +import java.util.Optional; + +public interface PlatformAdapter

{ + @NotNull Audience getGlobalAudience(); + + @NotNull Optional

stringToAudience(final @NotNull String string); + + @NotNull Collection playerSuggestions(); + + default Optional destinationFromString(final String string, final Audience audience) { + if (string.equalsIgnoreCase("self")) { + return Optional.of(audience); + } + if (string.equalsIgnoreCase("all")) { + return Optional.of(getGlobalAudience()); + } + if (string.startsWith("player:")) { + return stringToAudience(string.replace("player:", "")); + } + + return Optional.empty(); + } +} diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/annotations/DataFolder.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/annotations/DataFolder.java index aac58e5..16a0cac 100644 --- a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/annotations/DataFolder.java +++ b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/annotations/DataFolder.java @@ -2,6 +2,11 @@ import com.google.inject.BindingAnnotation; +import java.lang.annotation.Retention; + +import static java.lang.annotation.RetentionPolicy.RUNTIME; + @BindingAnnotation +@Retention(RUNTIME) public @interface DataFolder { } diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/AnnouncerCommand.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/AnnouncerCommand.java index 8f5000c..97392b7 100644 --- a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/AnnouncerCommand.java +++ b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/AnnouncerCommand.java @@ -1,8 +1,5 @@ package io.github._4drian3d.titleannouncer.common.commands; -import cloud.commandframework.CommandManager; -import io.github._4drian3d.titleannouncer.common.adapter.Commander; - public interface AnnouncerCommand { - void register(CommandManager commandManager); + void register(); } diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/TitleCommand.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/TitleCommand.java index bdc5916..937b621 100644 --- a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/TitleCommand.java +++ b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/commands/TitleCommand.java @@ -5,10 +5,10 @@ import cloud.commandframework.arguments.standard.StringArgument; import com.google.inject.Inject; import io.github._4drian3d.titleannouncer.common.adapter.Commander; +import io.github._4drian3d.titleannouncer.common.adapter.PlatformAdapter; import io.github._4drian3d.titleannouncer.common.configuration.Configuration; import io.github._4drian3d.titleannouncer.common.configuration.ConfigurationContainer; import io.github._4drian3d.titleannouncer.common.format.Formatter; -import net.kyori.adventure.audience.Audience; import net.kyori.adventure.text.Component; import net.kyori.adventure.title.Title; @@ -21,22 +21,25 @@ public final class TitleCommand implements AnnouncerCommand { private Formatter formatter; @Inject private ConfigurationContainer configurationContainer; + @Inject + private PlatformAdapter platformAdapter; + @Inject + private CommandManager commandManager; @Override - public void register(CommandManager commandManager) { + public void register() { final var destinationArgument = StringArgument.of("destination"); final var destinationFlag = commandManager.flagBuilder("d") .withArgument(destinationArgument) .build(); - final var durationArgument = DurationArgument.of("duration"); - final var fadeInFlag = commandManager.flagBuilder("fadeIn") - .withArgument(durationArgument) + final var fadeInArgument = DurationArgument.builder("fadeIn") + .asOptional() .build(); - final var stayFlag = commandManager.flagBuilder("stay") - .withArgument(durationArgument) + final var stayArgument = DurationArgument.builder("stay") + .asOptional() .build(); - final var fadeOutFlag = commandManager.flagBuilder("stay") - .withArgument(durationArgument) + final var fadeOutArgument = DurationArgument.builder("stay") + .asOptional() .build(); final var titleArgument = StringArgument.quoted("title"); final var subtitleArgument = StringArgument.quoted("subtitle"); @@ -44,25 +47,24 @@ public void register(CommandManager commandManager) { .permission("titleannouncer.announce.title") .argument(titleArgument) .argument(subtitleArgument) + .argument(fadeInArgument) + .argument(stayArgument) + .argument(fadeOutArgument) .flag(destinationFlag) - .flag(fadeInFlag) - .flag(stayFlag) - .flag(fadeOutFlag) .handler(ctx -> { final Commander sender = ctx.getSender(); final Component title = formatter.audienceFormat(ctx.get(titleArgument), sender); final Component subtitle = formatter.audienceFormat(ctx.get(subtitleArgument), sender); - final var flags = ctx.flags(); final var config = configurationContainer.get().title(); - final Duration fadeIn = requireNonNull(flags.getValue(fadeInFlag, config.defaultFadeIn())); - final Duration stay = requireNonNull(flags.getValue(fadeInFlag, config.defaultStay())); - final Duration fadeOut = requireNonNull(flags.getValue(fadeInFlag, config.defaultFadeOut())); + final Duration fadeIn = requireNonNull(ctx.getOrDefault(fadeInArgument, config.defaultFadeIn())); + final Duration stay = requireNonNull(ctx.getOrDefault(fadeInArgument, config.defaultStay())); + final Duration fadeOut = requireNonNull(ctx.getOrDefault(fadeInArgument, config.defaultFadeOut())); - // TODO: Implement Destination parser final String destinationString = ctx.flags().getValue(destinationFlag, "self"); - Audience destinationAudience = Audience.empty(); - destinationAudience.showTitle(Title.title(title, subtitle, Title.Times.times(fadeIn, stay, fadeOut))); + assert destinationString != null; + platformAdapter.destinationFromString(destinationString, sender) + .ifPresent(destination -> destination.showTitle(Title.title(title, subtitle, Title.Times.times(fadeIn, stay, fadeOut)))); })); } diff --git a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/manager/BossBarManager.java b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/manager/BossBarManager.java index 1e354fc..17b7656 100644 --- a/common/src/main/java/io/github/_4drian3d/titleannouncer/common/manager/BossBarManager.java +++ b/common/src/main/java/io/github/_4drian3d/titleannouncer/common/manager/BossBarManager.java @@ -49,7 +49,6 @@ private record BossBarTask ( Audience viewer, float toRest ) { - void run() { calculateTitle(); calculateProgress(); diff --git a/paper/build.gradle.kts b/paper/build.gradle.kts index 36597cf..cd16c7d 100644 --- a/paper/build.gradle.kts +++ b/paper/build.gradle.kts @@ -1,10 +1,10 @@ plugins { alias(libs.plugins.pluginyml) - id("xyz.jpenilla.run-paper") version "1.0.6" + alias(libs.plugins.runpaper) } dependencies { - compileOnly(projects.titleannouncerCommon) + implementation(projects.titleannouncerCommon) compileOnly(libs.paper) implementation(libs.cloud.paper) } diff --git a/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperAdapter.java b/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperAdapter.java index 330e2a1..1e8f5d7 100644 --- a/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperAdapter.java +++ b/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperAdapter.java @@ -1,6 +1,6 @@ package io.github._4drian3d.titleannouncer.paper; -import io.github._4drian3d.titleannouncer.common.adapter.CommandAdapter; +import io.github._4drian3d.titleannouncer.common.adapter.PlatformAdapter; import net.kyori.adventure.audience.Audience; import org.bukkit.Server; import org.bukkit.entity.Player; @@ -12,7 +12,7 @@ import java.util.Optional; @Singleton -public final class TitleAnnouncerPaperAdapter implements CommandAdapter { +public final class TitleAnnouncerPaperAdapter implements PlatformAdapter { @Inject private Server server; @@ -22,7 +22,7 @@ public final class TitleAnnouncerPaperAdapter implements CommandAdapter { } @Override - public @NotNull Optional stringToAudience(@NotNull String string) { + public @NotNull Optional stringToAudience(@NotNull String string) { return Optional.ofNullable(server.getPlayer(string)); } diff --git a/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperModule.java b/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperModule.java index 82015c4..e319edf 100644 --- a/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperModule.java +++ b/paper/src/main/java/io/github/_4drian3d/titleannouncer/paper/TitleAnnouncerPaperModule.java @@ -2,13 +2,13 @@ import com.google.inject.AbstractModule; import io.github._4drian3d.titleannouncer.common.TitleAnnouncerMainModule; -import io.github._4drian3d.titleannouncer.common.adapter.CommandAdapter; +import io.github._4drian3d.titleannouncer.common.adapter.PlatformAdapter; public final class TitleAnnouncerPaperModule extends AbstractModule { @Override protected void configure() { this.install(new TitleAnnouncerMainModule()); - this.bind(CommandAdapter.class).to(TitleAnnouncerPaperAdapter.class); + this.bind(PlatformAdapter.class).to(TitleAnnouncerPaperAdapter.class); } } diff --git a/velocity/build.gradle.kts b/velocity/build.gradle.kts index 8c1994f..c117307 100644 --- a/velocity/build.gradle.kts +++ b/velocity/build.gradle.kts @@ -1,6 +1,6 @@ dependencies { compileOnly(libs.velocity) annotationProcessor(libs.velocity) - compileOnly(projects.titleannouncerCommon) + implementation(projects.titleannouncerCommon) implementation(libs.cloud.velocity) } diff --git a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocity.java b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocity.java index d153b3b..a54cabb 100644 --- a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocity.java +++ b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocity.java @@ -1,13 +1,19 @@ package io.github._4drian3d.titleannouncer.velocity; +import com.google.inject.Inject; +import com.google.inject.Injector; import com.google.inject.Singleton; import io.github._4drian3d.titleannouncer.common.TitleAnnouncer; @Singleton -public final class TitleAnnouncerVelocity implements TitleAnnouncer { +public final class TitleAnnouncerVelocity extends TitleAnnouncer { + @Inject + public TitleAnnouncerVelocity(Injector injector) { + super(injector); + } @Override - public void init() { + public void stop() { } } diff --git a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityBootstrap.java b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityBootstrap.java index 44f5bc7..e99d016 100644 --- a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityBootstrap.java +++ b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityBootstrap.java @@ -1,17 +1,36 @@ package io.github._4drian3d.titleannouncer.velocity; +import com.google.inject.Inject; +import com.google.inject.Injector; import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.event.proxy.ProxyShutdownEvent; +import com.velocitypowered.api.plugin.annotation.DataDirectory; + +import java.nio.file.Path; public final class TitleAnnouncerVelocityBootstrap { + @Inject + private Injector injector; + @Inject + @DataDirectory + private Path path; + + private TitleAnnouncerVelocity plugin; + @Subscribe public void onStartup(final ProxyInitializeEvent event) { - + this.injector = injector.createChildInjector( + new TitleAnnouncerVelocityModule(path) + ); + this.plugin = injector.getInstance(TitleAnnouncerVelocity.class); + this.plugin.init(); } @Subscribe public void onShutdown(final ProxyShutdownEvent event) { - + if (this.plugin != null) { + this.plugin.stop(); + } } } diff --git a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityModule.java b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityModule.java index ae2293e..8332784 100644 --- a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityModule.java +++ b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/TitleAnnouncerVelocityModule.java @@ -1,19 +1,26 @@ package io.github._4drian3d.titleannouncer.velocity; import cloud.commandframework.CommandManager; -import cloud.commandframework.execution.CommandExecutionCoordinator; +import cloud.commandframework.execution.AsynchronousCommandExecutionCoordinator; import cloud.commandframework.velocity.VelocityCommandManager; -import com.google.inject.AbstractModule; -import com.google.inject.Provides; -import com.google.inject.Singleton; +import com.google.inject.*; import com.velocitypowered.api.command.CommandSource; import com.velocitypowered.api.plugin.PluginContainer; import com.velocitypowered.api.proxy.ProxyServer; import io.github._4drian3d.titleannouncer.common.TitleAnnouncerMainModule; -import io.github._4drian3d.titleannouncer.common.adapter.CommandAdapter; +import io.github._4drian3d.titleannouncer.common.adapter.PlatformAdapter; import io.github._4drian3d.titleannouncer.common.adapter.Commander; +import io.github._4drian3d.titleannouncer.common.annotations.DataFolder; + +import java.nio.file.Path; public final class TitleAnnouncerVelocityModule extends AbstractModule { + private final Path path; + + TitleAnnouncerVelocityModule(Path path) { + this.path = path; + } + @Provides @Singleton public CommandManager commandmanager( @@ -23,16 +30,17 @@ public CommandManager commandmanager( return new VelocityCommandManager<>( pluginContainer, proxyServer, - CommandExecutionCoordinator.simpleCoordinator(), + AsynchronousCommandExecutionCoordinator.simpleCoordinator(), VelocityCommander::new, commander -> ((VelocityCommander) commander).audience() ); } @Override protected void configure() { + this.bind(Path.class).annotatedWith(DataFolder.class).toInstance(path); this.install(new TitleAnnouncerMainModule()); - - this.bind(CommandAdapter.class).to(VelocityAdapter.class); + this.bind(PlatformAdapter.class).to(VelocityAdapter.class); + this.bind(Key.get(new TypeLiteral>(){})).to(VelocityAdapter.class); } private record VelocityCommander(CommandSource audience) implements Commander { diff --git a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/VelocityAdapter.java b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/VelocityAdapter.java index 2632004..0d6e991 100644 --- a/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/VelocityAdapter.java +++ b/velocity/src/main/java/io/github/_4drian3d/titleannouncer/velocity/VelocityAdapter.java @@ -4,7 +4,7 @@ import com.google.inject.Singleton; import com.velocitypowered.api.proxy.Player; import com.velocitypowered.api.proxy.ProxyServer; -import io.github._4drian3d.titleannouncer.common.adapter.CommandAdapter; +import io.github._4drian3d.titleannouncer.common.adapter.PlatformAdapter; import net.kyori.adventure.audience.Audience; import org.jetbrains.annotations.NotNull; @@ -14,7 +14,7 @@ import java.util.Optional; @Singleton -public final class VelocityAdapter implements CommandAdapter { +public final class VelocityAdapter implements PlatformAdapter { @Inject private ProxyServer proxyServer;