Skip to content

Instantly share code, notes, and snippets.

@SubSide
Created March 30, 2015 15:13
Show Gist options
  • Save SubSide/e4a6a343e0b0b61800ce to your computer and use it in GitHub Desktop.
Save SubSide/e4a6a343e0b0b61800ce to your computer and use it in GitHub Desktop.
Lang file for the new Koth
package subside.plugins.koth;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.bukkit.craftbukkit.libs.com.google.gson.GsonBuilder;
import org.bukkit.craftbukkit.libs.com.google.gson.JsonParser;
import org.bukkit.plugin.java.JavaPlugin;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Lang {
public static String KOTH_WON = "&bThe koth %area% ended! %player% won!";
public static String KOTH_STARTING = "&bThe koth %area% has begun!";
public static String KOTH_PLAYERCAP = "&b%player% has started to cap %area%!";
public static String KOTH_CAPTIME = "&b%player% is capping the koth! %minutes_left%:%seconds_left% left!";
public static String KOTH_LEFT = "&b%player% left the koth!";
public static String PREFIX = "&3[KOTH] &b";
public static String AREA_ALREADYRUNNING = "&bThe area %area% is already running!";
public static String AREA_ALREADYEXISTS = "&bThe area %area% already exists!";
public static String AREA_NOTEXIST = "&bThe area %area% doesn't exist!";
public static String COMMAND_AREATRIGGERED = "&bYou've started the area %area%!";
public static String COMMAND_AREACREATED = "&bYou successfully created the area %area%!";
public static String COMMAND_AREAREMOVED = "&bYou've successfully removed the area %area%!";
public static String COMMAND_ONLYFROMINGAME = "&bThis command can only be executed from ingame!";
public static String COMMAND_KOTHALREADYEXISTS = "&bThe area %area% already exists!";
public static String COMMAND_LOOTNOARGSEXPLANATION = "&bThis is the loot chest for the next running koth (%area%)!";
public static String COMMAND_LOOTEXPLANATION = "&bThis is the loot chest for the next running koth (%area%)!";
public static String COMMAND_SPECIFIC_KOTH_TERMINATED = "&bYou have terminated %area%!";
public static String COMMAND_ALL_KOTHS_TERMINATED = "&bYou have terminated all areas!";
public static String COMMAND_USAGE = "Usage: ";
public static String COMMAND_LIST_MESSAGE = "&3List of areas:";
public static String COMMAND_LIST_ENTRY = "&b- %area%";
public static void load(JavaPlugin plugin) {
try {
if (!new File(plugin.getDataFolder().getAbsolutePath() + File.separatorChar + "lang.json").exists()) {
save(plugin);
return;
}
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(plugin.getDataFolder().getAbsolutePath() + File.separatorChar + "lang.json"));
JSONObject jsonObject = (JSONObject) obj;
Field[] fields = Lang.class.getFields();
for (Field field : fields) {
try {
if (Modifier.isStatic(field.getModifiers())) {
field.set(null, jsonObject.get(field.getName()));
}
}
catch (Exception e) {
e.printStackTrace();
}
}
save(plugin);
}
catch (Exception e) {
System.out.println("///// LANG FILE NOT FOUND OR NOT CORRECTLY SET UP ////");
System.out.println("Will use default variables instead.");
System.out.println("You could try to delete the lang.json in the plugin folder.");
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public static void save(JavaPlugin plugin) {
try {
if (!new File(plugin.getDataFolder().getAbsolutePath() + File.separatorChar + "lang.json").exists()) {
plugin.getDataFolder().mkdirs();
new File(plugin.getDataFolder().getAbsolutePath() + File.separatorChar + "lang.json").createNewFile();
}
JSONObject obj = new JSONObject();
Field[] fields = Lang.class.getFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())) {
obj.put(field.getName(), field.get(null));
}
}
//FileWriter file = new FileWriter(plugin.getDataFolder().getAbsolutePath() + File.separatorChar + "lang.json");
FileOutputStream fileStream = new FileOutputStream(new File(plugin.getDataFolder().getAbsolutePath() + File.separatorChar + "lang.json"));
OutputStreamWriter file = new OutputStreamWriter(fileStream, "UTF-8");
System.out.println(new JsonParser().parse(obj.toJSONString()));
try {
file.write(new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create().toJson(new JsonParser().parse(obj.toJSONString())));
}
catch (IOException e) {
e.printStackTrace();
}
finally {
file.flush();
file.close();
}
}
catch (Exception e) {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment