diff --git a/src/main/java/com/github/ucchyocean/lc/channel/Channel.java b/src/main/java/com/github/ucchyocean/lc/channel/Channel.java index e7cd32d..1743b0e 100644 --- a/src/main/java/com/github/ucchyocean/lc/channel/Channel.java +++ b/src/main/java/com/github/ucchyocean/lc/channel/Channel.java @@ -90,7 +90,6 @@ public class Channel { * @deprecated Legacy Version */ public void chat(ChannelPlayer player, String message) { - // TODO player.getPlayer()で問題ないか、要確認 channel.chat(ChannelMember.getChannelMember(player.getPlayer()), message); } diff --git a/src/main/java/com/github/ucchyocean/lc3/LunaChatConfig.java b/src/main/java/com/github/ucchyocean/lc3/LunaChatConfig.java index 4b6c021..d985dcf 100644 --- a/src/main/java/com/github/ucchyocean/lc3/LunaChatConfig.java +++ b/src/main/java/com/github/ucchyocean/lc3/LunaChatConfig.java @@ -12,6 +12,9 @@ import java.util.logging.Level; import java.util.regex.Pattern; import com.github.ucchyocean.lc3.japanize.JapanizeType; +import com.github.ucchyocean.lc3.util.EventPriority; +import com.github.ucchyocean.lc3.util.Utility; +import com.github.ucchyocean.lc3.util.YamlConfig; /** * LunaChatのコンフィグクラス diff --git a/src/main/java/com/github/ucchyocean/lc3/LunaChatLogger.java b/src/main/java/com/github/ucchyocean/lc3/LunaChatLogger.java index 240eca0..7bdc0eb 100644 --- a/src/main/java/com/github/ucchyocean/lc3/LunaChatLogger.java +++ b/src/main/java/com/github/ucchyocean/lc3/LunaChatLogger.java @@ -19,6 +19,8 @@ import java.util.Calendar; import java.util.Collections; import java.util.Date; +import com.github.ucchyocean.lc3.util.Utility; + /** * LunaChatロガー * @author ucchy diff --git a/src/main/java/com/github/ucchyocean/lc3/LunaChatStandalone.java b/src/main/java/com/github/ucchyocean/lc3/LunaChatStandalone.java index d7678cc..523edce 100644 --- a/src/main/java/com/github/ucchyocean/lc3/LunaChatStandalone.java +++ b/src/main/java/com/github/ucchyocean/lc3/LunaChatStandalone.java @@ -83,7 +83,7 @@ public class LunaChatStandalone implements PluginInterface { @Override public void log(Level level, String msg) { - // TODO 未実装 + // TODO Standaloneサーバーのログ出力 未実装 } /** diff --git a/src/main/java/com/github/ucchyocean/lc3/Messages.java b/src/main/java/com/github/ucchyocean/lc3/Messages.java index 9fc841b..18a989b 100644 --- a/src/main/java/com/github/ucchyocean/lc3/Messages.java +++ b/src/main/java/com/github/ucchyocean/lc3/Messages.java @@ -10,6 +10,10 @@ import java.io.IOException; import java.util.jar.JarFile; import java.util.zip.ZipEntry; +import com.github.ucchyocean.lc3.util.KeywordReplacer; +import com.github.ucchyocean.lc3.util.Utility; +import com.github.ucchyocean.lc3.util.YamlConfig; + /** * プラグインのメッセージリソース管理クラス * @author ucchy diff --git a/src/main/java/com/github/ucchyocean/lc3/UUIDCacheData.java b/src/main/java/com/github/ucchyocean/lc3/UUIDCacheData.java index 2c813a1..d3e49d3 100644 --- a/src/main/java/com/github/ucchyocean/lc3/UUIDCacheData.java +++ b/src/main/java/com/github/ucchyocean/lc3/UUIDCacheData.java @@ -10,6 +10,8 @@ import java.io.IOException; import org.jetbrains.annotations.Nullable; +import com.github.ucchyocean.lc3.util.YamlConfig; + /** * UUIDのキャッシュデータを管理するクラス * @author ucchy @@ -80,4 +82,20 @@ public class UUIDCacheData { public @Nullable String get(String uuid) { return cache.getString(uuid); } + + /** + * プレイヤー名からUUIDを取得する + * @param name プレイヤー名 + * @return UUID(キャッシュされていない場合はnullが返される) + */ + public @Nullable String getUUIDFromName(String name) { + if ( name == null ) return null; + for ( String uuid : cache.getKeys(false) ) { + String n = cache.getString(uuid); + if ( name.equalsIgnoreCase(n) ) { + return uuid; + } + } + return null; + } } diff --git a/src/main/java/com/github/ucchyocean/lc3/YamlConfig.java b/src/main/java/com/github/ucchyocean/lc3/YamlConfig.java deleted file mode 100644 index 753a8c7..0000000 --- a/src/main/java/com/github/ucchyocean/lc3/YamlConfig.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * @author ucchy - * @license LGPLv3 - * @copyright Copyright ucchy 2020 - */ -package com.github.ucchyocean.lc3; - -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.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; - -import org.yaml.snakeyaml.Yaml; - -/** - * Yaml設定読み書きユーティリティクラス - * このクラスは、階層構造のデータ形式に対応していないので注意。 - * @author ucchy - */ -public class YamlConfig { - - private Map map = new HashMap<>(); - private Map defaults = new HashMap<>(); - - public static YamlConfig load(InputStream stream) throws IOException { - - YamlConfig config = new YamlConfig(); - if ( stream == null ) return config; - - Yaml yaml = new Yaml(); - - @SuppressWarnings("unchecked") - Map map = yaml.loadAs(stream, Map.class); - if ( map == null ) { - throw new IOException("Cannot load stream as yaml."); - } - - config.map = map; - return config; - } - - public static YamlConfig load(Reader reader) throws IOException { - - YamlConfig config = new YamlConfig(); - if ( reader == null ) return config; - - Yaml yaml = new Yaml(); - - @SuppressWarnings("unchecked") - Map map = yaml.loadAs(reader, Map.class); - if ( map == null ) { - throw new IOException("Cannot load reader as yaml."); - } - - config.map = map; - return config; - } - - public static 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(); - } - - 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); - } - } - - public String getRawData() { - Yaml yaml = new Yaml(); - return yaml.dumpAsMap(map); - } - - public void set(String key, Object value) { - map.put(key, value); - } - - public Object get(String key) { - return map.get(key); - } - - public boolean getBoolean(String key) { - return getBoolean(key, false); - } - - public boolean getBoolean(String key, boolean defaultValue) { - Object val = map.getOrDefault(key, defaults.get(key)); - return ( val != null && val instanceof Boolean ) ? (boolean)val : defaultValue; - } - - public int getInt(String key) { - return getInt(key, 0); - } - - public int getInt(String key, int defaultValue) { - Object val = map.getOrDefault(key, defaults.get(key)); - return ( val != null && val instanceof Integer ) ? (int)val : defaultValue; - } - - public String getString(String key) { - return getString(key, null); - } - - public String getString(String key, String defaultValue) { - Object val = map.getOrDefault(key, defaults.get(key)); - return ( val != null ) ? val.toString() : null; - } - - public List getStringList(String key) { - return getStringList(key, null); - } - - @SuppressWarnings("unchecked") - public List getStringList(String key, ArrayList defaultValue) { - Object val = map.getOrDefault(key, defaults.get(key)); - return ( val != null && val instanceof ArrayList ) ? (List)val : null; - } - - public boolean contains(String key) { - return map.containsKey(key); - } - - public Set getKeys(boolean deeps) { - if ( !deeps ) { - return map.keySet(); - } else { - // TODO 未実装 - return map.keySet(); - } - } - - public void addDefaults(YamlConfig defaults) { - if (defaults == null) { - this.defaults = new HashMap(); - } else { - this.defaults = defaults.map; - } - } -} diff --git a/src/main/java/com/github/ucchyocean/lc3/bridge/McMMOBridge.java b/src/main/java/com/github/ucchyocean/lc3/bridge/McMMOBridge.java index f24672c..6c8b663 100644 --- a/src/main/java/com/github/ucchyocean/lc3/bridge/McMMOBridge.java +++ b/src/main/java/com/github/ucchyocean/lc3/bridge/McMMOBridge.java @@ -18,10 +18,10 @@ import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.LunaChatAPI; import com.github.ucchyocean.lc3.LunaChatBukkit; import com.github.ucchyocean.lc3.LunaChatConfig; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.bukkit.BukkitRecipientChatJapanizeTask; import com.github.ucchyocean.lc3.japanize.JapanizeType; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.Utility; import com.gmail.nossr50.api.PartyAPI; import com.gmail.nossr50.events.chat.McMMOPartyChatEvent; diff --git a/src/main/java/com/github/ucchyocean/lc3/bukkit/BukkitEventListener.java b/src/main/java/com/github/ucchyocean/lc3/bukkit/BukkitEventListener.java index c93cf1e..70f19c9 100644 --- a/src/main/java/com/github/ucchyocean/lc3/bukkit/BukkitEventListener.java +++ b/src/main/java/com/github/ucchyocean/lc3/bukkit/BukkitEventListener.java @@ -22,19 +22,19 @@ import org.bukkit.event.player.AsyncPlayerChatEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerQuitEvent; -import com.github.ucchyocean.lc3.KeywordReplacer; import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.LunaChatAPI; import com.github.ucchyocean.lc3.LunaChatBukkit; import com.github.ucchyocean.lc3.LunaChatConfig; import com.github.ucchyocean.lc3.Messages; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.bridge.VaultChatBridge; import com.github.ucchyocean.lc3.channel.Channel; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.japanize.JapanizeType; import com.github.ucchyocean.lc3.member.ChannelMember; import com.github.ucchyocean.lc3.member.ChannelMemberBukkit; +import com.github.ucchyocean.lc3.util.KeywordReplacer; +import com.github.ucchyocean.lc3.util.Utility; /** * Bukkit関連のイベントを監視するリスナ diff --git a/src/main/java/com/github/ucchyocean/lc3/bungee/BungeeEventListener.java b/src/main/java/com/github/ucchyocean/lc3/bungee/BungeeEventListener.java index b568dcd..16de23f 100644 --- a/src/main/java/com/github/ucchyocean/lc3/bungee/BungeeEventListener.java +++ b/src/main/java/com/github/ucchyocean/lc3/bungee/BungeeEventListener.java @@ -14,20 +14,20 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.github.ucchyocean.lc3.ChatColor; -import com.github.ucchyocean.lc3.KeywordReplacer; import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.LunaChatAPI; import com.github.ucchyocean.lc3.LunaChatBungee; import com.github.ucchyocean.lc3.LunaChatConfig; import com.github.ucchyocean.lc3.Messages; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.bridge.BungeePermsBridge; import com.github.ucchyocean.lc3.bridge.LuckPermsBridge; import com.github.ucchyocean.lc3.channel.Channel; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.japanize.Japanizer; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.ChatColor; +import com.github.ucchyocean.lc3.util.KeywordReplacer; +import com.github.ucchyocean.lc3.util.Utility; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.chat.TextComponent; diff --git a/src/main/java/com/github/ucchyocean/lc3/channel/BukkitChannel.java b/src/main/java/com/github/ucchyocean/lc3/channel/BukkitChannel.java index 45c6b65..9d56be0 100644 --- a/src/main/java/com/github/ucchyocean/lc3/channel/BukkitChannel.java +++ b/src/main/java/com/github/ucchyocean/lc3/channel/BukkitChannel.java @@ -16,18 +16,18 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.entity.Player; -import com.github.ucchyocean.lc3.KeywordReplacer; import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.LunaChatAPI; import com.github.ucchyocean.lc3.LunaChatBukkit; import com.github.ucchyocean.lc3.LunaChatConfig; import com.github.ucchyocean.lc3.Messages; -import com.github.ucchyocean.lc3.Utility; -import com.github.ucchyocean.lc3.UtilityBukkit; import com.github.ucchyocean.lc3.bridge.DynmapBridge; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.member.ChannelMember; import com.github.ucchyocean.lc3.member.ChannelMemberBukkit; +import com.github.ucchyocean.lc3.util.KeywordReplacer; +import com.github.ucchyocean.lc3.util.Utility; +import com.github.ucchyocean.lc3.util.UtilityBukkit; /** * チャンネルの実装クラス @@ -81,6 +81,8 @@ public class BukkitChannel extends Channel { // 範囲チャット if ( player instanceof ChannelMemberBukkit ) { + // ↑常にtrueだと思うが、念のため。 + Location origin = ((ChannelMemberBukkit)player).getLocation(); for ( Player p : Bukkit.getOnlinePlayers() ) { ChannelMember cp = ChannelMember.getChannelMember(p); @@ -90,8 +92,6 @@ public class BukkitChannel extends Channel { recipients.add(ChannelMember.getChannelMember(p)); } } - } else { - // TODO 何かするか?検討する } } else { diff --git a/src/main/java/com/github/ucchyocean/lc3/channel/BungeeChannel.java b/src/main/java/com/github/ucchyocean/lc3/channel/BungeeChannel.java index b93364a..c25ab92 100644 --- a/src/main/java/com/github/ucchyocean/lc3/channel/BungeeChannel.java +++ b/src/main/java/com/github/ucchyocean/lc3/channel/BungeeChannel.java @@ -10,15 +10,15 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; -import com.github.ucchyocean.lc3.KeywordReplacer; import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.LunaChatAPI; import com.github.ucchyocean.lc3.LunaChatBungee; import com.github.ucchyocean.lc3.LunaChatConfig; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.member.ChannelMember; import com.github.ucchyocean.lc3.member.ChannelMemberProxiedPlayer; +import com.github.ucchyocean.lc3.util.KeywordReplacer; +import com.github.ucchyocean.lc3.util.Utility; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.connection.ProxiedPlayer; diff --git a/src/main/java/com/github/ucchyocean/lc3/channel/Channel.java b/src/main/java/com/github/ucchyocean/lc3/channel/Channel.java index ec10118..847a0be 100644 --- a/src/main/java/com/github/ucchyocean/lc3/channel/Channel.java +++ b/src/main/java/com/github/ucchyocean/lc3/channel/Channel.java @@ -16,7 +16,6 @@ import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.github.ucchyocean.lc3.ChatColor; import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.LunaChatAPI; import com.github.ucchyocean.lc3.LunaChatConfig; @@ -24,11 +23,12 @@ import com.github.ucchyocean.lc3.LunaChatLogger; import com.github.ucchyocean.lc3.LunaChatMode; import com.github.ucchyocean.lc3.Messages; import com.github.ucchyocean.lc3.NGWordAction; -import com.github.ucchyocean.lc3.Utility; -import com.github.ucchyocean.lc3.YamlConfig; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.japanize.JapanizeType; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.ChatColor; +import com.github.ucchyocean.lc3.util.Utility; +import com.github.ucchyocean.lc3.util.YamlConfig; /** * チャンネル diff --git a/src/main/java/com/github/ucchyocean/lc3/channel/ChannelManager.java b/src/main/java/com/github/ucchyocean/lc3/channel/ChannelManager.java index 70016e3..e78d1d2 100644 --- a/src/main/java/com/github/ucchyocean/lc3/channel/ChannelManager.java +++ b/src/main/java/com/github/ucchyocean/lc3/channel/ChannelManager.java @@ -16,10 +16,10 @@ import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.LunaChatAPI; import com.github.ucchyocean.lc3.LunaChatMode; import com.github.ucchyocean.lc3.Messages; -import com.github.ucchyocean.lc3.YamlConfig; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.japanize.JapanizeType; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.YamlConfig; /** * チャンネルマネージャー diff --git a/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java b/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java index 5dcd011..149180f 100644 --- a/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java +++ b/src/main/java/com/github/ucchyocean/lc3/channel/JapanizeConvertTask.java @@ -12,12 +12,12 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import com.github.ucchyocean.lc3.LunaChat; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.japanize.IMEConverter; import com.github.ucchyocean.lc3.japanize.JapanizeType; import com.github.ucchyocean.lc3.japanize.YukiKanaConverter; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.Utility; /** * Japanize変換を実行して、実行後に発言を行うタスク diff --git a/src/main/java/com/github/ucchyocean/lc3/command/DictionaryCommand.java b/src/main/java/com/github/ucchyocean/lc3/command/DictionaryCommand.java index 75f409d..bc4a303 100644 --- a/src/main/java/com/github/ucchyocean/lc3/command/DictionaryCommand.java +++ b/src/main/java/com/github/ucchyocean/lc3/command/DictionaryCommand.java @@ -7,9 +7,9 @@ package com.github.ucchyocean.lc3.command; import java.util.Map; -import com.github.ucchyocean.lc3.ChatColor; import com.github.ucchyocean.lc3.Messages; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.ChatColor; /** * dictionaryコマンドの実行クラス diff --git a/src/main/java/com/github/ucchyocean/lc3/command/HideCommand.java b/src/main/java/com/github/ucchyocean/lc3/command/HideCommand.java index bf3f8ab..7fcd0b9 100644 --- a/src/main/java/com/github/ucchyocean/lc3/command/HideCommand.java +++ b/src/main/java/com/github/ucchyocean/lc3/command/HideCommand.java @@ -8,9 +8,9 @@ package com.github.ucchyocean.lc3.command; import java.util.ArrayList; import com.github.ucchyocean.lc3.Messages; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.channel.Channel; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.Utility; /** * hideコマンドの実行クラス diff --git a/src/main/java/com/github/ucchyocean/lc3/command/ListCommand.java b/src/main/java/com/github/ucchyocean/lc3/command/ListCommand.java index 3e64e5c..29b33d5 100644 --- a/src/main/java/com/github/ucchyocean/lc3/command/ListCommand.java +++ b/src/main/java/com/github/ucchyocean/lc3/command/ListCommand.java @@ -9,10 +9,10 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; -import com.github.ucchyocean.lc3.ChatColor; import com.github.ucchyocean.lc3.Messages; import com.github.ucchyocean.lc3.channel.Channel; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.ChatColor; /** * listコマンドの実行クラス diff --git a/src/main/java/com/github/ucchyocean/lc3/command/OptionCommand.java b/src/main/java/com/github/ucchyocean/lc3/command/OptionCommand.java index d82d5b4..d4c949e 100644 --- a/src/main/java/com/github/ucchyocean/lc3/command/OptionCommand.java +++ b/src/main/java/com/github/ucchyocean/lc3/command/OptionCommand.java @@ -11,11 +11,11 @@ import java.util.Map; import com.github.ucchyocean.lc3.LunaChat; import com.github.ucchyocean.lc3.Messages; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.channel.Channel; import com.github.ucchyocean.lc3.event.EventResult; import com.github.ucchyocean.lc3.japanize.JapanizeType; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.Utility; /** * optionコマンドの実行クラス diff --git a/src/main/java/com/github/ucchyocean/lc3/command/UnhideCommand.java b/src/main/java/com/github/ucchyocean/lc3/command/UnhideCommand.java index 0c69ecf..46112a6 100644 --- a/src/main/java/com/github/ucchyocean/lc3/command/UnhideCommand.java +++ b/src/main/java/com/github/ucchyocean/lc3/command/UnhideCommand.java @@ -6,9 +6,9 @@ package com.github.ucchyocean.lc3.command; import com.github.ucchyocean.lc3.Messages; -import com.github.ucchyocean.lc3.Utility; import com.github.ucchyocean.lc3.channel.Channel; import com.github.ucchyocean.lc3.member.ChannelMember; +import com.github.ucchyocean.lc3.util.Utility; /** * unhideコマンドの実行クラス diff --git a/src/main/java/com/github/ucchyocean/lc3/tool/MessageParser.java b/src/main/java/com/github/ucchyocean/lc3/tool/MessageParser.java index 581e1c7..d6f3870 100644 --- a/src/main/java/com/github/ucchyocean/lc3/tool/MessageParser.java +++ b/src/main/java/com/github/ucchyocean/lc3/tool/MessageParser.java @@ -16,7 +16,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.github.ucchyocean.lc3.YamlConfig; +import com.github.ucchyocean.lc3.util.YamlConfig; /** * messages_ja.yml を読んで、Messagesクラス用のメソッドを生成するツール diff --git a/src/main/java/com/github/ucchyocean/lc3/ChatColor.java b/src/main/java/com/github/ucchyocean/lc3/util/ChatColor.java similarity index 93% rename from src/main/java/com/github/ucchyocean/lc3/ChatColor.java rename to src/main/java/com/github/ucchyocean/lc3/util/ChatColor.java index eaca301..e5de779 100644 --- a/src/main/java/com/github/ucchyocean/lc3/ChatColor.java +++ b/src/main/java/com/github/ucchyocean/lc3/util/ChatColor.java @@ -3,7 +3,7 @@ * @license LGPLv3 * @copyright Copyright ucchy 2020 */ -package com.github.ucchyocean.lc3; +package com.github.ucchyocean.lc3.util; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/com/github/ucchyocean/lc3/EventPriority.java b/src/main/java/com/github/ucchyocean/lc3/util/EventPriority.java similarity index 80% rename from src/main/java/com/github/ucchyocean/lc3/EventPriority.java rename to src/main/java/com/github/ucchyocean/lc3/util/EventPriority.java index 509ee5f..289640e 100644 --- a/src/main/java/com/github/ucchyocean/lc3/EventPriority.java +++ b/src/main/java/com/github/ucchyocean/lc3/util/EventPriority.java @@ -3,7 +3,7 @@ * @license LGPLv3 * @copyright Copyright ucchy 2020 */ -package com.github.ucchyocean.lc3; +package com.github.ucchyocean.lc3.util; /** * Event優先度列挙子 diff --git a/src/main/java/com/github/ucchyocean/lc3/KeywordReplacer.java b/src/main/java/com/github/ucchyocean/lc3/util/KeywordReplacer.java similarity index 93% rename from src/main/java/com/github/ucchyocean/lc3/KeywordReplacer.java rename to src/main/java/com/github/ucchyocean/lc3/util/KeywordReplacer.java index 89a4f2b..2f6bc2f 100644 --- a/src/main/java/com/github/ucchyocean/lc3/KeywordReplacer.java +++ b/src/main/java/com/github/ucchyocean/lc3/util/KeywordReplacer.java @@ -3,7 +3,7 @@ * @license LGPLv3 * @copyright Copyright ucchy 2020 */ -package com.github.ucchyocean.lc3; +package com.github.ucchyocean.lc3.util; /** * 文字列のキーワード置き換えを行うユーティリティクラス diff --git a/src/main/java/com/github/ucchyocean/lc3/Utility.java b/src/main/java/com/github/ucchyocean/lc3/util/Utility.java similarity index 94% rename from src/main/java/com/github/ucchyocean/lc3/Utility.java rename to src/main/java/com/github/ucchyocean/lc3/util/Utility.java index 34745a3..ac6abce 100644 --- a/src/main/java/com/github/ucchyocean/lc3/Utility.java +++ b/src/main/java/com/github/ucchyocean/lc3/util/Utility.java @@ -3,7 +3,7 @@ * @license LGPLv3 * @copyright Copyright ucchy 2020 */ -package com.github.ucchyocean.lc3; +package com.github.ucchyocean.lc3.util; import java.io.BufferedReader; import java.io.BufferedWriter; @@ -17,6 +17,9 @@ 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 @@ -176,7 +179,12 @@ public class Utility { * @return 接続したことがあるかどうか */ public static boolean existsOfflinePlayer(String name) { - // TODO 未実装 + if ( LunaChat.getUUIDCacheData().getUUIDFromName(name) != null ) { + return true; + } + if ( LunaChat.getMode() == LunaChatMode.BUKKIT ) { + return UtilityBukkit.existsOfflinePlayer(name); + } return false; } diff --git a/src/main/java/com/github/ucchyocean/lc3/UtilityBukkit.java b/src/main/java/com/github/ucchyocean/lc3/util/UtilityBukkit.java similarity index 78% rename from src/main/java/com/github/ucchyocean/lc3/UtilityBukkit.java rename to src/main/java/com/github/ucchyocean/lc3/util/UtilityBukkit.java index fa46c43..01e64e2 100644 --- a/src/main/java/com/github/ucchyocean/lc3/UtilityBukkit.java +++ b/src/main/java/com/github/ucchyocean/lc3/util/UtilityBukkit.java @@ -3,7 +3,7 @@ * @license LGPLv3 * @copyright Copyright ucchy 2020 */ -package com.github.ucchyocean.lc3; +package com.github.ucchyocean.lc3.util; import java.util.Collection; @@ -55,4 +55,13 @@ public class UtilityBukkit { 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); + } } diff --git a/src/main/java/com/github/ucchyocean/lc3/util/YamlConfig.java b/src/main/java/com/github/ucchyocean/lc3/util/YamlConfig.java new file mode 100644 index 0000000..cb5b2cb --- /dev/null +++ b/src/main/java/com/github/ucchyocean/lc3/util/YamlConfig.java @@ -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 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 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); + } + } +} diff --git a/src/main/java/com/github/ucchyocean/lc3/util/YamlSection.java b/src/main/java/com/github/ucchyocean/lc3/util/YamlSection.java new file mode 100644 index 0000000..ef59067 --- /dev/null +++ b/src/main/java/com/github/ucchyocean/lc3/util/YamlSection.java @@ -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 map; + private Map defaults = new HashMap<>(); + + /** + * コンストラクタ + */ + public YamlSection() { + map = new HashMap<>(); + } + + /** + * コンストラクタ + * @param map 初期データ + */ + public YamlSection(Map 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<String>に変換して返す + * @param key + * @return keyに対応する値(値が無いかList<String>に変換できない場合はnullが返される) + */ + public @Nullable List getStringList(String key) { + return getStringList(key, null); + } + + /** + * 指定されたkeyの値をList<String>に変換して返す + * @param key + * @param defaultValue デフォルト値 + * @return keyに対応する値(値が無いかList<String>に変換できない場合はdefaultValueが返される) + */ + @SuppressWarnings("unchecked") + public List getStringList(String key, ArrayList defaultValue) { + Object val = get(key, defaults.get(key)); + return ( val != null && val instanceof ArrayList ) ? (List)val : defaultValue; + } + + /** + * 指定されたkeyの値をMap<String, Object>に変換して返す + * @param key + * @return keyに対応する値(値が無いかMap<String, Object>に変換できない場合はnullが返される) + */ + public @Nullable Map getMap(String key) { + return getMap(key, null); + } + + /** + * 指定されたkeyの値をMap<String, Object>に変換して返す + * @param key + * @param defaultValue デフォルト値 + * @return keyに対応する値(値が無いかMap<String, Object>に変換できない場合はdefaultValueが返される) + */ + @SuppressWarnings("unchecked") + public Map getMap(String key, Map defaultValue) { + Object val = get(key, defaults.get(key)); + return ( val != null && val instanceof Map ) ? (Map)val : defaultValue; + } + + /** + * 指定されたkeyの値をYamlSectionに変換して返す + * @param key + * @return YamlSection(値が無いかMap<String, Object>に変換できない場合はnullが返される) + */ + public @Nullable YamlSection getSection(String key) { + return getSection(key, null); + } + + /** + * 指定されたkeyの値をYamlSectionに変換して返す + * @param key + * @param defaultValue デフォルト値 + * @return YamlSection(値が無いかMap<String, Object>に変換できない場合はdefaultValueが返される) + */ + public YamlSection getSection(String key, YamlSection defaultValue) { + Map 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のサブセクションを作成する
+ * 注意:階層構造指定でセクションを作成した場合、最下層のセクションが戻り値として返されることに注意すること。
+ * 例えば、createSection("aa.bb.cc") の戻り値は "cc" のセクションとなる。 + * @param key + * @return サブセクション + */ + public YamlSection createSection(String key) { + if ( containsSection(key) ) return getSection(key); + Map map = new HashMap(); + set(key, map); + return new YamlSection(map); + } + + /** + * 階層のkeyを羅列する + * @param deeps 再帰的にkeyを取得するかどうか + * @return keyの羅列 + */ + public Set getKeys(boolean deeps) { + if ( !deeps ) { + return map.keySet(); + } else { + Set result = new TreeSet(); + 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(); + } else { + this.defaults = defaults.map; + } + } +} diff --git a/src/test/java/com/github/ucchyocean/lc3/util/YamlConfigTester.java b/src/test/java/com/github/ucchyocean/lc3/util/YamlConfigTester.java new file mode 100644 index 0000000..f053e7c --- /dev/null +++ b/src/test/java/com/github/ucchyocean/lc3/util/YamlConfigTester.java @@ -0,0 +1,47 @@ +/* + * @author ucchy + * @license LGPLv3 + * @copyright Copyright ucchy 2020 + */ +package com.github.ucchyocean.lc3.util; + +import java.util.ArrayList; +import java.util.List; + +import junit.framework.TestCase; + +/** + * YamlConfigのテスト + * @author ucchy + */ +public class YamlConfigTester extends TestCase { + + public void testYamlConfig() { + + YamlConfig config = new YamlConfig(); + + config.set("aaa.bbb", "test"); + config.set("aaa.int", 1); + + YamlSection section = config.createSection("bbb"); + + section.set("aiueo.aaa", "testdesu"); + List list = new ArrayList<>(); + list.add("aa"); + list.add("bb"); + section.set("list", list); + + System.out.println("=== Yaml Values ==="); + System.out.println(config.getRawData()); + System.out.println(); + System.out.println("=== Yaml Keys (deeps=true) ==="); + for ( String key : config.getKeys(true) ) { + System.out.println(key); + } + + assertTrue("test".equals(config.getString("aaa.bbb"))); + assertTrue("testdesu".equals(config.getString("bbb.aiueo.aaa"))); + assertTrue(config.getInt("aaa.int") == 1); + assertTrue(config.getStringList("bbb.list").size() == 2); + } +}