Base Used : DarkScape [RichScape]
Difficulty : 2/10
Required Knowledge : Basic java terms, C&P, and a knowledge of your Server.java, and NPCHandler.java
Modified Classes : Server.java, NPCHandler.java
New Classes : Drop.java, DropHandler.java
A NPC drop system which uses Array Lists and a more compact drop method.
Why did I do this? There are a few ways of doing drops (that I know of)
-Loading them into an array from a cfg, using numbers as percents.
-Using int arrays for the item ID, and then using the array size for the chance
What was wrong? First method, I experienced exceptions when I added too many drops. Second, if you wanted an item to have a lower chance of dropping, you basically added a bunch of -1's or 'junk' drops to make it harder to pick the real drop, not very practical.
What does this do? It lets you add drops through a java file, have as many as you want, and not have to worry about messing with a CFG format, or even load a CFG at all! This also gives you the ability to have ANY monster drop a specific item. Perfect for making all monsters have a small chance of dropping something rare like a half shield or something.
You may have a method in your NPCHandler called "MonsterDropItems(int NPCID, NPC npc)"
If so, continue. If not, you may need to adjust for this tutorial.
Replace that, with this.
Code:
public void MonsterDropItems(int NPCID, NPC npc) {
try {
ArrayList<Drop> DropList = DropHandler.getDrop(NPCID);
if (DropList.size() == 0) { return; }
for (int i = 0; i < DropList.size(); i++) {
double chance = Math.random() * 100;
if (chance <= DropList.get(i).chance) {
int randomAmount = Misc.random(DropList.get(i).dropAmount);
if (randomAmount <= 0) { randomAmount = 1; }
Item.addItem(DropList.get(i).dropID, npc.absX, npc.absY, randomAmount, npc.getKiller(), false);
}
}
} catch (Exception e) {
System.out.println("Exception at drops");
e.printStackTrace();
}
}
Also, at the TOP of your NPCHandler, you should see something like
Below that, add this.
Code:
import java.util.ArrayList;
Next, add a new class called "Drop.java" and inside it, paste the following
Code:
/**
*
* @author andrew (blood argon)
*/
public class Drop {
public int npcID;
public int dropID;
public int dropAmount;
public int chance;
public Drop(int npc, int id, int amt, int c) {
npcID = npc;
dropID = id;
dropAmount = amt;
chance = c;
}
}
Next, make another class called "DropHandler,java", and in it, paste this.
Code:
import java.util.ArrayList;
/**
*
* @author andrew (Blood argon)
*/
public class DropHandler {
/* ArrayList of the Drop class
*/
private static ArrayList<Drop> drops = new ArrayList<Drop>();
private final int ANY = -1;
/* Sets up initial drops
* Usage : drops.add(new Drop(new int[] { npcIDs, npcIDs, ...} ID, Item ID, ItemAmount, Chance of Drop));
* NPCID can be changed to "ANY" to have all monsters drop this item.
*/
public void setDrops() {
/* Group : Men */
newDrop(new int[] {1,2}, 995, 500, 20);
newDrop(new int[] {1,2}, 526, 1, 100);
System.out.println("[DropHandler] Loaded " + drops.size() + " drops");
}
public void newDrop(int npc[], int item, int amount, int chance) {
for (int i = 0; i < npc.length; i++) {
drops.add(new Drop(npc[i], item, amount, chance));
}
}
/* Returns an array of possible drops based off the NPC id
*/
public static ArrayList getDrop(int npc) {
ArrayList<Drop> npcDrop = new ArrayList<Drop>();
for (int i = 0; i < drops.size(); i++) {
if (drops.get(i).npcID == npc || drops.get(i).npcID == -1) {
npcDrop.add(drops.get(i));
}
}
return npcDrop;
}
}
Finally, go into your Server.java, and declare the variable below.
Code:
public static DropHandler DropHandler = null;
And then where you see things like this
Code:
Dialogue = new Dialogue();
Frame = new Frame();
Special = new Special();
Text = new Text();
NpcManager = new NPCManager();
item = new Item();
shop = new Shop();
Prayer = new Prayer();
Add the following
Code:
DropHandler = new DropHandler();
DropHandler.setDrops();
And your done!
Your drops are set in the DropHandler, under the setUp() void. Iv included some examples on how the system works. If you have any questions, feedback, or design criticism, post here.
And for anyone who thinks its leeched because of the @author andrew, here is your picture.
IDE uses the user account as the author name.