首页 > 解决方案 > RandomAccessFile 不适用于 Minecraft Forge

问题描述

我目前正在为 Minecraft 开发一个 Mod,它有一个用 C++ 和 Qt5 编写的专用 Gui 系统。我让我的 GUI 和 Minecraft 通过命名管道进行通信,但我有一个小问题。我可以用一个简单的 Java 和 C++(Qt) 程序在管道中读写。但是当我在 Minecraft Forge 的初始化后创建我的 Pipeendpoint 类的新实例时,它无法从管道中读取任何内容。在独立系统中,它可以读取内容。

不工作 Forge 实施:

package de.CoderDE.CodersAnimationEditor;

import java.io.FileNotFoundException;

import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;
import net.minecraft.client.Minecraft;
import net.minecraftforge.fml.client.registry.ClientRegistry;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

@SideOnly(Side.CLIENT)
public class ClientProxy extends CommonProxy {
    static PipeEndpoint pendpoint;

    @Override
    public void preInit(FMLPreInitializationEvent e) {
        super.preInit(e);
    }

    @Override
    public void init(FMLInitializationEvent e) {
        super.init(e);
    }

    @Override
    public void postInit(FMLPostInitializationEvent e) {
        super.postInit(e);

        try {
            pendpoint = new PipeEndpoint();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
    }
}

工作独立实施:

import java.io.FileNotFoundException;

import de.CoderDE.CodersAnimationEditor.Pipe.PipeEndpoint;


public class Main {
    static PipeEndpoint pipe;

    public static void main(String[] args) {
        try {
            pipe = new PipeEndpoint();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

还有重要的 PipeEndpoint 类:

package de.CoderDE.CodersAnimationEditor.Pipe;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class PipeEndpoint {
    private Thread reciever;
    private RandomAccessFile pipe;

    public PipeEndpoint() throws FileNotFoundException {
        pipe = new RandomAccessFile("\\\\.\\pipe\\CodersAnimationEditor", "rw");
        reciever = new Thread(new PipeEndpointReciever());
        reciever.start();
    }

    private class PipeEndpointReciever implements Runnable {
        @Override
        public void run() {
            try {
                while (true) {
                    System.out.print((char)pipe.read());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

而“无法读取任何内容”我的意思是它永远不会从 pipe.read() 返回。哦,Java 应用程序在 C++(Qt) LocalServer 开始侦听并等待新连接之后启动。

标签: javanetworkingminecraftnamed-pipesminecraft-forge

解决方案


推荐阅读