Post: JSon NPC drops, supports 602 revision npcs
05-13-2015, 11:14 PM #1
Vesia
Haxor!
(adsbygoogle = window.adsbygoogle || []).push({});
    
package org.rs2.model.entity.npcs.drop;

public class Drop {

private int itemId, minAmount, maxAmount;
private double rate;
private boolean rare;

public static Drop create(int itemId, double rate, int minAmount,
int maxAmount, boolean rare) {
return new Drop((short) itemId, rate, minAmount, maxAmount, rare);
}

public Drop(int itemId, double rate, int minAmount, int maxAmount,
boolean rare) {
this.itemId = itemId;
this.rate = rate;
this.minAmount = minAmount;
this.maxAmount = maxAmount;
this.rare = rare;
}

public boolean isRare(){
return rare;
}
public int getMinAmount() {
return minAmount;
}

public int getExtraAmount() {
return maxAmount - minAmount;
}

public int getMaxAmount() {
return maxAmount;
}

public int getItemId() {
return itemId;
}

public double getRate() {
return rate;
}

public void setItemId(short itemId) {
this.itemId = itemId;
}

public void setRate(double rate) {
this.rate = rate;
}

public void setMinAmount(int amount) {
this.minAmount = amount;
}

public void setMaxAmount(int amount) {
this.maxAmount = amount;
}

public boolean isFromRareTable() {
return rare;
}

}





    
package org.rs2.model.entity.npcs.drop;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonIOException;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

public class NPCDrops {

/**
* The directory/location of drops.json
*/
private static final File DROP_DIR = new File("./data/cfg/npcdrops.json");

/**
* Our single instance of Gson
*/
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

private static HashMap<Integer, Drop[]> npcDrops;

public static final void init() {
load();
}

public static Drop[] getDrops(int npcId) {
return npcDrops.get(npcId);
}

private Map<Integer, ArrayList<Awesome facerop>> dropMapx = null;

public Map<Integer, ArrayList<Awesome facerop>> getDropArray() {

if (dropMapx == null)
dropMapx = new LinkedHashMap<Integer, ArrayList<Awesome facerop>>();
for (int i : npcDrops.keySet()) {
int npcId = i;
ArrayList<Awesome facerop> temp = new ArrayList<Awesome facerop>();
for (Drop mainDrop : npcDrops.get(npcId)) {
temp.add(mainDrop);
}
dropMapx.put(i, temp);
}

return dropMapx;
}

private static void load(){
final long start = System.currentTimeMillis();
try {
Type t = new TypeToken<HashMap<Integer, Drop[]>>(){}.getType();
npcDrops = GSON.fromJson(new FileReader(DROP_DIR), t);
} catch (JsonSyntaxException | JsonIOException | FileNotFoundException e) {
e.printStackTrace();
}
System.out.println("Loaded: " + npcDrops.size()
+ " NPC drops in: " + (System.currentTimeMillis() - start) + "ms.");
}

public HashMap<Integer, Drop[]> getDropMap() {
return npcDrops;
}
}





In NPCHandler, under void dropItems(int i)

    
Drop[] drops = NPCDrops.getDrops(npcs[i].npcType);
Drop[] possibleDrops = new Drop[drops.length];
int possibleDropsCount = 0;
for (Drop drop : drops) {
if (drop.getRate() == 100)
sendDrop(killer, drop, i);
else {
if ((Misc.random(99) + 1) <= drop.getRate() * 1.0)
possibleDrops[possibleDropsCount++] = drop;
}
}
if (possibleDropsCount > 0)
sendDrop(killer,
possibleDrops[Misc.random(possibleDropsCount - 1)], i);

}



Add this method in NPCHandler also

    
public void sendDrop(Player player, Drop drop, int i) {
if(drop.getItemId() >= Constants.ITEM_LIMIT){
return;
}
player.getItems();
if(ItemAssistant.getItemName(drop.getItemId()) == null){
return;
}
GameItem item = new GameItem(drop.getItemId(), 1).stackable ? new GameItem(
drop.getItemId(), (drop.getMinAmount() * Constants.DROP_RATE)
+ Misc.random(drop.getExtraAmount() * Constants.DROP_RATE))
: new GameItem(drop.getItemId(), drop.getMinAmount()
+ Misc.random(drop.getExtraAmount()));

Server.itemHandler.createGroundItem(player, item.id, npcs[i].absX,
npcs[i].absY, npcs[i].heightLevel, item.amount, player.getIndex());

}



If you don't have the GameItem class

    
package org.rs2.model.items;

public class GameItem {
public int id, amount;
public boolean stackable = false;

public GameItem(int id, int amount) {
if (Item.itemStackable[id]) {
stackable = true;
}
this.id = id;
this.amount = amount;
}
}



File: You must login or register to view this content.
(adsbygoogle = window.adsbygoogle || []).push({});

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo