semi-auto cleanup
This commit is contained in:
1 parent
cad80f82c6
commit
8f61271606
99 files changed
+11463
-8007
No files matched your search
@@ -14,23 +14,24 @@ import carpet.script.argument.FileArgument;
|
||||
import net.minecraft.nbt.Tag;
|
||||
import net.minecraft.world.level.storage.LevelResource;
|
||||
|
||||
public record Module(String name, String code, boolean library) {
|
||||
public record Module(String name, String code, boolean library)
|
||||
{
|
||||
public Module
|
||||
{
|
||||
Objects.requireNonNull(name);
|
||||
Objects.requireNonNull(code);
|
||||
}
|
||||
|
||||
public static Module fromPath(Path path)
|
||||
public static Module fromPath(final Path path)
|
||||
{
|
||||
boolean library = path.getFileName().toString().endsWith(".scl");
|
||||
final boolean library = path.getFileName().toString().endsWith(".scl");
|
||||
try
|
||||
{
|
||||
String name = path.getFileName().toString().replaceFirst("\\.scl?","").toLowerCase(Locale.ROOT);
|
||||
String code = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
||||
final String name = path.getFileName().toString().replaceFirst("\\.scl?", "").toLowerCase(Locale.ROOT);
|
||||
final String code = new String(Files.readAllBytes(path), StandardCharsets.UTF_8);
|
||||
return new Module(name, code, library);
|
||||
}
|
||||
catch (IOException e)
|
||||
catch (final IOException e)
|
||||
{
|
||||
throw new IllegalArgumentException("Failed to load scarpet module", e);
|
||||
}
|
||||
@@ -38,79 +39,96 @@ public record Module(String name, String code, boolean library) {
|
||||
|
||||
/**
|
||||
* Creates a new {@link Module} with an app located in Carpet's JAR.
|
||||
*
|
||||
* @param scriptName A {@link String} being the name of the script. The extension will be autocompleted
|
||||
* @param isLibrary A {@link boolean} indicating whether or not the script is a library
|
||||
* @param isLibrary A {@link boolean} indicating whether or not the script is a library
|
||||
* @return The created {@link BundledModule}
|
||||
*/
|
||||
public static Module carpetNative(String scriptName, boolean isLibrary)
|
||||
public static Module carpetNative(final String scriptName, final boolean isLibrary)
|
||||
{
|
||||
return fromJarPath("assets/carpet/scripts/", scriptName, isLibrary);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link Module} with an app located at a specified path inside some mod's JAR.
|
||||
* @see #fromJarPathWithCustomName(String, String, boolean)
|
||||
*
|
||||
* @param path A {@link String} being the path to the directory where the app is located.
|
||||
*
|
||||
* @param path A {@link String} being the path to the directory where the app is located.
|
||||
* @param scriptName A {@link String} being the name of the script. The extension will be autocompleted
|
||||
* @param isLibrary A {@link boolean} indicating whether or not the script is a library
|
||||
* @param isLibrary A {@link boolean} indicating whether or not the script is a library
|
||||
* @return The created {@link BundledModule}
|
||||
* @see #fromJarPathWithCustomName(String, String, boolean)
|
||||
*/
|
||||
public static Module fromJarPath(String path, String scriptName, boolean isLibrary) {
|
||||
return fromJarPathWithCustomName(path + scriptName + (isLibrary ? ".scl":".sc"), scriptName, isLibrary);
|
||||
public static Module fromJarPath(final String path, final String scriptName, final boolean isLibrary)
|
||||
{
|
||||
return fromJarPathWithCustomName(path + scriptName + (isLibrary ? ".scl" : ".sc"), scriptName, isLibrary);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link Module} with an app located at the specified fullPath (inside a mod jar)with a custom name.
|
||||
* @see #fromJarPath(String, String, boolean)
|
||||
*
|
||||
* @param fullPath A {@link String} being the full path to the app's code, including file and extension.
|
||||
*
|
||||
* @param fullPath A {@link String} being the full path to the app's code, including file and extension.
|
||||
* @param customName A {@link String} being the custom name for the script.
|
||||
* @param isLibrary A {@link boolean} indicating whether or not the script is a library
|
||||
* @param isLibrary A {@link boolean} indicating whether or not the script is a library
|
||||
* @return The created {@link Module}
|
||||
* @see #fromJarPath(String, String, boolean)
|
||||
*/
|
||||
public static Module fromJarPathWithCustomName(String fullPath, String customName, boolean isLibrary) {
|
||||
public static Module fromJarPathWithCustomName(final String fullPath, final String customName, final boolean isLibrary)
|
||||
{
|
||||
try
|
||||
{
|
||||
String name = customName.toLowerCase(Locale.ROOT);
|
||||
String code = IOUtils.toString(
|
||||
final String name = customName.toLowerCase(Locale.ROOT);
|
||||
final String code = IOUtils.toString(
|
||||
Module.class.getClassLoader().getResourceAsStream(fullPath),
|
||||
StandardCharsets.UTF_8
|
||||
);
|
||||
return new Module(name, code, isLibrary);
|
||||
}
|
||||
catch (IOException e)
|
||||
catch (final IOException e)
|
||||
{
|
||||
throw new IllegalArgumentException("Failed to load bundled module", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Tag getData(Module module)
|
||||
|
||||
public static Tag getData(final Module module)
|
||||
{
|
||||
Path dataFile = resolveResource(module);
|
||||
if (dataFile == null) return null;
|
||||
if (!Files.exists(dataFile) || !(Files.isRegularFile(dataFile))) return null;
|
||||
synchronized (FileArgument.writeIOSync) { return FileArgument.readTag(dataFile); }
|
||||
final Path dataFile = resolveResource(module);
|
||||
if (dataFile == null || !Files.exists(dataFile) || !(Files.isRegularFile(dataFile)))
|
||||
{
|
||||
return null;
|
||||
}
|
||||
synchronized (FileArgument.writeIOSync)
|
||||
{
|
||||
return FileArgument.readTag(dataFile);
|
||||
}
|
||||
}
|
||||
|
||||
public static void saveData(Module module, Tag globalState)
|
||||
public static void saveData(final Module module, final Tag globalState)
|
||||
{
|
||||
Path dataFile = resolveResource(module);
|
||||
if (dataFile == null) return;
|
||||
if (!Files.exists(dataFile.getParent())) {
|
||||
try {
|
||||
final Path dataFile = resolveResource(module);
|
||||
if (dataFile == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!Files.exists(dataFile.getParent()))
|
||||
{
|
||||
try
|
||||
{
|
||||
Files.createDirectories(dataFile.getParent());
|
||||
} catch (IOException e) {
|
||||
}
|
||||
catch (final IOException e)
|
||||
{
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
synchronized (FileArgument.writeIOSync) { FileArgument.writeTagDisk(globalState, dataFile, false); }
|
||||
synchronized (FileArgument.writeIOSync)
|
||||
{
|
||||
FileArgument.writeTagDisk(globalState, dataFile, false);
|
||||
}
|
||||
}
|
||||
|
||||
private static Path resolveResource(Module module)
|
||||
private static Path resolveResource(final Module module)
|
||||
{
|
||||
if (module == null) return null; // commandline app
|
||||
return CarpetServer.minecraft_server.getWorldPath(LevelResource.ROOT).resolve("scripts/"+module.name()+".data.nbt");
|
||||
return module == null ? null : CarpetServer.minecraft_server.getWorldPath(LevelResource.ROOT).resolve("scripts/" + module.name() + ".data.nbt"); // commandline app
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user