Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Detect tabs in Bukkit Yaml files.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
/**
* Our class name is going to be YamlTabParser. Replace with your main class
* name!
*
*/
public class YamlTabParser extends JavaPlugin {
/*
* These two member variables are pointers to the config.yml file we will be
* creating. They are fields because they have a very broad scope and must
* be handled appropriately during save and load operations
*/
private File file;
private FileConfiguration config;
@Override
public void onEnable() {
// define member variables
// NOTE: You cannot use a constructor in the main class! This would
// crash your plugin!
file = new File(getDataFolder(), "config.yml");
config = new YamlConfiguration();
// reload and save files, we will be overriding the JavaPlugin
// implementations in a later step.
reloadConfig();
saveConfig();
}
/**
* Overrides by returning our defined FileConfiguration variable
*/
@Override
public FileConfiguration getConfig() {
return config;
}
/**
* Overrides the default save operation by saving using our predefined File
* and FileConfiguration variables.
*/
@Override
public void saveConfig() {
try {
config.save(file);
}
catch (IOException e) {
// print stacktrace if you prefer
getLogger().severe(
"Could not save config.yml due to: " + e.getMessage());
}
}
/**
* Override default implementation for reloading
*/
@Override
public void reloadConfig() {
if (!file.exists()) {
// Create file if it doesn't exist. Change appropriately based on
// your setup
saveDefaultConfig();
}
scanConfig();
}
/**
* Opens a java.util.Scanner to scan the config file for any tabs.
*/
private void scanConfig() {
// declare our scanner variable
Scanner scan = null;
try {
scan = new Scanner(file);
int row = 0;
String line = "";
// iterate through the file line by line
while (scan.hasNextLine()) {
line = scan.nextLine();
// add to the row
row++;
// If a tab is found ... \t = tab in regex
if (line.indexOf("\t") != -1) {
/*
* Tell the user where the tab is! We throw an
* IllegalArgumentException here.
*/
String error = ("Tab found in config-file on line # " + row + "!");
throw new IllegalArgumentException(error);
}
}
/*
* load the file, if tabs were found then this will never execute
* because of IllegalArgumentException
*/
config.load(file);
}
catch (FileNotFoundException e) {
// this error should never happen if the file exists
e.printStackTrace();
}
catch (IOException e) {
// failed loading error
e.printStackTrace();
}
catch (InvalidConfigurationException e) {
// snakeyaml error if the config setup is incorrect.
e.printStackTrace();
}
finally {
// Close the scanner to avoid memory leaks.
if (scan != null) {
scan.close();
}
}
}
}
@jwpjrdev

This comment has been minimized.

Copy link

@jwpjrdev jwpjrdev commented Feb 1, 2019

Amazing! This is so useful! I'm gonna change this to automatically changes the tabs to four spaces, I recommend you add that as a comment to show people how to do that (just a simple replace and save to the config)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment