Setting procentage of doing things, HOW?

Discussion in 'Plugin Development' started by quinster08, Sep 4, 2013.

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

    quinster08

    Hey guys i want to set 33% of giving myself poison effect and 33% chance of giving the player that i hit poison effect, ik how to do the poition thing but how can i do the procentage thingy? Plz help.
     
  2. Offline

    FurmigaHumana

    This might work:

    Code:java
    1. Random rnd = new Random(101);
    2. if (rnd.nextInt(101) <= 33) {
    3. // 33% of change of doind something
    4. }
     
  3. Offline

    FozZeW

    this is really simple java Random usage

    Random r = new Random();
    //gives random number from 0-2
    int randomNumber = r.nextInt(3);
    //do randomNumber++ if you want it from 1-3

    if(randomNumber == 1){
    //33% chance to get here
    }else if(randomNumber == 2){
    //another 33 % chance to get here
    }else{
    //and yet another 33% to get here
    }
     
  4. Offline

    quinster08

    FozZeW It will gives a error by the Random<-(error)r = new Random<-(error)(); thing
     
  5. Offline

    sheigutn

    it won't, import java.util.Random;
     
  6. Offline

    viper_monster

    quinster08
    import "java.util.Random;" ?
    EDIT: Ninja'd
     
  7. Offline

    quinster08

    sheigutn I imported it and then it will say "Remove" as error
     
  8. Offline

    sheigutn

    Show us your code please, post it in [.syntax] tags (without the dot)
     
  9. Offline

    quinster08

    Code:java
    1. import java.util.Random;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. import org.bukkit.potion.PotionEffect;
    10. import org.bukkit.potion.PotionEffectType;
    11.  
    12. public class Main extends JavaPlugin implements Listener {
    13.  
    14.  
    15. @Override
    16. public void onEnable() {
    17. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    18. }
    19.  
    20. @Override
    21. public void onDisable() {
    22.  
    23. }
    24.  
    25. @EventHandler
    26. public void Simoons(EntityDamageByEntityEvent e){
    27. if(!(e.getEntity() instanceof Player)){
    28. return; }
    29. if(!(e.getDamager() instanceof Player)){
    30. return;
    31. }
    32. Player PlayerWhoGotHit = (Player)e.getEntity();
    33. Player PlayerWhoHit = (Player)e.getDamager();
    34. if(!(PlayerWhoHit.getItemInHand().getItemMeta().getDisplayName().equals("Kikuichimonji "))){
    35. return;
    36. Random r = new Random(); <- error in eclipse - quick fix; remove
    37. int randomNumber = r.nextInt(3);
    38. if(randomNumber == 1){
    39. e.setDamage(5.0);
    40. PotionEffect poison = new PotionEffect(PotionEffectType.POISON, 65, (int) 0.5);
    41. PlayerWhoGotHit.addPotionEffect(poison);
    42. }else if(randomNumber == 2){
    43. e.setDamage(5.0);
    44. }else if (randomNumber == 3) {
    45. e.setDamage(5.0);
    46. PotionEffect poison = new PotionEffect(PotionEffectType.POISON, 65, (int) 0.5);
    47. PlayerWhoHit.addPotionEffect(poison);
    48. }
    49. }
    50. }
    51. }
    52.  

    sheigutn Ikr
     
  10. Offline

    sheigutn

    add an "}" after the "return;" and before "Random" and it should work ;) and remove one "}" of the 4 "}"
     
  11. Offline

    quinster08

    sheigutn Nice will give no errors for so far - will test it if it real works in-game

    sheigutn Nope it loads in console :( But it doesnt give potion effects :(How can i fix this?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  12. Offline

    sheigutn

  13. Offline

    quinster08

  14. Offline

    sheigutn

    quinster08 try to debug logging messages in the console
    Did you add a plugin.yml?
     
  15. Offline

    quinster08

    Code:
    [SEVERE] Could not pass event EntityDamageByEntityEvent to Kikinogwatte
    s v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callEvent(Craf
    tEventFactory.java:95)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callEntityDama
    geEvent(CraftEventFactory.java:383)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.handleEntityDa
    mageEvent(CraftEventFactory.java:408)
            at net.minecraft.server.v1_6_R2.EntityLiving.damageEntity(EntityLiving.j
    ava:614)
            at net.minecraft.server.v1_6_R2.EntityHuman.damageEntity(EntityHuman.jav
    a:704)
            at net.minecraft.server.v1_6_R2.EntityPlayer.damageEntity(EntityPlayer.j
    ava:370)
            at net.minecraft.server.v1_6_R2.EntityHuman.attack(EntityHuman.java:874)
     
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :1115)
            at net.minecraft.server.v1_6_R2.Packet7UseEntity.handle(SourceFile:36)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :118)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.NullPointerException
            at Me.krickie.Main.Simoons(Main.java:40)
            at sun.reflect.GeneratedMethodAccessor14.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 21 more
    sheigutn It gives 1 error in console, and yes i have a plugin.yml ofcourse i do im not that dumb lol.
     
  16. Offline

    sheigutn

    quinster08 It was only a question^^ What's line 40?
     
  17. Offline

    quinster08

    sheigutn line 40 is this;int randomNumber = r.nextInt(3);
     
  18. Offline

    sheigutn

    CaptainBern I asked if he had a plugin.yml before he posted the stacktrace.
     
  19. Offline

    quinster08

  20. quinster08 You probably didn't defined the Random variable properly, let me take a look...
     
  21. Offline

    quinster08

  22. Offline

    quinster08

    CaptainBern I dont understand that im not that good with bukkit - im new :)
     
  23. quinster08 That's what we call Java my young padawan.
     
    Chinwe likes this.
  24. Offline

    quinster08

    CaptainBern Yeah very fine for java but i dont get it, i can't learn java in 2 secs
     
  25. quinster08 I give you 5 minutes. Lol no just kidding, uhrm well take a look at furminga humana's post, try that code. (it's the second answer on this thread)
     
  26. Offline

    FozZeW

    Are you sure you dont have any returning before line 40?

    @quinster08 I told u doing r.nextInt(3); will generate number from 0-2 not 1-3 so you need to just add 1 to it
    like randomNumber++; or just doing r.nextInt(3) + 1;

    Tip that you must remember FOREVER java starts counting from 0 not from 1

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  27. FozZeW I hate java's indexing -.-
     
Thread Status:
Not open for further replies.

Share This Page