Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 36 additions & 10 deletions worldedit-core/src/main/java/com/sk89q/util/yaml/YAMLProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -86,7 +89,7 @@ public class YAMLProcessor extends YAMLNode {
*/
private final Map<String, String> 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;

Expand All @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

/**
Expand Down