Skip to content

Commit

Permalink
fix: Almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
4drian3d committed Nov 22, 2023
1 parent 4b3d4e6 commit 604bb46
Show file tree
Hide file tree
Showing 17 changed files with 171 additions and 89 deletions.
53 changes: 27 additions & 26 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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<JavaPlugin>()
}
apply<ShadowPlugin>()

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<JavaCompile> {
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<JavaCompile> {
options.encoding = Charsets.UTF_8.name()
options.release.set(17)
}
if (projectName != "Common") {
withType<ShadowJar> {
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<JavaCompile>() {
options.encoding = "UTF-8"
}
Original file line number Diff line number Diff line change
@@ -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<Set<AnnouncerCommand>>() {}));
for (final AnnouncerCommand command : commands) {
command.register();
}
}

public abstract void stop();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,4 +46,10 @@ public Formatter formatter() {
return new Formatter();
}
}

@Override
protected void configure() {
final Multibinder<AnnouncerCommand> commandBinder = Multibinder.newSetBinder(binder(), AnnouncerCommand.class);
commandBinder.addBinding().to(TitleCommand.class).in(Scopes.SINGLETON);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<P extends Audience> {
@NotNull Audience getGlobalAudience();

@NotNull Optional<P> stringToAudience(final @NotNull String string);

@NotNull Collection<String> playerSuggestions();

default Optional<? extends Audience> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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<Commander> commandManager);
void register();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,48 +21,50 @@ public final class TitleCommand implements AnnouncerCommand {
private Formatter formatter;
@Inject
private ConfigurationContainer<Configuration> configurationContainer;
@Inject
private PlatformAdapter<?> platformAdapter;
@Inject
private CommandManager<Commander> commandManager;

@Override
public void register(CommandManager<Commander> commandManager) {
public void register() {
final var destinationArgument = StringArgument.<Commander>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.<Commander>builder("fadeIn")
.asOptional()
.build();
final var stayFlag = commandManager.flagBuilder("stay")
.withArgument(durationArgument)
final var stayArgument = DurationArgument.<Commander>builder("stay")
.asOptional()
.build();
final var fadeOutFlag = commandManager.flagBuilder("stay")
.withArgument(durationArgument)
final var fadeOutArgument = DurationArgument.<Commander>builder("stay")
.asOptional()
.build();
final var titleArgument = StringArgument.<Commander>quoted("title");
final var subtitleArgument = StringArgument.<Commander>quoted("subtitle");
commandManager.command(commandManager.commandBuilder("announcetitle")
.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))));
}));

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ private record BossBarTask (
Audience viewer,
float toRest
) {

void run() {
calculateTitle();
calculateProgress();
Expand Down
4 changes: 2 additions & 2 deletions paper/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -12,7 +12,7 @@
import java.util.Optional;

@Singleton
public final class TitleAnnouncerPaperAdapter implements CommandAdapter {
public final class TitleAnnouncerPaperAdapter implements PlatformAdapter<Player> {
@Inject
private Server server;

Expand All @@ -22,7 +22,7 @@ public final class TitleAnnouncerPaperAdapter implements CommandAdapter {
}

@Override
public @NotNull Optional<Audience> stringToAudience(@NotNull String string) {
public @NotNull Optional<Player> stringToAudience(@NotNull String string) {
return Optional.ofNullable(server.getPlayer(string));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion velocity/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
dependencies {
compileOnly(libs.velocity)
annotationProcessor(libs.velocity)
compileOnly(projects.titleannouncerCommon)
implementation(projects.titleannouncerCommon)
implementation(libs.cloud.velocity)
}
Original file line number Diff line number Diff line change
@@ -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() {

}
}
Loading

0 comments on commit 604bb46

Please sign in to comment.