Post: Custom Drop System
11-20-2015, 02:22 AM #1
Stunz
Former Staff
(adsbygoogle = window.adsbygoogle || []).push({});
Very easy guide to follow, very useful for PvM based servers, enjoy. Any problems feel free to post here or send me a PM, - Stunz.

create RealDrop.java
    package server.model.npcs;

/*Author Scott*/

public class RealDrop {

public int item;
public int amount;

public RealDrop(int item, int amount) {
this.item = item;
this.amount = amount;
}
}


NPCDrops.java (replace all)
    package server.model.npcs;

import java.util.*;
import java.io.*;
import server.model.npcs.RealDrop;
import server.util.Misc;

/*Author Scott*/

public class NPCDrops {

public static List<NPCD> npcd = new ArrayList<NPCD>();

private NPCDrops() {

}

public static List<RealDrop> roll(int id, boolean ring) {
List<RealDrop> list = new ArrayList<RealDrop>();
for(NPCD n : npcd) {
if(n.npcId == id) {
for(Drop d : n.drops) {
if(ring) {
double rolled = Misc.randomDouble(0, 100);
if(d.percentage <= 5)
rolled /= 1.5;
if(d.percentage > 5 && d.percentage <= 10)
rolled /= 1.75;
if(d.percentage > 10)
rolled /= 2;
if(d.itemId == 995)
d.amount *= 1.5;
if(rolled <= d.percentage) {
list.add(new RealDrop(d.itemId, d.amount));
}
} else {
if(Misc.randomDouble(0, 100) <= d.percentage) {
list.add(new RealDrop(d.itemId, d.amount));
}
}
}
return list;
}
}
return null;
}

public static void loadDrops() {
try {
npcd = new ArrayList<NPCD>();
Scanner scan = new Scanner(new File("./Data/cfg/NPCDrops.RBO"));
scan.nextLine(); //[Format ... ] useless
String s = scan.nextLine();
while(!s.equalsIgnoreCase("[EndOfFile]")) {
if(s.startsWith("#")) {
List<Awesome facerop> list = new ArrayList<Awesome facerop>();
int npcId = Integer.parseInt(s.substring(1, s.indexOf(" -")));
s = scan.nextLine();
while(!s.startsWith("#") && !s.equalsIgnoreCase("[EndOfFile]")) {
list.add(readDrop(s));
s = scan.nextLine();
}
npcd.add(new NPCD(npcId, list));
}
}
scan.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static Drop readDrop(String s) {
String[] args = s.split(":");
return new Drop(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Double.parseDouble(args[2]));
}
}

class NPCD {

public int npcId;
public List<Awesome facerop> drops;

public NPCD(int npcId, List<Awesome facerop> list) {
this.npcId = npcId;
drops = new ArrayList<Awesome facerop>();
for(Drop d : list) //avoiding weird referencing stuff
drops.add(d);
}
}

class Drop {

public int itemId;
public int amount;
public double percentage;

public Drop(int a, int b, double c) { //bein' lazy
this.itemId = a;
this.amount = b;
this.percentage = c;
}

}


NPCHandler.java (replace dropItem() and add the bone thing)
    	public void dropItems(int i) {
long start = System.currentTimeMillis();
Client c = (Client)Server.playerHandler.players[npcs[i].killedBy];
if(c != null) {
boolean wealthring = false;
if(c.playerEquipment[c.playerRing] == 2572)
wealthring = true;
List<RealDrop> drops = NPCDrops.roll(npcs[i].npcType, wealthring);
if(drops != null) {
for(RealDrop r : drops) {
Server.itemHandler.createGroundItem(c, r.item, npcs[i].absX, npcs[i].absY, r.amount, c.playerId);
}
}
}
System.out.println("Took: " + (System.currentTimeMillis() - start));
}


If you want to be able to reload without restarting server, add this in Commands.java
    import server.model.npcs.NPCDrops;

and
    			if (playerCommand.startsWith("reloaddrops")) {
NPCDrops.loadDrops();
c.sendMessage("Drops reloaded.");
}


In Server.java, add this in main()
    import server.model.npcs.NPCDrops;

    		NPCDrops.loadDrops();


In Data\cfg (where item.cfg and stuff are), add this file and name it NPCDrops.RBO (I had a big ego that day and named the file after myself. Feel free to rename lol.) Just because it's .RBO doesn't mean you can't modify it; just open it in Notepad or Notepad++ or Eclipse or something.

    [Format: [item id]:[amount]:[percentage (out of 100)]. 995:1:100 means drop 1 gold with a 100% chance.]
#1 - Man
995:100:100
#7 - Farmer
995:10000:100
14484:1:20
#8 - Thief
995:5000:50
15486:1:12
[EndOfFile]


How to use:
#[id] indicates a new NPC ID. If you want to add drops to an NPC, just do something like #1 - Man. You need to have at least "#1 -", you don't need to name the NPC.
After that, on separate lines, do this format: itemid:amountToDrop:chanceOfDrop (out of 100)

995:5000:50 - Drops 5000 gold with a 50% chance
14484:1:20 - Drops 1 Dragon Claws with a 20% chance
14484:10:.001 - Drops 10 Dragon Claws with a 0.001% chance

The following user thanked Stunz for this useful post:

Kam
11-20-2015, 02:29 AM #2
Kam
Investor - Future Millionaire
Originally posted by Stunz View Post
Very easy guide to follow, very useful for PvM based servers, enjoy. Any problems feel free to post here or send me a PM, - Stunz.

create RealDrop.java
    package server.model.npcs;

/*Author Scott*/

public class RealDrop {

public int item;
public int amount;

public RealDrop(int item, int amount) {
this.item = item;
this.amount = amount;
}
}


NPCDrops.java (replace all)
    package server.model.npcs;

import java.util.*;
import java.io.*;
import server.model.npcs.RealDrop;
import server.util.Misc;

/*Author Scott*/

public class NPCDrops {

public static List<NPCD> npcd = new ArrayList<NPCD>();

private NPCDrops() {

}

public static List<RealDrop> roll(int id, boolean ring) {
List<RealDrop> list = new ArrayList<RealDrop>();
for(NPCD n : npcd) {
if(n.npcId == id) {
for(Drop d : n.drops) {
if(ring) {
double rolled = Misc.randomDouble(0, 100);
if(d.percentage <= 5)
rolled /= 1.5;
if(d.percentage > 5 && d.percentage <= 10)
rolled /= 1.75;
if(d.percentage > 10)
rolled /= 2;
if(d.itemId == 995)
d.amount *= 1.5;
if(rolled <= d.percentage) {
list.add(new RealDrop(d.itemId, d.amount));
}
} else {
if(Misc.randomDouble(0, 100) <= d.percentage) {
list.add(new RealDrop(d.itemId, d.amount));
}
}
}
return list;
}
}
return null;
}

public static void loadDrops() {
try {
npcd = new ArrayList<NPCD>();
Scanner scan = new Scanner(new File("./Data/cfg/NPCDrops.RBO"));
scan.nextLine(); //[Format ... ] useless
String s = scan.nextLine();
while(!s.equalsIgnoreCase("[EndOfFile]")) {
if(s.startsWith("#")) {
List<Awesome facerop> list = new ArrayList<Awesome facerop>();
int npcId = Integer.parseInt(s.substring(1, s.indexOf(" -")));
s = scan.nextLine();
while(!s.startsWith("#") && !s.equalsIgnoreCase("[EndOfFile]")) {
list.add(readDrop(s));
s = scan.nextLine();
}
npcd.add(new NPCD(npcId, list));
}
}
scan.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public static Drop readDrop(String s) {
String[] args = s.split(":");
return new Drop(Integer.parseInt(args[0]), Integer.parseInt(args[1]), Double.parseDouble(args[2]));
}
}

class NPCD {

public int npcId;
public List<Awesome facerop> drops;

public NPCD(int npcId, List<Awesome facerop> list) {
this.npcId = npcId;
drops = new ArrayList<Awesome facerop>();
for(Drop d : list) //avoiding weird referencing stuff
drops.add(d);
}
}

class Drop {

public int itemId;
public int amount;
public double percentage;

public Drop(int a, int b, double c) { //bein' lazy
this.itemId = a;
this.amount = b;
this.percentage = c;
}

}


NPCHandler.java (replace dropItem() and add the bone thing)
    	public void dropItems(int i) {
long start = System.currentTimeMillis();
Client c = (Client)Server.playerHandler.players[npcs[i].killedBy];
if(c != null) {
boolean wealthring = false;
if(c.playerEquipment[c.playerRing] == 2572)
wealthring = true;
List<RealDrop> drops = NPCDrops.roll(npcs[i].npcType, wealthring);
if(drops != null) {
for(RealDrop r : drops) {
Server.itemHandler.createGroundItem(c, r.item, npcs[i].absX, npcs[i].absY, r.amount, c.playerId);
}
}
}
System.out.println("Took: " + (System.currentTimeMillis() - start));
}


If you want to be able to reload without restarting server, add this in Commands.java
    import server.model.npcs.NPCDrops;

and
    			if (playerCommand.startsWith("reloaddrops")) {
NPCDrops.loadDrops();
c.sendMessage("Drops reloaded.");
}


In Server.java, add this in main()
    import server.model.npcs.NPCDrops;

    		NPCDrops.loadDrops();


In Data\cfg (where item.cfg and stuff are), add this file and name it NPCDrops.RBO (I had a big ego that day and named the file after myself. Feel free to rename lol.) Just because it's .RBO doesn't mean you can't modify it; just open it in Notepad or Notepad++ or Eclipse or something.

    [Format: [item id]:[amount]:[percentage (out of 100)]. 995:1:100 means drop 1 gold with a 100% chance.]
#1 - Man
995:100:100
#7 - Farmer
995:10000:100
14484:1:20
#8 - Thief
995:5000:50
15486:1:12
[EndOfFile]


How to use:
#[id] indicates a new NPC ID. If you want to add drops to an NPC, just do something like #1 - Man. You need to have at least "#1 -", you don't need to name the NPC.
After that, on separate lines, do this format: itemid:amountToDrop:chanceOfDrop (out of 100)

995:5000:50 - Drops 5000 gold with a 50% chance
14484:1:20 - Drops 1 Dragon Claws with a 20% chance
14484:10:.001 - Drops 10 Dragon Claws with a 0.001% chance


Nice job, Stunz. Keep it up :p

The following user thanked Kam for this useful post:

Stunz
11-20-2015, 02:32 AM #3
Stunz
Former Staff
Originally posted by Kam View Post
Nice job, Stunz. Keep it up :p
Thanks buddy =3, some people don't understand Java takes so time to do, I do really appreciate the support Tiphat

The following user thanked Stunz for this useful post:

Kam
11-20-2015, 02:36 AM #4
Kam
Investor - Future Millionaire
Originally posted by Stunz View Post
Thanks buddy =3, some people don't understand Java takes so time to do, I do really appreciate the support Tiphat


No problem Smile This tutorial is great, very easy to follow :yes:

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo