5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ForgeMODとBukkitPluginの連携方法

Last updated at Posted at 2014-08-26

ForgeMODとBukkitPluginを連携させるにはPluginMessageを使う
例外は一部省いているので各自つけてください

ForgeMOD側の処理(1.7.2/1.7.10で確認済み)

チャンネル名をTestChannelで作り、送受信できるようにする

Forge側
    public static FMLEventChannel channel = NetworkRegistry.INSTANCE.newEventDrivenChannel("TestChannel");

    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {
        channel.register(this);
    }

    /**
     * クライアントからサーバーへパケットを送るときに呼ばれる
     */
    @SubscribeEvent
    public void onServerPacket(FMLNetworkEvent.ServerCustomPacketEvent event) {
        Packet packet = event.packet.toC17Packet();
        if (packet instanceof C17PacketCustomPayload){
            C17PacketCustomPayload pluginmessage = (C17PacketCustomPayload) packet;
            System.out.println("送信チャンネル: " + pluginmessage.func_149559_c());
            System.out.println("内容"+ new String(pluginmessage.func_149558_e(), "UTF-8"));
        }
    }



    /**
     * サーバーから受信したときに呼ばれる
     */
    @SubscribeEvent
    public void onClientPacket(FMLNetworkEvent.ClientCustomPacketEvent event) {
        Packet packet = event.packet.toC17Packet();
        if (packet instanceof C17PacketCustomPayload){
            C17PacketCustomPayload pluginmessage = (C17PacketCustomPayload) packet;
            System.out.println("受信チャンネル: " + pluginmessage.func_149559_c());
            System.out.println("内容"+ new String(pluginmessage.func_149558_e(), "UTF-8"));
        }
    }
プラグインメッセージの送り方
		try {
			byte[] message = str.getBytes("UTF-8");
			ByteBuf data = Unpooled.wrappedBuffer(message);
			C17PacketCustomPayload packet = new C17PacketCustomPayload("TestChannel", data);
			FMLProxyPacket pkt = new FMLProxyPacket(packet);
			channel.sendToServer(pkt);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

Bukkit側の処理

プラグインメッセージを送受信するために、PluginMessageListenerの実装と、
onEnableに以下のように記述します

onEnable
	@Override
	public void onEnable(){
		Bukkit.getMessenger().registerOutgoingPluginChannel(this, "TestChannel");
		Bukkit.getMessenger().registerIncomingPluginChannel(this, "TestChannel", this);
	}

	@Override
	public void onPluginMessageReceived(String channel, Player player, byte[] message) {
		System.out.println("チャンネル名:"+channel);
		System.out.println("プレイヤー名:"+player.getName());
		try {
			System.out.println("内容:"+new String(message, "UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
プラグインメッセージの送り方
player.sendPluginMessage(this, "TestChannel", new String("テスト").getBytes("UTF-8"));

参考にしたもの

http://wiki.vg/Protocol#Plugin_Message
http://wiki.vg/Plugin_channel

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?