Solved Each x Time

Discussion in 'Plugin Development' started by khave, Aug 7, 2014.

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

    khave

    I'll already say sorry for my english if it's hard to understand what I'm trying to explain. I'm not a country with english as their main language.

    What I want is each time the First Integer is at another Integer.
    Say, each time the Kills of the players hits 10,20,30,4o,50 etc. then do something. Is there an easy way to accomplish this? And what about a way to increment it?
     
  2. Offline

    Neymar11

    khave I assume you have some kind of a kill counter
    Here you go:
    Code:java
    1. if (killsCounter == 10)
    2.  
    3. {
    4. //Do something when the player has 10 kills
    5. }
    6. else if (killsCounter == 20)
    7. {
    8. //Do something when the player has 20 kills
    9. }


    Instead of the killsCounter place your variable in which you count the kills!
     
  3. Offline

    JBoss925

    If you want to do the same thing or call the same method every increment of 10, simply do:
    Code:java
    1. if(killcounter % 10 == 0){
    2. //method call or do stuff
    3. killcounter++;
    4. }
    5. else{
    6. killcounter++;
    7. }
     
  4. Code:java
    1. int Test = 10
    2. int Killcounter = player.getKills(); /*player.getKills(); does not exist in this case LOL*/
    3. if (Killcounter == Test){
    4. player.sendMessage("Quack");
    5. } /*Choose whatever you like*/
    6.  
     
  5. Offline

    khave


    Thank you that was what I was looking for.
    Somehow though this does not seem to work:
    Code:java
    1. int unarmed = manager.getPlayerConfig(hitter).getInt("Skills.Unarmed");
    2. int level = 10;
    3.  
    4. if(unarmed % level == 0){
    5. }


    Dw, I have tried testing it with a message and no message pops up. My config does get the number correctly too. The integer is 1


    Already knew you could do that, but it would take ages. I'd have to do that for every number divisible by 10...
     
  6. Offline

    AoH_Ruthless

    khave
    Full class please.
     
  7. Offline

    khave


    Code:java
    1. package me.khave.lowe.listeners;
    2.  
    3. import me.khave.lowe.Manager;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    9.  
    10. import java.io.IOException;
    11.  
    12. /**
    13. * Created by hellehave on 07/08/14.
    14. */
    15. public class EntityDamageByEntity implements Listener {
    16.  
    17. @EventHandler
    18. public void onEntityDamageByEntity(EntityDamageByEntityEvent e){
    19. if (!(e.getEntity() instanceof Player && e.getDamager() instanceof Player)) return;
    20.  
    21. Player p = (Player) e.getEntity();
    22. Player hitter = (Player) e.getDamager();
    23.  
    24. Manager manager = new Manager(hitter);
    25.  
    26. int unarmed = manager.getPlayerConfig(hitter).getInt("Skills.Unarmed");
    27. int level = 10;
    28.  
    29. if(unarmed % level == 0){
    30. hitter.sendMessage("Good job!");
    31. try {
    32. manager.getPlayerConfig(hitter).set("Skills.Unarmed", manager.getPlayerConfig(hitter).getInt("Skills.Unarmed") + 1);
    33. level = level * 2;
    34. hitter.sendMessage(ChatColor.GREEN + "Your Unarmed leveled up to " + manager.getPlayerConfig(hitter).getInt("Skills.Unarmed") + "!");
    35. manager.savePlayerConfig(hitter);
    36. } catch(IOException IO){
    37. IO.printStackTrace();
    38. }
    39. }
    40. }
    41. }
    42.  
     
  8. Offline

    AoH_Ruthless

    khave
    1. Do you know what value 'unarmed' is?
    2. Are your events registered?
    3. Could another plugin (if you have any) be conflicting?
     
  9. Offline

    khave

    1. Yes it's 1
    2. Yes
    3. I'm going to check right now, cause that's probably it xD
     

  10. What do you want in your config?
     
  11. Code:Java
    1.  
    2. int each = 10;
    3. for (int i = 0; i < 10000; i += each) {
    4. if (killsOfPlayer == i) {
    5. player.sendMessage("You have a killstreak of " + i + ".");
    6. break;
    7. }
    8. }
    9.  


    you can make the value 'each' too e.g 50. so it will send the player the message when he have 50, 100, 150, 200, 250 and so on.. kills :p
     
  12. Offline

    AoH_Ruthless

    FisheyLP
    Code offers no help to the problem at hand.

    khave If 'unarmed' is one than the code block will not execute. 1 % 10 = 1. % is modulus operator, or remainders. So, x % 10 will only equal zero if x is 10, 20, 30, etc... So, that code block does not execute if unarmed is 1.
     
  13. Offline

    khave


    Haha yea that's basic math. Derp xD. Though when I change it to something like 2 to 10 ( if(1 % 10 == 0) ) it still does not work. What am I doing wrong.. ;-;
     
  14. Offline

    AoH_Ruthless

    khave ... 1 % 10 == 0 will never execute because 1 % 10 is 1. Change your if check to - if (10 % 10 == 0)
     
  15. Offline

    khave

    That makes it paste the message each time I hit something though? ALSO: I didn't mean to offend you at the basic math. That was a derp towards myself, like I was dumb for not realizing
     
  16. Offline

    AoH_Ruthless

    khave
    Well yeah ... 10 % 10 == 0 will always execute.

    khave
    Your code is invalid because:

    unarmed = 1.
    if Unarmed % 10 = 0
    -- Add one to unarmed


    With that code above, unarmed will never be raised higher.

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

    khave


    I've tried adding one to unarmed so it's 2 % 10 = 0, but that doesn't make it say anything at all no matter how many times I hit an entity.
     
  18. Offline

    AoH_Ruthless

    khave
    That's also partly because you don't have any idea about what you are doing. As I have told you already, something like 2 % 10 can never have a remainder of 0. 2%10 is equal to 2. 3 % 10 is equal to 3. You need to actually read into the modulus operator in finer detail..
     
  19. Offline

    khave

    Okay I get that, but then how the heck am I supposed to make it do it each 10th time?

    EDIT: derp, I'm an idiot. I figured it out now. I now see what you mean by you saying that it can't have a remainder. I need to count each time the player punches an entity and have that instead of the config value. Thanks! ;3

    (Your math is wrong though. 2 % 10 is equal to 0.2 and 3 % 10 is equal to 0.3, as you move the comma each time you divide by a number in the tens, hundreds, thousands.)
     
Thread Status:
Not open for further replies.

Share This Page