Last active
August 26, 2016 17:10
-
-
Save ittekikun/d5edfc5146cb20a8115a05ba83924128 to your computer and use it in GitHub Desktop.
Configファイル実装を楽にできるかもしれないクラスです。 丸コピだと動かないと思います。参考程度にどうぞ。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ittekikun.plugin.eewalert; | |
import org.bukkit.configuration.file.FileConfiguration; | |
import org.bukkit.configuration.file.YamlConfiguration; | |
import org.bukkit.plugin.java.JavaPlugin; | |
import java.io.File; | |
import java.io.IOException; | |
public class ConfigAccessor | |
{ | |
private final String fileName; | |
private final JavaPlugin plugin; | |
private File configFile; | |
private FileConfiguration fileConfiguration; | |
/** | |
* Configファイル実装を楽にできるかもしれないクラス<br/> | |
* 1.9以降のAPIにも対応してます。 | |
* | |
* @param plugin プラグインクラス | |
* @param fileName Configファイルネーム | |
*/ | |
public ConfigAccessor(JavaPlugin plugin, String fileName) | |
{ | |
if(plugin == null) | |
{ | |
throw new IllegalArgumentException("plugin cannot be null"); | |
} | |
//非推奨だが放置 | |
if(!plugin.isInitialized()) | |
{ | |
throw new IllegalArgumentException("plugin must be initialized"); | |
} | |
this.plugin = plugin; | |
this.fileName = fileName; | |
File dataFolder = plugin.getDataFolder(); | |
if(dataFolder == null) | |
{ | |
throw new IllegalStateException(); | |
} | |
this.configFile = new File(plugin.getDataFolder(), fileName); | |
} | |
public void reloadConfig() | |
{ | |
fileConfiguration = YamlConfiguration.loadConfiguration(configFile); | |
} | |
public FileConfiguration getConfig() | |
{ | |
if(fileConfiguration == null) | |
{ | |
this.reloadConfig(); | |
} | |
return fileConfiguration; | |
} | |
public void saveConfig() | |
{ | |
if(fileConfiguration == null || configFile == null) | |
{ | |
return; | |
} | |
else | |
{ | |
try | |
{ | |
getConfig().save(configFile); | |
} | |
catch(IOException ex) | |
{ | |
EEWAlert.log.severe("Could not save config to " + configFile); | |
ex.printStackTrace(); | |
} | |
} | |
} | |
public void saveDefaultConfig() | |
{ | |
if(!configFile.exists()) | |
{ | |
//1.9のBUKKITAPIの仕様変更によりコピー処理を分ける必要がある。 | |
//※1.8以前では環境によってファイルエンコードを自動的に変更する。1.9以降はそのままコピーする | |
//※Configファイルはプラグインのjarファイル内にUTF-8(BOM無し)で保存してください。 | |
//下記のようなコードで判別する。 | |
//String ver = getServer().getBukkitVersion(); | |
//isV19 = (ver.startsWith("1.9-R") || ver.startsWith("1.9.1-R") || ver.startsWith("1.9.2-R") || ver.startsWith("1.9.3-R") || ver.startsWith("1.9.4-R") || ver.startsWith("1.9.5-R") || ver.startsWith("1.9.6-R") || ver.startsWith("1.9.7-R")); | |
if(EEWAlert.isV19) | |
{ | |
Utility.copyRawFileFromJar(EEWAlert.instance.getPluginJarFile(), configFile, fileName); | |
} | |
else | |
{ | |
Utility.copyFileFromJar(EEWAlert.instance.getPluginJarFile(), configFile, fileName); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyPluginConfig | |
{ | |
public MyPlugin myPlugin; | |
public ConfigAccessor config; | |
public String test; | |
public MyPluginConfig(MyPlugin myPlugin) | |
{ | |
this.MyPlugin = myPlugin; | |
} | |
//このメソッドを呼ぶことで設定を読み込む。 | |
public void loadConfig() | |
{ | |
//Configファイルの拡張子忘れないで! | |
system = new ConfigAccessor(myPlugin, "system.yml"); | |
//Configファイルがない場合はjarファイルからコピーする。 | |
system.saveDefaultConfig(); | |
//使い方は一緒 | |
test = system.getConfig().getString("test", "test"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Utility | |
{ | |
/** | |
* jarファイルの中に格納されているテキストファイルを、jarファイルの外にコピーするメソッド<br/> | |
* ファイルをそのままコピーします。 | |
* | |
* @param jarFile jarファイル | |
* @param targetFile コピー先 | |
* @param sourceFilePath コピー元 | |
*/ | |
public static void copyRawFileFromJar(File jarFile, File targetFile, String sourceFilePath) | |
{ | |
JarFile jar = null; | |
InputStream is = null; | |
File parent = targetFile.getParentFile(); | |
if (!parent.exists()) | |
{ | |
parent.mkdirs(); | |
} | |
try | |
{ | |
jar = new JarFile(jarFile); | |
ZipEntry zipEntry = jar.getEntry(sourceFilePath); | |
is = jar.getInputStream(zipEntry); | |
Files.copy(is, targetFile.toPath()); | |
} | |
catch (IOException e) | |
{ | |
EEWAlert.log.severe(targetFile + "のコピーに失敗しました。"); | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
if (jar != null) | |
{ | |
try | |
{ | |
jar.close(); | |
} | |
catch (IOException e) | |
{ | |
// do nothing. | |
} | |
} | |
if (is != null) | |
{ | |
try | |
{ | |
is.close(); | |
} | |
catch (IOException e) | |
{ | |
// do nothing. | |
} | |
} | |
} | |
} | |
/** | |
* jarファイルの中に格納されているテキストファイルを、jarファイルの外にコピーするメソッド<br/> | |
* WindowsだとS-JISで、MacintoshやLinuxだとUTF-8で保存されます。 | |
* | |
* @param jarFile jarファイル | |
* @param targetFile コピー先 | |
* @param sourceFilePath コピー元 | |
* @author https://github.com/ucchyocean/ | |
*/ | |
public static void copyFileFromJar(File jarFile, File targetFile, String sourceFilePath) | |
{ | |
JarFile jar = null; | |
InputStream is = null; | |
FileOutputStream fos = null; | |
BufferedReader reader = null; | |
BufferedWriter writer = null; | |
File parent = targetFile.getParentFile(); | |
if (!parent.exists()) | |
{ | |
parent.mkdirs(); | |
} | |
try | |
{ | |
jar = new JarFile(jarFile); | |
ZipEntry zipEntry = jar.getEntry(sourceFilePath); | |
is = jar.getInputStream(zipEntry); | |
fos = new FileOutputStream(targetFile); | |
reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); | |
writer = new BufferedWriter(new OutputStreamWriter(fos)); | |
String line; | |
while ((line = reader.readLine()) != null) | |
{ | |
writer.write(line); | |
writer.newLine(); | |
} | |
} | |
catch (FileNotFoundException e) | |
{ | |
EEWAlert.log.severe(targetFile + "のコピーに失敗しました。"); | |
e.printStackTrace(); | |
} | |
catch (IOException e) | |
{ | |
EEWAlert.log.severe(targetFile + "のコピーに失敗しました。"); | |
e.printStackTrace(); | |
} | |
finally | |
{ | |
if (jar != null) | |
{ | |
try | |
{ | |
jar.close(); | |
} | |
catch (IOException e) | |
{ | |
// do nothing. | |
} | |
} | |
if (writer != null) | |
{ | |
try | |
{ | |
writer.flush(); | |
writer.close(); | |
} | |
catch (IOException e) | |
{ | |
// do nothing. | |
} | |
} | |
if (reader != null) | |
{ | |
try | |
{ | |
reader.close(); | |
} | |
catch (IOException e) | |
{ | |
// do nothing. | |
} | |
} | |
if (fos != null) | |
{ | |
try | |
{ | |
fos.flush(); | |
fos.close(); | |
} | |
catch (IOException e) | |
{ | |
// do nothing. | |
} | |
} | |
if (is != null) | |
{ | |
try | |
{ | |
is.close(); | |
} | |
catch (IOException e) | |
{ | |
// do nothing. | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment