#問題
Minecraft のプラグインで半角英字をひらがなにして、それを漢字にするというプラグインがあります。
それを参考に同じようなものを作ったのですが、半角英字をひらがなにはできますが、漢字にする段階でうまく動作いたしません。
原因がわかる方はいますか?(変換部分はMinecraft とは互換性はないと思います。
#書いたコード
GoogleIME.java
Java
1package jp.dip.monsterlifeserver.mlsplugin.mlsplugin.API.honyaku; 2 3import com.google.common.io.CharStreams; 4import com.google.gson.Gson; 5import com.google.gson.JsonArray; 6import com.google.gson.JsonElement; 7import sun.net.www.protocol.http.HttpURLConnection; 8 9import java.io.BufferedReader; 10import java.io.IOException; 11import java.io.InputStreamReader; 12import java.net.MalformedURLException; 13import java.net.ProtocolException; 14import java.net.URL; 15import java.net.URLEncoder; 16 17public class GoogleIME { 18 19 private static final String GOOGLE_IME_URL = 20 "https://www.google.com/transliterate?langpair=ja-Hira|ja&text="; 21 22 public static String convByGoogleIME(String org) { 23 return conv(org); 24 } 25 26 // 変換の実行 27 private static String conv(String org) { 28 if ( org.length() == 0 ) { 29 return ""; 30 } 31 32 HttpURLConnection urlconn = null; 33 BufferedReader reader = null; 34 try { 35 String baseurl; 36 String encode = "UTF-8"; 37 baseurl = GOOGLE_IME_URL + URLEncoder.encode(org , "UTF-8"); 38 39 URL url = new URL(baseurl); 40 41 urlconn = (HttpURLConnection)url.openConnection(); 42 urlconn.setRequestMethod("GET"); 43 urlconn.setInstanceFollowRedirects(false); 44 urlconn.connect(); 45 46 reader = new BufferedReader( 47 new InputStreamReader(urlconn.getInputStream(), encode)); 48 49 String json = CharStreams.toString(reader); 50 String parsed = GoogleIME.parseJson(json); 51 52 return parsed; 53 54 } catch (MalformedURLException e) { 55 e.printStackTrace(); 56 } catch (ProtocolException e) { 57 e.printStackTrace(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } finally { 61 if ( urlconn != null ) { 62 urlconn.disconnect(); 63 } 64 if ( reader != null ) { 65 try { 66 reader.close(); 67 } catch (IOException e) { // do nothing. 68 } 69 } 70 } 71 72 return ""; 73 } 74 75 // デバッグ用エントリ 76 /** 77 * GoogleIMEの最初の変換候補を抽出して結合します 78 * 79 * @param json 変換元のJson形式の文字列 80 * @return 変換後の文字列 81 * @since 2.8.10 82 */ 83 public static String parseJson(String json) { 84 StringBuilder result = new StringBuilder(); 85 for (JsonElement response : new Gson().fromJson(json, JsonArray.class)) { 86 result.append(response.getAsJsonArray().get(1).getAsJsonArray().get(0).getAsString()); 87 } 88 return result.toString(); 89 } 90 91}
文字数の都合上YukiKanaConveter.javaは参考にしたGitHubのリンクとする(コピペなので中身はまったく同じです)
YukiKanaConveter.java
文字数の都合上Chat.javaはコードのコピペです。
Chat.java
参考にしたもの:
GitHub
#エラー
[10:53:09 ERROR]: Could not pass event AsyncPlayerChatEvent to MLSPlugin v1.0-SNAPSHOT org.bukkit.event.EventException: null at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:72) ~[patched_1.12.2.jar:git-Paper-1613] at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:74) ~[patched_1.12.2.jar:git-Paper-1613] at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[patched_1.12.2.jar:git-Paper-1613] at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:513) ~[patched_1.12.2.jar:git-Paper-1613] at net.minecraft.server.v1_12_R1.PlayerConnection.chat(PlayerConnection.java:1393) ~[patched_1.12.2.jar:git-Paper-1613] at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:1308) ~[patched_1.12.2.jar:git-Paper-1613] at net.minecraft.server.v1_12_R1.PacketPlayInChat$1.run(PacketPlayInChat.java:39) ~[patched_1.12.2.jar:git-Paper-1613] at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) ~[?:1.8.0_211] at java.util.concurrent.FutureTask.run(Unknown Source) ~[?:1.8.0_211] at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_211] at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_211] at java.lang.Thread.run(Unknown Source) [?:1.8.0_211] Caused by: java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to sun.net.www.protocol.http.HttpURLConnection at jp.dip.monsterlifeserver.mlsplugin.mlsplugin.API.honyaku.GoogleIME.conv(GoogleIME.java:41) ~[?:?] at jp.dip.monsterlifeserver.mlsplugin.mlsplugin.API.honyaku.GoogleIME.convByGoogleIME(GoogleIME.java:23) ~[?:?] at jp.dip.monsterlifeserver.mlsplugin.mlsplugin.Listener.Chat.onChat(Chat.java:56) ~[?:?] at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor9.execute(Unknown Source) ~[?:?] at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:70) ~[patched_1.12.2.jar:git-Paper-1613] ... 11 more [10:53:09 INFO]: <Monster2408> test
回答3件
あなたの回答
tips
プレビュー