Solved Damage Reduction

Discussion in 'Plugin Development' started by ProMCKingz, Jul 16, 2014.

?

Was this helpful to you?

  1. Yes,

    50.0%
  2. Nope!

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

    ProMCKingz

    Hello,
    I really wanted to know, if it is possible to reduce the damage of mobs and players, whilst you are holding a specific item in your hand (PISTON_BASE).
    My trial, which does not work
    Code:java
    1. @EventHandler
    2. public void mobdamage(EntityDamageByEntityEvent e) {
    3. Player entity = (Player) e.getEntity();
    4. if(entity instanceof Player) {
    5. //the attacker is a player
    6. e.isCancelled();
    7. } else {
    8. //the attacker is a mob
    9. Player p = (Player) e.getDamager();
    10. if(p instanceof Player) {
    11.  
    12. // gets the damager
    13. Player d = (Player) e.getDamager();
    14.  
    15. // if the damager is holding a regular piston
    16. if (d.getItemInHand().getType().equals((Material.PISTON_BASE))){
    17.  
    18. // set the damage that the damager is dealing to the target
    19. e.setDamage(5);
    20. }
    21. // if the damager is holding a sticky piston
    22. if (d.getItemInHand().getType().equals(Material.PISTON_STICKY_BASE)){
    23.  
    24. // set the damage that the damager is dealing to the target
    25. e.setDamage(1);
    26. }
    27. }
    28.  
    29. }
    30. }}
     
  2. Offline

    ImIreEZz

    I dont know if it makes any difference but i use
    Code:
    if (d.getItemInHand().getType() == Material.PISTON_STICKY_BASE)){
        e.setDamage(1);
    Try using that?
     
  3. Wait... why are you calling e.isCancelled() without associating it to a variable? It simply returns whether the event has been cancelled. Also, your ClassCastException safety if beyond broken. You need to check if the attacker/target is an "instanceof Player" before casting, otherwise you'll be spamming the console with errors.
    Gosh, it's the second time I have to point this out to someone in here today!
    Also, your instanceof checks are redundant, you're checking if the damager is a player AFTER casting, you should check first, if true, then cast and do your magic.
    If you want to reduce the damage, you have to think, how much damage is being dealt? And by how much do you want to reduce it? 50%? You should multiply whatever ratio you're using by e.getDamage(). And don't reduce/set the damage to a magic value unless you know exactly what you're doing, otherwise you might end up healing the player.
     
  4. Offline

    NoChanceSD

    ProMCKingz You can't cast an entity to Player before checking if it's a Player.
    Also, if an entity is not an instance of Player doesn't mean it's a mob, it could also be a Projectile.
     
  5. Offline

    xTigerRebornx

    ImIreEZz Your code's logic is all messed up
    Code does something like this:
    if player is entity:
    randomly call e.isCancelled() (?)
    else:
    cast the entity to a Player even though he clearly isn't
    check if the entity that can never be a Player is a Player after casting:
    (...)

    You need to fix the logic. Take some time to sit down and plan out how you want your code to flow instead of just writing stuff that makes no sense.
     
  6. Offline

    ProMCKingz

    Sorry guys, I have just started to code:'(
    And am still learning the basics

    NoChanceSD
    Hey!
    You seem to be looking good with this kind of stuff, since your plugins are all based on PvP.
    So do I have to make my code again from scratch? if so, can you please guide me, since I am a noob

    Do I need to create separate ones for different mobs?

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

    NoChanceSD

    ProMCKingz You just need to check properly what type of entity you have before casting anything.

    First you should check if e.getEntity() is not a Player and just "return;" since it doesn't matter to you.
    Then as you are sure you have a Player being attacked, you can cast entity to Player, check if he has a Piston in his hand and modify the damage as you want.

    If you do this, you will only modify damage when a player is attacker by either a mob or other player and holding a piston, which is what you want i believe.
     
  8. Offline

    ProMCKingz

    I want it so that a player (not holding) the item and/or a mob, when they hit the person with a PISTON_BASE, the person with the piston base has less damage than usual.
     
  9. Offline

    NoChanceSD

    ProMCKingz Yes, that's what i said. But does it matter if the attacker has a piston too?
     
  10. Offline

    ProMCKingz

    1.Ermm, do you mean if he has a piston, it will have the same affect as him not having it? and will reduce the damage
    2.Or, do you mean him having a piston, will give the affect of less damage? But him not having a piston gives the same damage

    If 1. then that's fine,
    And if you have any video links or tutorials to help me learn from this, please post a link.
    Thanks for your help so far!

    NoChanceSD
    Ermm, how can I do this?
    I tried - but I get errors - Note I am a noob at this
    Code:java
    1. @EventHandler
    2. public void mobdamage(EntityDamageByEntityEvent e) {
    3. Player entity = (Player) e.getEntity();
    4. if(entity instanceof Player) {
    5. //the attacker is a player
    6. Player p = (Player) e.getDamager();
    7. if(p instanceof Player) {
    8. return;
    9.  
    10. if (e.getPlayer().getItemInHand().getType() == Material.PISTON_BASE){
    11.  
    12.  
    13. // set the damage that the damager is dealing to the target
    14. e.setDamage(5);
    15. }
    16. }
    17.  
    18. }
    19. }}


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

    NoChanceSD

    ProMCKingz That's pretty much step by step instructions... Anyway, i will show you what i meant and hopefully you understand.
    I didn't test this code but it should work.
    Code:java
    1. @EventHandler
    2. public void onDamage(EntityDamageByEntityEvent e) {
    3. //First you should check if e.getEntity() is not a Player and just return;
    4. //since it doesn't matter to you
    5. if (!(e.getEntity() instanceof Player))
    6. return;
    7. //Then as you are sure you have a Player being attacked, you can cast entity to Player
    8. Player entity = (Player) e.getEntity();
    9. //check if he has a Piston in his hand
    10. if (entity.getItemInHand().getType() == Material.PISTON_BASE) {
    11. //and modify the damage as you want
    12. }
    13. }
    14. }
     
  12. Offline

    ProMCKingz

    NoChanceSD
    Thanks alot!
    This is really going to benefit me and the community!
    And encourage me to continue to learn JAVA
    [SOLVED]
     
Thread Status:
Not open for further replies.

Share This Page