Skip to content

Commit

Permalink
run spotlessApply
Browse files Browse the repository at this point in the history
  • Loading branch information
Azhrei committed Feb 15, 2024
1 parent 454e420 commit 43a4951
Showing 1 changed file with 104 additions and 109 deletions.
213 changes: 104 additions & 109 deletions src/main/java/net/rptools/maptool/client/functions/LibraryFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;
import net.rptools.maptool.language.I18N;
import net.rptools.maptool.model.library.Library;
import net.rptools.maptool.model.library.LibraryInfo;
Expand All @@ -27,126 +30,118 @@
import net.rptools.parser.VariableResolver;
import net.rptools.parser.function.AbstractFunction;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.ExecutionException;

/**
* Class that implements player macro functions.
*/
/** Class that implements player macro functions. */
public class LibraryFunctions extends AbstractFunction {

/**
* Creates a new {@code PlayerFunctions} object.
*/
public LibraryFunctions() {
super(
0,
1,
"library.listAddOnLibraries",
"library.getInfo",
"library.listTokenLibraries",
"library.getContents");
}

@Override
public Object childEvaluate(
Parser parser, VariableResolver resolver, String functionName, List<Object> parameters)
throws ParserException {
/** Creates a new {@code PlayerFunctions} object. */
public LibraryFunctions() {
super(
0,
1,
"library.listAddOnLibraries",
"library.getInfo",
"library.listTokenLibraries",
"library.getContents");
}

String fName = functionName.toLowerCase();
try {
var libraryManager = new LibraryManager();
@Override
public Object childEvaluate(
Parser parser, VariableResolver resolver, String functionName, List<Object> parameters)
throws ParserException {

switch (fName) {
case "library.listaddonlibraries" -> {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 0);
return librariesAsJson(libraryManager.getLibraries(LibraryType.ADD_ON));
}
case "library.getinfo" -> {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
String namespace = parameters.get(0).toString();
Optional<LibraryInfo> libraryInfo = libraryManager.getLibraryInfo(namespace);
if (libraryInfo.isPresent()) {
return libraryAsJson(libraryInfo.get());
} else {
return "";
}
}
case "library.listtokenlibraries" -> {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 0);
return librariesAsJson(libraryManager.getLibraries(LibraryType.TOKEN));
}
String fName = functionName.toLowerCase();
try {
var libraryManager = new LibraryManager();

case "library.getcontents" -> {
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
String namespace = parameters.get(0).toString();
Optional<Library> library = libraryManager.getLibrary(namespace);
if (library.isPresent()) {
return listLibraryContents(library.get());
} else {
return "";
}
}

default -> throw new ParserException(
I18N.getText("macro.function.general.unknownFunction", functionName));
}
} catch (InterruptedException | ExecutionException e) {
throw new ParserException(e);
switch (fName) {
case "library.listaddonlibraries" -> {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 0);
return librariesAsJson(libraryManager.getLibraries(LibraryType.ADD_ON));
}
case "library.getinfo" -> {
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
String namespace = parameters.get(0).toString();
Optional<LibraryInfo> libraryInfo = libraryManager.getLibraryInfo(namespace);
if (libraryInfo.isPresent()) {
return libraryAsJson(libraryInfo.get());
} else {
return "";
}
}
case "library.listtokenlibraries" -> {
FunctionUtil.checkNumberParam(functionName, parameters, 0, 0);
return librariesAsJson(libraryManager.getLibraries(LibraryType.TOKEN));
}
}

private JsonArray listLibraryContents(Library library)
throws ExecutionException, InterruptedException {
JsonArray json = new JsonArray();
library
.getAllFiles()
.thenAccept(
l -> {
l.forEach(json::add);
})
.get();
case "library.getcontents" -> {
FunctionUtil.blockUntrustedMacro(functionName);
FunctionUtil.checkNumberParam(functionName, parameters, 1, 1);
String namespace = parameters.get(0).toString();
Optional<Library> library = libraryManager.getLibrary(namespace);
if (library.isPresent()) {
return listLibraryContents(library.get());
} else {
return "";
}
}

return json;
default -> throw new ParserException(
I18N.getText("macro.function.general.unknownFunction", functionName));
}
} catch (InterruptedException | ExecutionException e) {
throw new ParserException(e);
}
}

/**
* Returns the list of {@link LibraryInfo} records as a json list.
*
* @param libraries the {@link LibraryInfo} list to convert to json.
* @return the json list.
*/
private JsonArray librariesAsJson(List<LibraryInfo> libraries) {
JsonArray librariesJson = new JsonArray();
libraries.stream().map(this::libraryAsJson).forEach(librariesJson::add);
return librariesJson;
}
private JsonArray listLibraryContents(Library library)
throws ExecutionException, InterruptedException {
JsonArray json = new JsonArray();
library
.getAllFiles()
.thenAccept(
l -> {
l.forEach(json::add);
})
.get();

/**
* Returns the json representation of a {@link LibraryInfo} object.
*
* @param library the {@link LibraryInfo} to convert to json.
* @return the json representation.
*/
private JsonObject libraryAsJson(LibraryInfo library) {
JsonObject libraryJson = new JsonObject();
JsonArray authors = new JsonArray();
for (String author : library.authors()) {
authors.add(author);
}
libraryJson.addProperty("name", library.name());
libraryJson.addProperty("namespace", library.namespace());
libraryJson.addProperty("version", library.version());
libraryJson.addProperty("website", library.website());
libraryJson.addProperty("gitUrl", library.gitUrl());
libraryJson.add("authors", authors);
libraryJson.addProperty("license", library.license());
libraryJson.addProperty("description", library.description());
libraryJson.addProperty("shortDescription", library.shortDescription());
libraryJson.addProperty("allowsUrlAccess", library.allowsUrlAccess());
return json;
}

return libraryJson;
/**
* Returns the list of {@link LibraryInfo} records as a json list.
*
* @param libraries the {@link LibraryInfo} list to convert to json.
* @return the json list.
*/
private JsonArray librariesAsJson(List<LibraryInfo> libraries) {
JsonArray librariesJson = new JsonArray();
libraries.stream().map(this::libraryAsJson).forEach(librariesJson::add);
return librariesJson;
}

/**
* Returns the json representation of a {@link LibraryInfo} object.
*
* @param library the {@link LibraryInfo} to convert to json.
* @return the json representation.
*/
private JsonObject libraryAsJson(LibraryInfo library) {
JsonObject libraryJson = new JsonObject();
JsonArray authors = new JsonArray();
for (String author : library.authors()) {
authors.add(author);
}
libraryJson.addProperty("name", library.name());
libraryJson.addProperty("namespace", library.namespace());
libraryJson.addProperty("version", library.version());
libraryJson.addProperty("website", library.website());
libraryJson.addProperty("gitUrl", library.gitUrl());
libraryJson.add("authors", authors);
libraryJson.addProperty("license", library.license());
libraryJson.addProperty("description", library.description());
libraryJson.addProperty("shortDescription", library.shortDescription());
libraryJson.addProperty("allowsUrlAccess", library.allowsUrlAccess());

return libraryJson;
}
}

0 comments on commit 43a4951

Please sign in to comment.