package net.bendercraft.spigot.commandsigns.utils;

import net.bendercraft.spigot.commandsigns.CommandSignsPlugin;

import java.io.*;
import java.util.Properties;

public abstract class Messages {

	private static final String CHARSET = "UTF-8";
	private static final String FILENAME = "messages.properties";

	private static Properties defaultMessages = null;
	private static Properties messages = null;

	public static String get(String key) {
		if (defaultMessages == null && messages == null) {
			loadMessages();
		}
		try {
			return parseColor(messages.getProperty(key));
		}
		catch (NullPointerException ex) {
			CommandSignsPlugin.getPlugin().getLogger().severe("A null pointer exception occurred while parsing color on key : " + key);
			return key;
		}
	}

	private static void loadMessages() {
		defaultMessages = new Properties();

		try {
			InputStream in = Messages.class.getClassLoader().getResourceAsStream(FILENAME);
			InputStreamReader reader = new InputStreamReader(in, CHARSET);
			defaultMessages.load(reader);
			reader.close();
		}
		catch (IOException e) {
			// Should never happens
		}
		messages = new Properties(defaultMessages);
		try {
			File folder = CommandSignsPlugin.getPlugin().getDataFolder();
			if (!folder.exists()) {
				folder.mkdirs();
			}
			File custom = new File(folder, FILENAME);
			if (custom.exists()) {
				InputStream in = new FileInputStream(custom);
				InputStreamReader reader = new InputStreamReader(in, CHARSET);
				messages.load(reader);
				reader.close();
			}
			else {
				OutputStream out = new FileOutputStream(custom);
				defaultMessages.store(out, "See https://github.com/Nokorbis/ar-command-signs/wiki/Custom-messages for more information about this file");
				out.close();
			}
		}
		catch (IOException ignored) {
		}
	}

	private static String parseColor(String line) {
		return line.replace("&", "§");
	}
}

@@ -127,16 +127,58 @@
 a.io.*;%0A
+import java.nio.charset.StandardCharsets;%0A
 import j
@@ -233,59 +233,10 @@
 ages
- %7B%0A%0A%09private static final String CHARSET = %22UTF-8%22;
+%0A%7B
 %0A%09pr
@@ -429,17 +429,18 @@
 ing key)
- 
+%0A%09
 %7B%0A%09%09if (
@@ -483,17 +483,19 @@
 == null)
- 
+%0A%09%09
 %7B%0A%09%09%09loa
@@ -512,25 +512,27 @@
 );%0A%09%09%7D%0A%09%09try
- 
+%0A%09%09
 %7B%0A%09%09%09return 
@@ -607,17 +607,19 @@
 tion ex)
- 
+%0A%09%09
 %7B%0A%09%09%09Com
@@ -797,17 +797,18 @@
 ssages()
- 
+%0A%09
 %7B%0A%09%09defa
@@ -839,23 +839,24 @@
 ties();%0A
-%0A
 %09%09try
- 
+%0A%09%09
 %7B%0A%09%09%09Inp
@@ -984,31 +984,46 @@
 mReader(in, 
-CHARSET
+StandardCharsets.UTF_8
 );%0A%09%09%09defaul
@@ -1096,42 +1096,18 @@
 n e)
- %7B
 %0A%09%09
-%09// Should never happens
+%7B
 %0A%09%09%7D%0A
+%0A
 %09%09me
@@ -1153,17 +1153,19 @@
 );%0A%09%09try
- 
+%0A%09%09
 %7B%0A%09%09%09Fil
@@ -1243,25 +1243,28 @@
 er.exists())
- 
+%0A%09%09%09
 %7B%0A%09%09%09%09folder
@@ -1347,17 +1347,20 @@
 xists())
- 
+%0A%09%09%09
 %7B%0A%09%09%09%09In
@@ -1464,15 +1464,30 @@
 in, 
-CHARSET
+StandardCharsets.UTF_8
 );%0A%09
@@ -1544,17 +1544,20 @@
 %0A%09%09%09else
- 
+%0A%09%09%09
 %7B%0A%09%09%09%09Ou
@@ -1800,17 +1800,19 @@
 ignored)
- 
+%0A%09%09
 %7B%0A%09%09%7D%0A%09%7D

Use standard charset instead of self loaded one