% chances

Discussion in 'Plugin Development' started by meowkittie, May 13, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    meowkittie

    I am trying to use random number generation for chances for certain variables to be changed during an event but can't seem to get it right, i just get tons of errors and it doesn't work.

    Here are my conditions. I want it to be so that if a player is crouching and holding a diamond block in hand, then there is a chance for them to take no damage or reduced damage, which i'm trying to do with random number generation.

    Code:
    Random chance = new Random();
    int chance1 = chance.nextInt(1000);
    Material inhand = ((PlayerInventory) event.getEntity()).getItemInHand().getType();
     
    //The rest of my code that already works
     
     
                 
    if (**Not sure how to find if player is crouching** && inhand == Material.DIAMOND_BLOCK);
    {
             
    if (chance1 > 650);
    fb = 0.0;
     
                     
    if (chance1 < 550);
    sd = 0.6;
                     
    if (chance1 < 8);
    player.setItemInHand(null);
    }
    
    I am not totally sure what is causing the errors, but the things I believe I may have screwed up are how to find if a player is sneaking or not, how to set the item in hand to nothing, and I also think I may be defining "inhand" incorrectly. Theres just too much I think I screwed up, so if you see something let me know.
     
  2. Offline

    coldandtired

    You get the error because you are casting an entity to a PlayerInventory. Cast to a player and then check the item in hand.

    Player also has an isSneaking() method which you can use.
     
Thread Status:
Not open for further replies.

Share This Page