diff --git a/src/main/java/com/github/ucchyocean/lc/EventPriority.java b/src/main/java/com/github/ucchyocean/lc/EventPriority.java index 33a0ca4..ded4610 100644 --- a/src/main/java/com/github/ucchyocean/lc/EventPriority.java +++ b/src/main/java/com/github/ucchyocean/lc/EventPriority.java @@ -10,16 +10,10 @@ package com.github.ucchyocean.lc; * @author ucchy */ public enum EventPriority { - LOWEST, - LOW, - NORMAL, - HIGH, - HIGHEST, - MONITOR, } diff --git a/src/main/java/com/github/ucchyocean/lc/LunaChat.java b/src/main/java/com/github/ucchyocean/lc/LunaChat.java index f53cc9d..7089924 100644 --- a/src/main/java/com/github/ucchyocean/lc/LunaChat.java +++ b/src/main/java/com/github/ucchyocean/lc/LunaChat.java @@ -14,7 +14,7 @@ import java.io.File; public class LunaChat { private static PluginInterface instance; - private static boolean isBukkitMode = false; + private static LunaChatMode mode; public static void setPlugin(PluginInterface plugin) { instance = plugin; @@ -24,14 +24,15 @@ public class LunaChat { return instance; } - public static boolean isBukkitMode() { - return isBukkitMode; + public static void setMode(LunaChatMode _mode) { + mode = _mode; } - public static void setBukkitMode(boolean mode) { - isBukkitMode = mode; + public static LunaChatMode getMode() { + return mode; } + // public static EventSenderInterface getEventSender() { // // TODO 未実装 // return null; diff --git a/src/main/java/com/github/ucchyocean/lc/LunaChatAPI.java b/src/main/java/com/github/ucchyocean/lc/LunaChatAPI.java index d327f8f..8f47776 100644 --- a/src/main/java/com/github/ucchyocean/lc/LunaChatAPI.java +++ b/src/main/java/com/github/ucchyocean/lc/LunaChatAPI.java @@ -10,8 +10,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; -import org.bukkit.command.CommandSender; - import com.github.ucchyocean.lc.channel.Channel; import com.github.ucchyocean.lc.channel.ChannelPlayer; import com.github.ucchyocean.lc.japanize.JapanizeType; @@ -76,13 +74,13 @@ public interface LunaChatAPI { */ public Channel createChannel(String channelName); - /** - * 新しいチャンネルを作成する - * @param channelName チャンネル名 - * @param sender チャンネルを作成した人 - * @return 作成されたチャンネル - */ - public Channel createChannel(String channelName, CommandSender sender); +// /** +// * 新しいチャンネルを作成する +// * @param channelName チャンネル名 +// * @param sender チャンネルを作成した人 +// * @return 作成されたチャンネル +// */ +// public Channel createChannel(String channelName, CommandSender sender); /** * チャンネルを削除する @@ -91,13 +89,13 @@ public interface LunaChatAPI { */ public boolean removeChannel(String channelName); - /** - * チャンネルを削除する - * @param channelName 削除するチャンネル名 - * @param sender チャンネルを削除した人 - * @return 削除したかどうか - */ - public boolean removeChannel(String channelName, CommandSender sender); +// /** +// * チャンネルを削除する +// * @param channelName 削除するチャンネル名 +// * @param sender チャンネルを削除した人 +// * @return 削除したかどうか +// */ +// public boolean removeChannel(String channelName, CommandSender sender); /** * テンプレートを取得する diff --git a/src/main/java/com/github/ucchyocean/lc/LunaChatMode.java b/src/main/java/com/github/ucchyocean/lc/LunaChatMode.java new file mode 100644 index 0000000..1549619 --- /dev/null +++ b/src/main/java/com/github/ucchyocean/lc/LunaChatMode.java @@ -0,0 +1,16 @@ +/* + * @author ucchy + * @license LGPLv3 + * @copyright Copyright ucchy 2020 + */ +package com.github.ucchyocean.lc; + +/** + * LunaChatの動作モード + * @author ucchy + */ +public enum LunaChatMode { + BUKKIT, + BUNGEE, + STANDALONE, +} diff --git a/src/main/java/com/github/ucchyocean/lc/bukkit/LunaChatBukkit.java b/src/main/java/com/github/ucchyocean/lc/bukkit/LunaChatBukkit.java index 2f2cebd..99abb65 100644 --- a/src/main/java/com/github/ucchyocean/lc/bukkit/LunaChatBukkit.java +++ b/src/main/java/com/github/ucchyocean/lc/bukkit/LunaChatBukkit.java @@ -14,9 +14,11 @@ import org.bukkit.command.CommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; +import com.github.ucchyocean.lc.LunaChat; import com.github.ucchyocean.lc.LunaChatAPI; import com.github.ucchyocean.lc.LunaChatConfig; import com.github.ucchyocean.lc.LunaChatLogger; +import com.github.ucchyocean.lc.LunaChatMode; import com.github.ucchyocean.lc.PluginInterface; import com.github.ucchyocean.lc.bridge.DynmapBridge; import com.github.ucchyocean.lc.bridge.McMMOBridge; @@ -58,6 +60,9 @@ public class LunaChatBukkit extends JavaPlugin implements PluginInterface { @Override public void onEnable() { + LunaChat.setPlugin(this); + LunaChat.setMode(LunaChatMode.BUKKIT); + // 変数などの初期化 config = new LunaChatConfig(getDataFolder(), getFile()); manager = new ChannelManager(); diff --git a/src/main/java/com/github/ucchyocean/lc/bungee/LunaChatBungee.java b/src/main/java/com/github/ucchyocean/lc/bungee/LunaChatBungee.java index fdef437..3d00df4 100644 --- a/src/main/java/com/github/ucchyocean/lc/bungee/LunaChatBungee.java +++ b/src/main/java/com/github/ucchyocean/lc/bungee/LunaChatBungee.java @@ -14,9 +14,11 @@ import java.util.concurrent.TimeUnit; import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.github.ucchyocean.lc.LunaChat; import com.github.ucchyocean.lc.LunaChatAPI; import com.github.ucchyocean.lc.LunaChatConfig; import com.github.ucchyocean.lc.LunaChatLogger; +import com.github.ucchyocean.lc.LunaChatMode; import com.github.ucchyocean.lc.PluginInterface; import com.github.ucchyocean.lc.Utility; import com.github.ucchyocean.lc.channel.ChannelManager; @@ -56,6 +58,9 @@ public class LunaChatBungee extends Plugin implements Listener, PluginInterface @Override public void onEnable() { + LunaChat.setPlugin(this); + LunaChat.setMode(LunaChatMode.BUNGEE); + // 初期化 history = new HashMap(); dateFormat = new SimpleDateFormat(DATE_FORMAT_PATTERN); diff --git a/src/main/java/com/github/ucchyocean/lc/channel/ChannelManager.java b/src/main/java/com/github/ucchyocean/lc/channel/ChannelManager.java index 00b78a3..b0c766c 100644 --- a/src/main/java/com/github/ucchyocean/lc/channel/ChannelManager.java +++ b/src/main/java/com/github/ucchyocean/lc/channel/ChannelManager.java @@ -12,15 +12,10 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.configuration.file.YamlConfiguration; - import com.github.ucchyocean.lc.LunaChat; import com.github.ucchyocean.lc.LunaChatAPI; import com.github.ucchyocean.lc.Resources; -import com.github.ucchyocean.lc.event.LunaChatChannelCreateEvent; -import com.github.ucchyocean.lc.event.LunaChatChannelRemoveEvent; +import com.github.ucchyocean.lc.YamlConfig; import com.github.ucchyocean.lc.japanize.JapanizeType; /** @@ -63,20 +58,13 @@ public class ChannelManager implements LunaChatAPI { public void reloadAllData() { // デフォルトチャンネル設定のロード - fileDefaults = new File( - LunaChat.getDataFolder(), FILE_NAME_DCHANNELS); + fileDefaults = new File(LunaChat.getDataFolder(), FILE_NAME_DCHANNELS); if ( !fileDefaults.exists() ) { - YamlConfiguration conf = new YamlConfiguration(); - try { - conf.save(fileDefaults); - } catch (IOException e) { - e.printStackTrace(); - } + makeEmptyFile(fileDefaults); } - YamlConfiguration config = - YamlConfiguration.loadConfiguration(fileDefaults); + YamlConfig config = YamlConfig.load(fileDefaults); defaultChannels = new HashMap(); for ( String key : config.getKeys(false) ) { @@ -87,20 +75,13 @@ public class ChannelManager implements LunaChatAPI { } // テンプレート設定のロード - fileTemplates = new File( - LunaChat.getDataFolder(), FILE_NAME_TEMPLATES); + fileTemplates = new File(LunaChat.getDataFolder(), FILE_NAME_TEMPLATES); if ( !fileTemplates.exists() ) { - YamlConfiguration conf = new YamlConfiguration(); - try { - conf.save(fileTemplates); - } catch (IOException e) { - e.printStackTrace(); - } + makeEmptyFile(fileTemplates); } - YamlConfiguration configTemplates = - YamlConfiguration.loadConfiguration(fileTemplates); + YamlConfig configTemplates = YamlConfig.load(fileTemplates); templates = new HashMap(); for ( String key : configTemplates.getKeys(false) ) { @@ -108,20 +89,13 @@ public class ChannelManager implements LunaChatAPI { } // Japanize設定のロード - fileJapanize = new File( - LunaChat.getDataFolder(), FILE_NAME_JAPANIZE); + fileJapanize = new File(LunaChat.getDataFolder(), FILE_NAME_JAPANIZE); if ( !fileJapanize.exists() ) { - YamlConfiguration conf = new YamlConfiguration(); - try { - conf.save(fileJapanize); - } catch (IOException e) { - e.printStackTrace(); - } + makeEmptyFile(fileJapanize); } - YamlConfiguration configJapanize = - YamlConfiguration.loadConfiguration(fileJapanize); + YamlConfig configJapanize = YamlConfig.load(fileJapanize); japanize = new HashMap(); for ( String key : configJapanize.getKeys(false) ) { @@ -129,20 +103,13 @@ public class ChannelManager implements LunaChatAPI { } // dictionaryのロード - fileDictionary = new File( - LunaChat.getDataFolder(), FILE_NAME_DICTIONARY); + fileDictionary = new File(LunaChat.getDataFolder(), FILE_NAME_DICTIONARY); if ( !fileDictionary.exists() ) { - YamlConfiguration conf = new YamlConfiguration(); - try { - conf.save(fileDictionary); - } catch (IOException e) { - e.printStackTrace(); - } + makeEmptyFile(fileDictionary); } - YamlConfiguration configDictionary = - YamlConfiguration.loadConfiguration(fileDictionary); + YamlConfig configDictionary = YamlConfig.load(fileDictionary); dictionary = new HashMap(); for ( String key : configDictionary.getKeys(false) ) { @@ -150,20 +117,13 @@ public class ChannelManager implements LunaChatAPI { } // hideリストのロード - fileHidelist = new File( - LunaChat.getDataFolder(), FILE_NAME_HIDELIST); + fileHidelist = new File(LunaChat.getDataFolder(), FILE_NAME_HIDELIST); if ( !fileHidelist.exists() ) { - YamlConfiguration conf = new YamlConfiguration(); - try { - conf.save(fileHidelist); - } catch (IOException e) { - e.printStackTrace(); - } + makeEmptyFile(fileHidelist); } - YamlConfiguration configHidelist = - YamlConfiguration.loadConfiguration(fileHidelist); + YamlConfig configHidelist = YamlConfig.load(fileHidelist); hidelist = new HashMap>(); for ( String key : configHidelist.getKeys(false) ) { @@ -196,7 +156,7 @@ public class ChannelManager implements LunaChatAPI { private boolean saveDefaults() { try { - YamlConfiguration config = new YamlConfiguration(); + YamlConfig config = new YamlConfig(); for ( String key : defaultChannels.keySet() ) { config.set(key, defaultChannels.get(key)); } @@ -215,7 +175,7 @@ public class ChannelManager implements LunaChatAPI { private boolean saveTemplates() { try { - YamlConfiguration config = new YamlConfiguration(); + YamlConfig config = new YamlConfig(); for ( String key : templates.keySet() ) { config.set(key, templates.get(key)); } @@ -234,7 +194,7 @@ public class ChannelManager implements LunaChatAPI { private boolean saveJapanize() { try { - YamlConfiguration config = new YamlConfiguration(); + YamlConfig config = new YamlConfig(); for ( String key : japanize.keySet() ) { config.set(key, japanize.get(key)); } @@ -253,7 +213,7 @@ public class ChannelManager implements LunaChatAPI { private boolean saveDictionary() { try { - YamlConfiguration config = new YamlConfiguration(); + YamlConfig config = new YamlConfig(); for ( String key : dictionary.keySet() ) { config.set(key, dictionary.get(key)); } @@ -272,7 +232,7 @@ public class ChannelManager implements LunaChatAPI { private boolean saveHidelist() { try { - YamlConfiguration config = new YamlConfiguration(); + YamlConfig config = new YamlConfig(); for ( String key : hidelist.keySet() ) { config.set(key, getIdList(hidelist.get(key))); } @@ -426,26 +386,16 @@ public class ChannelManager implements LunaChatAPI { */ @Override public Channel createChannel(String channelName) { - return createChannel(channelName, null); - } - - /** - * 新しいチャンネルを作成する - * @param channelName チャンネル名 - * @return 作成されたチャンネル - * @see com.github.ucchyocean.lc.LunaChatAPI#createChannel(java.lang.String, org.bukkit.command.CommandSender) - */ - @Override - public Channel createChannel(String channelName, CommandSender sender) { // イベントコール - LunaChatChannelCreateEvent event = - new LunaChatChannelCreateEvent(channelName, sender); - Bukkit.getPluginManager().callEvent(event); - if ( event.isCancelled() ) { - return null; - } - String name = event.getChannelName(); +// LunaChatChannelCreateEvent event = +// new LunaChatChannelCreateEvent(channelName, sender); +// Bukkit.getPluginManager().callEvent(event); +// if ( event.isCancelled() ) { +// return null; +// } +// String name = event.getChannelName(); + String name = channelName; Channel channel = new BukkitChannel(name); channels.put(name.toLowerCase(), channel); @@ -461,27 +411,16 @@ public class ChannelManager implements LunaChatAPI { */ @Override public boolean removeChannel(String channelName) { - return removeChannel(channelName, null); - } - - /** - * チャンネルを削除する - * @param channelName 削除するチャンネル名 - * @return 削除したかどうか - * @see com.github.ucchyocean.lc.LunaChatAPI#removeChannel(java.lang.String, org.bukkit.command.CommandSender) - */ - @Override - public boolean removeChannel(String channelName, CommandSender sender) { channelName = channelName.toLowerCase(); // イベントコール - LunaChatChannelRemoveEvent event = - new LunaChatChannelRemoveEvent(channelName, sender); - Bukkit.getPluginManager().callEvent(event); - if ( event.isCancelled() ) { - return false; - } +// LunaChatChannelRemoveEvent event = +// new LunaChatChannelRemoveEvent(channelName, sender); +// Bukkit.getPluginManager().callEvent(event); +// if ( event.isCancelled() ) { +// return false; +// } Channel channel = getChannel(channelName); if ( channel != null ) { @@ -647,8 +586,8 @@ public class ChannelManager implements LunaChatAPI { } // Japanize変換タスクを作成して、同期で実行する。 - DelayedJapanizeConvertTask task = new DelayedJapanizeConvertTask( - message, type, null, null, "%japanize"); + // TODO スレッド作成はサーバーに任せる + JapanizeConvertTask task = new JapanizeConvertTask(message, type, "%japanize"); if ( task.runSync() ) { return task.getResult(); } @@ -678,4 +617,17 @@ public class ChannelManager implements LunaChatAPI { } return results; } + + /** + * 指定されたファイル出力先に、空のYamlファイルを作成する + * @param file 出力先 + */ + private void makeEmptyFile(File file) { + YamlConfig conf = new YamlConfig(); + try { + conf.save(file); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/main/java/com/github/ucchyocean/lc/channel/JapanizeConvertTask.java b/src/main/java/com/github/ucchyocean/lc/channel/JapanizeConvertTask.java new file mode 100644 index 0000000..be5ce69 --- /dev/null +++ b/src/main/java/com/github/ucchyocean/lc/channel/JapanizeConvertTask.java @@ -0,0 +1,161 @@ +/* + * @author ucchy + * @license LGPLv3 + * @copyright Copyright ucchy 2020 + */ +package com.github.ucchyocean.lc.channel; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.github.ucchyocean.lc.LunaChat; +import com.github.ucchyocean.lc.Utility; +import com.github.ucchyocean.lc.japanize.IMEConverter; +import com.github.ucchyocean.lc.japanize.JapanizeType; +import com.github.ucchyocean.lc.japanize.YukiKanaConverter; + +/** + * Japanize変換を実行して、実行後に発言を行うタスク + * @author ucchy + */ +public class JapanizeConvertTask { + + private static final String REGEX_URL = "https?://[\\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+"; + + private String org; + private JapanizeType type; + private String format; + private String result; + + /** + * コンストラクタ + * @param org 変換前の文字列 + * @param type 変換タイプ + * @param japanizeFormat 変換後に発言するときの、発言フォーマット + */ + public JapanizeConvertTask(String org, JapanizeType type, String japanizeFormat) { + this.org = org; + this.type = type; + this.format = japanizeFormat; + } + + public void run() { + runSync(); + } + + /** + * 同期処理で変換を行います。結果は getResult() で取得してください。 + * @return 処理を実行したかどうか(イベントでキャンセルされた場合はfalseになります) + */ + public boolean runSync() { + + // 変換対象外のキーワード + HashMap keywordMap = new HashMap(); + ArrayList keywords = new ArrayList(); + if ( LunaChat.getConfig().isJapanizeIgnorePlayerName() ) { + // TODO +// for ( Player player : Bukkit.getOnlinePlayers() ) { +// keywords.add(player.getName()); +// } + } + HashMap dictionary = + LunaChat.getAPI().getAllDictionary(); + + // カラーコード削除、URL削除 + String deletedURL = Utility.stripColorCode(org.replaceAll(REGEX_URL, " ")); + + // キーワードをロック + int index = 0; + String keywordLocked = deletedURL; + for ( String keyword : keywords ) { + if ( keywordLocked.contains(keyword) ) { + index++; + String key = "<" + makeMultibytesDigit(index) + ">"; + keywordLocked = keywordLocked.replace(keyword, key); + keywordMap.put(key, keyword); + } + } + for ( String dickey : dictionary.keySet() ) { + if ( keywordLocked.contains(dickey) ) { + index++; + String key = "<" + makeMultibytesDigit(index) + ">"; + keywordLocked = keywordLocked.replace(dickey, key); + keywordMap.put(key, dictionary.get(dickey)); + } + } + + // カナ変換 + String japanized = YukiKanaConverter.conv(keywordLocked); + + // IME変換 + if ( type == JapanizeType.GOOGLE_IME ) { + japanized = IMEConverter.convByGoogleIME(japanized); + } + + // キーワードのアンロック + for ( String key : keywordMap.keySet() ) { + japanized = japanized.replace(key, keywordMap.get(key)); + } + + // 変換後の文字列にNGワードが含まれている場合は、マスクする + for ( Pattern pattern : LunaChat.getConfig().getNgwordCompiled() ) { + Matcher matcher = pattern.matcher(japanized); + if ( matcher.find() ) { + japanized = matcher.replaceAll( + Utility.getAstariskString(matcher.group(0).length())); + } + } + + // イベントコール +// String channelName = (channel == null) ? "" : channel.getName(); +// EventResult event = LunaChat.getEventSender().sendLunaChatPostJapanizeEvent( +// channelName, player, org, japanized); +// if ( event.isCancelled() ) { +// return false; +// } +// japanized = event.getValueAsString("japanized"); + + // フォーマットする + result = format.replace("%msg", org); + result = result.replace("%japanize", japanized); + + return true; + } + + /** + * Japanize変換の結果を返します。 + * @return 変換結果 + */ + public String getResult() { + return result; + } + + /** + * 数値を、全角文字の文字列に変換して返す + * @param digit + * @return + */ + private String makeMultibytesDigit(int digit) { + + String half = Integer.toString(digit); + StringBuilder result = new StringBuilder(); + for ( int index=0; index < half.length(); index++ ) { + switch ( half.charAt(index) ) { + case '0' : result.append("0"); break; + case '1' : result.append("1"); break; + case '2' : result.append("2"); break; + case '3' : result.append("3"); break; + case '4' : result.append("4"); break; + case '5' : result.append("5"); break; + case '6' : result.append("6"); break; + case '7' : result.append("7"); break; + case '8' : result.append("8"); break; + case '9' : result.append("9"); break; + } + } + return result.toString(); + } +} +