Jump to content

NPC Data Retriever [No External Libs Required]


BravoTaco

Recommended Posts

This will grab NPC information from OSRS Box using the NPC id.

Class:

Quote

import javax.net.ssl.HttpsURLConnection;
import java.io.*;
import java.net.URL;
import java.util.HashMap;

public final class NPCDataRetriever {

    // This is used to cache the NPC data you have already retrieved to
    // avoid re-downloading the information.
    private static HashMap<Integer, NPCData> npcData = new HashMap<>();

    private NPCDataRetriever() {
    }

    /**
     * Retrieves the BufferedReader from the specified URL.
     * @param url The URL of the WebPage.
     * @return BufferedReader
     * @throws IOException
     */
    private static BufferedReader getBufferedReader(String url) throws IOException {
        URL temp = new URL(url);
        HttpsURLConnection connection = (HttpsURLConnection) temp.openConnection();
        InputStream inputStream = connection.getInputStream();
        return new BufferedReader(new InputStreamReader(inputStream));
    }

    /**
     * Retrieves the data from the BufferedReader and puts it into a StringBuilder.
     * @param bufferedReader BufferedReader from a WebPage.
     * @return StringBuilder
     * @throws IOException
     */
    private static StringBuilder getData(BufferedReader bufferedReader) throws IOException {
        String data;
        StringBuilder sb = new StringBuilder();

        while ((data = bufferedReader.readLine()) != null)
            sb.append(data);

        return sb;
    }

    /**
     * Retrieves the NPC data from 'https://www.osrsbox.com/projects/osrsbox-db/'.
     * @param npcID The ID of the NPC.
     * @return NPCData
     * @throws IOException
     */
    public static NPCData getNPCData(int npcID) throws IOException {
        if (npcData.containsKey(npcID))
            return npcData.get(npcID);
        NPCData temp = new NPCData(getData(getBufferedReader(String.format("https://www.osrsbox.com/osrsbox-db/monsters-json/%s.json", npcID))));
        npcData.put(npcID, temp);
        return temp;
    }

    public static class NPCData implements Serializable {
        private static long serialVersionUID = 1L;

        private StringBuilder rawData;

        private int id, combatLevel, tileSize, hitPoints, maxHit,
                attackSpeed, slayerLevel, attackLevel,
                strengthLevel, defenceLevel, magicLevel, rangeLevel;

        private double slayerXP;

        private String name, releaseDate, examineText;

        private boolean isCompleteData, isMembers, isAggressive,
                isPoisonous, isImmuneToPoison, isImmuneToVenom, isSlayerMonster;

        private NPCData(StringBuilder rawData) {
            this.rawData = rawData;

            // Int data
            id = parseIntData(Identifiers.ID.identifier);
            combatLevel = parseIntData(Identifiers.COMBAT_LEVEL.identifier);
            tileSize = parseIntData(Identifiers.SIZE.identifier);
            hitPoints = parseIntData(Identifiers.HITPOINTS.identifier);
            maxHit = parseIntData(Identifiers.MAX_HIT.identifier);
            attackSpeed = parseIntData(Identifiers.ATTACK_SPEED.identifier);
            slayerLevel = parseIntData(Identifiers.SLAYER_LEVEL.identifier);
            attackLevel = parseIntData(Identifiers.ATTACK_LEVEL.identifier);
            strengthLevel = parseIntData(Identifiers.STRENGTH_LEVEL.identifier);
            defenceLevel = parseIntData(Identifiers.DEFENCE_LEVEL.identifier);
            magicLevel = parseIntData(Identifiers.MAGIC_LEVEL.identifier);
            rangeLevel = parseIntData(Identifiers.RANGED_LEVEL.identifier);

            // Double data
            slayerXP = parseDoubleData(Identifiers.SLAYER_XP.identifier);

            // Boolean data
            isCompleteData = parseBooleanData(Identifiers.INCOMPLETE.identifier);
            isMembers = parseBooleanData(Identifiers.MEMBERS.identifier);
            isAggressive = parseBooleanData(Identifiers.AGGRESSIVE.identifier);
            isPoisonous = parseBooleanData(Identifiers.POISONOUS.identifier);
            isImmuneToPoison = parseBooleanData(Identifiers.IMMUNE_POISON.identifier);
            isImmuneToVenom = parseBooleanData(Identifiers.IMMUNE_VENOM.identifier);
            isSlayerMonster = parseBooleanData(Identifiers.SLAYER_MONSTER.identifier);

            // String data
            name = parseStringData(Identifiers.NAME.identifier);
            releaseDate = parseStringData(Identifiers.RELEASE_DATE.identifier);
            examineText = parseStringData(Identifiers.EXAMINE.identifier);
        }

        /**
         * Parses the rawData for an Integer value using an identifier.
         * @param identifier Identifier of the data phrase.
         * @return int
         */
        private int parseIntData(String identifier) {
            String string = rawData.toString().substring(rawData.indexOf(identifier), rawData.indexOf(",", rawData.indexOf(identifier)));
            string = string.substring(string.indexOf(" ")).replaceAll(" ", "");
            try {
                if (!string.equalsIgnoreCase("null")) {
                    int parsedInt = Integer.parseInt(string);
                    return parsedInt;
                } else {
                    return -1;
                }
            } catch (NumberFormatException ne) {
                ne.printStackTrace();
            }
            return -1;
        }

        /**
         * Parses the rawData for an Double value using an identifier.
         * @param identifier Identifier of the data phrase.
         * @return double
         */
        private double parseDoubleData(String identifier) {
            String string = rawData.toString().substring(rawData.indexOf(identifier), rawData.indexOf(",", rawData.indexOf(identifier)));
            string = string.substring(string.indexOf(" ")).replaceAll(" ", "");
            try {
                if (!string.equalsIgnoreCase("null")) {
                    double parseDouble = Double.parseDouble(string);
                    return parseDouble;
                } else {
                    return -1;
                }
            } catch (NumberFormatException ne) {
                ne.printStackTrace();
            }
            return -1;
        }

        /**
         * Parses the rawData for an String value using an identifier.
         * @param identifier Identifier of the data phrase.
         * @return String
         */
        private String parseStringData(String identifier) {
            String string = rawData.toString().substring(rawData.indexOf(identifier),
                    rawData.indexOf(",", rawData.indexOf(identifier)));

            string = string.substring(string.indexOf(" ")).replaceFirst(" ", "").
                    replaceAll("\"", "");

            return string;
        }

        /**
         * Parses the rawData for an Boolean value using an identifier.
         * @param identifier Identifier of the data phrase.
         * @return boolean
         */
        private boolean parseBooleanData(String identifier) {
            String string = rawData.toString().substring(rawData.indexOf(identifier), rawData.indexOf(",", rawData.indexOf(identifier)));
            string = string.substring(string.indexOf(" ")).replaceAll(" ", "");
            return string.equalsIgnoreCase("true");
        }

        @Override
        public String toString() {
            return "id=" + id +
                    ", combatLevel=" + combatLevel +
                    ", tileSize=" + tileSize +
                    ", hitPoints=" + hitPoints +
                    ", maxHit=" + maxHit +
                    ", attackSpeed=" + attackSpeed +
                    ", slayerLevel=" + slayerLevel +
                    ", slayerXP=" + slayerXP +
                    ", attackLevel=" + attackLevel +
                    ", strengthLevel=" + strengthLevel +
                    ", defenceLevel=" + defenceLevel +
                    ", magicLevel=" + magicLevel +
                    ", rangeLevel=" + rangeLevel +
                    ", name='" + name + '\'' +
                    ", releaseDate='" + releaseDate + '\'' +
                    ", examineText='" + examineText + '\'' +
                    ", isCompleteData=" + isCompleteData +
                    ", isMembers=" + isMembers +
                    ", isAggressive=" + isAggressive +
                    ", isPoisonous=" + isPoisonous +
                    ", isImmuneToPoison=" + isImmuneToPoison +
                    ", isImmuneToVenom=" + isImmuneToVenom +
                    ", isSlayerMonster=" + isSlayerMonster;
        }

        public String getName() {
            return name;
        }

        public String getReleaseDate() {
            return releaseDate;
        }

        public String getExamineText() {
            return examineText;
        }

        public double getSlayerXP() {
            return slayerXP;
        }

        public int getAttackLevel() {
            return attackLevel;
        }

        public int getAttackSpeed() {
            return attackSpeed;
        }

        public int getCombatLevel() {
            return combatLevel;
        }

        public int getDefenceLevel() {
            return defenceLevel;
        }

        public int getHitPoints() {
            return hitPoints;
        }

        public int getId() {
            return id;
        }

        public int getMagicLevel() {
            return magicLevel;
        }

        public int getMaxHit() {
            return maxHit;
        }

        public int getRangeLevel() {
            return rangeLevel;
        }

        public int getSlayerLevel() {
            return slayerLevel;
        }

        public int getStrengthLevel() {
            return strengthLevel;
        }

        public int getTileSize() {
            return tileSize;
        }

        public boolean isAggressive() {
            return isAggressive;
        }

        public boolean isCompleteData() {
            return isCompleteData;
        }

        public boolean isImmuneToPoison() {
            return isImmuneToPoison;
        }

        public boolean isImmuneToVenom() {
            return isImmuneToVenom;
        }

        public boolean isMembers() {
            return isMembers;
        }

        public boolean isPoisonous() {
            return isPoisonous;
        }

        public boolean isSlayerMonster() {
            return isSlayerMonster;
        }

        enum Identifiers {

            // Strings Start Here
            NAME,
            RELEASE_DATE,
            EXAMINE,

            // Integers Start Here
            ID,
            COMBAT_LEVEL,
            SIZE,
            HITPOINTS,
            MAX_HIT,
            ATTACK_SPEED,
            SLAYER_LEVEL,
            SLAYER_XP,
            ATTACK_LEVEL,
            STRENGTH_LEVEL,
            DEFENCE_LEVEL,
            MAGIC_LEVEL,
            RANGED_LEVEL,

            // Booleans Start Here
            INCOMPLETE,
            MEMBERS,
            AGGRESSIVE,
            POISONOUS,
            IMMUNE_POISON,
            IMMUNE_VENOM,
            SLAYER_MONSTER;

            String identifier;

            Identifiers() {
                this.identifier = this.name().toLowerCase();
            }
        }
    }
}

Usage:

Quote

@ScriptManifest(name = "@Test", author = "BravoTaco", version = 0, info = "", logo = "")
public class Main extends Script {

    NPCDataRetriever.NPCData wolfData;

    @Override
    public void onStart() throws InterruptedException {
        // Set the wolf data.
        try {
            wolfData = NPCDataRetriever.getNPCData(117);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (wolfData != null) {
            // Log some information about the wolf.
            log(wolfData.getHitPoints());
            log(wolfData.isAggressive());
            log(wolfData.isSlayerMonster());
            log(wolfData.getSlayerXP());
        }
    }

    @Override
    public int onLoop() throws InterruptedException {

        return random(800, 3000);
    }

    @Override
    public void onExit() throws InterruptedException {

    }

    @Override
    public void onPaint(Graphics2D g) {

    }

}

 

Edited by BravoTaco
  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...