Loading NPC data through SQL
This is to go along with my autospawn tut. This will create a list that contains some NPC Information. Name, MaxHP, maxHit, etc.
Step 1: In your SQLHandler, add this:
Code:
public int loadNPCData()
{
int count = 0;
try{
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM npc");
while(rs.next()) {
count = rs.getRow();
int id = rs.getInt(1);
int maxhp = rs.getInt(2);
int maxhit = rs.getInt(3);
String name = rs.getString(4);
server.npcHandler.addToList(id, maxhp, maxhp, maxhit, name);
}
}catch(SQLException e){
e.printStackTrace();
}
return count;
}
Step 2: In NPCHandler, add this:
Code:
public static final int maxNPCs = 6500;
public RSNPC[] npcList = new RSNPC[maxNPCs];
public boolean addToList(int slot, int mHP, int cHP, int mHit, String name)
{
RSNPC npcL = new RSNPC();
npcL.maxHP = mHP;
npcL.currentHP = cHP;
npcL.maxHit = mHit;
npcL.name = name;
npcList[slot] = npcL;
return true;
}
Step 3: In RSNPC, add this:
Code:
public int maxHP = 0;
public int maxHit = 0;
public int currentHP = 0;
public String name = "name";
Step 4: In your server class, add this:
Under:
Code:
npcHandler = new NPCHandler();
Add: IT MUST GO BEFORE MY AUTOSPAWN ONE IF YOU'VE DONE THAT
Code:
System.out.println("[SQL] Loaded "+sqlHandler.loadNPCData()+" NPC's data.");
Step 5: Execute this query
Code:
-- phpMyAdmin SQL Dump
-- version 2.11.6
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jul 10, 2008 at 03:17 PM
-- Server version: 5.0.51
-- PHP Version: 5.2.6
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
--
-- Database: `459server`
--
-- --------------------------------------------------------
--
-- Table structure for table `npc`
--
CREATE TABLE `npc` (
`id` int(4) NOT NULL,
`maxhp` int(4) NOT NULL,
`maxhit` int(4) NOT NULL,
`name` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
--
-- Dumping data for table `npc`
--
INSERT INTO `npc` (`id`, `maxhp`, `maxhit`, `name`) VALUES
(50, 223, 20, 'King Black Dragon');
This doesn't have all NPC data, but it has a start. And the KBD values are off, but I just use them to test.
To get an NPCs name or something.
Code:
String name = server.npcHandler.npcList[<ID>].name;