public class TranslatableContents implements ComponentContents {
public static final Object[] NO_ARGS = new Object[0];
private static final Codec<Object> PRIMITIVE_ARG_CODEC = ExtraCodecs.JAVA.validate(TranslatableContents::filterAllowedArguments);
private static final Codec<Object> ARG_CODEC = Codec.either(PRIMITIVE_ARG_CODEC, ComponentSerialization.CODEC)
.xmap(
e -> e.map(o -> o, component -> Objects.requireNonNullElse(component.tryCollapseToString(), component)),
o -> o instanceof Component c ? Either.right(c) : Either.left(o)
);
public static final MapCodec<TranslatableContents> MAP_CODEC = RecordCodecBuilder.mapCodec(
i -> i.group(
Codec.STRING.fieldOf("translate").forGetter(o -> o.key),
Codec.STRING.lenientOptionalFieldOf("fallback").forGetter(o -> Optional.ofNullable(o.fallback)),
ARG_CODEC.listOf().optionalFieldOf("with").forGetter(o -> adjustArgs(o.args))
)
.apply(i, TranslatableContents::create)
);
private static final FormattedText TEXT_PERCENT = FormattedText.of("%");
private static final FormattedText TEXT_NULL = FormattedText.of("null");
private final String key;
private final @Nullable String fallback;
private final Object[] args;
private @Nullable Language decomposedWith;
private List<FormattedText> decomposedParts = ImmutableList.of();
private static final Pattern FORMAT_PATTERN = Pattern.compile("%(?:(\\d+)\\$)?([A-Za-z%]|$)");
private static DataResult<Object> filterAllowedArguments(@Nullable Object result) {
return !isAllowedPrimitiveArgument(result) ? DataResult.error(() -> "This value needs to be parsed as component") : DataResult.success(result);
}
public static boolean isAllowedPrimitiveArgument(@Nullable Object object) {
return object instanceof Number || object instanceof Boolean || object instanceof String;
}
private static Optional<List<Object>> adjustArgs(Object[] args) {
return args.length == 0 ? Optional.empty() : Optional.of(Arrays.asList(args));
}
private static Object[] adjustArgs(Optional<List<Object>> args) {
return args.<Object[]>map(a -> a.isEmpty() ? NO_ARGS : a.toArray()).orElse(NO_ARGS);
}
private static TranslatableContents create(String key, Optional<String> fallback, Optional<List<Object>> args) {
return new TranslatableContents(key, fallback.orElse(null), adjustArgs(args));
}
public TranslatableContents(String key, @Nullable String fallback, Object[] args) {
this.key = key;
this.fallback = fallback;
this.args = args;
}
@Override
public MapCodec<TranslatableContents> codec() {
return MAP_CODEC;
}
private void decompose() {
Language currentLanguage = Language.getInstance();
if (currentLanguage != this.decomposedWith) {
this.decomposedWith = currentLanguage;
String format = this.fallback != null ? currentLanguage.getOrDefault(this.key, this.fallback) : currentLanguage.getOrDefault(this.key);
try {
Builder<FormattedText> parts = ImmutableList.builder();
this.decomposeTemplate(format, parts::add);
this.decomposedParts = parts.build();
} catch (TranslatableFormatException var4) {
this.decomposedParts = ImmutableList.of(FormattedText.of(format));
}
}
}
private void decomposeTemplate(String template, Consumer<FormattedText> decomposedParts) {
Matcher matcher = FORMAT_PATTERN.matcher(template);
try {
int replacementIndex = 0;
int current = 0;
while (matcher.find(current)) {
int start = matcher.start();
int end = matcher.end();
if (start > current) {
String prefix = template.substring(current, start);
if (prefix.indexOf(37) != -1) {
throw new IllegalArgumentException();
}
decomposedParts.accept(FormattedText.of(prefix));
}
String formatType = matcher.group(2);
String formatString = template.substring(start, end);
if ("%".equals(formatType) && "%%".equals(formatString)) {
decomposedParts.accept(TEXT_PERCENT);
} else {
if (!"s".equals(formatType)) {
throw new TranslatableFormatException(this, "Unsupported format: '" + formatString + "'");
}
String possiblePositionIndex = matcher.group(1);
int index = possiblePositionIndex != null ? Integer.parseInt(possiblePositionIndex) - 1 : replacementIndex++;
decomposedParts.accept(this.getArgument(index));
}
current = end;
}
if (current < template.length()) {
String tail = template.substring(current);
if (tail.indexOf(37) != -1) {
throw new IllegalArgumentException();
}
decomposedParts.accept(FormattedText.of(tail));
}
} catch (IllegalArgumentException var12) {
throw new TranslatableFormatException(this, var12);
}
}
private FormattedText getArgument(int index) {
if (index >= 0 && index < this.args.length) {
Object arg = this.args[index];
if (arg instanceof Component componentArg) {
return componentArg;
} else {
return arg == null ? TEXT_NULL : FormattedText.of(arg.toString());
}
} else {
throw new TranslatableFormatException(this, index);
}
}
@Override
public <T> Optional<T> visit(FormattedText.StyledContentConsumer<T> output, Style currentStyle) {
this.decompose();
for (FormattedText part : this.decomposedParts) {
Optional<T> result = part.visit(output, currentStyle);
if (result.isPresent()) {
return result;
}
}
return Optional.empty();
}
@Override
public <T> Optional<T> visit(FormattedText.ContentConsumer<T> output) {
this.decompose();
for (FormattedText part : this.decomposedParts) {
Optional<T> result = part.visit(output);
if (result.isPresent()) {
return result;
}
}
return Optional.empty();
}
@Override
public MutableComponent resolve(ResolutionContext context, int recursionDepth) throws CommandSyntaxException {
Object[] argsCopy = new Object[this.args.length];
for (int i = 0; i < argsCopy.length; i++) {
Object param = this.args[i];
if (param instanceof Component component) {
argsCopy[i] = ComponentUtils.resolve(context, component, recursionDepth);
} else {
argsCopy[i] = param;
}
}
return MutableComponent.create(new TranslatableContents(this.key, this.fallback, argsCopy));
}
@Override
public boolean equals(Object o) {
return this == o
? true
: o instanceof TranslatableContents that
&& Objects.equals(this.key, that.key)
&& Objects.equals(this.fallback, that.fallback)
&& Arrays.equals(this.args, that.args);
}
@Override
public int hashCode() {
int result = Objects.hashCode(this.key);
result = 31 * result + Objects.hashCode(this.fallback);
return 31 * result + Arrays.hashCode(this.args);
}
@Override
public String toString() {
return "translation{key='"
+ this.key
+ "'"
+ (this.fallback != null ? ", fallback='" + this.fallback + "'" : "")
+ ", args="
+ Arrays.toString(this.args)
+ "}";
}
public String getKey() {
return this.key;
}
public @Nullable String getFallback() {
return this.fallback;
}
public Object[] getArgs() {
return this.args;
}
}