我正试图通过xml为游戏加载我的npc drop。我已经开始加载过程但是我遇到了一些问题。首先,我只能在每个npc下降1次,而不会创建另一个具有相同npc id的'newNpc',否则会出现加载错误。我想每个npc有多滴。另外,我需要帮助阅读并使用稀有的定义。例: if(Math.random(100)> 95){ //在此处删除VERY_RARE项目 |
我的XStreamUtil类(加载xml文件):
package server.engine.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import server.game.entity.npc.impl.drops.NpcDropDefinition;
import server.game.entity.npc.impl.drops.NpcDropDefinition.LoadDropDefinitions;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.reflection.Sun14ReflectionProvider;
public class XStreamUtil {
private static XStreamUtil instance = new XStreamUtil();
private static XStream xStream = new XStream(new Sun14ReflectionProvider());
public static XStreamUtil getInstance() {
return instance;
}
public static XStream getxStream() {
return xStream;
}
static {
xStream.alias("newNpc", NpcDropDefinition.class);
}
public static void loadAllFiles() throws IOException {
LoadDropDefinitions.load();
}
public static void writeXML(Object object, File file) throws IOException {
FileOutputStream out = new FileOutputStream(file);
try {
xStream.toXML(object, out);
out.flush();
} finally {
out.close();
}
}
}
我的Xml定义类:
package server.game.entity.npc.impl.drops;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;
import server.engine.util.XStreamUtil;
/**
* NpcDropDefinition.java
*
* @author Notorious
*/
public class NpcDropDefinition {
private int npcId, itemId, itemAmount;
private DropRarity dropRarity;
enum DropRarity {
CONSTANT,
COMMON,
UNCOMMON,
RARE,
VERY_RARE;
}
/**
* @return the npcId
*/
public int getNpcId() {
return npcId;
}
/**
* @return the itemId
*/
public int getItemId() {
return itemId;
}
/**
* @return the itemAmount
*/
public int getItemAmount() {
return itemAmount;
}
public DropRarity getDropRarity() {
return dropRarity;
}
public static class LoadDropDefinitions {
/**
* Holds all of the definitions for prayer
*/
private static NpcDropDefinition[] def = null;
public static NpcDropDefinition[] getDefinitions() {
return def;
}
/**
* Loads the definitions of pvm points from the XML file
*
* @throws FileNotFoundException
*/
public static void load() throws FileNotFoundException {
@SuppressWarnings("unchecked")
List<NpcDropDefinition> XMLlist = (List<NpcDropDefinition>) XStreamUtil
.getxStream().fromXML(
new FileInputStream("./data/xml/npcDrops.xml"));
def = new NpcDropDefinition[XMLlist.size()];
for (int i = 0; i < def.length; i++) {
def[i] = XMLlist.get(i);
}
if (def.length > 1 || def.length < 1)
System.out.println("[Npc-Drops]: " + def.length
+ " npc drops have been loaded..");
else
System.out
.println("[Npc-Drops]: Only one npc drop has been loaded..");
}
}
}
My Drop Handler类:
package server.game.entity.npc.impl.drops;
import server.Server;
import server.game.entity.Client;
import server.game.entity.npc.NpcHandler;
import server.game.entity.player.PlayerHandler;
/**
* NpcDropHandler.java
*
* @author Notorious
*/
public class NpcDropHandler {
public void dropItems(int i) {
Client c = (Client) PlayerHandler.players[NpcHandler.npcs[i]
.getKillerIndex()];
if (c != null) {
Server.itemHandler.createGroundItem(c, 1, NpcHandler.npcs[i].absX,
NpcHandler.npcs[i].absY, 1, c.playerId);
}
}
}
My Current Xml文件:
<list>
<newNpc>
<npcId>1265</npcId>
<itemId>995</itemId>
<itemAmount>10</itemAmount>
<dropRarity>COMMON</dropRarity>
</newNpc>
</list>