0

我正在尝试制作一个 minecraft mod,但我无法弄清楚这个错误:

src\minecraft\net\minecraft\src\Packet2ClientProtocol.java:35: error: cannot fin
d symbol
                par1DataOutputStream.writeByte(this.protocolVersion);
                                               ^

symbol: variable protocolVersion
src\minecraft\net\minecraft\src\Packet2ClientProtocol.java:36: error: cannot fin
d symbol
        writeString(this.serverHost, par1DataOutputStream);
                    ^

symbol: variable serverHost
src\minecraft\net\minecraft\src\Packet2ClientProtocol.java:37: error: cannot fin
d symbol
        par1DataOutputStream.writeInt(this.serverPort);
                                      ^

symbol: variable serverPort
src\minecraft\net\minecraft\src\Packet2ClientProtocol.java:38: error: cannot fin
d symbol
            writeString(this.username, par1DataOutputStream);
                            ^

symbol: variable username
src\minecraft\net\minecraft\src\Packet2ClientProtocol.java:54: error: cannot fin
d symbol
        return 3 + 2 * this.username.length();
                       ^

symbol: variable username
src\minecraft\net\minecraft\src\Packet2ClientProtocol.java:62: error: cannot fin
d symbol
    return this.protocolVersion;
               ^

symbol: variable protocolVersion
src\minecraft\net\minecraft\src\Packet2ClientProtocol.java:70: error: cannot fin
d symbol
    return this.username;
               ^

这是我的 Packet2ClientProtocol.java:

package net.minecraft.src;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Packet2ClientProtocol extends Packet
{
    public Packet2ClientProtocol() {}

    public void Packet2ClientProtocol(int par1, String par2Str, int par3, String par4Str)
    {
        int protocolVersion = par1;
        String serverHost = par2Str;
        int serverPort = par3;
        String username = par4Str;
    }

    /**
    * Abstract. Reads the raw packet data from the data stream.
    */
    public void readPacketData(DataInputStream par1DataInputStream) throws IOException
    {
    int protocolVersion = par1DataInputStream.readByte();
        String serverHost = readString(par1DataInputStream, 255);
        int serverPort = par1DataInputStream.readInt();
    String username = Info.Username;
    }

    /**
    * Abstract. Writes the raw packet data to the data stream.
    */
    public void writePacketData(DataOutputStream par1DataOutputStream) throws IOException
    {
    par1DataOutputStream.writeByte(this.protocolVersion);
        writeString(this.serverHost, par1DataOutputStream);
        par1DataOutputStream.writeInt(this.serverPort);
    writeString(this.username, par1DataOutputStream);
    }

    /**
    * Passes this Packet on to the NetHandler for processing.
    */
    public void processPacket(NetHandler par1NetHandler)
    {
        par1NetHandler.handleClientProtocol(this);
    }

    /**
    * Abstract. Return the size of the packet (not counting the header).
    */
    public int getPacketSize()
    {
        return 3 + 2 * this.username.length();
    }

    /**
    * Returns the protocol version.
    */
    public int getProtocolVersion()
    {
        return this.protocolVersion;
    }

    /**
    * Returns the username.
    */
    public String getUsername()
    {
        return this.username;
    }
}

我在这方面做错了什么?我确定我拼写正确,是我忘记了什么吗?

4

2 回答 2

1

关于您尝试创建构造函数的几点:-

public void Packet2ClientProtocol(int par1, String par2Str, int par3, String par4Str)
{
    int protocolVersion = par1;
    String serverHost = par2Str;
    int serverPort = par3;
    String username = par4Str;
}
  1. 构造函数没有任何返回类型(void从那里删除)。

  2. 您尚未将参数值分配给实例变量。相反,您创建了一些局部变量,您当然无法在该构造函数之外访问它们。

所以,使用this.protocolVersion会给你错误,因为你没有声明任何具有该名称的实例变量。

您应该将变量声明为类中的字段。

于 2013-01-13T17:41:24.260 回答
1

你所有的变量:

int protocolVersion = par1;
    String serverHost = par2Str;
    int serverPort = par3;
String username = par4Str;

是构造函数的局部变量。使它们成为字段。

于 2013-01-13T17:41:41.253 回答