Allow loading apps from a global path
This commit is contained in:
1 parent
5dfaf12d92
commit
210ad834f6
2 files changed
+49
-27
No files matched your search
@@ -16,19 +16,24 @@ import com.mojang.brigadier.arguments.StringArgumentType;
|
|||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
|
||||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||||
import com.mojang.brigadier.tree.CommandNode;
|
import com.mojang.brigadier.tree.CommandNode;
|
||||||
|
|
||||||
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.command.ServerCommandSource;
|
import net.minecraft.server.command.ServerCommandSource;
|
||||||
import net.minecraft.server.network.ServerPlayerEntity;
|
import net.minecraft.server.network.ServerPlayerEntity;
|
||||||
import net.minecraft.util.WorldSavePath;
|
import net.minecraft.util.WorldSavePath;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -127,19 +132,29 @@ public class CarpetScriptServer
|
|||||||
|
|
||||||
public Module getModule(String name, boolean allowLibraries)
|
public Module getModule(String name, boolean allowLibraries)
|
||||||
{
|
{
|
||||||
File folder = server.getSavePath(WorldSavePath.ROOT).resolve("scripts").toFile();
|
try {
|
||||||
File[] listOfFiles = folder.listFiles();
|
Path folder = server.getSavePath(WorldSavePath.ROOT).resolve("scripts");
|
||||||
if (listOfFiles != null)
|
Files.createDirectories(folder);
|
||||||
for (File script : listOfFiles)
|
Optional<Path>
|
||||||
{
|
scriptPath = Files.walk(folder)
|
||||||
if (script.getName().equalsIgnoreCase(name+".sc"))
|
.filter(script ->
|
||||||
{
|
script.getFileName().toString().equalsIgnoreCase(name+".sc") ||
|
||||||
return new FileModule(script);
|
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name+".scl"))
|
||||||
}
|
).findFirst();
|
||||||
if (allowLibraries && script.getName().equalsIgnoreCase(name+".scl"))
|
if (scriptPath.isPresent())
|
||||||
{
|
return new FileModule(scriptPath.get());
|
||||||
return new FileModule(script);
|
|
||||||
}
|
Path globalFolder = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
||||||
|
Files.createDirectories(globalFolder);
|
||||||
|
scriptPath = Files.walk(globalFolder)
|
||||||
|
.filter(script ->
|
||||||
|
script.getFileName().toString().equalsIgnoreCase(name+".sc") ||
|
||||||
|
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name+".scl"))
|
||||||
|
).findFirst();
|
||||||
|
if (scriptPath.isPresent())
|
||||||
|
return new FileModule(scriptPath.get());
|
||||||
|
} catch (IOException e) {
|
||||||
|
CarpetSettings.LOG.error("Exception while loading the app: ", e);
|
||||||
}
|
}
|
||||||
for (Module moduleData : bundledModuleData)
|
for (Module moduleData : bundledModuleData)
|
||||||
{
|
{
|
||||||
@@ -173,17 +188,23 @@ public class CarpetScriptServer
|
|||||||
if (!mi.isLibrary() && !mi.getName().endsWith("_beta")) moduleNames.add(mi.getName());
|
if (!mi.isLibrary() && !mi.getName().endsWith("_beta")) moduleNames.add(mi.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File folder = server.getSavePath(WorldSavePath.ROOT).resolve("scripts").toFile();
|
try {
|
||||||
File[] listOfFiles = folder.listFiles();
|
Path worldScripts = server.getSavePath(WorldSavePath.ROOT).resolve("scripts");
|
||||||
if (listOfFiles == null)
|
Files.createDirectories(worldScripts);
|
||||||
return moduleNames;
|
Files.walk(worldScripts)
|
||||||
for (File script : listOfFiles)
|
.filter(f -> f.toString().endsWith(".sc"))
|
||||||
|
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc","").toLowerCase(Locale.ROOT)));
|
||||||
|
|
||||||
|
if (includeBuiltIns)
|
||||||
{
|
{
|
||||||
if (script.getName().endsWith(".sc"))
|
Path globalScripts = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
||||||
{
|
Files.createDirectories(globalScripts);
|
||||||
String name = script.getName().replaceFirst("\\.sc","").toLowerCase(Locale.ROOT);
|
Files.walk(globalScripts)
|
||||||
moduleNames.add(name);
|
.filter(f -> f.toString().endsWith(".sc"))
|
||||||
|
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc","").toLowerCase(Locale.ROOT)));
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
CarpetSettings.LOG.error("Exception while searching for apps: ", e);
|
||||||
}
|
}
|
||||||
return moduleNames;
|
return moduleNames;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ import java.io.OutputStream;
|
|||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.nio.file.StandardOpenOption;
|
import java.nio.file.StandardOpenOption;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -26,13 +27,13 @@ public class FileModule extends Module
|
|||||||
private String name;
|
private String name;
|
||||||
private String code;
|
private String code;
|
||||||
private boolean library;
|
private boolean library;
|
||||||
public FileModule(File sourceFile)
|
public FileModule(Path sourcePath)
|
||||||
{
|
{
|
||||||
library = sourceFile.getName().endsWith(".scl");
|
library = sourcePath.getFileName().toString().endsWith(".scl");
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
name = sourceFile.getName().replaceFirst("\\.scl?","").toLowerCase(Locale.ROOT);
|
name = sourcePath.getFileName().toString().replaceFirst("\\.scl?","").toLowerCase(Locale.ROOT);
|
||||||
code = new String(Files.readAllBytes(sourceFile.toPath()), StandardCharsets.UTF_8);
|
code = new String(Files.readAllBytes(sourcePath), StandardCharsets.UTF_8);
|
||||||
}
|
}
|
||||||
catch ( IOException e)
|
catch ( IOException e)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in new issue
Block a user