Solved Math.

Discussion in 'Plugin Development' started by Squid_Boss, Jul 6, 2014.

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

    Squid_Boss

    If I'd be willing to put in the time I could probably fix a math issue, but why not save time and ask others?

    So, in my CaptureFlag runnable it increases a number for each player near the flag in the blue team, and decreases for each player near the flag in the red team. When it hits 30+ (blue), or -30-(red), the flag gets captured. I need to apply this to a dragon's health bar (which has a max health of 200) so that if there is one player capturing the flag after 30 seconds the dragon's bar needs to be at 200. But, as more and more players from opposing teams approach the flag, the dragon bar needs to increment up perfectly so that it will always hit exactly 200 when a team captures the flag.

    It's a bit tricky to explain, but here is a snippet of my code to show what's going on:
    Code:java
    1. if (g.getBluePlayers().contains(p.getUniqueId())) {
    2. timer++;
    3. } else if (g.getRedPlayers().contains(p.getUniqueId())) {
    4. timer--;
    5. }
    6.  
    7. if (timer >= 1) {
    8. Game_DragonBar.displayDragonBar(ChatColor.DARK_BLUE + "Blue " + ChatColor.AQUA + "is capturing flag " + ChatColor.GOLD + flagName + ChatColor.AQUA + "!", p, dragonSpawn, timer * (20 / 3));
    9. } else if (timer <= -1) {
    10. Game_DragonBar.displayDragonBar(ChatColor.DARK_RED + "Red " + ChatColor.RED + "is capturing flag " + ChatColor.GOLD + flagName + ChatColor.RED + "!", p, dragonSpawn, timer * (20 / 3) * -1);
    11. } else if (timer == 0) {
    12. Game_DragonBar.deleteDragonBar(p);
    13. }

    The "timer * (20/3)" sets the dragonbar's health to exactly 200 at 30 seconds if only one player from one team is capturing the flag.

    So yea this has my head spinning a bit so if anyone wants to help feel free. If it's any sort of non-constructive criticism though I'd much prefer it not be posted.
     
  2. Offline

    JasonDL13


    Yeah, lets just let other people fix my plugins for me.
     
  3. Offline

    xTigerRebornx

    Squid_Boss Are you using BarAPI to set the bar? If not, what are you using (and could you provide the source?)
    BarAPI has a nice method that'll display a bar with a certain percentage filled. Since you want to "save time" by not doing the math yourself, use that.
    Basic Math: x/30 returns a decimal. Take care to not do the division with integers (Integer division will round it to the closest integer), convert them to floats and divide a float by a float to get a float, then use BarAPI's method that takes a float percentage (note percentage, not decimal, you'll most likely need to convert the decimal to a percent)
     
  4. Offline

    Necrodoom

    CTF games would handle it by if its above 200, set it to 200.
     
  5. Offline

    Squid_Boss

    xTigerRebornx No I'm not using the BarAPI. Thanks with the whole "percentages" thing. I just converted the "x/30" into a percentage, and then took that percentage and found out the % of 200. Works perfectly now, so thanks. :p
    Incase anyone is wondering:
    Code:java
    1. float health = ((((float) timer / 30F) * 100F) * 200F) / 100F;
     
Thread Status:
Not open for further replies.

Share This Page