added serverinfo command

added freemarker extension in order to split up elements in fields at a point which makes sense (necessary for emotes to render)
added additional check to split fields if they are indeed over the max length
This commit is contained in:
Sheldan
2020-05-27 13:07:51 +02:00
parent 2aa55f6ab6
commit be97e825cc
19 changed files with 213 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package dev.sheldan.abstracto.templating.config;
import dev.sheldan.abstracto.templating.loading.DatabaseTemplateLoader;
import dev.sheldan.abstracto.templating.methods.DurationMethod;
import dev.sheldan.abstracto.templating.methods.InstantMethod;
import dev.sheldan.abstracto.templating.methods.SafeFieldIterations;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,6 +28,9 @@ public class FreemarkerConfiguration {
@Autowired
private InstantMethod instantMethod;
@Autowired
private SafeFieldIterations safeFieldIterations;
/**
* Creates a {@link Configuration} bean with the appropriate configuration which includes:
* The correct compatibility version and the provided formatter methods to be used in the templates.
@@ -40,6 +44,7 @@ public class FreemarkerConfiguration {
Configuration configuration = factory.createConfiguration();
configuration.setSharedVariable("fmtDuration", durationMethod);
configuration.setSharedVariable("formatInstant", instantMethod);
configuration.setSharedVariable("safeFieldLength", safeFieldIterations);
configuration.setEncoding(Locale.getDefault(), "utf-8");
// needed to support default methods in interfaces
configuration.setIncompatibleImprovements(Configuration.VERSION_2_3_29);

View File

@@ -39,7 +39,6 @@ public class DurationMethod implements TemplateMethodModelEx {
throw new TemplateModelException("Passed argument was not a duration object");
}
Duration duration = (Duration) wrappedObject;
StringBuilder stringBuilder = new StringBuilder();
// upgrading to java 9 makes this nicer
HashMap<String, Object> parameters = new HashMap<>();
long days = duration.toDays();

View File

@@ -0,0 +1,72 @@
package dev.sheldan.abstracto.templating.methods;
import dev.sheldan.abstracto.templating.service.TemplateService;
import freemarker.template.DefaultListAdapter;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Component
public class SafeFieldIterations implements TemplateMethodModelEx {
@Autowired
private TemplateService service;
@Override
public Object exec(List arguments) throws TemplateModelException {
List wrappedObject = (List) ((DefaultListAdapter) arguments.get(0)).getWrappedObject();
String appliedTemplate = ((SimpleScalar) arguments.get(1)).getAsString();
String nameTemplate = ((SimpleScalar) arguments.get(2)).getAsString();
String inline = ((SimpleScalar) arguments.get(3)).getAsString();
List<StringBuilder> result = new ArrayList<>();
StringBuilder currentBuilder = new StringBuilder();
String firstEmbedTitle = service.renderTemplateWithMap(nameTemplate, getEmbedCountParameters(1, wrappedObject));
currentBuilder.append(newFieldHeader(firstEmbedTitle, inline));
String finalClosingString = "\"}";
String closingString = finalClosingString + ",";
int splitFieldCounts = 1;
for (Object ob: wrappedObject) {
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("object", ob);
String s = service.renderTemplateWithMap(appliedTemplate, parameters);
if((currentBuilder.toString().length() + s.length() > 1024)) {
currentBuilder.append(closingString);
result.add(currentBuilder);
currentBuilder = new StringBuilder();
splitFieldCounts += 1;
String renderedName = service.renderTemplateWithMap(nameTemplate, getEmbedCountParameters(splitFieldCounts, wrappedObject));
currentBuilder.append(newFieldHeader(renderedName, inline));
}
currentBuilder.append(s);
}
currentBuilder.append(finalClosingString);
result.add(currentBuilder);
StringBuilder bigBuilder = new StringBuilder();
for (StringBuilder innerBuilder: result) {
bigBuilder.append(innerBuilder.toString());
}
return bigBuilder;
}
private String newFieldHeader(String name, String inline) {
return String.format("{ \"name\": \"%s\", \"inline\": \"%s\", \"value\": \"", name, inline);
}
private HashMap<String, Object> getEmbedCountParameters(Integer count, List objects) {
HashMap<String, Object> parameters = new HashMap<>();
parameters.put("count", count);
parameters.put("list", objects);
return parameters;
}
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.templating.model;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;
@@ -8,6 +9,7 @@ import lombok.Setter;
*/
@Getter
@Setter
@Builder
public class EmbedField {
/**
* The name of the field to be set

View File

@@ -90,6 +90,15 @@ public class TemplateServiceBean implements TemplateService {
firstBuilder.setFooter(footer.getText(), footer.getIcon());
}
if(configuration.getFields() != null) {
for (int i = 0; i < configuration.getFields().size(); i++) {
EmbedField field = configuration.getFields().get(i);
if(field.getValue().length() > 1024) {
String substring = field.getValue().substring(1024);
field.setValue(field.getValue().substring(0, 1024));
EmbedField secondPart = EmbedField.builder().inline(field.getInline()).name(field.getName() + " 2").value(substring).build();
configuration.getFields().add(i + 1, secondPart);
}
}
double neededIndex = Math.ceil(configuration.getFields().size() / 25D) - 1;
extendIfNecessary(embedBuilders, neededIndex);
for (int i = 0; i < configuration.getFields().size(); i++) {