Advertisement
Guest User

Untitled

a guest
Oct 18th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.68 KB | None | 0 0
  1. package com.rs2.model.npcs.drop;
  2.  
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10.  
  11. import com.google.gson.Gson;
  12. import com.google.gson.reflect.TypeToken;
  13. import com.rs2.model.Entity;
  14. import com.rs2.model.content.combat.util.RingEffect;
  15. import com.rs2.model.npcs.NpcDefinition;
  16. import com.rs2.model.npcs.drop.NpcDrop.DropChance;
  17. import com.rs2.model.players.Player;
  18. import com.rs2.model.players.item.Item;
  19. import com.rs2.util.Misc;
  20.  
  21. public class NpcDropManager {
  22.    
  23.     public static List<NpcDropTable> drop_tables = new ArrayList<NpcDropTable>();
  24.     public static NpcDrop[] rareTable;
  25.  
  26.     public static void init() throws IOException {
  27.         drop_tables.clear();
  28.         rareTable = null;
  29.         FileReader reader = new FileReader("./data/npcs/npcDrops.json");
  30.         try {
  31. //          drop_tables = new Gson().fromJson(reader, new TypeToken<List<NpcDropTable>>(){}.getType());
  32.             List<NpcDropTable> tables = new Gson().fromJson(reader, new TypeToken<List<NpcDropTable>>(){}.getType());
  33.             for(NpcDropTable table : tables) {
  34.                 table.sortDrops();
  35.                 drop_tables.add(table);
  36.             }
  37.             reader.close();
  38.             System.out.println("Loaded " + drop_tables.size() + " npc drops json.");
  39.         } catch (IOException e) {
  40.             reader.close();
  41.             System.err.println("failed to load npc drops json.");
  42.         }
  43.         reader = new FileReader("./data/npcs/rareDrops.json");
  44.         try {
  45.             rareTable = new Gson().fromJson(reader, new TypeToken<NpcDrop[]>(){}.getType());
  46.             reader.close();
  47.             System.out.println("Loaded " + rareTable.length + " rare drops json.");
  48.         } catch (IOException e) {
  49.             reader.close();
  50.             System.err.println("failed to load rare drops json.");
  51.         }
  52.     }
  53.    
  54.     public static void testDrops(int npcID, int tests, int killCount) {
  55.         for(int test = 0; test < tests; test++) {
  56.             Map<String, Integer> test_map = new HashMap<String, Integer>();
  57.             for(int kill = 0; kill < killCount; kill++) {
  58.                 for(Item item : getDropItems(null, npcID)) {
  59.                     int base_count = test_map.containsKey(item.getDefinition().getName()) ? test_map.get(item.getDefinition().getName()) : 0;
  60.                     test_map.put(item.getDefinition().getName(), base_count + item.getCount());
  61.                 }
  62.             }
  63.             System.out.println("Test #"+ (test+1) +" | " + npcID + "-" + NpcDefinition.forId(npcID).getName() + " | Kill count: " + killCount);
  64.             System.out.println(test_map);
  65.         }
  66.     }
  67.    
  68.     public static Item[] getDropItems(Entity killer, int npcID) {
  69.         NpcDropTable dropTable = getDropTable(npcID);
  70.         if(dropTable == null) return null;
  71.         List<Item> items = new LinkedList<Item>();
  72.         boolean wealthRing = (killer != null && killer.isPlayer() && RingEffect.ringOfWealth((Player) killer));
  73.         boolean rareTableRoll = (rareTable != null && rareTable.length > 0) && dropTable.rareTableChance() >= 0 ? Misc.random(dropTable.rareTableChance()) == 0 : false;
  74.         if(dropTable.getDrops() != null) {
  75.             List<Item> getAlwaysItems = getAlwaysItems(dropTable.getDrops());
  76.             if(getAlwaysItems != null)
  77.                 items.addAll(getAlwaysItems);
  78.             Item randomItem = getRandomItem(rareTableRoll ? rareTable : dropTable.getDrops());
  79.             if(randomItem != null)
  80.                 items.add(randomItem);
  81.         }
  82.         return items.toArray(new Item[items.size()]);
  83.     }
  84.    
  85.     private static Item getRandomItem(NpcDrop[] drops) {
  86.         if(drops == null || drops.length == 0) return null;
  87.         Item item = null;
  88.         for(NpcDrop drop : drops) { //(NpcDrop[])Misc.shuffle(drops)) {
  89.             if(drop == null || drop.getChance().equals(DropChance.ALWAYS)) continue;
  90.             if(drop.attemptRoll()) {
  91.                 item = drop.getItem();
  92.                 break;
  93.             }
  94.         }
  95.         return item;
  96.     }
  97.    
  98.     private static List<Item> getAlwaysItems(NpcDrop[] drops) {
  99.         if(drops == null || drops.length == 0) return null;
  100.         List<Item> items = new LinkedList<Item>();
  101.         for(NpcDrop drop : drops) {
  102.             if(drop == null || !drop.getChance().equals(DropChance.ALWAYS)) continue;
  103.             items.add(drop.getItem());
  104.         }
  105.         return items;
  106.     }
  107.  
  108.     /*private static List<Item> getItems(NpcDrop[] drops) {
  109.         if(drops == null || drops.length == 0) return null;
  110.         List<Item> items = new LinkedList<Item>();
  111.         boolean canAddDynamic = true;
  112.         for(NpcDrop drop : (NpcDrop[])Misc.shuffle(drops)) {
  113.             if(drop == null) continue;
  114.             if(drop.getChance().equals(DropChance.ALWAYS)) {
  115.                 items.add(drop.getItem());
  116.             } else {
  117.                 if(canAddDynamic && drop.attemptRoll()) {
  118.                     items.add(drop.getItem());
  119.                     canAddDynamic = false;
  120.                 }
  121.             }
  122.         }
  123.         return items;
  124.     }*/
  125.    
  126.     public static NpcDropTable getDropTable(final int npcID) {
  127.         for(NpcDropTable table : drop_tables) {
  128.             if(table == null) continue;
  129.             if(table.containsNpcID(npcID)) {
  130.                 return table;
  131.             }
  132.         }
  133.         return null;
  134.     }
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement