Manually Damaging Players

Discussion in 'Plugin Development' started by AnOrangeGamer, May 22, 2014.

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

    AnOrangeGamer

    I'm making a plugin for a server that has Towny installed, and the plugin involves combat. I figured out a way to bypass this but it won't work.

    Code:java
    1. if(p.getInventory().contains(new ItemStack(Material.GLOWSTONE_DUST))){
    2. p.damage(e.getDamage());
    3. Bukkit.broadcastMessage("OMG YOU'RE DAMAGING HIM");
    4. }


    This basically damages the player if that player is holding glowstone dust. The broadcast is a debug test.

    Any help? Thanks.
     
  2. Offline

    Aqua

    AnOrangeGamer
    What exactly are you trying to do?
    The code you provided will damage the player if he has glowstone dust in his inventory, use p.getItemInHand().getType() == Material.GLOWSTONE_DUST instead.
     
  3. Offline

    AoH_Ruthless

  4. Offline

    tastybento

    What is not working? You are not applying any damage to the player? What object is "e"?
     
  5. Offline

    AnOrangeGamer

    Aqua
    AoH_Ruthless
    tastybento

    Sorry, here's the extended version:

    Code:java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamageByEntityEvent e) {
    3. if(!(e.getEntity() instanceof Player)) return;
    4. if(!(e.getDamager() instanceof Player)) return;
    5. Player p = (Player)e.getEntity();
    6. if(p.getInventory().contains(new ItemStack(Material.GLOWSTONE_DUST))){
    7. p.damage(e.getDamage());
    8. Bukkit.broadcastMessage("OMG YOU'RE DAMAGING HIM");
    9. }
    10. }
     
  6. AnOrangeGamer
    But what are you trying to do? That's the main question here.
     
  7. Offline

    AnOrangeGamer

    Since there is Towny on the server, I need another way of dealing damage to the player. So, I'm trying to instead of having the ingame damage, I'll cancel the damage, then use p.damage to damage the player manually. But what I want to do is only have the player with glowstone dust to get damaged. But my method is not working.
     
  8. Offline

    tastybento

    Just to establish what you want:
    1. Do you want a player to be hurt/damaged if they pick up glowstone?
    2. Or do you want a player to be able to damage another player if they hit them with glowstone?
    3. Or do you want a player to be hurt if they are holding glowstone and they get hit by another player?

    # 3 then in my list?

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

    AnOrangeGamer

    Yes, number 3.
     
  10. AnOrangeGamer
    The code you have right now doesn't cancel the event damage. If I'm not mistaken here's what you're looking for:

    Code:java
    1. @EventHandler
    2. public void onPlayerDamage(EntityDamageByEntityEvent e) {
    3. if(!(e.getEntity() instanceof Player)) return;
    4. if(!(e.getDamager() instanceof Player)) return;
    5. Player p = (Player)e.getEntity();
    6. if(p.getInventory().contains(new ItemStack(Material.GLOWSTONE_DUST))){
    7. double dmg = e.getDamage();
    8. e.setCancelled(true);
    9. p.setHealth(p.getHealth()-dmg);
    10. Bukkit.broadcastMessage("OMG YOU'RE DAMAGING HIM");
    11. }
    12. }


    I'm not sure what Towny does because I've never actually used it buy from what I've read it cancels player damage on another player. So technically using the setHealth method, the players never hit eachother but the receiver still gets dealt damage since his health will go down.
     
  11. Offline

    AnOrangeGamer

    RAFA_GATO_FOFO
    Thanks, forgot to cancel the event. I'll try this way.
     
  12. Offline

    AoH_Ruthless

    RAFA_GATO_FOFO AnOrangeGamer
    That whole event is completely useless. You aren't doing anything at all .... You might as well just do nothing because you are damaging the player either way.
     
  13. Offline

    AnOrangeGamer

    Like I said before, the server has Towny. The plugin involves combat. If a player is in the same town as the other player, no damage will be dealt. This is why I'm using this method.
     
  14. Offline

    AoH_Ruthless

    AnOrangeGamer
    I mean that the event given to you might as well not be there ... you are damaging the players regardless. Look at the code. You are damaging the player the exact same amount as if you didn't have those 10 lines written at all ... You are just cancelling what Minecraft does internally and doing the same exact thing
     
  15. Offline

    AnOrangeGamer

    AoH_Ruthless
    Yes, I know that Minecraft does that internally, but when you have Towny on the server, it'll interfere with it. If both players are in the same town, they won't be able to attack each other. That's why I'm doing this.
     
  16. Offline

    AoH_Ruthless

    AnOrangeGamer
    You realize what I'm saying is that the event RAFA_GATO_FOFO gave you does absolutely nothing, right?

    Let's say the initial damage is 5, and the initial health was 20.

    This means if you did not have this event, the player's health at the end would be 15 (20 - 5 = 15)

    Code:java
    1. System.out.println(p.getHealth()); // prints 20, the if-check hasn't happened yet.
    2. if(p.getInventory().contains(new ItemStack(Material.GLOWSTONE_DUST))){
    3. System.out.println(p.getHealth()); // prints 20, the event hasn't happened yet.
    4. double dmg = e.getDamage();
    5. System.out.println(p.getHealth()); // prints 20, nothing has happened
    6. e.setCancelled(true);
    7. System.out.println(p.getHealth()); // prints 20, the event was cancelled.
    8. p.setHealth(p.getHealth()-dmg);
    9. System.out.println(p.getHealth()); // prints 15, you damaged the player.
    10. }
    11. System.out.println(p.getHealth()); // Outside the if-check, you didn't cancel the event. So if the itemstack wasn't glowstone dust, this would print 15 because they were damaged....
     
  17. Offline

    AnOrangeGamer

    AoH_Ruthless
    Ahhh, I get what you're saying right now. I have moved the setCancelled method outside of the if statement. Thanks for the help.
     
  18. Offline

    LlmDl

    You could also set the towny plots to arena (/plot set arena,) those plots do not care about friendly fire.
     
  19. Offline

    Garris0n

    Why don't you just...enable pvp with towny or something...
     
  20. Offline

    AnOrangeGamer

    It's not my server, I'm just an admin on that server. Me and the other admin usually install plugins that we create for the server, so players can have fun with it.
     
  21. Offline

    Garris0n

    So, tell the owner to do it. Or something. Using Bukkit to forcibly circumvent the plugin's PvP rules instead of just turning them off is incredibly backwards and illogical.
     
  22. AoH_Ruthless
    Well, as far as I know, Towny blocks player on player damage in certain areas. What I did was take the amount of damage and apply it in another fashion. So it's not useless, it just might not be helpful in this case.

    AnOrangeGamer
    Assuming Towny has this event cancelled in the plots whatever they're called, to override Towny just make the priority highest on the EventHandler.
     
  23. Offline

    AoH_Ruthless

    RAFA_GATO_FOFO
    No you didnt.. You didn't change the outcome of the event in anyway, the player is still damaged
     
  24. Offline

    ESSHD

    Hurt and damaged are both the same.
     
  25. AoH_Ruthless
    The player is not being damaged by the event, so yes I did change the outcome of the event.
     
  26. Offline

    AoH_Ruthless

    RAFA_GATO_FOFO
    They are being damaged still ... so you didn't effectively cancel it.
     
Thread Status:
Not open for further replies.

Share This Page