Thread: NPC Drop system using JSON.

Page 1 of 2 12 LastLast
Results 1 to 10 of 19
  1. #1 NPC Drop system using JSON. 
    Respected Member


    George's Avatar
    Join Date
    Mar 2009
    Posts
    7,099
    Thanks given
    2,226
    Thanks received
    3,146
    Rep Power
    5000
    SORRY FOR LACK OF DOCUMENTING AND SHITTY NAMING, ETC ETC ETC, THIS WAS WRITTEN REAL QUICK (can tell from the naming as well lol) as I was testing / writing multiple systems

    Attached image

    Attached image

    Code:
    	public List<Item> getLoot() {
    		final List<Item> drops = new ArrayList<>();
    		final NPCJsonDrops npcDrops = NPCJsonDrops.get(npcType);
    		if (npcDrops == null) {
    			return null;
    		}
    		final List<Item> alwaysDrops = Arrays.asList(npcDrops.getAlwaysDrops().get().getItems());
    		alwaysDrops.forEach((alwaysDrop) -> {
    			if (alwaysDrop.getId() > 0)
    				drops.add(new Item(alwaysDrop.getId(), alwaysDrop.getAmount()));
    		});
    		final double roll = (new Random().nextDouble() * 100);
    		final RarityTable rarityTable = NPCJsonDrops.getRarityTable(roll);
    		NPCDropDefs drop = null;
    		if (rarityTable == RarityTable.COMMON && npcDrops.getCommonDrops().isPresent()) {
    			drop = npcDrops.getCommonDrops().get();
    		} else if (rarityTable == RarityTable.UNCOMMON && npcDrops.getUncommonDrops().isPresent()) {
    			drop = npcDrops.getUncommonDrops().get();
    		} else if (rarityTable == RarityTable.RARE && npcDrops.getRareDrops().isPresent()) {
    			drop = npcDrops.getRareDrops().get();
    		} else if (rarityTable == RarityTable.VERY_RARE && npcDrops.getVeryRareDrops().isPresent()) {
    			drop = npcDrops.getVeryRareDrops().get();
    		}
    		final int index = new Random().nextInt(drop.getItems().length);
    		final int id = drop.getItems()[index].getId();
    		final int count = drop.getItems()[index].getAmount();
    		if (id > 0) {
    			drops.add(new Item(id, count));
    		}
    		return drops;
    	}
    Code:
    package org.os_exile.entity.impl.npc;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
    import java.util.Random;
    
    import org.os_exile.model.item.Item;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    
    public class NPCJsonDrops {
    
    	private static final NPCJsonDrops[] instance = new NPCJsonDrops[NPCDefinitions.MAX_NPC_AMOUNT];
    
    	public static final NPCJsonDrops get(final int id) {
    		return instance[id];
    	}
    
    	public static final void writeDropDef() {
    		final NPCJsonDrops generalGraardor = new NPCJsonDrops(2215,
    				Optional.of(new NPCDropDefs(RarityTable.ALWAYS, new Item[] { new Item(532, 1) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.COMMON,
    						new Item[] { new Item(1319, 1), new Item(1275, 1), new Item(1303, 1), new Item(1127, 1),
    								new Item(450, 20), new Item(454, 120), new Item(995, 121000) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.UNCOMMON, new Item[] { new Item(561, 70), new Item(5300, 1),
    						new Item(1514, 20), new Item(3024, 3), new Item(3001, 3) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.RARE,
    						new Item[] { new Item(1289, 1), new Item(11832, 1), new Item(11834, 1), new Item(11836, 1),
    								new Item(11818, 1), new Item(11820, 1), new Item(11822, 1), new Item(12073, 1),
    								new Item(10976, 1), new Item(11812, 1) })),
    
    				Optional.of(
    						new NPCDropDefs(RarityTable.VERY_RARE, new Item[] { new Item(10977, 1), new Item(12650, 1) })));
    
    		try {
    			final BufferedWriter writer = new BufferedWriter(
    					new FileWriter("./Data/json/drops/" + generalGraardor.id + ".json", true));
    			final GsonBuilder builder = new GsonBuilder();
    			builder.setPrettyPrinting();
    			final Gson gson = builder.create();
    			gson.toJson(generalGraardor, writer);
    			writer.close();
    		} catch (IOException e) {
    
    		}
    	}
    
    	public static RarityTable getRarityTable(final double roll) {
    		RarityTable table = RarityTable.COMMON;
    		if (roll <= 30) {
    			table = RarityTable.UNCOMMON;
    		}
    		if (roll <= 5) {
    			table = RarityTable.RARE;
    		}
    		if (roll <= .01) {
    			table = RarityTable.VERY_RARE;
    		}
    		return table;
    	}
    
    	public static final void LoadNPCJsonDrops() {
    		writeDropDef();
    		for (int index = 0; index < NPCDefinitions.MAX_NPC_AMOUNT; index++) {
    			final File file = new File("./Data/json/drops/" + index + ".json");
    			if (!file.exists()) {
    				continue;
    			}
    			try {
    				final NPCJsonDrops in = new Gson().fromJson(new FileReader(file), NPCJsonDrops.class);
    				instance[index] = in;
    			} catch (IOException e) {
    
    			}
    		}
    		System.out.println(instance.length + " npc drops has been loaded...");
    	}
    
    	public NPCJsonDrops(final int id, final Optional<NPCDropDefs> alwaysDrops, Optional<NPCDropDefs> commonDrops,
    			Optional<NPCDropDefs> uncommonDrops, Optional<NPCDropDefs> rareDrops, Optional<NPCDropDefs> veryRareDrops) {
    		this.id = id;
    		this.alwaysDrops = alwaysDrops;
    		this.commonDrops = commonDrops;
    		this.uncommonDrops = uncommonDrops;
    		this.rareDrops = rareDrops;
    		this.veryRareDrops = veryRareDrops;
    	}
    
    	private int id;
    	private Optional<NPCDropDefs> alwaysDrops = Optional.empty();
    	private Optional<NPCDropDefs> commonDrops = Optional.empty();
    	private Optional<NPCDropDefs> uncommonDrops = Optional.empty();
    	private Optional<NPCDropDefs> rareDrops = Optional.empty();
    	private Optional<NPCDropDefs> veryRareDrops = Optional.empty();
    
    	public int getId() {
    		return this.id;
    	}
    
    	public Optional<NPCDropDefs> getAlwaysDrops() {
    		return alwaysDrops;
    	}
    
    	public Optional<NPCDropDefs> getCommonDrops() {
    		return commonDrops;
    	}
    
    	public Optional<NPCDropDefs> getUncommonDrops() {
    		return this.uncommonDrops;
    	}
    
    	public Optional<NPCDropDefs> getRareDrops() {
    		return this.rareDrops;
    	}
    
    	public Optional<NPCDropDefs> getVeryRareDrops() {
    		return this.veryRareDrops;
    	}
    
    	public static class NPCDropDefs {
    
    		public NPCDropDefs(final RarityTable rarityTable, final Item[] items) {
    			this.rarityTable = rarityTable;
    			this.items = items;
    		}
    
    		private final RarityTable rarityTable;
    		private final Item[] items;
    
    		public RarityTable getRarityTable() {
    			return this.rarityTable;
    		}
    
    		public Item[] getItems() {
    			return this.items;
    		}
    
    	}
    
    	public enum RarityTable {
    
    		ALWAYS, COMMON, UNCOMMON, RARE, VERY_RARE
    
    	}
    }
    format
    Code:
    {
      "id": 2215,
      "alwaysDrops": {
        "value": {
          "rarityTable": "ALWAYS",
          "items": [
            {
              "id": 532,
              "amount": 1
            }
          ]
        }
      },
      "commonDrops": {
        "value": {
          "rarityTable": "COMMON",
          "items": [
            {
              "id": 1319,
              "amount": 1
            },
            {
              "id": 1275,
              "amount": 1
            },
            {
              "id": 1303,
              "amount": 1
            },
            {
              "id": 1127,
              "amount": 1
            },
            {
              "id": 450,
              "amount": 20
            },
            {
              "id": 454,
              "amount": 120
            },
            {
              "id": 995,
              "amount": 121000
            }
          ]
        }
      },
      "uncommonDrops": {
        "value": {
          "rarityTable": "UNCOMMON",
          "items": [
            {
              "id": 561,
              "amount": 70
            },
            {
              "id": 5300,
              "amount": 1
            },
            {
              "id": 1514,
              "amount": 20
            },
            {
              "id": 3024,
              "amount": 3
            },
            {
              "id": 3001,
              "amount": 3
            }
          ]
        }
      },
      "rareDrops": {
        "value": {
          "rarityTable": "RARE",
          "items": [
            {
              "id": 1289,
              "amount": 1
            },
            {
              "id": 11832,
              "amount": 1
            },
            {
              "id": 11834,
              "amount": 1
            },
            {
              "id": 11836,
              "amount": 1
            },
            {
              "id": 11818,
              "amount": 1
            },
            {
              "id": 11820,
              "amount": 1
            },
            {
              "id": 11822,
              "amount": 1
            },
            {
              "id": 12073,
              "amount": 1
            },
            {
              "id": 10976,
              "amount": 1
            },
            {
              "id": 11812,
              "amount": 1
            }
          ]
        }
      },
      "veryRareDrops": {
        "value": {
          "rarityTable": "VERY_RARE",
          "items": [
            {
              "id": 10977,
              "amount": 1
            },
            {
              "id": 12650,
              "amount": 1
            }
          ]
        }
      }
    }
    Attached image

    Spoiler for Spoilers!:
    Attached image
    Attached image
    Attached image
    Attached image
    Reply With Quote  
     

  2. #2  
    Registered Member
    Join Date
    Sep 2015
    Posts
    50
    Thanks given
    1
    Thanks received
    5
    Rep Power
    11
    Quote Originally Posted by Idiot Bird View Post
    SORRY FOR LACK OF DOCUMENTING AND SHITTY NAMING, ETC ETC ETC, THIS WAS WRITTEN REAL QUICK (can tell from the naming as well lol) as I was testing / writing multiple systems

    Attached image

    Code:
    	public List<Item> getLoot() {
    		final List<Item> drops = new ArrayList<>();
    		final NPCJsonDrops npcDrops = NPCJsonDrops.get(npcType);
    		if (npcDrops == null) {
    			return null;
    		}
    		final List<Item> alwaysDrops = Arrays.asList(npcDrops.getAlwaysDrops().get().getItems());
    		alwaysDrops.forEach((alwaysDrop) -> {
    			if (alwaysDrop.getId() > 0)
    				drops.add(new Item(alwaysDrop.getId(), alwaysDrop.getAmount()));
    		});
    		final double roll = (new Random().nextDouble() * 100);
    		final RarityTable rarityTable = NPCJsonDrops.getRarityTable(roll);
    		NPCDropDefs drop = null;
    		if (rarityTable == RarityTable.COMMON && npcDrops.getCommonDrops().isPresent()) {
    			drop = npcDrops.getCommonDrops().get();
    		} else if (rarityTable == RarityTable.UNCOMMON && npcDrops.getUncommonDrops().isPresent()) {
    			drop = npcDrops.getUncommonDrops().get();
    		} else if (rarityTable == RarityTable.RARE && npcDrops.getRareDrops().isPresent()) {
    			drop = npcDrops.getRareDrops().get();
    		} else if (rarityTable == RarityTable.VERY_RARE && npcDrops.getVeryRareDrops().isPresent()) {
    			drop = npcDrops.getVeryRareDrops().get();
    		}
    		final int index = new Random().nextInt(drop.getItems().length);
    		final int id = drop.getItems()[index].getId();
    		final int count = drop.getItems()[index].getAmount();
    		if (id > 0) {
    			drops.add(new Item(id, count));
    		}
    		return drops;
    	}
    Code:
    package org.os_exile.entity.impl.npc;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
    import java.util.Random;
    
    import org.os_exile.model.item.Item;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    
    public class NPCJsonDrops {
    
    	private static final NPCJsonDrops[] instance = new NPCJsonDrops[NPCDefinitions.MAX_NPC_AMOUNT];
    
    	public static final NPCJsonDrops get(final int id) {
    		return instance[id];
    	}
    
    	public static final void writeDropDef() {
    		final NPCJsonDrops generalGraardor = new NPCJsonDrops(2215,
    				Optional.of(new NPCDropDefs(RarityTable.ALWAYS, new Item[] { new Item(532, 1) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.COMMON,
    						new Item[] { new Item(1319, 1), new Item(1275, 1), new Item(1303, 1), new Item(1127, 1),
    								new Item(450, 20), new Item(454, 120), new Item(995, 121000) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.UNCOMMON, new Item[] { new Item(561, 70), new Item(5300, 1),
    						new Item(1514, 20), new Item(3024, 3), new Item(3001, 3) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.RARE,
    						new Item[] { new Item(1289, 1), new Item(11832, 1), new Item(11834, 1), new Item(11836, 1),
    								new Item(11818, 1), new Item(11820, 1), new Item(11822, 1), new Item(12073, 1),
    								new Item(10976, 1), new Item(11812, 1) })),
    
    				Optional.of(
    						new NPCDropDefs(RarityTable.VERY_RARE, new Item[] { new Item(10977, 1), new Item(12650, 1) })));
    
    		try {
    			final BufferedWriter writer = new BufferedWriter(
    					new FileWriter("./Data/json/drops/" + generalGraardor.id + ".json", true));
    			final GsonBuilder builder = new GsonBuilder();
    			builder.setPrettyPrinting();
    			final Gson gson = builder.create();
    			gson.toJson(generalGraardor, writer);
    			writer.close();
    		} catch (IOException e) {
    
    		}
    	}
    
    	public static RarityTable getRarityTable(final double roll) {
    		RarityTable table = RarityTable.COMMON;
    		if (roll <= 30) {
    			table = RarityTable.UNCOMMON;
    		}
    		if (roll <= 5) {
    			table = RarityTable.RARE;
    		}
    		if (roll <= .01) {
    			table = RarityTable.VERY_RARE;
    		}
    		return table;
    	}
    
    	public static final void LoadNPCJsonDrops() {
    		writeDropDef();
    		for (int index = 0; index < NPCDefinitions.MAX_NPC_AMOUNT; index++) {
    			final File file = new File("./Data/json/drops/" + index + ".json");
    			if (!file.exists()) {
    				continue;
    			}
    			try {
    				final NPCJsonDrops in = new Gson().fromJson(new FileReader(file), NPCJsonDrops.class);
    				instance[index] = in;
    			} catch (IOException e) {
    
    			}
    		}
    		System.out.println(instance.length + " npc drops has been loaded...");
    	}
    
    	public NPCJsonDrops(final int id, final Optional<NPCDropDefs> alwaysDrops, Optional<NPCDropDefs> commonDrops,
    			Optional<NPCDropDefs> uncommonDrops, Optional<NPCDropDefs> rareDrops, Optional<NPCDropDefs> veryRareDrops) {
    		this.id = id;
    		this.alwaysDrops = alwaysDrops;
    		this.commonDrops = commonDrops;
    		this.uncommonDrops = uncommonDrops;
    		this.rareDrops = rareDrops;
    		this.veryRareDrops = veryRareDrops;
    	}
    
    	private int id;
    	private Optional<NPCDropDefs> alwaysDrops = Optional.empty();
    	private Optional<NPCDropDefs> commonDrops = Optional.empty();
    	private Optional<NPCDropDefs> uncommonDrops = Optional.empty();
    	private Optional<NPCDropDefs> rareDrops = Optional.empty();
    	private Optional<NPCDropDefs> veryRareDrops = Optional.empty();
    
    	public int getId() {
    		return this.id;
    	}
    
    	public Optional<NPCDropDefs> getAlwaysDrops() {
    		return alwaysDrops;
    	}
    
    	public Optional<NPCDropDefs> getCommonDrops() {
    		return commonDrops;
    	}
    
    	public Optional<NPCDropDefs> getUncommonDrops() {
    		return this.uncommonDrops;
    	}
    
    	public Optional<NPCDropDefs> getRareDrops() {
    		return this.rareDrops;
    	}
    
    	public Optional<NPCDropDefs> getVeryRareDrops() {
    		return this.veryRareDrops;
    	}
    
    	public static class NPCDropDefs {
    
    		public NPCDropDefs(final RarityTable rarityTable, final Item[] items) {
    			this.rarityTable = rarityTable;
    			this.items = items;
    		}
    
    		private final RarityTable rarityTable;
    		private final Item[] items;
    
    		public RarityTable getRarityTable() {
    			return this.rarityTable;
    		}
    
    		public Item[] getItems() {
    			return this.items;
    		}
    
    	}
    
    	public enum RarityTable {
    
    		ALWAYS, COMMON, UNCOMMON, RARE, VERY_RARE
    
    	}
    }
    format
    Code:
    {
      "id": 2215,
      "alwaysDrops": {
        "value": {
          "rarityTable": "ALWAYS",
          "items": [
            {
              "id": 532,
              "amount": 1
            }
          ]
        }
      },
      "commonDrops": {
        "value": {
          "rarityTable": "COMMON",
          "items": [
            {
              "id": 1319,
              "amount": 1
            },
            {
              "id": 1275,
              "amount": 1
            },
            {
              "id": 1303,
              "amount": 1
            },
            {
              "id": 1127,
              "amount": 1
            },
            {
              "id": 450,
              "amount": 20
            },
            {
              "id": 454,
              "amount": 120
            },
            {
              "id": 995,
              "amount": 121000
            }
          ]
        }
      },
      "uncommonDrops": {
        "value": {
          "rarityTable": "UNCOMMON",
          "items": [
            {
              "id": 561,
              "amount": 70
            },
            {
              "id": 5300,
              "amount": 1
            },
            {
              "id": 1514,
              "amount": 20
            },
            {
              "id": 3024,
              "amount": 3
            },
            {
              "id": 3001,
              "amount": 3
            }
          ]
        }
      },
      "rareDrops": {
        "value": {
          "rarityTable": "RARE",
          "items": [
            {
              "id": 1289,
              "amount": 1
            },
            {
              "id": 11832,
              "amount": 1
            },
            {
              "id": 11834,
              "amount": 1
            },
            {
              "id": 11836,
              "amount": 1
            },
            {
              "id": 11818,
              "amount": 1
            },
            {
              "id": 11820,
              "amount": 1
            },
            {
              "id": 11822,
              "amount": 1
            },
            {
              "id": 12073,
              "amount": 1
            },
            {
              "id": 10976,
              "amount": 1
            },
            {
              "id": 11812,
              "amount": 1
            }
          ]
        }
      },
      "veryRareDrops": {
        "value": {
          "rarityTable": "VERY_RARE",
          "items": [
            {
              "id": 10977,
              "amount": 1
            },
            {
              "id": 12650,
              "amount": 1
            }
          ]
        }
      }
    }
    Thanks, great contribution!
    Reply With Quote  
     

  3. #3  
    Jake from State Farm

    FKN Jake's Avatar
    Join Date
    Nov 2013
    Posts
    675
    Thanks given
    45
    Thanks received
    58
    Rep Power
    153
    Why would you want to switch to json drops though? I use json drops and I absolutely hate it.
    Reply With Quote  
     

  4. #4  
    Super Donator

    Chivvon's Avatar
    Join Date
    May 2016
    Posts
    487
    Thanks given
    69
    Thanks received
    142
    Rep Power
    285
    Thanks for the contribution, there is definitely space for improvement but all and all it's decent.
    Reply With Quote  
     

  5. #5  
    plz dont take my wizard mind bombs Women's Avatar
    Join Date
    Mar 2010
    Posts
    1,881
    Thanks given
    724
    Thanks received
    1,162
    Rep Power
    4763
    looks good as far as effectiveness and simplicity.
    Reply With Quote  
     

  6. #6  
    Banned
    Join Date
    Jul 2016
    Posts
    95
    Thanks given
    78
    Thanks received
    10
    Rep Power
    0
    This is nice, you should consider dumping all the drop data from fansites and wikis. Boost your projects data
    Good job man.
    Reply With Quote  
     

  7. #7  
    Registered Member
    Zivik's Avatar
    Join Date
    Oct 2007
    Age
    28
    Posts
    4,421
    Thanks given
    891
    Thanks received
    1,527
    Rep Power
    3285
    Thanks for the share.
    Reply With Quote  
     

  8. #8  
    Registered Member
    Join Date
    Apr 2016
    Posts
    59
    Thanks given
    19
    Thanks received
    24
    Rep Power
    28
    You should specify the minimum and maximum amount of an item a npc can drop.

    Or else you will have npc's like the tzhaar dropping x amount of tokkul every kill.

    Other than that, this isn't bad.

    Thanks for sharing.
    Reply With Quote  
     

  9. #9  
    Respected Member


    George's Avatar
    Join Date
    Mar 2009
    Posts
    7,099
    Thanks given
    2,226
    Thanks received
    3,146
    Rep Power
    5000
    Quote Originally Posted by Mysticcccccc View Post
    You should specify the minimum and maximum amount of an item a npc can drop.

    Or else you will have npc's like the tzhaar dropping x amount of tokkul every kill.

    Other than that, this isn't bad.

    Thanks for sharing.
    maximum effort lol.

    Code:
    package org.os_exile.entity.impl.npc;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;
    import java.util.Optional;
    import java.util.Random;
    
    import org.os_exile.model.item.Item;
    import org.os_exile.util.Util;
    
    import com.google.gson.Gson;
    import com.google.gson.GsonBuilder;
    
    public class NPCJsonDrops {
    
    	private static final NPCJsonDrops[] instance = new NPCJsonDrops[NPCDefinitions.MAX_NPC_AMOUNT];
    
    	public static final NPCJsonDrops get(final int id) {
    		return instance[id];
    	}
    
    	public static final void writeDropDef() {
    		final NPCJsonDrops generalGraardor = new NPCJsonDrops(2215,
    				Optional.of(new NPCDropDefs(RarityTable.ALWAYS, new NPCDropItem[] { new NPCDropItem(532, 1) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.COMMON,
    						new NPCDropItem[] { new NPCDropItem(1319, 1), new NPCDropItem(1275, 1),
    								new NPCDropItem(1303, 1), new NPCDropItem(1127, 1), new NPCDropItem(450, 15, 5),
    								new NPCDropItem(454, 115, 5), new NPCDropItem(995, 19581, 509) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.UNCOMMON,
    						new NPCDropItem[] { new NPCDropItem(561, 60, 10), new NPCDropItem(5300, 1),
    								new NPCDropItem(1514, 15, 5), new NPCDropItem(3024, 3), new NPCDropItem(3001, 3) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.RARE,
    						new NPCDropItem[] { new NPCDropItem(1289, 1), new NPCDropItem(11832, 1),
    								new NPCDropItem(11834, 1), new NPCDropItem(11836, 1), new NPCDropItem(11818, 1),
    								new NPCDropItem(11820, 1), new NPCDropItem(11822, 1), new NPCDropItem(12073, 1),
    								new NPCDropItem(10976, 1), new NPCDropItem(11812, 1) })),
    
    				Optional.of(new NPCDropDefs(RarityTable.VERY_RARE,
    						new NPCDropItem[] { new NPCDropItem(10977, 1), new NPCDropItem(12650, 1) })));
    
    		try {
    			final BufferedWriter writer = new BufferedWriter(
    					new FileWriter("./Data/json/drops/" + generalGraardor.id + ".json", true));
    			final GsonBuilder builder = new GsonBuilder();
    			builder.setPrettyPrinting();
    			final Gson gson = builder.create();
    			gson.toJson(generalGraardor, writer);
    			writer.close();
    		} catch (IOException e) {
    
    		}
    	}
    
    	public static RarityTable getRarityTable(final double roll) {
    		RarityTable table = RarityTable.COMMON;
    		if (roll <= 30) {
    			table = RarityTable.UNCOMMON;
    		}
    		if (roll <= 5) {
    			table = RarityTable.RARE;
    		}
    		if (roll <= .01) {
    			table = RarityTable.VERY_RARE;
    		}
    		return table;
    	}
    
    	public static final void LoadNPCJsonDrops() {
    		//writeDropDef();
    		for (int index = 0; index < NPCDefinitions.MAX_NPC_AMOUNT; index++) {
    			final File file = new File("./Data/json/drops/" + index + ".json");
    			if (!file.exists()) {
    				continue;
    			}
    			try {
    				final NPCJsonDrops in = new Gson().fromJson(new FileReader(file), NPCJsonDrops.class);
    				instance[index] = in;
    			} catch (IOException e) {
    
    			}
    		}
    		System.out.println(instance.length + " npc drops has been loaded...");
    	}
    
    	public NPCJsonDrops(final int id, final Optional<NPCDropDefs> alwaysDrops, Optional<NPCDropDefs> commonDrops,
    			Optional<NPCDropDefs> uncommonDrops, Optional<NPCDropDefs> rareDrops, Optional<NPCDropDefs> veryRareDrops) {
    		this.id = id;
    		this.alwaysDrops = alwaysDrops;
    		this.commonDrops = commonDrops;
    		this.uncommonDrops = uncommonDrops;
    		this.rareDrops = rareDrops;
    		this.veryRareDrops = veryRareDrops;
    	}
    
    	private int id;
    	private Optional<NPCDropDefs> alwaysDrops = Optional.empty();
    	private Optional<NPCDropDefs> commonDrops = Optional.empty();
    	private Optional<NPCDropDefs> uncommonDrops = Optional.empty();
    	private Optional<NPCDropDefs> rareDrops = Optional.empty();
    	private Optional<NPCDropDefs> veryRareDrops = Optional.empty();
    
    	public int getId() {
    		return this.id;
    	}
    
    	public Optional<NPCDropDefs> getAlwaysDrops() {
    		return alwaysDrops;
    	}
    
    	public Optional<NPCDropDefs> getCommonDrops() {
    		return commonDrops;
    	}
    
    	public Optional<NPCDropDefs> getUncommonDrops() {
    		return this.uncommonDrops;
    	}
    
    	public Optional<NPCDropDefs> getRareDrops() {
    		return this.rareDrops;
    	}
    
    	public Optional<NPCDropDefs> getVeryRareDrops() {
    		return this.veryRareDrops;
    	}
    
    	public static class NPCDropDefs {
    
    		public NPCDropDefs(final RarityTable rarityTable, final NPCDropItem[] items) {
    			this.rarityTable = rarityTable;
    			this.items = items;
    		}
    
    		private final RarityTable rarityTable;
    		private final NPCDropItem[] items;
    
    		public RarityTable getRarityTable() {
    			return this.rarityTable;
    		}
    
    		public NPCDropItem[] getItems() {
    			return this.items;
    		}
    
    	}
    
    	public static class NPCDropItem {
    
    		public NPCDropItem(final int id, final int minimumCount) {
    			this.id = id;
    			this.minimumCount = minimumCount;
    			this.randomCount = 0;
    		}
    
    		public NPCDropItem(final int id, final int minimumCount, final int randomCount) {
    			this.id = id;
    			this.minimumCount = minimumCount;
    			this.randomCount = randomCount;
    		}
    
    		private final int id;
    		private final int minimumCount;
    		private final int randomCount;
    		
    		public int getId() {
    			return this.id;
    		}
    		
    		public int getMinimumCount() {
    			return this.minimumCount + Util.random(randomCount);
    		}
    
    	}
    
    	public enum RarityTable {
    
    		ALWAYS, COMMON, UNCOMMON, RARE, VERY_RARE
    
    	}
    }
    Code:
    public List<Item> getLoot() {
    		final List<Item> drops = new ArrayList<>();
    		final NPCJsonDrops npcDrops = NPCJsonDrops.get(npcType);
    		if (npcDrops == null) {
    			return null;
    		}
    		final List<NPCDropItem> alwaysDrops = Arrays.asList(npcDrops.getAlwaysDrops().get().getItems());
    		alwaysDrops.forEach((alwaysDrop) -> {
    			if (alwaysDrop.getId() > 0)
    				drops.add(new Item(alwaysDrop.getId(), alwaysDrop.getMinimumCount()));
    		});
    		final double roll = (new Random().nextDouble() * 100);
    		final RarityTable rarityTable = NPCJsonDrops.getRarityTable(roll);
    		NPCDropDefs drop = null;
    		if (rarityTable == RarityTable.COMMON && npcDrops.getCommonDrops().isPresent()) {
    			drop = npcDrops.getCommonDrops().get();
    		} else if (rarityTable == RarityTable.UNCOMMON && npcDrops.getUncommonDrops().isPresent()) {
    			drop = npcDrops.getUncommonDrops().get();
    		} else if (rarityTable == RarityTable.RARE && npcDrops.getRareDrops().isPresent()) {
    			drop = npcDrops.getRareDrops().get();
    		} else if (rarityTable == RarityTable.VERY_RARE && npcDrops.getVeryRareDrops().isPresent()) {
    			drop = npcDrops.getVeryRareDrops().get();
    		}
    		final int index = new Random().nextInt(drop.getItems().length);
    		final int id = drop.getItems()[index].getId();
    		final int count = drop.getItems()[index].getMinimumCount();
    		if (id > 0) {
    			drops.add(new Item(id, count));
    		}
    		return drops;
    	}
    Code:
    {
      "id": 2215,
      "alwaysDrops": {
        "value": {
          "rarityTable": "ALWAYS",
          "items": [
            {
              "id": 532,
              "minimumCount": 1,
              "randomCount": 0
            }
          ]
        }
      },
      "commonDrops": {
        "value": {
          "rarityTable": "COMMON",
          "items": [
            {
              "id": 1319,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 1275,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 1303,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 1127,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 450,
              "minimumCount": 15,
              "randomCount": 5
            },
            {
              "id": 454,
              "minimumCount": 115,
              "randomCount": 5
            },
            {
              "id": 995,
              "minimumCount": 19581,
              "randomCount": 509
            }
          ]
        }
      },
      "uncommonDrops": {
        "value": {
          "rarityTable": "UNCOMMON",
          "items": [
            {
              "id": 561,
              "minimumCount": 60,
              "randomCount": 10
            },
            {
              "id": 5300,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 1514,
              "minimumCount": 15,
              "randomCount": 5
            },
            {
              "id": 3024,
              "minimumCount": 3,
              "randomCount": 0
            },
            {
              "id": 3001,
              "minimumCount": 3,
              "randomCount": 0
            }
          ]
        }
      },
      "rareDrops": {
        "value": {
          "rarityTable": "RARE",
          "items": [
            {
              "id": 1289,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 11832,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 11834,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 11836,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 11818,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 11820,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 11822,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 12073,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 10976,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 11812,
              "minimumCount": 1,
              "randomCount": 0
            }
          ]
        }
      },
      "veryRareDrops": {
        "value": {
          "rarityTable": "VERY_RARE",
          "items": [
            {
              "id": 10977,
              "minimumCount": 1,
              "randomCount": 0
            },
            {
              "id": 12650,
              "minimumCount": 1,
              "randomCount": 0
            }
          ]
        }
      }
    }
    Attached image

    Spoiler for Spoilers!:
    Attached image
    Attached image
    Attached image
    Attached image
    Reply With Quote  
     

  10. #10  
    Banned

    Join Date
    Oct 2012
    Posts
    4,710
    Thanks given
    1,679
    Thanks received
    1,105
    Rep Power
    0
    Thanks for the share.
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Using Math Random in Array (NPC Drop System)
    By Project-317 in forum Help
    Replies: 10
    Last Post: 05-01-2012, 12:29 PM
  2. NPC drops system
    By pljay03 in forum Show-off
    Replies: 3
    Last Post: 02-16-2010, 03:22 PM
  3. NPC drop Systems.
    By Bando in forum RS2 Server
    Replies: 12
    Last Post: 01-20-2010, 12:03 AM
  4. [Shard] Alternative NPC Drop System
    By Chachi in forum Snippets
    Replies: 6
    Last Post: 11-15-2009, 12:46 AM
  5. Alternate NPC Drop System
    By bloodargon in forum Tutorials
    Replies: 18
    Last Post: 07-15-2009, 07:45 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •