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)