我的代码有什么问题?“线程中的异常”;“主要”;java.lang.NumberFormatException:对于输入字符串:";npcId“;
线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“npcId” 当我试着运行它时,我就明白了 怎么了? 该类所做的大部分工作是读取NPC的未打包定义并将其打包到一个文件中,但它没有正确打包。(我使用的格式见下文) 班级:我的代码有什么问题?“线程中的异常”;“主要”;java.lang.NumberFormatException:对于输入字符串:";npcId“;,java,Java,线程“main”java.lang.NumberFormatException中的异常:对于输入字符串:“npcId” 当我试着运行它时,我就明白了 怎么了? 该类所做的大部分工作是读取NPC的未打包定义并将其打包到一个文件中,但它没有正确打包。(我使用的格式见下文) 班级: package com.rs.utils; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.File; imp
package com.rs.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import com.rs.game.npc.combat.NPCCombatDefinitions;
// Referenced classes of package com.rs.utils:
// Logger
public final class NPCCombatDefinitionsL
{
public static void init()
{
if((new File("data/npcs/packedCombatDefinitions.ncd")).exists())
loadPackedNPCCombatDefinitions();
else
loadUnpackedNPCCombatDefinitions();
}
public static NPCCombatDefinitions getNPCCombatDefinitions(int npcId)
{
NPCCombatDefinitions def = (NPCCombatDefinitions)npcCombatDefinitions.get(Integer.valueOf(npcId));
if(def == null)
return DEFAULT_DEFINITION;
else
return def;
}
private static void loadUnpackedNPCCombatDefinitions()
{
int count = 0;
Logger.log("NPCCombatDefinitionsL", "Packing npc combat definitions...");
try
{
DataOutputStream out = new DataOutputStream(new FileOutputStream("data/npcs/packedCombatDefinitions.ncd"));
BufferedReader in = new BufferedReader(new FileReader("data/npcs/unpackedCombatDefinitionsList.txt"));
do
{
String line = in.readLine();
count++;
if(line == null)
break;
if(!line.startsWith("//"))
{
String splitedLine[] = line.split(" - ", 2);
if(splitedLine.length != 2)
throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(count).append(", ").append(line).toString());
int npcId = Integer.parseInt(splitedLine[0]);
String splitedLine2[] = splitedLine[1].split(" ", 12);
if(splitedLine2.length != 12)
throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(count).append(", ").append(line).toString());
int hitpoints = Integer.parseInt(splitedLine2[0]);
int attackAnim = Integer.parseInt(splitedLine2[1]);
int defenceAnim = Integer.parseInt(splitedLine2[2]);
int deathAnim = Integer.parseInt(splitedLine2[3]);
int attackDelay = Integer.parseInt(splitedLine2[4]);
int deathDelay = Integer.parseInt(splitedLine2[5]);
int respawnDelay = Integer.parseInt(splitedLine2[6]);
int maxHit = Integer.parseInt(splitedLine2[7]);
int attackStyle;
if(splitedLine2[8].equalsIgnoreCase("MELEE"))
attackStyle = 0;
else
if(splitedLine2[8].equalsIgnoreCase("RANGE"))
attackStyle = 1;
else
if(splitedLine2[8].equalsIgnoreCase("MAGE"))
attackStyle = 2;
else
if(splitedLine2[8].equalsIgnoreCase("SPECIAL"))
attackStyle = 3;
else
if(splitedLine2[8].equalsIgnoreCase("SPECIAL2"))
attackStyle = 4;
else
throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(line).toString());
int attackGfx = Integer.parseInt(splitedLine2[9]);
int attackProjectile = Integer.parseInt(splitedLine2[10]);
int agressivenessType;
if(splitedLine2[11].equalsIgnoreCase("PASSIVE"))
agressivenessType = 0;
else
if(splitedLine2[11].equalsIgnoreCase("AGRESSIVE"))
agressivenessType = 1;
else
throw new RuntimeException((new StringBuilder("Invalid NPC Combat Definitions line: ")).append(line).toString());
out.writeShort(npcId);
out.writeShort(hitpoints);
out.writeShort(attackAnim);
out.writeShort(defenceAnim);
out.writeShort(deathAnim);
out.writeByte(attackDelay);
out.writeByte(deathDelay);
out.writeInt(respawnDelay);
out.writeShort(maxHit);
out.writeByte(attackStyle);
out.writeShort(attackGfx);
out.writeShort(attackProjectile);
out.writeByte(agressivenessType);
npcCombatDefinitions.put(Integer.valueOf(npcId), new NPCCombatDefinitions(hitpoints, attackAnim, defenceAnim, deathAnim, attackDelay, deathDelay, respawnDelay, maxHit, attackStyle, attackGfx, attackProjectile, agressivenessType));
}
} while(true);
in.close();
out.close();
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
private static void loadPackedNPCCombatDefinitions()
{
try
{
RandomAccessFile in = new RandomAccessFile("data/npcs/packedCombatDefinitions.ncd", "r");
FileChannel channel = in.getChannel();
int npcId;
int hitpoints;
int attackAnim;
int defenceAnim;
int deathAnim;
int attackDelay;
int deathDelay;
int respawnDelay;
int maxHit;
int attackStyle;
int attackGfx;
int attackProjectile;
int agressivenessType;
for(ByteBuffer buffer = channel.map(java.nio.channels.FileChannel.MapMode.READ_ONLY, 0L, channel.size()); buffer.hasRemaining(); npcCombatDefinitions.put(Integer.valueOf(npcId), new NPCCombatDefinitions(hitpoints, attackAnim, defenceAnim, deathAnim, attackDelay, deathDelay, respawnDelay, maxHit, attackStyle, attackGfx, attackProjectile, agressivenessType)))
{
npcId = buffer.getShort() & 0xffff;
hitpoints = buffer.getShort() & 0xffff;
attackAnim = buffer.getShort() & 0xffff;
defenceAnim = buffer.getShort() & 0xffff;
deathAnim = buffer.getShort() & 0xffff;
attackDelay = buffer.get() & 0xff;
deathDelay = buffer.get() & 0xff;
respawnDelay = buffer.getInt();
maxHit = buffer.getShort() & 0xffff;
attackStyle = buffer.get() & 0xff;
attackGfx = buffer.getShort() & 0xffff;
attackProjectile = buffer.getShort() & 0xffff;
agressivenessType = buffer.get() & 0xff;
}
channel.close();
in.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
private NPCCombatDefinitionsL()
{
}
private static final HashMap<Integer, NPCCombatDefinitions> npcCombatDefinitions = new HashMap<Integer, NPCCombatDefinitions>();
private static final NPCCombatDefinitions DEFAULT_DEFINITION = new NPCCombatDefinitions(1, -1, -1, -1, 5, 1, 1, 0, 0, -1, -1, 0);
@SuppressWarnings("unused")
private static final String PACKED_PATH = "data/npcs/packedCombatDefinitions.ncd";
}
package com.rs.utils;
导入java.io.BufferedReader;
导入java.io.DataOutputStream;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.RandomAccessFile;
导入java.nio.ByteBuffer;
导入java.nio.channels.FileChannel;
导入java.util.HashMap;
导入com.rs.game.npc.combat定义;
//包com.rs.utils的引用类:
//记录器
公共期末课程NPCCombatdDefinitionSL
{
公共静态void init()
{
如果((新文件(“data/npcs/packedCombatDefinitions.ncd”)).exists())
loadPackedNPCCombatDefinitions();
其他的
LoadUnpactedNPCCombatDefinitions();
}
公共静态NPCCombatDefinitions getNPCCombatDefinitions(int-npcId)
{
NPCCombatDefinitions def=(NPCCombatDefinitions)NPCCombatDefinitions.get(Integer.valueOf(npcId));
如果(def==null)
返回默认的_定义;
其他的
返回def;
}
私有静态void loadUnpactedNPCCombatdefinitions()
{
整数计数=0;
Logger.log(“NPCCombatDefinitionsL”,“打包npc战斗定义…”);
尝试
{
DataOutputStream out=新的DataOutputStream(新文件OutputStream(“data/npcs/packedCombatDefinitions.ncd”);
BufferedReader in=新的BufferedReader(新文件阅读器(“data/npcs/unpacketCombatDefinitionsList.txt”);
做
{
String line=in.readLine();
计数++;
如果(行==null)
打破
如果(!line.startsWith(“/”)
{
String splitedLine[]=line.split(“-”,2);
if(splitedLine.length!=2)
抛出新的RuntimeException((新的StringBuilder(“无效NPC战斗定义行:”).append(count).append(“,”).append(行).toString());
int npcId=Integer.parseInt(splitedLine[0]);
字符串splitedLine2[]=splitedLine[1]。拆分(“,12”);
如果(拆分线2.length!=12)
抛出新的RuntimeException((新的StringBuilder(“无效NPC战斗定义行:”).append(count).append(“,”).append(行).toString());
int hitpoints=Integer.parseInt(splitedLine2[0]);
int attackAnim=Integer.parseInt(splitedLine2[1]);
int defenceAnim=Integer.parseInt(splitedLine2[2]);
int deatanim=Integer.parseInt(splitedLine2[3]);
int attackDelay=Integer.parseInt(splitedLine2[4]);
int deathDelay=Integer.parseInt(splitedLine2[5]);
int respawnDelay=Integer.parseInt(splitedLine2[6]);
int maxHit=Integer.parseInt(splitedLine2[7]);
int攻击风格;
如果(分割线2[8]。相等信号(“近战”))
attackStyle=0;
其他的
if(splitedLine2[8].equalsIgnoreCase(“范围”))
attackStyle=1;
其他的
if(splitedLine2[8]。相等信号状态(“MAGE”))
attackStyle=2;
其他的
if(splitedLine2[8]。等效信号案例(“特殊”))
attackStyle=3;
其他的
if(splitedLine2[8]。等效信号案例(“SPECIAL2”))
attackStyle=4;
其他的
抛出新的RuntimeException((新的StringBuilder(“无效的NPC战斗定义行:)).append(行).toString());
int attackGfx=Integer.parseInt(splitedLine2[9]);
int attackspollect=Integer.parseInt(splitedLine2[10]);
int agressivenessType;
if(splitedLine2[11].equalsIgnoreCase(“被动”))
agressivenessType=0;
其他的
if(splitedLine2[11]。相等信号状态(“AGRESSIVE”))
agressivenessType=1;
其他的
抛出新的RuntimeException((新的StringBuilder(“无效的NPC战斗定义行:)).append(行).toString());
out.writeShort(npcId);
out.writeShort(生命值);
out.writeShort(attackAnim);
out.writeShort(defenceAnim);
out.writeShort(迪塔尼姆);
out.writeByte(攻击延迟);
out.writeByte(死亡延迟);
注销、注销(延期偿还);
out.writeShort(maxit);
out.writeByte(attackStyle);
out.writeShort(attackGfx);
out.writeShort(攻击投射物);
out.writeByte(agressivenessType);
npcCombatDefinitions.put(Integer.valueOf(npcId)、新的npcCombatDefinitions(生命点、攻击动画、防御动画、死亡动画、攻击延迟、死亡延迟、重生延迟、最大命中、攻击样式、攻击GFX、攻击投射物、攻击类型));
}
}虽然(正确);
in.close();
out.close();
}
catch(filenotfounde异常)
{
e、 printStackTrace();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
专用静态无效lo
int npcId = Integer.parseInt(splitedLine[1]);
int npcId = Integer.parseInt(splitedLine[0]);