Solved Please Help, Why Math Doesn't Work

Discussion in 'Spigot Plugin Development' started by Nbrthx, Jul 14, 2021.

  1. why the rdc value is the same as dmg in this code
    why math doesn't work
    how to fix it

    code:
    Code (Java):
    @EventHandler
        public void onDamage(EntityDamageEvent event) {
            if(event.getEntity() instanceof Player) {
                PlayerLevelManager playerLevelManager = levelManagerHashMap.get(event.getEntity().getUniqueId());
                int lvl = playerLevelManager.getLevel();
                int dmg = (int) event.getDamage();
             
                int maxmath = Integer.max(lvl/5, (lvl-(4 * dmg)/8));
                int total = Integer.min(20, maxmath);
                // total output: 20
             
                event.getEntity()
                .sendMessage("dmg: "+dmg+" | rdc: "+(dmg * (1 - total / 25)));
                // dmg output: 3
                // rdc output: should be 0.6 but why this output 3
                event.setDamage(dmg * (1 - total / 25));
            }
        }
     
  2. You're using integer division for all of your division operations. This probably isn't what you want to do. You'll have to make sure at least one of the operands is a double.
     
    • Like Like x 1
    • Agree Agree x 1
  3. hey, that's right, I tried converting all integers to double, and it's working

    code:
    Code (Java):
    @EventHandler
        public void onDamage(EntityDamageEvent event) {
            if(event.getEntity() instanceof Player) {
                PlayerLevelManager playerLevelManager = levelManagerHashMap.get(event.getEntity().getUniqueId());
                int lvl = playerLevelManager.getLevel();
                double dmg = event.getDamage();
               
                double maxmath = Double.max(lvl/5, (lvl-(4 * dmg)/8));
                double total = Double.min(20, maxmath);
                // a
               
                event.getEntity()
                .sendMessage("dmg: "+dmg+" | rdc: "+(dmg * (1 - total / 25)));
                // a
                // a
                event.setDamage(dmg * (1 - total / 25));
            }
        }