LunaChat v3.0.2 : implementation of TODO tasks.

このコミットが含まれているのは:
ucchy committed 2020-06-26 23:18:18 +09:00
1 親 7f877d0674
コミット f0fe3f02e6
29ファイル変更
+525 -196

検索条件に一致するファイルはありません

+71
ファイルの表示
@@ -0,0 +1,71 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.util;
import org.jetbrains.annotations.NotNull;
/**
* チャットカラー
* @author ucchy
*/
public enum ChatColor {
BLACK('0', 0x00),
DARK_BLUE('1', 0x1),
DARK_GREEN('2', 0x2),
DARK_AQUA('3', 0x3),
DARK_RED('4', 0x4),
DARK_PURPLE('5', 0x5),
GOLD('6', 0x6),
GRAY('7', 0x7),
DARK_GRAY('8', 0x8),
BLUE('9', 0x9),
GREEN('a', 0xA),
AQUA('b', 0xB),
RED('c', 0xC),
LIGHT_PURPLE('d', 0xD),
YELLOW('e', 0xE),
WHITE('f', 0xF),
MAGIC('k', 0x10, true),
BOLD('l', 0x11, true),
STRIKETHROUGH('m', 0x12, true),
UNDERLINE('n', 0x13, true),
ITALIC('o', 0x14, true),
RESET('r', 0x15);
public static final char COLOR_CHAR = '\u00A7';
@SuppressWarnings("unused")
private final int intCode;
private final char code;
private final boolean isFormat;
private final String toString;
private ChatColor(char code, int intCode) {
this(code, intCode, false);
}
private ChatColor(char code, int intCode, boolean isFormat) {
this.code = code;
this.intCode = intCode;
this.isFormat = isFormat;
this.toString = new String(new char[] {COLOR_CHAR, code});
}
public char getChar() {
return code;
}
@NotNull
@Override
public String toString() {
return toString;
}
public boolean isFormat() {
return isFormat;
}
}
+19
ファイルの表示
@@ -0,0 +1,19 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.util;
/**
* Event優先度列挙子
* @author ucchy
*/
public enum EventPriority {
LOWEST,
LOW,
NORMAL,
HIGH,
HIGHEST,
MONITOR,
}
+54
ファイルの表示
@@ -0,0 +1,54 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.util;
/**
* 文字列のキーワード置き換えを行うユーティリティクラス
* @author ucchy
*/
public class KeywordReplacer {
private StringBuilder str;
/**
* 指定された文字列で初期化を行う
* @param src
*/
public KeywordReplacer(String src) {
str = new StringBuilder(src);
}
/**
* 文字列keywordえを、文字列valueに置き替える
* @param keyword キーワード
* @param value 値
*/
public void replace(String keyword, String value) {
int start;
while ( (start = str.indexOf(keyword)) > -1 ) {
str.replace(start, start + keyword.length(), value);
}
}
/**
* 指定された文字列が含まれているかどうかを判定する
* @param keyword キーワード
* @return 含まれているかどうか
*/
public boolean contains(String keyword) {
return str.indexOf(keyword) > -1;
}
/**
* 文字列をStringで取得する
* @return String
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return str.toString();
}
}
+200
ファイルの表示
@@ -0,0 +1,200 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Locale;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import com.github.ucchyocean.lc3.LunaChat;
import com.github.ucchyocean.lc3.LunaChatMode;
/**
* ユーティリティクラス
* @author ucchy
*/
public class Utility {
/**
* jarファイルの中に格納されているファイルを、jarファイルの外にコピーするメソッド
* @param jarFile jarファイル
* @param targetFile コピー先
* @param sourceFilePath コピー元
* @param isBinary バイナリファイルかどうか
*/
public static void copyFileFromJar(
File jarFile, File targetFile, String sourceFilePath, boolean isBinary) {
File parent = targetFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
try ( JarFile jar = new JarFile(jarFile) ) {
ZipEntry zipEntry = jar.getEntry(sourceFilePath);
InputStream is = jar.getInputStream(zipEntry);
try ( FileOutputStream fos = new FileOutputStream(targetFile) ) {
if (isBinary) {
byte[] buf = new byte[8192];
int len;
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
} else {
try ( BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8")) ) {
String line;
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine();
}
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文字列内のカラーコード候補(&a)を、カラーコード(§a)に置き換えする
* @param source 置き換え元の文字列
* @return 置き換え後の文字列
*/
public static String replaceColorCode(String source) {
if (source == null) return null;
return source.replaceAll("&([0-9a-fk-or])", "\u00A7$1");
}
/**
* 文字列に含まれているカラーコード(§a)を除去して返す
* @param source 置き換え元の文字列
* @return 置き換え後の文字列
*/
public static String stripColorCode(String source) {
if (source == null) return null;
return stripAltColorCode(source).replaceAll("\u00A7([0-9a-fk-or])", "").replaceAll("&([0-9a-fk-or])", "");
}
/**
* 文字列に含まれているカラーコード候補(&a)を除去して返す
* @param source 置き換え元の文字列
* @return 置き換え後の文字列
*/
public static String stripAltColorCode(String source) {
if (source == null) return null;
return source.replaceAll("&([0-9a-fk-or])", "");
}
/**
* カラーコード(§a)かどうかを判断する
* @param code カラーコード
* @return カラーコードかどうか
*/
public static boolean isColorCode(String code) {
if (code == null) return false;
return code.matches("\u00A7[0-9a-fk-orA-FK-OR]");
}
/**
* カラーコード候補(&a)かどうかを判断する
* @param color カラーコード候補
* @return カラーコード候補かどうか
*/
public static boolean isAltColorCode(String code) {
if (code == null) return false;
return code.matches("&[0-9a-fk-orA-FK-OR]");
}
/**
* ChatColorで指定可能な色(REDとかGREENとか)かどうかを判断する
* @param color カラー表記の文字列
* @return 指定可能かどうか
*/
public static boolean isValidColor(String color) {
if ( color == null ) return false;
for (ChatColor c : ChatColor.values()) {
if (c.name().equals(color.toUpperCase())) {
return true;
}
}
return false;
}
/**
* カラー表記の文字列を、カラーコードに変換する
* @param color カラー表記の文字列
* @return カラーコード
*/
public static String changeToColorCode(String color) {
return "&" + changeToChatColor(color).getChar();
}
/**
* カラー表記の文字列を、ChatColorクラスに変換する
* @param color カラー表記の文字列
* @return ChatColorクラス
*/
public static ChatColor changeToChatColor(String color) {
if (isValidColor(color)) {
return ChatColor.valueOf(color.toUpperCase());
}
return ChatColor.WHITE;
}
/**
* 指定された文字数のアスタリスクの文字列を返す
* @param length アスタリスクの個数
* @return 指定された文字数のアスタリスク
*/
public static String getAstariskString(int length) {
StringBuilder buf = new StringBuilder();
for (int i = 0; i < length; i++) {
buf.append("*");
}
return buf.toString();
}
/**
* 指定された名前のプレイヤーが接続したことがあるかどうかを検索する
* @param name プレイヤー名
* @return 接続したことがあるかどうか
*/
public static boolean existsOfflinePlayer(String name) {
if ( LunaChat.getUUIDCacheData().getUUIDFromName(name) != null ) {
return true;
}
if ( LunaChat.getMode() == LunaChatMode.BUKKIT ) {
return UtilityBukkit.existsOfflinePlayer(name);
}
return false;
}
/**
* 動作環境のロケールを取得する。
* @return 動作環境のロケール
*/
public static Locale getDefaultLocale() {
Locale locale = Locale.getDefault();
if ( locale == null ) return Locale.ENGLISH;
return locale;
}
}
+67
ファイルの表示
@@ -0,0 +1,67 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.util;
import java.util.Collection;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
/**
* Bukkit関連のユーティリティクラス
* @author ucchy
*/
public class UtilityBukkit {
/**
* 現在接続中のプレイヤーを全て取得する
* @return 接続中の全てのプレイヤー
*/
public static Collection<? extends Player> getOnlinePlayers() {
return Bukkit.getOnlinePlayers();
}
/**
* 現在のサーバー接続人数を返します。
* @return サーバー接続人数
*/
public static int getOnlinePlayersCount() {
return getOnlinePlayers().size();
}
/**
* 指定された名前のオフラインプレイヤーを取得する
* @param name プレイヤー名
* @return オフラインプレイヤー
*/
@SuppressWarnings("deprecation")
public static OfflinePlayer getOfflinePlayer(String name) {
if (name == null) return null;
OfflinePlayer player = Bukkit.getOfflinePlayer(name);
if (player == null || (!player.hasPlayedBefore() && !player.isOnline()))
return null;
return player;
}
/**
* 指定された名前のプレイヤーを取得する
* @param name プレイヤー名
* @return プレイヤー
*/
public static Player getPlayerExact(String name) {
return Bukkit.getPlayer(Utility.stripColorCode(name));
}
/**
* 指定された名前のプレイヤーが接続したことがあるかどうかを検索する
* @param name プレイヤー名
* @return 接続したことがあるかどうか
*/
public static boolean existsOfflinePlayer(String name) {
return (getOfflinePlayer(name) != null);
}
}
+108
ファイルの表示
@@ -0,0 +1,108 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.yaml.snakeyaml.Yaml;
/**
* Yaml設定読み書きユーティリティクラス
* @author ucchy
*/
public class YamlConfig extends YamlSection {
/**
* InputStreamからYamlConfigをロードする
* @param stream InputStream
* @return ロードされたYamlConfig
* @throws IOException InputStreamの入力がyamlとして解析できない場合など
*/
public static YamlConfig load(InputStream stream) throws IOException {
YamlConfig config = new YamlConfig();
if ( stream == null ) return config;
Yaml yaml = new Yaml();
@SuppressWarnings("unchecked")
Map<String, Object> map = yaml.loadAs(stream, Map.class);
if ( map == null ) {
throw new IOException("Cannot load stream as yaml.");
}
config.map = map;
return config;
}
/**
* ReaderからYamlConfigをロードする
* @param reader Reader
* @return ロードされたYamlConfig
* @throws IOException Readerの入力がyamlとして解析できない場合など
*/
public static YamlConfig load(Reader reader) throws IOException {
YamlConfig config = new YamlConfig();
if ( reader == null ) return config;
Yaml yaml = new Yaml();
@SuppressWarnings("unchecked")
Map<String, Object> map = yaml.loadAs(reader, Map.class);
if ( map == null ) {
throw new IOException("Cannot load reader as yaml.");
}
config.map = map;
return config;
}
/**
* FileからYamlConfigをロードする
* @param file File
* @return ロードされたYamlConfig(ロードに失敗した場合は空のYamlConfigが返される)
*/
public static @NotNull YamlConfig load(File file) {
// 対象ファイルが存在しない場合やからっぽの場合は、からっぽのYamlConfigを返す
if ( !file.exists() || !file.isFile() || file.length() == 0 ) return new YamlConfig();
// 読み込む
try ( InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "UTF-8") ) {
return load(reader);
} catch (IOException e) {
e.printStackTrace();
}
// 読み込みに失敗した場合は、からっぽのYamlConfigを返す
return new YamlConfig();
}
/**
* 保存する
* @param file 保存先
* @throws IOException 保存に失敗した場合など
*/
public void save(File file) throws IOException {
Yaml yaml = new Yaml();
String data = yaml.dumpAsMap(map);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
writer.write(data);
}
}
}
+295
ファイルの表示
@@ -0,0 +1,295 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.util;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.jetbrains.annotations.Nullable;
import org.yaml.snakeyaml.Yaml;
/**
* Yaml設定読み書きユーティリティクラス
* @author ucchy
*/
public class YamlSection {
Map<String, Object> map;
private Map<String, Object> defaults = new HashMap<>();
/**
* コンストラクタ
*/
public YamlSection() {
map = new HashMap<>();
}
/**
* コンストラクタ
* @param map 初期データ
*/
public YamlSection(Map<String, Object> map) {
this.map = map;
}
/**
* 保持しているデータをStringに変換して返す
* @return Stringに変換されたデータ
*/
public String getRawData() {
Yaml yaml = new Yaml();
return yaml.dumpAsMap(map);
}
/**
* 指定されたkeyにvalueを設定する
* @param key
* @param value
*/
public void set(String key, Object value) {
if ( key.contains(".") ) {
String parent = key.substring(0, key.indexOf("."));
YamlSection section = getSection(parent);
if ( section == null ) {
section = createSection(parent);
}
String remain = key.substring(key.indexOf(".") + 1);
section.set(remain, value);
} else {
map.put(key, value);
}
}
/**
* 指定されたkeyの値を取得する
* @param key
* @return keyに対応する値(値が無い場合はnullが返される)
*/
public @Nullable Object get(String key) {
return get(key, null);
}
/**
* 指定されたkeyの値を取得する
* @param key
* @param defaultValue デフォルト値
* @return keyに対応する値(値が無い場合はdefaultValueが返される)
*/
public Object get(String key, Object defaultValue) {
if ( key.contains(".") ) {
String parent = key.substring(0, key.indexOf("."));
YamlSection section = getSection(parent);
if ( section == null ) return defaultValue;
String remain = key.substring(key.indexOf(".") + 1);
return section.get(remain, defaultValue);
} else {
Object val = map.getOrDefault(key, defaults.get(key));
return ( val != null ) ? val : defaultValue;
}
}
/**
* 指定されたkeyの値をbooleanに変換して返す
* @param key
* @return keyに対応する値(値が無いかbooleanに変換できない場合はfalseが返される)
*/
public boolean getBoolean(String key) {
return getBoolean(key, false);
}
/**
* 指定されたkeyの値をbooleanに変換して返す
* @param key
* @param defaultValue デフォルト値
* @return keyに対応する値(値が無いかbooleanに変換できない場合はdefaultValueが返される)
*/
public boolean getBoolean(String key, boolean defaultValue) {
Object val = get(key, defaults.get(key));
return ( val != null && val instanceof Boolean ) ? (boolean)val : defaultValue;
}
/**
* 指定されたkeyの値をintに変換して返す
* @param key
* @return keyに対応する値(値が無いかintに変換できない場合は0が返される)
*/
public int getInt(String key) {
return getInt(key, 0);
}
/**
* 指定されたkeyの値をintに変換して返す
* @param key
* @param defaultValue デフォルト値
* @return keyに対応する値(値が無いかintに変換できない場合はdefaultValueが返される)
*/
public int getInt(String key, int defaultValue) {
Object val = get(key, defaults.get(key));
return ( val != null && val instanceof Integer ) ? (int)val : defaultValue;
}
/**
* 指定されたkeyの値をStringに変換して返す
* @param key
* @return keyに対応する値(値が無いかStringに変換できない場合は0が返される)
*/
public @Nullable String getString(String key) {
return getString(key, null);
}
/**
* 指定されたkeyの値をStringに変換して返す
* @param key
* @param defaultValue デフォルト値
* @return keyに対応する値(値が無いかStringに変換できない場合はdefaultValueが返される)
*/
public String getString(String key, String defaultValue) {
Object val = get(key, defaults.get(key));
return ( val != null ) ? val.toString() : defaultValue;
}
/**
* 指定されたkeyの値をList&lt;String&gt;に変換して返す
* @param key
* @return keyに対応する値(値が無いかList&lt;String&gt;に変換できない場合はnullが返される)
*/
public @Nullable List<String> getStringList(String key) {
return getStringList(key, null);
}
/**
* 指定されたkeyの値をList&lt;String&gt;に変換して返す
* @param key
* @param defaultValue デフォルト値
* @return keyに対応する値(値が無いかList&lt;String&gt;に変換できない場合はdefaultValueが返される)
*/
@SuppressWarnings("unchecked")
public List<String> getStringList(String key, ArrayList<String> defaultValue) {
Object val = get(key, defaults.get(key));
return ( val != null && val instanceof ArrayList<?> ) ? (List<String>)val : defaultValue;
}
/**
* 指定されたkeyの値をMap&lt;String, Object&gt;に変換して返す
* @param key
* @return keyに対応する値(値が無いかMap&lt;String, Object&gt;に変換できない場合はnullが返される)
*/
public @Nullable Map<String, Object> getMap(String key) {
return getMap(key, null);
}
/**
* 指定されたkeyの値をMap&lt;String, Object&gt;に変換して返す
* @param key
* @param defaultValue デフォルト値
* @return keyに対応する値(値が無いかMap&lt;String, Object&gt;に変換できない場合はdefaultValueが返される)
*/
@SuppressWarnings("unchecked")
public Map<String, Object> getMap(String key, Map<String, Object> defaultValue) {
Object val = get(key, defaults.get(key));
return ( val != null && val instanceof Map<?, ?> ) ? (Map<String, Object>)val : defaultValue;
}
/**
* 指定されたkeyの値をYamlSectionに変換して返す
* @param key
* @return YamlSection(値が無いかMap&lt;String, Object&gt;に変換できない場合はnullが返される)
*/
public @Nullable YamlSection getSection(String key) {
return getSection(key, null);
}
/**
* 指定されたkeyの値をYamlSectionに変換して返す
* @param key
* @param defaultValue デフォルト値
* @return YamlSection(値が無いかMap&lt;String, Object&gt;に変換できない場合はdefaultValueが返される)
*/
public YamlSection getSection(String key, YamlSection defaultValue) {
Map<String, Object> m = getMap(key);
if ( m == null ) return defaultValue;
return new YamlSection(m);
}
/**
* 指定されたkeyの値があるかどうか判定する
* @param key
* @return 値があるかどうか
*/
public boolean contains(String key) {
if ( key.contains(".") ) {
String parent = key.substring(0, key.indexOf("."));
YamlSection section = getSection(parent);
if ( section == null ) return false;
String remain = key.substring(key.indexOf(".") + 1);
return section.contains(remain);
} else {
return map.containsKey(key);
}
}
/**
* 指定されたkeyに対応するサブセクションがあるかどうか判定する
* @param key
* @return サブセクションがあるかどうか
*/
public boolean containsSection(String key) {
return (getMap(key) != null);
}
/**
* 指定されたkeyのサブセクションを作成する<br/>
* 注意:階層構造指定でセクションを作成した場合、最下層のセクションが戻り値として返されることに注意すること。<br/>
* 例えば、createSection("aa.bb.cc") の戻り値は "cc" のセクションとなる。
* @param key
* @return サブセクション
*/
public YamlSection createSection(String key) {
if ( containsSection(key) ) return getSection(key);
Map<String, Object> map = new HashMap<String, Object>();
set(key, map);
return new YamlSection(map);
}
/**
* 階層のkeyを羅列する
* @param deeps 再帰的にkeyを取得するかどうか
* @return keyの羅列
*/
public Set<String> getKeys(boolean deeps) {
if ( !deeps ) {
return map.keySet();
} else {
Set<String> result = new TreeSet<String>();
for ( String key : map.keySet() ) {
result.add(key);
if ( containsSection(key) ) {
YamlSection sec = getSection(key);
for ( String sub : sec.getKeys(true) ) {
result.add(key + "." + sub);
}
}
}
return result;
}
}
/**
* デフォルト値を設定する
* @param defaults デフォルト値
*/
public void addDefaults(YamlSection defaults) {
if (defaults == null) {
this.defaults = new HashMap<String, Object>();
} else {
this.defaults = defaults.map;
}
}
}