Well, thought this could be useful to most of you who don't like adding npc's manually through Json. Like making a shit load of these, might just take up most of your time if you do it that way so therefore why I created this! Yeah im sure this could be done better or improved, I did this quickly but it does work
Note:
You will have to make directions work yourself as it manually faces the npc south. Also create your own spawns! To do that, simply add a new line in the text file "spawns" using this format:
Code:
//----NpcId-----PosX----PosY----------------
spawn = 1 3223 3219
Here's the results;
Add this class in your npc package and name it NPCLoader
Code:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
*
* @author Pwnhub <Captain Pwn> [http://www.rune-server.org/]
*
*/
public class NPCLoader {
private static final String FILE_DIRECTORY = "./data/def/txt/";
private static int npcLimit = 50000;
private static NPCList npcList[] = new NPCList[npcLimit];
private static int areaSpawnAmount = 0;
/**
* Loads custom spawns via java file.
*/
public static void loadCustomSpawns() {
//spawnNPC(1, new Position(3222, 3220));
//spawnNPC(2, new Position(3222, 3220));
}
/**
* Handles the positions.
* @param x
* @param y
* @param z
* @return
*/
public static Position newPos(int x, int y, int z) {
return new Position(x, y, z);
}
/**
* Spawns the npc in the specified location.
* @param id
* @param x
* @param y
* @param z
* @param direction
* @param combatLvl
* @param name
*/
public static void spawnNPC(int id, Position pos) {
NPC npc = new NPC(id, pos);
World.register(npc);
npc.setEntityInteraction(npc);
npc.setDirection(Direction.NORTH);
}
/**
* Loads the spawns from the text file.
* @return
*/
@SuppressWarnings("resource")
public static boolean loadSpawns() {
loadCustomSpawns();
String line = "";
String str = "";
String str2 = "";
String str3 = "";
String[] str4 = new String[10];
boolean ending = false;
BufferedReader npcFile = null;
try {
npcFile = new BufferedReader(new FileReader(FILE_DIRECTORY+"spawns.txt"));
} catch (FileNotFoundException fileex) {
System.out.println(npcFile + " couldn't be located.");
return false;
}
try {
line = npcFile.readLine();
} catch (IOException ioexception) {
System.out.println(npcFile + " couldn't be read.");
return false;
}
while (ending == false && line != null) {
line = line.trim();
int space = line.indexOf("=");
if (space > -1) {
str = line.substring(0, space);
str = str.trim();
str2 = line.substring(space + 1);
str2 = str2.trim();
str3 = str2.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str4 = str3.split("\t");
if (str.equals("spawn")) {
spawnNPC(Integer.parseInt(str4[0]), new Position(Integer.parseInt(str4[1]), Integer.parseInt(str4[1])));
}
} else {
if (line.equals("[ENDOFSPAWNLIST]")) {
try {
npcFile.close();
} catch (IOException io) {
io.printStackTrace();
}
return true;
}
}
try {
line = npcFile.readLine();
} catch (IOException io) {
ending = true;
}
}
try {
npcFile.close();
} catch (IOException io) {
io.printStackTrace();
}
return false;
}
/**
* Loads the npc list from a text file.
* @return
*/
@SuppressWarnings("resource")
public boolean loadNPCList() {
String line = "";
String str = "";
String str2 = "";
String str3 = "";
String[] str4 = new String[10];
boolean ending = false;
BufferedReader npcFile = null;
try {
npcFile = new BufferedReader(new FileReader(FILE_DIRECTORY+"list.txt"));
} catch (FileNotFoundException fileex) {
System.out.println(npcFile + " couldn't be located.");
return false;
}
try {
line = npcFile.readLine();
} catch (IOException io) {
System.out.println(npcFile + " couldn't be read.");
return false;
}
while (ending == false && line != null) {
line = line.trim();
int spot = line.indexOf("=");
if (spot > -1) {
str = line.substring(0, spot);
str = str.trim();
str2 = line.substring(spot + 1);
str2 = str2.trim();
str3 = str3.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str3 = str3.replaceAll("\t\t", "\t");
str4 = str3.split("\t");
if (str.equals("npc")) {
createNewList(Integer.parseInt(str4[0]), str4[1],
Integer.parseInt(str4[3]), Integer.parseInt(str4[4]));
}
} else {
if (line.equals("[ENDOFNPCLIST]")) {
try {
npcFile.close();
} catch (IOException io) {
io.printStackTrace();
}
return true;
}
}
try {
line = npcFile.readLine();
} catch (IOException io) {
ending = true;
}
}
try {
npcFile.close();
} catch (IOException io) {
io.printStackTrace();
}
return false;
}
/**
* The class of the npc list.
*/
public class NPCList extends NPC {
public int npcId = id;
public String npcName = definition.name;
public int npcHp = defaultConstitution;
/**
* The main constructor of the list.
* @param _npcId
* @param position
*/
public NPCList(int _npcId, Position position) {
super(_npcId, position);
npcId = _npcId;
}
}
/**
* Creates a new npc list.
* @param npcId
* @param npcName
* @param constitution
*/
public void createNewList(int npcId, String npcName, int combatLvl, int constitution) {
Position defaultPos = null;
int slot = -1;
for (int i = 0; i < npcLimit; i++) {
if (npcList[i] == null) {
slot = i;
break;
}
}
if (slot == -1)
return;
npcList[slot] = new NPCList(npcId, defaultPos);
npcList[slot].npcName = npcName;
npcList[slot].npcHp = constitution;
npcList[slot].definition.setCombatLevel(combatLvl);
}
}
Go to your NPC class in the same package and above;
Add;
Code:
NPCLoader.loadSpawns();
Then download these and put them into the txt folder within the data folder a.k.a (data/txt) of your source. Make sure to read the notice above! The first npc has been spawned for you. Then you're done!
Before you go, if you dont want to just load the npcs from a file, you can load them from the class. Just uncomment the lines in this void;
Code:
/**
* Loads custom spawns via java file.
*/
public static void loadCustomSpawns() {
//spawnNPC(1, new Position(3222, 3220));
//spawnNPC(2, new Position(3222, 3220));
}
That's it, that's all. Enjoy