Not a lot of people seem to have very accurate NPC drop chance systems, so here you go. Enjoy.
I do NOT give permission to re-release and/or distribute this in anyway.
Code:
package org.rs_reborn.game.model.npcs.drop;
import java.util.Random;
/**
* Represents an npc drop chance.
*
* @author Michael P
*
*/
public enum NpcDropChance {
ALWAYS(1),
COMMON("2-50"),
UNCOMMON("51-100"),
RARE("101-512"),
VERY_RARE("513-1000");
/**
* The rate of the chance.
*/
private final Object rate;
/**
* The {@link Random} used for generating a random number.
*/
private final Random random = new Random();
/**
* Constructs new a {@link NpcDropChance}.
*
* @param rate
* The rate {@link Object}.
*/
private NpcDropChance(Object rate) {
this.rate = rate;
}
/**
* Checks to see if the chance is a success.
* @return If the chance is a success {@code true};
* if not {@code false}.
*/
public boolean isSuccess() {
if (rate instanceof String) {
String[] args = ((String) rate).split("-");
int n = getN(Integer.parseInt(args[0]), Integer.parseInt(args[1]));
int success = Integer.parseInt(args[0]);
return n % success == 0;
}
return true; // rate is 1 (ALWAYS), therefore it's a success 100%.
}
/**
* Gets a random number within a certain range.
*
* @param min
* The minimum number range.
* @param max
* The maximum number range.
* @return The random number.
*/
private int getN(int min, int max) {
final int n = Math.abs(max - min);
return Math.min(min, max) + (n == 0 ? 0 : random.nextInt(n));
}
}
Here is a dump of the drop attempts using this system for those who want to see the accuracy of it.
Code:
Took 0 attempts for: COMMON
Took 0 attempts for: COMMON
Took 2 attempts for: COMMON
Took 0 attempts for: COMMON
Took 2 attempts for: COMMON
<---- New Drop ---->
Took 162 attempts for: UNCOMMON
Took 8 attempts for: UNCOMMON
Took 112 attempts for: UNCOMMON
Took 2 attempts for: UNCOMMON
Took 39 attempts for: UNCOMMON
<---- New Drop ---->
Took 15 attempts for: RARE
Took 47 attempts for: RARE
Took 123 attempts for: RARE
Took 180 attempts for: RARE
Took 70 attempts for: RARE
<---- New Drop ---->
Took 335 attempts for: VERY_RARE
Took 518 attempts for: VERY_RARE
Took 1215 attempts for: VERY_RARE
Took 88 attempts for: VERY_RARE
Took 123 attempts for: VERY_RARE
<---- New Drop ---->