Fix some resource leaks
This commit is contained in:
1 parent
2a4d678aa7
commit
d273d4ffb8
3 files changed
+40
-29
No files matched your search
@@ -51,6 +51,7 @@ import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CarpetScriptServer extends ScriptServer
|
||||
{
|
||||
@@ -144,26 +145,29 @@ public class CarpetScriptServer extends ScriptServer
|
||||
Path folder = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
|
||||
if (!Files.exists(folder))
|
||||
Files.createDirectories(folder);
|
||||
Optional<Path>
|
||||
scriptPath = Files.list(folder)
|
||||
.filter(script ->
|
||||
script.getFileName().toString().equalsIgnoreCase(name+".sc") ||
|
||||
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name+".scl"))
|
||||
).findFirst();
|
||||
if (scriptPath.isPresent())
|
||||
return Module.fromPath(scriptPath.get());
|
||||
try (Stream<Path> folderLister = Files.list(folder)) {
|
||||
Optional<Path> scriptPath = folderLister
|
||||
.filter(script ->
|
||||
script.getFileName().toString().equalsIgnoreCase(name + ".sc") ||
|
||||
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name+".scl"))
|
||||
).findFirst();
|
||||
if (scriptPath.isPresent())
|
||||
return Module.fromPath(scriptPath.get());
|
||||
}
|
||||
|
||||
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT)
|
||||
{
|
||||
Path globalFolder = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
||||
if (!Files.exists(globalFolder))
|
||||
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 Module.fromPath(scriptPath.get());
|
||||
try (Stream<Path> folderWalker = Files.walk(globalFolder)) {
|
||||
Optional<Path> scriptPath = folderWalker
|
||||
.filter(script -> script.getFileName().toString().equalsIgnoreCase(name + ".sc") ||
|
||||
(allowLibraries && script.getFileName().toString().equalsIgnoreCase(name + ".scl")))
|
||||
.findFirst();
|
||||
if (scriptPath.isPresent())
|
||||
return Module.fromPath(scriptPath.get());
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
CarpetSettings.LOG.error("Exception while loading the app: ", e);
|
||||
@@ -204,18 +208,22 @@ public class CarpetScriptServer extends ScriptServer
|
||||
Path worldScripts = server.getWorldPath(LevelResource.ROOT).resolve("scripts");
|
||||
if (!Files.exists(worldScripts))
|
||||
Files.createDirectories(worldScripts);
|
||||
Files.list(worldScripts)
|
||||
.filter(f -> f.toString().endsWith(".sc"))
|
||||
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
|
||||
try (Stream<Path> folderLister = Files.list(worldScripts)) {
|
||||
folderLister
|
||||
.filter(f -> f.toString().endsWith(".sc"))
|
||||
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
|
||||
}
|
||||
|
||||
if (includeBuiltIns && (FabricLoader.getInstance().getEnvironmentType() == EnvType.CLIENT))
|
||||
{
|
||||
Path globalScripts = FabricLoader.getInstance().getConfigDir().resolve("carpet/scripts");
|
||||
if (!Files.exists(globalScripts))
|
||||
Files.createDirectories(globalScripts);
|
||||
Files.walk(globalScripts, FileVisitOption.FOLLOW_LINKS)
|
||||
.filter(f -> f.toString().endsWith(".sc"))
|
||||
.forEach(f -> moduleNames.add(f.getFileName().toString().replaceFirst("\\.sc$","").toLowerCase(Locale.ROOT)));
|
||||
try (Stream<Path> folderWalker = Files.walk(globalScripts, FileVisitOption.FOLLOW_LINKS)) {
|
||||
folderWalker
|
||||
.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);
|
||||
|
||||
Reference in new issue
Block a user