0

我正在尝试通过 xml 为游戏加载我的 npc 掉落物。我已经开始加载过程,但遇到了一些问题。首先,我每个 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..");
        }
    }
}

我的 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);
        }
    }
}

我当前的 Xml 文件:

<list>
    <newNpc>
        <npcId>1265</npcId>
        <itemId>995</itemId>
        <itemAmount>10</itemAmount>
        <dropRarity>COMMON</dropRarity>
    </newNpc>
</list>
4

0 回答 0