Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.rs2.model.npcs.drop;
- import java.io.FileReader;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import com.google.gson.Gson;
- import com.google.gson.reflect.TypeToken;
- import com.rs2.model.Entity;
- import com.rs2.model.content.combat.util.RingEffect;
- import com.rs2.model.npcs.NpcDefinition;
- import com.rs2.model.npcs.drop.NpcDrop.DropChance;
- import com.rs2.model.players.Player;
- import com.rs2.model.players.item.Item;
- import com.rs2.util.Misc;
- public class NpcDropManager {
- public static List<NpcDropTable> drop_tables = new ArrayList<NpcDropTable>();
- public static NpcDrop[] rareTable;
- public static void init() throws IOException {
- drop_tables.clear();
- rareTable = null;
- FileReader reader = new FileReader("./data/npcs/npcDrops.json");
- try {
- // drop_tables = new Gson().fromJson(reader, new TypeToken<List<NpcDropTable>>(){}.getType());
- List<NpcDropTable> tables = new Gson().fromJson(reader, new TypeToken<List<NpcDropTable>>(){}.getType());
- for(NpcDropTable table : tables) {
- table.sortDrops();
- drop_tables.add(table);
- }
- reader.close();
- System.out.println("Loaded " + drop_tables.size() + " npc drops json.");
- } catch (IOException e) {
- reader.close();
- System.err.println("failed to load npc drops json.");
- }
- reader = new FileReader("./data/npcs/rareDrops.json");
- try {
- rareTable = new Gson().fromJson(reader, new TypeToken<NpcDrop[]>(){}.getType());
- reader.close();
- System.out.println("Loaded " + rareTable.length + " rare drops json.");
- } catch (IOException e) {
- reader.close();
- System.err.println("failed to load rare drops json.");
- }
- }
- public static void testDrops(int npcID, int tests, int killCount) {
- for(int test = 0; test < tests; test++) {
- Map<String, Integer> test_map = new HashMap<String, Integer>();
- for(int kill = 0; kill < killCount; kill++) {
- for(Item item : getDropItems(null, npcID)) {
- int base_count = test_map.containsKey(item.getDefinition().getName()) ? test_map.get(item.getDefinition().getName()) : 0;
- test_map.put(item.getDefinition().getName(), base_count + item.getCount());
- }
- }
- System.out.println("Test #"+ (test+1) +" | " + npcID + "-" + NpcDefinition.forId(npcID).getName() + " | Kill count: " + killCount);
- System.out.println(test_map);
- }
- }
- public static Item[] getDropItems(Entity killer, int npcID) {
- NpcDropTable dropTable = getDropTable(npcID);
- if(dropTable == null) return null;
- List<Item> items = new LinkedList<Item>();
- boolean wealthRing = (killer != null && killer.isPlayer() && RingEffect.ringOfWealth((Player) killer));
- boolean rareTableRoll = (rareTable != null && rareTable.length > 0) && dropTable.rareTableChance() >= 0 ? Misc.random(dropTable.rareTableChance()) == 0 : false;
- if(dropTable.getDrops() != null) {
- List<Item> getAlwaysItems = getAlwaysItems(dropTable.getDrops());
- if(getAlwaysItems != null)
- items.addAll(getAlwaysItems);
- Item randomItem = getRandomItem(rareTableRoll ? rareTable : dropTable.getDrops());
- if(randomItem != null)
- items.add(randomItem);
- }
- return items.toArray(new Item[items.size()]);
- }
- private static Item getRandomItem(NpcDrop[] drops) {
- if(drops == null || drops.length == 0) return null;
- Item item = null;
- for(NpcDrop drop : drops) { //(NpcDrop[])Misc.shuffle(drops)) {
- if(drop == null || drop.getChance().equals(DropChance.ALWAYS)) continue;
- if(drop.attemptRoll()) {
- item = drop.getItem();
- break;
- }
- }
- return item;
- }
- private static List<Item> getAlwaysItems(NpcDrop[] drops) {
- if(drops == null || drops.length == 0) return null;
- List<Item> items = new LinkedList<Item>();
- for(NpcDrop drop : drops) {
- if(drop == null || !drop.getChance().equals(DropChance.ALWAYS)) continue;
- items.add(drop.getItem());
- }
- return items;
- }
- /*private static List<Item> getItems(NpcDrop[] drops) {
- if(drops == null || drops.length == 0) return null;
- List<Item> items = new LinkedList<Item>();
- boolean canAddDynamic = true;
- for(NpcDrop drop : (NpcDrop[])Misc.shuffle(drops)) {
- if(drop == null) continue;
- if(drop.getChance().equals(DropChance.ALWAYS)) {
- items.add(drop.getItem());
- } else {
- if(canAddDynamic && drop.attemptRoll()) {
- items.add(drop.getItem());
- canAddDynamic = false;
- }
- }
- }
- return items;
- }*/
- public static NpcDropTable getDropTable(final int npcID) {
- for(NpcDropTable table : drop_tables) {
- if(table == null) continue;
- if(table.containsNpcID(npcID)) {
- return table;
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement