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

feat: add support for @\Coloured on String lists #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This project is a GSON-like project for Bukkit's YML Config API. This project is
<dependency>
<groupId>xyz.mkotb</groupId>
<artifactId>config-api</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>
</dependency>
```

Expand Down
61 changes: 32 additions & 29 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>xyz.mkotb</groupId>
<artifactId>config-api</artifactId>
<version>1.0.1</version>
<version>1.1.0</version>

<name>ConfigAPI</name>
<description>GSON-like ORM for the Bukkit Config API</description>
Expand Down Expand Up @@ -112,34 +112,37 @@
<version>2.4</version>
</plugin>

<plugin>
<groupId>com.mycila.maven-license-plugin</groupId>
<artifactId>maven-license-plugin</artifactId>
<executions>
<execution>
<configuration>
<quiet>true</quiet>
<encoding>UTF-8</encoding>
<strictCheck>true</strictCheck>
<header>./LICENSE</header>
<mapping>
<java>SLASHSTAR_STYLE</java>
</mapping>
<keywords>
<keyword>license</keyword>
</keywords>
<includes>
<include>src/main/java/**</include>
<include>src/test/java/**</include>
</includes>
</configuration>
<phase>clean</phase>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
<!--
Plugin 'com.mycila.maven-license-plugin:maven-license-plugin:' not found
-->
<!-- <plugin>-->
<!-- <groupId>com.mycila.maven-license-plugin</groupId>-->
<!-- <artifactId>maven-license-plugin</artifactId>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <configuration>-->
<!-- <quiet>true</quiet>-->
<!-- <encoding>UTF-8</encoding>-->
<!-- <strictCheck>true</strictCheck>-->
<!-- <header>./LICENSE</header>-->
<!-- <mapping>-->
<!-- <java>SLASHSTAR_STYLE</java>-->
<!-- </mapping>-->
<!-- <keywords>-->
<!-- <keyword>license</keyword>-->
<!-- </keywords>-->
<!-- <includes>-->
<!-- <include>src/main/java/**</include>-->
<!-- <include>src/test/java/**</include>-->
<!-- </includes>-->
<!-- </configuration>-->
<!-- <phase>clean</phase>-->
<!-- <goals>-->
<!-- <goal>format</goal>-->
<!-- </goals>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->
</plugins>
</build>

Expand Down
37 changes: 28 additions & 9 deletions src/main/java/xyz/mkotb/configapi/ConfigFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public final class ConfigFactory {
private volatile NamingStrategy namingStrategy = new CamelCaseNamingStrategy();
Expand Down Expand Up @@ -130,16 +133,32 @@ private <T> T colourizeFields(T object) {
continue;
}

if (field.getType() != String.class) {
continue;
}

Coloured annotation = field.getDeclaredAnnotation(Coloured.class);
String value = InternalsHelper.getField(field, object);
if (field.getType() == String.class) {
Coloured annotation = field.getDeclaredAnnotation(Coloured.class);
String value = InternalsHelper.getField(field, object);

if (value != null) {
InternalsHelper.setField(field, object, AdapterHandler.translateAlternateColorCodes(
ChatColor.COLOR_CHAR, annotation.value(), value));
if (value != null) {
InternalsHelper.setField(field, object, AdapterHandler.translateAlternateColorCodes(
ChatColor.COLOR_CHAR, annotation.value(), value));
}
} else if (field.getType() == List.class) {
Class<?> genericType = (Class<?>) ((ParameterizedType) field.getGenericType())
.getActualTypeArguments()[0];

if (genericType.equals(String.class)) {
Coloured annotation = field.getDeclaredAnnotation(Coloured.class);
List<String> value = InternalsHelper.getField(field, object);

if (value != null) {
List<String> translated = value
.stream()
.map(s -> AdapterHandler.translateAlternateColorCodes(
ChatColor.COLOR_CHAR, annotation.value(), s
)).collect(Collectors.toList());

InternalsHelper.setField(field, object, translated);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.enchantment.Enchantment;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.MemorySection;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
Expand All @@ -35,9 +35,11 @@

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.*;
import java.util.stream.Collectors;

public final class AdapterHandler {
private static final Map<Class<?>, Class<?>> PRIMITIVE_BOXES = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -198,6 +200,20 @@ public <I, O> O adaptOut(I input, Class<O> outClass, Class<?> type) {
obj = translateAlternateColorCodes(annotation.value(), ChatColor.COLOR_CHAR, (String) obj);
}

if (obj instanceof List && field.isAnnotationPresent(Coloured.class)) {
Class<?> genericType = (Class<?>) ((ParameterizedType) field.getGenericType())
.getActualTypeArguments()[0];

if (genericType.equals(String.class)) {
Coloured annotation = field.getDeclaredAnnotation(Coloured.class);
obj = ((List<String>) obj)
.stream()
.map(text -> translateAlternateColorCodes(
annotation.value(), ChatColor.COLOR_CHAR, text)
).collect(Collectors.toList());
}
}

section.set(namingStrategy.rename(field.getName()), obj);
}
}
Expand Down