Thread: Elvarg drop system bug / nullpointer

Results 1 to 3 of 3
  1. #1 Elvarg drop system bug / nullpointer 
    Registered Member
    Join Date
    Jan 2018
    Posts
    31
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    Okay so for the life of me, i can't figure out this issue. I think i know what is causing it but not sure how to fix it.
    So on Elvarg eco release, the drop system i think works like this.

    You have 5 different types of drops:
    Common,
    Uncommon,
    Rare,
    Very Rare,
    Special (handled separately not apart of the issue)

    Now it loads the drop data from a .json file like this

    Code:
    {
        "npcIds": [
          5007,
          5008
        ],
        "rdtChance": -1,
        "alwaysDrops": [
          {
            "itemId": 592,
            "minAmount": 1,
            "maxAmount": 1
          }
        ],
        "commonDrops": [
          {
            "itemId": 1474,
            "minAmount": 1,
            "maxAmount": 1
          },
    etc, etc..

    Now the issue lies when an npc doesnt have all of those drops defined in the json file.
    So when the roll lands on lets say, uncommonDrop and it hasnt been declared it will flag a nullpointer error and not give me any drop at all, not even the 100% bones drop.
    I have got around this by adding a try catch around the switch block to stop the nullpointer and to give me loot whenever it does catch something.
    However that is obviously not solving the problem but rather 'cheap' fixing it, can anyone help ?

    Code:
    //If we didn't get a special drop, attempt to find a different table..
    			if(!table.isPresent()) {
    				double chance = random.get().nextDouble(100);
    				if((table = getDropTable(chance)).isPresent()) {
    					//Make sure we haven't already parsed this table.
    					if(parsedTables.contains(table.get())) {
    						continue;
    					}					
    					//Get the items related to this drop table..
    					Optional<NPCDrop[]> dropTableItems = Optional.empty();
    					try {
    					switch(table.get()) {
    					case COMMON:
    						dropTableItems = Optional.of(def.getCommonDrops());
    						break;
    					case UNCOMMON:
    						dropTableItems = Optional.of(def.getUncommonDrops());
    						break;
    					case RARE:
    						dropTableItems = Optional.of(def.getRareDrops());
    						break;
    					case VERY_RARE:
    						dropTableItems = Optional.of(def.getVeryRareDrops());
    						break;
    					default:
    						dropTableItems = Optional.of(def.getCommonDrops());
    						break;
    					}
    					} catch (NullPointerException e) {
    						System.out.println("caught nullpointer");
    					}
    					if(!dropTableItems.isPresent()) {
    						continue;
    					}
    					
    					NPCDrop npcDrop = dropTableItems.get()[random.get().nextInt(dropTableItems.get().length)];
    
    					//Add the drop to the drop list.
    					items.add(npcDrop.toItem(random));
    
    					//Flag this table as visited..
    					parsedTables.add(table.get());
    				}
    			}
    The block of code the error occurs, (can be different lines depending on what 'case' it tried to roll and found nothing)

    The error
    Code:
    java.lang.NullPointerException
    	at java.util.Objects.requireNonNull(Unknown Source)
    	at java.util.Optional.<init>(Unknown Source)
    	at java.util.Optional.of(Unknown Source)
    	at com.elvarg.game.entity.impl.npc.NPCDropGenerator.getDropList(NPCDropGenerator.java:132)
    	at com.elvarg.game.entity.impl.npc.NPCDropGenerator.start(NPCDropGenerator.java:32)
    	at com.elvarg.game.task.impl.NPCDeathTask.execute(NPCDeathTask.java:75)
    	at com.elvarg.game.task.Task.tick(Task.java:142)
    	at com.elvarg.game.task.TaskManager.sequence(TaskManager.java:32)
    	at com.elvarg.game.World.sequence(World.java:87)
    	at com.elvarg.game.GameEngine.run(GameEngine.java:35)
    	at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
    	at java.util.concurrent.FutureTask.runAndReset(Unknown Source)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(Unknown Source)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    	at java.lang.Thread.run(Unknown Source)
    Reply With Quote  
     

  2. #2  
    Respected Member


    Kris's Avatar
    Join Date
    Jun 2016
    Age
    26
    Posts
    3,638
    Thanks given
    820
    Thanks received
    2,642
    Rep Power
    5000
    1. That drop system sucks dicks.
    2. The error is caused by def.get*Rarity*Drops() returning null. You can't make an Optional.of(null). You'll have to first check and ensure that the array/list w/e of drops for the given rarity ain't null, and then handle the given drops.
    Attached image
    Reply With Quote  
     

  3. #3  
    Registered Member
    Join Date
    Jan 2018
    Posts
    31
    Thanks given
    1
    Thanks received
    0
    Rep Power
    11
    Quote Originally Posted by Kris View Post
    1. That drop system sucks dicks.
    2. The error is caused by def.get*Rarity*Drops() returning null. You can't make an Optional.of(null). You'll have to first check and ensure that the array/list w/e of drops for the given rarity ain't null, and then handle the given drops.
    Do you know a drop system which is better ? that i can rip from ? not really experienced enough to create one from nothing.
    Reply With Quote  
     


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. PVP armor drop system
    By Delta* in forum Tutorials
    Replies: 54
    Last Post: 11-25-2010, 08:46 PM
  2. [Shard] Alternative NPC Drop System
    By Chachi in forum Snippets
    Replies: 6
    Last Post: 11-15-2009, 12:46 AM
  3. iClarity Drop system
    By GwasDdos in forum Help
    Replies: 2
    Last Post: 10-25-2009, 09:35 AM
  4. Alternate NPC Drop System
    By bloodargon in forum Tutorials
    Replies: 18
    Last Post: 07-15-2009, 07:45 AM
  5. Release the drops system
    By Silverz. in forum Requests
    Replies: 9
    Last Post: 02-16-2009, 04:08 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •