Solved Need help making a player bleed 1/2 heart

Discussion in 'Plugin Development' started by Aiddamaid, Aug 4, 2014.

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

    Aiddamaid

    Hello, I have been learning Java for the past week or 2 and have come into a bump on the plugin i am working on. I want the plugin to make you "Bleed" 1/2 Hearts every 20 seconds or so, I tried using the wither effect but that sadly didn't work because i cannot alter the strength to a decimal or percent. This is the code I have so far:
    Code:java
    1. package com.aidan.TeamHeal;
    2.  
    3. import org.bukkit.entity.Entity;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.event.EventHandler;
    6. import org.bukkit.event.Listener;
    7. import org.bukkit.event.entity.EntityDamageByEntityEvent;
    8. import org.bukkit.potion.PotionEffect;
    9. import org.bukkit.potion.PotionEffectType;
    10.  
    11.  
    12. public class BukkitListener implements Listener {
    13.  
    14. public static Bukkit plugin;
    15.  
    16. public BukkitListener(Bukkit instance) {
    17. plugin = instance;
    18. }
    19.  
    20. @EventHandler
    21. public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    22. if(Math.random() <0.10) {
    23. Entity e = event.getEntity();
    24. // Entity z = event.getDamager();
    25.  
    26. if(e instanceof Player) {
    27. Player player = (Player) e;
    28.  
    29. player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 100, 1));
    30.  
    31. }
    32.  
    33.  
    34. }
    35. if(Math.random()<0.30) {
    36. Entity e = event.getEntity();
    37.  
    38. if(e instanceof Player) {
    39. Player player = (Player) e;
    40.  
    41. player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 100000, 0));
    42. }
    43.  
    44.  
    45. }
    46. }
    47.  
     
  2. Offline

    Aengo

    Aiddamaid Can't you just use a scheduler and Player.damage(1.0D); ?
     
  3. Offline

    mine-care

    Aiddamaid as Aengo said, use a sheduler :) i sugest a repeating task and every X seconds loop thru all players in the server and do the damage thing
    PS: just a tim you may alreaby be aware of but... ok, in bukkit when u do sethealth(gethealth-1) the -1 is a/2 if a heart :)
     
  4. Offline

    Aiddamaid

    Aengo mine-care , Ok, I will try your methods of doing this, thanks for the help, Just one question, i can use this on just specific players that get damaged by entity's, right?
     
  5. Offline

    mine-care

    Aiddamaid wait wait wait, you want the players to be damaged when they are hit by a entity or randomly damage them?
     
  6. Offline

    Aiddamaid

    mine-care I want the players to have a %10 chance that they will bleed for __ amount of time until they heal themselves, and i need them to bleed 1/2 heart every 20 seconds.
     
  7. Offline

    mythbusterma

    Aiddamaid

    Create a repeating task that damages all players in a list using a random number to determine whether they will be damaged. Add and remove players from this list when the are "poisoned" or "damaged."
     
    Aiddamaid likes this.
  8. Offline

    mine-care

  9. Offline

    Aiddamaid

    Ok, So this is the new code I have for the bleeding effect but it doesn't make you bleed, am i doing something wrong with it?
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
    3. if(Math.random()<0.10) {
    4. Entity entity = event.getEntity();
    5.  
    6. if(entity instanceof Player) {
    7. Player player = (Player) entity;
    8.  
    9. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    10. public void run() {
    11. EntityDamageEvent.DamageCause.STARVATION;
    12. }
    13. }, 20, 300);
    14.  
    15. }
    16. }
    17. }
     
  10. Offline

    kps1796

    Aiddamaid Change
    Code:java
    1. EntityDamageEvent.DamageCause.STARVATION;
    to
    Code:java
    1. player.damage(1.0D);
     
  11. Offline

    Aiddamaid


    I changed the damageEvent to player.damage but it is not repeatedly damaging me every 15 seconds, Do you know a fix for that?
     
  12. Offline

    kps1796

    Aiddamaid You're only factoring in the randomness when scheduling the task.
    Make it factor randomness on every task run, like so:
    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
    3. Entity entity = event.getEntity();
    4. if(entity instanceof Player) {
    5. Player player = (Player) entity;
    6. getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
    7. public void run() {
    8. if(Math.random()<0.10) {
    9. EntityDamageEvent.DamageCause.STARVATION;
    10. }
    11. }
    12. }, 20, 300);
    13. }
    14. }
     
  13. Offline

    Aiddamaid


    Everything you helped me with worked out, is there any way to make it so that when you are bleeding, and you get damaged, you won't get another bleeding effect, because currently the bleeding effects can stack so every 15 seconds you lose 5 hearts instead of 1 half heart, this is my updated code:

    Code:java
    1. @EventHandler
    2. public void onEntityDamageByEntityEvent(EntityDamageEvent event) {
    3. Entity e = event.getEntity();
    4.  
    5.  
    6.  
    7. plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() {
    8. public void run() {
    9. if(Math.random()<=1);
    10.  
    11. if(e instanceof Player) {
    12. Player player = (Player) e;
    13.  
    14. player.damage(1.0D);
    15. player.sendMessage(ChatColor.RED + "You are bleeding, find a bandage and heal yourself");
    16. }
    17.  
    18. }
    19. }, 20, 300);
    20. }
    21.  
    22. }
     
  14. Offline

    kps1796

    Aiddamaid You could make a List<String> for the names of players that are bleeding. If the list contains the player, don't let them bleed again. Once they bandage up, remove them from the list.
     
  15. Offline

    Aiddamaid


    Ok, i will try that,

    kps1796 So I made it so the players only get damaged once, but how would you make a piece of paper on right click cancel the event?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page