Question

Ok, i am creating a bukkit plugin which uses this event:

public void onCreatureSpawn(CreatureSpawnEvent e) {
    // Do stuff
}

Inside of the event i need and if statement to fire every (x) amount of time the Creature event has been fired.

For instance if 10 creatures spawned, out of those 10, i would need the if statement to fire 2 times.

Out of the two creatures, 2 of them will get a special characteristic. My issue is, how can i make it so where this statement only fires (x) out of the many times it has been fired?

I have also tried something along the lines of the example below, but ran into some issues:

int outOf = Plugin.getConfig().getInt("armourSpawn");
int count = 1;
if(count%outOf==0) {
    // This will fire (x) out of (x) times
}

count++;

Basically, the user will chose (x) out of 10 Monsters will have some certain characteristic in the config.

Out of ALL the creatures (that are monsters) that spawn i want a ration of (x) to 10 that have the characteristic.

Was it helpful?

Solution

To better illustrate the Random choice solution:

import java.util.Random;

Random rand = new Random();
int percentage = 25; //Set this to whatever percentage you want.

public void onCreatureSpawn(CreatureSpawnEvent e) {
  //This will generate a number between 0 and 99 (inclusive)
  int generatedNumber = rand.nextInt(100);

  //If the generated number is smaller than the percentage you chose, you have "success".
  if(generatedNumber < percentage){
    addCharacteristic(creature, characteristic);
  }else{
    //Do whatever you need to normally.
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top