Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 7.x.x #34

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
YetAnotherMessagesLibrary (YAML)
===========
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
![Version](https://repo.titanvale.net/api/badge/latest/releases/dev/peri/yetanothermessageslibrary/core?color=42c611&name=Releases&prefix=v)
![Version](https://repo.titanvale.net/api/badge/latest/snapshots/dev/peri/yetanothermessageslibrary/core?color=d45f48&name=Snapshots&prefix=v)
![Version](https://repo.mythicprojects.org/api/badge/latest/releases/org/mythicprojects/yetanothermessageslibrary/core?color=42c611&name=Releases&prefix=v)
![Version](https://repo.mythicprojects.org/api/badge/latest/snapshots/org/mythicprojects/yetanothermessageslibrary/core?color=d45f48&name=Snapshots&prefix=v)
![GitHub repo size](https://img.shields.io/github/repo-size/P3ridot/YetAnotherMessagesLibrary)

Lightweight and modular *Minecraft Messages Library* based on [adventure](https://github.com/KyoriPowered/adventure).
Expand All @@ -21,8 +21,8 @@ Implementations of values/messages suppliers or utilities class to easily add su

### Replaceable
Implementations of `Replaceable` interface to easily replace placeholders in messages
- [okaeri-placeholders](https://github.com/OkaeriPoland/okaeri-placeholders)
- [PlaceholderAPI](https://github.com/PlaceholderAPI/PlaceholderAPI)
- [okaeri-placeholders **\[EXPERIMENTAL\]**](https://github.com/OkaeriPoland/okaeri-placeholders)


## Maven/Gradle
Expand All @@ -31,13 +31,13 @@ Implementations of `Replaceable` interface to easily replace placeholders in mes
```xml
<!-- Releases -->
<repository>
<id>titanvale-releases</id>
<url>https://repo.titanvale.net/releases</url>
<id>mythic-releases</id>
<url>https://repo.mythicprojects.org/releases</url>
</repository>
<!-- Snapshots -->
<repository>
<id>titanvale-snapshots</id>
<url>https://repo.titanvale.net/snapshots</url>
<id>mythic-snapshots</id>
<url>https://repo.mythicprojects.org/snapshots</url>
</repository>
```

Expand All @@ -55,13 +55,13 @@ Implementations of `Replaceable` interface to easily replace placeholders in mes
repositories {
// Releases
maven {
name = "titanvale-releases"
url = "https://repo.titanvale.net/releases"
name = "mythic-releases"
url = "https://repo.mythicprojects.org/releases"
}
// Snapshots
maven {
name = "titanvale-snapshots"
url = "https://repo.titanvale.net/snapshots"
name = "mythic-snapshots"
url = "https://repo.mythicprojects.org/snapshots"
}
}
```
Expand Down
17 changes: 8 additions & 9 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {

allprojects {
group 'dev.peri.yetanothermessageslibrary'
version '6.8.0-SNAPSHOT'
version '7.0.0-SNAPSHOT'

apply plugin: 'java-library'
apply plugin: 'maven-publish'
Expand Down Expand Up @@ -43,13 +43,12 @@ subprojects {
publications {
libraries(MavenPublication) {
artifactId = project.name.toLowerCase()

from components.java

pom {
url = "https://github.com/P3ridot/YetAnotherMessagesLibrary"
url = "https://github.com/Mythic-Projects/YetAnotherMessagesLibrary"
name = project.name
description = "YetAnotherMessagesLibrary is simple Miecraft Messages Library based on `adventure`. It allows to easily send messages to players with built-in support for localization and placeholders."
description = "YetAnotherMessagesLibrary is simple Minecraft Messages Library based on `adventure`. It allows to easily send messages to players with built-in support for localization and placeholders."

developers {
developer {
Expand All @@ -60,9 +59,9 @@ subprojects {
}

scm {
url = "https://github.com/P3ridot/YetAnotherMessagesLibrary.git"
connection = "git@github.com:P3ridot/YetAnotherMessagesLibrary.git"
developerConnection = "git@github.com:P3ridot/YetAnotherMessagesLibrary.git"
url = "https://github.com/Mythic-Projects/YetAnotherMessagesLibrary.git"
connection = "git@github.com:Mythic-Projects/YetAnotherMessagesLibrary.git"
developerConnection = "git@github.com:Mythic-Projects/YetAnotherMessagesLibrary.git"
}

licenses {
Expand Down Expand Up @@ -93,8 +92,8 @@ subprojects {

repositories {
maven {
name "titanvale-repo"
url "https://repo.titanvale.net/${version.toString().endsWith('SNAPSHOT') ? 'snapshots' : 'releases'}"
name "mythic"
url "https://repo.mythicprojects.org/${version.toString().endsWith('SNAPSHOT') ? 'snapshots' : 'releases'}"

credentials {
username = System.getenv("MAVEN_NAME")
Expand Down
4 changes: 1 addition & 3 deletions core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
dependencies {
compileOnlyApi adventure.api
compileOnlyApi adventure.minimessage
compileOnlyApi adventure.serializer.legacy
api project(':tools')
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package dev.peri.yetanothermessageslibrary;

import dev.peri.yetanothermessageslibrary.locale.LocaleProvider;
import dev.peri.yetanothermessageslibrary.replace.ComponentReplacer;
import dev.peri.yetanothermessageslibrary.replace.Replaceable;
import dev.peri.yetanothermessageslibrary.replace.StringReplaceable;
import dev.peri.yetanothermessageslibrary.replace.StringReplacer;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Function;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

public interface MessageService<C extends MessageRepository> {
public interface MessageService<REPOSITORY extends MessageRepository> {

/**
* Get message from repository
Expand All @@ -23,7 +26,7 @@ public interface MessageService<C extends MessageRepository> {
* @param <T> type of message
* @return message
*/
default <T> T get(@Nullable Object entity, @NotNull Function<@NotNull C, @Nullable T> valueSupplier) {
default <T> T get(@Nullable Object entity, @NotNull Function<@NotNull REPOSITORY, @Nullable T> valueSupplier) {
Locale locale = this.getLocale(entity);
return valueSupplier.apply(this.getRepository(locale));
}
Expand All @@ -36,7 +39,7 @@ default <T> T get(@Nullable Object entity, @NotNull Function<@NotNull C, @Nullab
* @param <T> type of message
* @return message
*/
default <T> T get(@NotNull Function<@NotNull C, @Nullable T> valueSupplier) {
default <T> T get(@NotNull Function<@NotNull REPOSITORY, @Nullable T> valueSupplier) {
return this.get(null, valueSupplier);
}

Expand All @@ -50,7 +53,7 @@ default <T> T get(@NotNull Function<@NotNull C, @Nullable T> valueSupplier) {
* @param replacements replacements to replace in message
* @return message with replaced placeholders
*/
default String get(@Nullable Object entity, @NotNull Function<@NotNull C, @Nullable String> stringSupplier, @NotNull Replaceable... replacements) {
default String getString(@Nullable Object entity, @NotNull Function<@NotNull REPOSITORY, @Nullable String> stringSupplier, @NotNull StringReplaceable... replacements) {
String string = this.get(entity, stringSupplier);
if (string == null) {
return null;
Expand All @@ -59,6 +62,25 @@ default String get(@Nullable Object entity, @NotNull Function<@NotNull C, @Nulla
return StringReplacer.replace(locale, string, replacements);
}

/**
* Get message from repository and replace placeholders
*
* @param entity entity to find locale for using registered {@link LocaleProvider}
* use default locale if entity is null or no {@link LocaleProvider} is registered
* can be {@link Locale} if you want to use specific locale
* @param componentSupplier function to get message from repository
* @param replacements replacements to replace in message
* @return message with replaced placeholders
*/
default Component getComponent(@Nullable Object entity, @NotNull Function<@NotNull REPOSITORY, @Nullable Component> componentSupplier, @NotNull Replaceable... replacements) {
Component component = this.get(entity, componentSupplier);
if (component == null) {
return null;
}
Locale locale = this.getLocale(entity);
return ComponentReplacer.replace(locale, component, replacements);
}

/**
* Get message from repository with default locale and replace placeholders
*
Expand All @@ -69,7 +91,7 @@ default String get(@Nullable Object entity, @NotNull Function<@NotNull C, @Nulla
* @param replacements replacements to replace in message
* @return message with replaced placeholders
*/
default List<String> getList(@Nullable Object entity, @NotNull Function<@NotNull C, @Nullable List<String>> listSupplier, @NotNull Replaceable... replacements) {
default List<String> getStringList(@Nullable Object entity, @NotNull Function<@NotNull REPOSITORY, @Nullable List<String>> listSupplier, @NotNull StringReplaceable... replacements) {
List<String> stringList = this.get(entity, listSupplier);
if (stringList == null || stringList.isEmpty()) {
return stringList;
Expand All @@ -78,6 +100,25 @@ default List<String> getList(@Nullable Object entity, @NotNull Function<@NotNull
return StringReplacer.replace(locale, stringList, replacements);
}

/**
* Get message from repository with default locale and replace placeholders
*
* @param entity entity to find locale for using registered {@link LocaleProvider}
* use default locale if entity is null or no {@link LocaleProvider} is registered
* can be {@link Locale} if you want to use specific locale
* @param listSupplier function to get message from repository
* @param replacements replacements to replace in message
* @return message with replaced placeholders
*/
default List<Component> getComponentList(@Nullable Object entity, @NotNull Function<@NotNull REPOSITORY, @Nullable List<Component>> listSupplier, @NotNull Replaceable... replacements) {
List<Component> componentList = this.get(entity, listSupplier);
if (componentList == null || componentList.isEmpty()) {
return componentList;
}
Locale locale = this.getLocale(entity);
return ComponentReplacer.replace(locale, componentList, replacements);
}

/**
* Set default locale that will be used if:
* <ul>
Expand Down Expand Up @@ -148,31 +189,31 @@ default List<String> getList(@Nullable Object entity, @NotNull Function<@NotNull
*
* @return message repositories
*/
@NotNull @Unmodifiable Map<Locale, C> getMessageRepositories();
@NotNull @Unmodifiable Map<Locale, REPOSITORY> getMessageRepositories();

/**
* Get message repository for locale
*
* @param locale locale
* @return message repository
*/
@NotNull C getRepository(@NotNull Locale locale);
@NotNull REPOSITORY getRepository(@NotNull Locale locale);

/**
* Register message repository for locale
*
* @param locale locale
* @param messageRepository message repository
*/
void registerRepository(@NotNull Locale locale, @NotNull C messageRepository);
void registerRepository(@NotNull Locale locale, @NotNull REPOSITORY messageRepository);

/**
* Register message repository for locale and set it as default
*
* @param defaultLocale default locale
* @param messageRepository message repository
*/
default void registerDefaultRepository(@NotNull Locale defaultLocale, @NotNull C messageRepository) {
default void registerDefaultRepository(@NotNull Locale defaultLocale, @NotNull REPOSITORY messageRepository) {
this.setDefaultLocale(defaultLocale);
this.registerRepository(defaultLocale, messageRepository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public interface SendableMessageService<R, C extends MessageRepository, D extends MessageDispatcher<R, ? extends D>> extends MessageService<C> {
public interface SendableMessageService<RECEIVER, REPOSITORY extends MessageRepository, DISPATCHER extends MessageDispatcher<RECEIVER, ? extends DISPATCHER>>
extends MessageService<REPOSITORY> {

/**
* Wrap message in {@link MessageDispatcher} to easily send it
Expand All @@ -17,12 +18,12 @@ public interface SendableMessageService<R, C extends MessageRepository, D extend
* @return MessageDispatcher
*/
@SuppressWarnings("unchecked")
default D getMessage(@NotNull Function<@NotNull C, @Nullable Sendable> messageSupplier) {
default DISPATCHER getMessage(@NotNull Function<@NotNull REPOSITORY, @Nullable Sendable> messageSupplier) {
return this.getDispatcherFactory().prepareDispatcher(this.getViewerService(), this::getLocale, (entity) -> this.get(entity, messageSupplier));
}

@NotNull ViewerService<R> getViewerService();
@NotNull ViewerService<RECEIVER> getViewerService();

@NotNull MessageDispatcherFactory<R, ? extends D> getDispatcherFactory();
@NotNull MessageDispatcherFactory<RECEIVER, ? extends DISPATCHER> getDispatcherFactory();

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;

public abstract class SimpleMessageService<C extends MessageRepository> implements MessageService<C> {
public abstract class SimpleMessageService<REPOSITORY extends MessageRepository> implements MessageService<REPOSITORY> {

private Locale defaultLocale = Locale.getDefault();
private final Collection<LocaleProvider<?>> localeProviders = new LinkedHashSet<>();
private final Map<Locale, C> messageRepositories = new LinkedHashMap<>();
private final Map<Locale, REPOSITORY> messageRepositories = new LinkedHashMap<>();

@Override
public @NotNull Locale getDefaultLocale() {
Expand Down Expand Up @@ -45,13 +45,13 @@ public void registerLocaleProvider(@NotNull LocaleProvider<?> localeProvider) {
}

@Override
public @NotNull @Unmodifiable Map<Locale, C> getMessageRepositories() {
public @NotNull @Unmodifiable Map<Locale, REPOSITORY> getMessageRepositories() {
return Collections.unmodifiableMap(this.messageRepositories);
}

@Override
public @NotNull C getRepository(@NotNull Locale locale) {
C messageRepository = this.messageRepositories.get(locale);
public @NotNull REPOSITORY getRepository(@NotNull Locale locale) {
REPOSITORY messageRepository = this.messageRepositories.get(locale);
if (messageRepository == null) {
// If we can't find message repository for language wariant (for e.g. en_GB) we will try to find message repository for language (for e.g. en)
messageRepository = this.messageRepositories.get(Locale.forLanguageTag(locale.getLanguage()));
Expand All @@ -69,7 +69,7 @@ public void registerLocaleProvider(@NotNull LocaleProvider<?> localeProvider) {
}

@Override
public void registerRepository(@NotNull Locale locale, @NotNull C messageRepository) {
public void registerRepository(@NotNull Locale locale, @NotNull REPOSITORY messageRepository) {
Validate.notNull(locale, "Locale cannot be null");
Validate.notNull(messageRepository, "Message repository cannot be null");
this.messageRepositories.put(locale, messageRepository);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@
import dev.peri.yetanothermessageslibrary.viewer.ViewerService;
import org.jetbrains.annotations.NotNull;

public abstract class SimpleSendableMessageService<R, C extends MessageRepository, D extends MessageDispatcher<R, ? extends D>> extends SimpleMessageService<C> implements SendableMessageService<R, C, D> {
public abstract class SimpleSendableMessageService<RECEIVER, REPOSITORY extends MessageRepository, DISPATCHER extends MessageDispatcher<RECEIVER, ? extends DISPATCHER>>
extends SimpleMessageService<REPOSITORY>
implements SendableMessageService<RECEIVER, REPOSITORY, DISPATCHER> {

private final ViewerService<R> viewerService;
private final MessageDispatcherFactory<R, ? extends D> dispatcherFactory;
private final ViewerService<RECEIVER> viewerService;
private final MessageDispatcherFactory<RECEIVER, ? extends DISPATCHER> dispatcherFactory;

public SimpleSendableMessageService(
@NotNull ViewerService<R> viewerService,
@NotNull MessageDispatcherFactory<R, ? extends D> dispatcherFactory
@NotNull ViewerService<RECEIVER> viewerService,
@NotNull MessageDispatcherFactory<RECEIVER, ? extends DISPATCHER> dispatcherFactory
) {
this.viewerService = viewerService;
this.dispatcherFactory = dispatcherFactory;
}

public SimpleSendableMessageService(
@NotNull ViewerDataSupplier<R> viewerDataSupplier,
@NotNull ViewerFactory<R> viewerFactory,
@NotNull MessageDispatcherFactory<R, ? extends D> dispatcherFactory
@NotNull ViewerDataSupplier<RECEIVER> viewerDataSupplier,
@NotNull ViewerFactory<RECEIVER> viewerFactory,
@NotNull MessageDispatcherFactory<RECEIVER, ? extends DISPATCHER> dispatcherFactory
) {
this(
new ViewerService<>(viewerDataSupplier, viewerFactory),
Expand All @@ -32,12 +34,12 @@ public SimpleSendableMessageService(
}

@Override
public @NotNull ViewerService<R> getViewerService() {
public @NotNull ViewerService<RECEIVER> getViewerService() {
return this.viewerService;
}

@Override
public @NotNull MessageDispatcherFactory<R, ? extends D> getDispatcherFactory() {
public @NotNull MessageDispatcherFactory<RECEIVER, ? extends DISPATCHER> getDispatcherFactory() {
return this.dispatcherFactory;
}

Expand Down

This file was deleted.

Loading
Loading