From 0e0dcc73407171362386dd50125dd332c0b629cf Mon Sep 17 00:00:00 2001 From: Matt <4009945+MattBDev@users.noreply.github.com> Date: Wed, 4 Nov 2026 02:14:44 -0500 Subject: [PATCH] Use NIO in the YAMLProcessor class (#3715) https://github.com/EngineHub/WorldEdit/pull/2802 Co-authored-by: Maddy Miller Co-authored-by: Octavia Togami --- .../wepif/PermissionsResolverManager.java | 13 +++--- .../worldedit/bukkit/WorldEditPlugin.java | 2 +- .../com/sk89q/util/yaml/YAMLProcessor.java | 46 +++++++++++++++---- 3 files changed, 44 insertions(+), 17 deletions(-) diff --git a/worldedit-bukkit/src/main/java/com/sk89q/wepif/PermissionsResolverManager.java b/worldedit-bukkit/src/main/java/com/sk89q/wepif/PermissionsResolverManager.java index b7e1d0f4f3..410a7e9372 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/wepif/PermissionsResolverManager.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/wepif/PermissionsResolverManager.java @@ -30,9 +30,10 @@ import org.bukkit.event.server.PluginEnableEvent; import org.bukkit.plugin.Plugin; -import java.io.File; import java.io.IOException; import java.lang.reflect.Method; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; @@ -101,7 +102,7 @@ protected PermissionsResolverManager(Plugin plugin) { this.server = plugin.getServer(); (new ServerListener()).register(plugin); // Register the events - loadConfig(new File("wepif.yml")); + loadConfig(Path.of("wepif.yml")); findResolver(); } @@ -186,16 +187,16 @@ public String getDetectionMessage() { return "Using WEPIF for permissions"; } - private boolean loadConfig(File file) { + private boolean loadConfig(Path path) { boolean isUpdated = false; - if (!file.exists()) { + if (!Files.exists(path)) { try { - file.createNewFile(); + Files.createFile(path); } catch (IOException e) { LOGGER.warn("Failed to create new configuration file", e); } } - config = new YAMLProcessor(file, false, YAMLFormat.EXTENDED); + config = new YAMLProcessor(path, false, YAMLFormat.EXTENDED); try { config.load(); } catch (IOException e) { diff --git a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java index 78e909744c..e48278e675 100644 --- a/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java +++ b/worldedit-bukkit/src/main/java/com/sk89q/worldedit/bukkit/WorldEditPlugin.java @@ -177,7 +177,7 @@ public void onLoad() { //FAWE end //FAWE start - Modify WorldEdit config name - config = new BukkitConfiguration(new YAMLProcessor(new File(getDataFolder(), "worldedit-config.yml"), true), this); + config = new BukkitConfiguration(new YAMLProcessor(new File(getDataFolder(), "worldedit-config.yml").toPath(), true), this); // Load config before we say we've loaded platforms as it is used in listeners of the event // Load config in onLoad to ensure it is loaded before FAWE settings to allow (inelegant) copying of values across // where needed diff --git a/worldedit-core/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java b/worldedit-core/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java index f50bd991fc..bbf81c4d85 100644 --- a/worldedit-core/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java +++ b/worldedit-core/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java @@ -20,6 +20,8 @@ package com.sk89q.util.yaml; import com.sk89q.util.StringUtil; +import com.sk89q.worldedit.internal.util.LogManagerCompat; +import org.apache.logging.log4j.Logger; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.LoaderOptions; import org.yaml.snakeyaml.Yaml; @@ -29,13 +31,13 @@ import org.yaml.snakeyaml.representer.Representer; import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashMap; @@ -72,10 +74,11 @@ */ public class YAMLProcessor extends YAMLNode { + private static final Logger LOGGER = LogManagerCompat.getLogger(); public static final String LINE_BREAK = DumperOptions.LineBreak.getPlatformLineBreak().getString(); public static final char COMMENT_CHAR = '#'; protected final Yaml yaml; - protected final File file; + protected final Path path; protected String header = null; protected YAMLFormat format; @@ -86,7 +89,7 @@ public class YAMLProcessor extends YAMLNode { */ private final Map comments = new HashMap<>(); - public YAMLProcessor(File file, boolean writeDefaults, YAMLFormat format) { + public YAMLProcessor(Path path, boolean writeDefaults, YAMLFormat format) { super(new LinkedHashMap<>(), writeDefaults); this.format = format; @@ -109,9 +112,29 @@ public YAMLProcessor(File file, boolean writeDefaults, YAMLFormat format) { yaml = new Yaml(new SafeConstructor(loaderOptions), representer, dumperOptions, loaderOptions); - this.file = file; + this.path = path; + } + + /** + * Create a new YAML processor. + * + * @deprecated Use {@link #YAMLProcessor(Path, boolean, YAMLFormat)} instead. + */ + @Deprecated + public YAMLProcessor(File file, boolean writeDefaults, YAMLFormat format) { + this(file.toPath(), writeDefaults, format); + } + + public YAMLProcessor(Path path, boolean writeDefaults) { + this(path, writeDefaults, YAMLFormat.COMPACT); } + /** + * Create a new YAML processor. + * + * @deprecated Use {@link #YAMLProcessor(Path, boolean)} instead. + */ + @Deprecated public YAMLProcessor(File file, boolean writeDefaults) { this(file, writeDefaults, YAMLFormat.COMPACT); } @@ -178,10 +201,13 @@ public String getHeader() { * @return true if it was successful */ public boolean save() { - - File parent = file.getParentFile(); + Path parent = path.getParent(); if (parent != null) { - parent.mkdirs(); + try { + Files.createDirectories(parent); + } catch (IOException e) { + LOGGER.warn("Exception while saving yaml file to " + path, e); + } } try (OutputStream stream = getOutputStream()) { if (stream == null) { @@ -230,11 +256,11 @@ private void read(Object input) throws YAMLProcessorException { } public InputStream getInputStream() throws IOException { - return new FileInputStream(file); + return Files.newInputStream(path); } public OutputStream getOutputStream() throws IOException { - return new FileOutputStream(file); + return Files.newOutputStream(path); } /**