LunaChat v3-k2 : tidied up japanize tasks.

このコミットが含まれているのは:
ucchy committed 2020-06-05 00:18:05 +09:00
1 親 89763cfe4d
コミット a127034d91
12ファイル変更
+34 -348

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

+2 -2
ファイルの表示
@@ -17,8 +17,8 @@ import com.github.ucchyocean.lc3.LunaChat;
import com.github.ucchyocean.lc3.LunaChatAPI;
import com.github.ucchyocean.lc3.LunaChatConfig;
import com.github.ucchyocean.lc3.Utility;
import com.github.ucchyocean.lc3.bukkit.BukkitRecipientChatJapanizeTask;
import com.github.ucchyocean.lc3.bukkit.LunaChatBukkit;
import com.github.ucchyocean.lc3.channel.DelayedJapanizeRecipientChatTask;
import com.github.ucchyocean.lc3.japanize.JapanizeType;
import com.github.ucchyocean.lc3.member.ChannelMember;
import com.gmail.nossr50.api.PartyAPI;
@@ -99,7 +99,7 @@ public class McMMOBridge implements Listener {
String taskFormat = Utility.replaceColorCode(config.getJapanizeLine2Format());
DelayedJapanizeRecipientChatTask task = new DelayedJapanizeRecipientChatTask(
BukkitRecipientChatJapanizeTask task = new BukkitRecipientChatJapanizeTask(
message, config.getJapanizeType(), player, taskFormat, recipients);
// 発言処理を必ず先に実施させるため、遅延を入れてタスクを実行する。
+1 -2
ファイルの表示
@@ -29,7 +29,6 @@ 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.channel.DelayedJapanizeNormalChatTask;
import com.github.ucchyocean.lc3.event.LunaChatPreChatEvent;
import com.github.ucchyocean.lc3.japanize.JapanizeType;
import com.github.ucchyocean.lc3.member.ChannelMember;
@@ -365,7 +364,7 @@ public class BukkitEventListener implements Listener {
String taskFormat = Utility.replaceColorCode(config.getJapanizeLine2Format());
DelayedJapanizeNormalChatTask task = new DelayedJapanizeNormalChatTask(
BukkitNormalChatJapanizeTask task = new BukkitNormalChatJapanizeTask(
message, config.getJapanizeType(), player, taskFormat, event);
// 発言処理を必ず先に実施させるため、遅延を入れてタスクを実行する。
@@ -3,14 +3,15 @@
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.channel;
package com.github.ucchyocean.lc3.bukkit;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.scheduler.BukkitRunnable;
import com.github.ucchyocean.lc3.LunaChat;
import com.github.ucchyocean.lc3.bukkit.LunaChatBukkit;
import com.github.ucchyocean.lc3.channel.JapanizeConvertTask;
import com.github.ucchyocean.lc3.japanize.JapanizeType;
import com.github.ucchyocean.lc3.member.ChannelMember;
import com.github.ucchyocean.lc3.member.ChannelMemberPlayer;
@@ -19,11 +20,13 @@ import com.github.ucchyocean.lc3.member.ChannelMemberPlayer;
* Japanize2行表示のときに、変換結果を遅延して通常チャットに表示するためのタスク
* @author ucchy
*/
public class DelayedJapanizeNormalChatTask extends DelayedJapanizeConvertTask {
public class BukkitNormalChatJapanizeTask extends BukkitRunnable {
private ChannelMember player;
private AsyncPlayerChatEvent event;
private JapanizeConvertTask task;
/**
* コンストラクタ
* @param org 変換前の文字列
@@ -32,9 +35,10 @@ public class DelayedJapanizeNormalChatTask extends DelayedJapanizeConvertTask {
* @param japanizeFormat 変換後に発言するときの、発言フォーマット
* @param event イベント
*/
public DelayedJapanizeNormalChatTask(String org, JapanizeType type,
public BukkitNormalChatJapanizeTask(String org, JapanizeType type,
ChannelMember player, String japanizeFormat, final AsyncPlayerChatEvent event) {
super(org, type, null, player, japanizeFormat);
task = new JapanizeConvertTask(org, type, japanizeFormat);
this.player = player;
this.event = event;
}
@@ -45,9 +49,9 @@ public class DelayedJapanizeNormalChatTask extends DelayedJapanizeConvertTask {
@Override
public void run() {
if ( runSync() ) {
if ( task.runSync() ) {
String result = getResult();
String result = task.getResult();
// 送信
for ( Player p : event.getRecipients() ) {
@@ -3,13 +3,15 @@
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.channel;
package com.github.ucchyocean.lc3.bukkit;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import com.github.ucchyocean.lc3.channel.JapanizeConvertTask;
import com.github.ucchyocean.lc3.japanize.JapanizeType;
import com.github.ucchyocean.lc3.member.ChannelMember;
@@ -18,10 +20,12 @@ import com.github.ucchyocean.lc3.member.ChannelMember;
* 他プラグインのチャンネル(mcMMOのパーティチャットや、TownyChatなど)に、Japanize変換結果を表示するために使用する。
* @author ucchy
*/
public class DelayedJapanizeRecipientChatTask extends DelayedJapanizeConvertTask {
public class BukkitRecipientChatJapanizeTask extends BukkitRunnable {
private List<Player> recipients;
private JapanizeConvertTask task;
/**
* コンストラクタ
* @param org 変換前の文字列
@@ -30,9 +34,9 @@ public class DelayedJapanizeRecipientChatTask extends DelayedJapanizeConvertTask
* @param japanizeFormat 変換後に発言するときの、発言フォーマット
* @param recipients メッセージ受信者
*/
public DelayedJapanizeRecipientChatTask(String org, JapanizeType type, ChannelMember player,
public BukkitRecipientChatJapanizeTask(String org, JapanizeType type, ChannelMember player,
String japanizeFormat, List<Player> recipients) {
super(org, type, null, player, japanizeFormat);
task = new JapanizeConvertTask(org, type, japanizeFormat);
this.recipients = recipients;
}
@@ -42,9 +46,9 @@ public class DelayedJapanizeRecipientChatTask extends DelayedJapanizeConvertTask
@Override
public void run() {
if ( runSync() ) {
if ( task.runSync() ) {
String result = getResult();
String result = task.getResult();
// 送信
for ( Player p : recipients ) {
-94
ファイルの表示
@@ -1,94 +0,0 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.bungee;
import java.util.HashMap;
import com.github.ucchyocean.lc3.LunaChat;
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.CommandSender;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.plugin.Command;
/**
* LunaChatのdictionaryコマンド実装クラス
* @author ucchy
*/
public class DictionaryCommand extends Command {
private LunaChatBungee parent;
/**
* コンストラクタ
* @param parent
* @param name コマンド
* @param permission パーミッション
* @param aliases エイリアス
*/
public DictionaryCommand(LunaChatBungee parent, String name, String permission, String... aliases) {
super(name, permission, aliases);
this.parent = parent;
}
/**
* コマンドが実行された時に呼び出されるメソッド
* @param sender コマンド実行者
* @param args コマンド引数
* @see net.md_5.bungee.api.plugin.Command#execute(net.md_5.bungee.api.CommandSender, java.lang.String[])
*/
@Override
public void execute(CommandSender sender, String[] args) {
// パーミッションチェック
if ( !sender.hasPermission("bungeejapanizemessenger.dictionary") ) {
sendMessage(sender, "コマンドの実行権限がありません。");
return;
}
if ( args.length >= 3 && args[0].equalsIgnoreCase("add") ) {
LunaChat.getAPI().setDictionary(args[1], args[2]);
String message = String.format("[BJM日本語変換] %s を %s と覚えました。", args[1], args[2]);
parent.getProxy().broadcast(TextComponent.fromLegacyText(message));
return;
} else if ( args.length >= 2 && args[0].equalsIgnoreCase("remove") ) {
LunaChat.getAPI().removeDictionary(args[1]);
String message = String.format("[BJM日本語変換] %s を辞書から削除しました。", args[1]);
parent.getProxy().broadcast(TextComponent.fromLegacyText(message));
return;
} else if ( args.length >= 1 && args[0].equalsIgnoreCase("view") ) {
HashMap<String, String> dictionary = LunaChat.getAPI().getAllDictionary();
for ( String key : dictionary.keySet() ) {
String value = dictionary.get(key);
sendMessage(sender, key + ChatColor.GRAY + " -> " + ChatColor.WHITE + value);
}
return;
} else {
sendMessage(sender, "日本語変換の辞書登録コマンド:");
sendMessage(sender, "/dic add (辞書登録する単語) (変換後の単語) - 新しい単語を登録する。");
sendMessage(sender, "/dic remove (辞書から削除する単語) - 単語を辞書から削除する。");
sendMessage(sender, "/dic view - 辞書に登録されている単語一覧を参照する。");
return;
}
}
/**
* 指定した対象にメッセージを送信する
* @param reciever 送信先
* @param message メッセージ
*/
private void sendMessage(CommandSender reciever, String message) {
if ( message == null ) return;
reciever.sendMessage(TextComponent.fromLegacyText(message));
}
}
+1 -3
ファイルの表示
@@ -64,11 +64,9 @@ public class LunaChatBungee extends Plugin implements PluginInterface {
getProxy().getPluginManager().registerCommand(this,
new LunaChatCommandImpl("lunachat", "", "lc", "ch"));
getProxy().getPluginManager().registerCommand(this,
new TellCommand(this, "tell", "", "msg", "message", "m", "w", "t"));
new TellCommand(this, "tell", "", "msg", "message", "m", "t"));
getProxy().getPluginManager().registerCommand(this,
new ReplyCommand(this, "reply", "", "r"));
getProxy().getPluginManager().registerCommand(this,
new DictionaryCommand(this, "dictionary", "", "dic"));
// リスナー登録
getProxy().getPluginManager().registerListener(this, new BungeeEventListener(this));
+4 -3
ファイルの表示
@@ -130,7 +130,7 @@ public class BukkitChannel extends Channel {
// Japanize変換タスクを作成する
boolean isIncludeSyncChat = true;
DelayedJapanizeConvertTask delayedTask = null;
ChannelChatJapanizeTask delayedTask = null;
JapanizeType japanizeType = (getJapanizeType() == null)
? config.getJapanizeType() : getJapanizeType();
@@ -150,7 +150,7 @@ public class BukkitChannel extends Channel {
}
// タスクを作成しておく
delayedTask = new DelayedJapanizeChannelChatTask(maskedMessage,
delayedTask = new ChannelChatJapanizeTask(maskedMessage,
japanizeType, this, player, jpFormat, messageFormat);
}
@@ -161,7 +161,8 @@ public class BukkitChannel extends Channel {
// 非同期実行タスクがある場合、追加で実行する
if ( delayedTask != null ) {
delayedTask.runTaskAsynchronously(LunaChatBukkit.getInstance());
Bukkit.getScheduler().runTaskAsynchronously(
LunaChatBukkit.getInstance(), delayedTask);
}
// NGワード発言者に、NGワードアクションを実行する
+2 -2
ファイルの表示
@@ -132,7 +132,7 @@ public class BungeeChannel extends Channel {
// Japanize変換タスクを作成する
boolean isIncludeSyncChat = true;
JapanizeChannelChatTask delayedTask = null;
ChannelChatJapanizeTask delayedTask = null;
JapanizeType japanizeType = (getJapanizeType() == null)
? config.getJapanizeType() : getJapanizeType();
@@ -152,7 +152,7 @@ public class BungeeChannel extends Channel {
}
// タスクを作成しておく
delayedTask = new JapanizeChannelChatTask(maskedMessage,
delayedTask = new ChannelChatJapanizeTask(maskedMessage,
japanizeType, this, player, jpFormat, messageFormat);
}
@@ -12,7 +12,7 @@ import com.github.ucchyocean.lc3.member.ChannelMember;
* Japanize2行表示のときに、変換結果を遅延してチャンネルに表示するためのタスク
* @author ucchy
*/
public class JapanizeChannelChatTask implements Runnable {
public class ChannelChatJapanizeTask implements Runnable {
private Channel channel;
private ChannelMember player;
@@ -29,7 +29,7 @@ public class JapanizeChannelChatTask implements Runnable {
* @param japanizeFormat 変換後に発言するときの、発言フォーマット
* @param lineFormat
*/
public JapanizeChannelChatTask(String org, JapanizeType type, Channel channel,
public ChannelChatJapanizeTask(String org, JapanizeType type, Channel channel,
ChannelMember player, String japanizeFormat, String lineFormat) {
task = new JapanizeConvertTask(org, type, japanizeFormat);
-50
ファイルの表示
@@ -1,50 +0,0 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.channel;
import com.github.ucchyocean.lc3.japanize.JapanizeType;
import com.github.ucchyocean.lc3.member.ChannelMember;
/**
* Japanize2行表示のときに、変換結果を遅延してチャンネルに表示するためのタスク
* @author ucchy
*/
public class DelayedJapanizeChannelChatTask extends DelayedJapanizeConvertTask {
private Channel channel;
private ChannelMember player;
private String lineFormat;
/**
* コンストラクタ
* @param org 変換前の文字列
* @param type 変換タイプ
* @param channel 変換後に発言する、発言先チャンネル
* @param player 発言したプレイヤー
* @param japanizeFormat 変換後に発言するときの、発言フォーマット
* @param lineFormat
*/
public DelayedJapanizeChannelChatTask(String org, JapanizeType type, Channel channel,
ChannelMember player, String japanizeFormat, String lineFormat) {
super(org, type, channel, player, japanizeFormat);
this.channel = channel;
this.player = player;
this.lineFormat = lineFormat;
}
/**
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
if ( runSync() ) {
// チャンネルへ送信
String name = (player != null) ? player.getDisplayName() : "";
channel.sendMessage(player, getResult(), lineFormat, true, name);
}
}
}
-176
ファイルの表示
@@ -1,176 +0,0 @@
/*
* @author ucchy
* @license LGPLv3
* @copyright Copyright ucchy 2020
*/
package com.github.ucchyocean.lc3.channel;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import com.github.ucchyocean.lc3.LunaChat;
import com.github.ucchyocean.lc3.Utility;
import com.github.ucchyocean.lc3.event.LunaChatPostJapanizeEvent;
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;
/**
* Japanize変換を実行して、実行後に発言を行うタスク
* @author ucchy
*/
public class DelayedJapanizeConvertTask extends BukkitRunnable {
private static final String REGEX_URL = "https?://[\\w/:%#\\$&\\?\\(\\)~\\.=\\+\\-]+";
private String org;
private JapanizeType type;
private Channel channel;
private ChannelMember player;
private String format;
private String result;
/**
* コンストラクタ
* @param org 変換前の文字列
* @param type 変換タイプ
* @param channel 変換後に発言する、発言先チャンネル
* @param player 発言したプレイヤー
* @param japanizeFormat 変換後に発言するときの、発言フォーマット
*/
public DelayedJapanizeConvertTask(String org, JapanizeType type, Channel channel,
ChannelMember player, String japanizeFormat) {
this.org = org;
this.type = type;
this.channel = channel;
this.player = player;
this.format = japanizeFormat;
}
@Override
public void run() {
runSync();
}
/**
* 同期処理で変換を行います。結果は getResult() で取得してください。
* @return 処理を実行したかどうか(イベントでキャンセルされた場合はfalseになります)
*/
public boolean runSync() {
// 変換対象外のキーワード
HashMap<String, String> keywordMap = new HashMap<String, String>();
ArrayList<String> keywords = new ArrayList<String>();
if ( LunaChat.getConfig().isJapanizeIgnorePlayerName() ) {
for ( Player player : Bukkit.getOnlinePlayers() ) {
keywords.add(player.getName());
}
}
HashMap<String, String> 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();
LunaChatPostJapanizeEvent event =
new LunaChatPostJapanizeEvent(channelName, player, org, japanized);
Bukkit.getPluginManager().callEvent(event);
if ( event.isCancelled() ) {
return false;
}
japanized = event.getJapanized();
// フォーマットする
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();
}
}