Solved Damage an entity through code for 1.7.2

Discussion in 'Plugin Development' started by Ward1246, Jan 27, 2015.

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

    Ward1246

    I have tried damaging a entity for 1.7.2 but i can't get it to work, what i need is a entitydamage code that works by event (I got this) and it makes mobs near the player get damaged. I want it to damage players as well, but not the person causing the damage, so basically it hurts every mob even players in a specific range except does not damage the user. All i need is the damaging code, i have part of the code here:

    for (Entity e : player.getNearbyEntities(3, 3, 3))

    Full plugin code (open)

    import net.minecraft.server.v1_7_R3.DamageSource;

    import org.bukkit.Material;
    import org.bukkit.block.BlockFace;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.Vector;

    public class Exoskeleton extends JavaPlugin implements Listener {



    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("exoskeleton")) {


    return true;
    } else {
    sender.sendMessage("You must be a player!");

    }
    return false;
    }

    public void onEnable() {
    getServer().getPluginManager().registerEvents(this, this);

    }

    @EventHandler
    public void onPlayerMove(PlayerMoveEvent event) {
    final Player player = event.getPlayer();
    if(player.getInventory().getChestplate().getType() == Material.IRON_CHESTPLATE) {
    if (player.isFlying()) {
    player.setVelocity(new Vector(player.getVelocity().getX(), 1,
    player.getVelocity().getZ()));
    player.setFlying(false);
    player.setAllowFlight(false); }
    if (player.getAllowFlight() == (false)){
    event.isCancelled(); }

    if (player.isSneaking())
    getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    public void run() {
    player.setSneaking(false);
    }
    }, 10L);

    {
    if (player.isSneaking())
    if (!(player.getLocation().getPitch() < 0))
    player.setVelocity(player.getLocation().getDirection()
    .multiply(2));

    if(!(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR)) {
    player.setAllowFlight(true);
    }
    {
    if (player.isSneaking())
    if (!(player.getLocation().getPitch() < 0))
    player.setVelocity(player.getLocation().getDirection()
    .multiply(2));
    }
    if (player.isSneaking())
    if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR) {
    player.setVelocity(new Vector(player.getVelocity().getX(), -2,
    player.getVelocity().getZ()));
    {
    if (player.isSneaking()) {
    if(!(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.AIR)) {
    if (!(player.getLocation().getPitch() < 0))
    player.setVelocity(player.getLocation().getDirection()
    .multiply(5));

    if (player.isSneaking()) {
    for (Entity e : player.getNearbyEntities(3, 3, 3))
    //this will be where the damaging entity stuff goes. currently has an error as it is unfinished.
    }
    }
    }
    }
    }
    }
    }
    }
    }



    I tried doing some damaging stuff but it had errors. i can make the rest on my own.

    TL;DR I need a code to damage all entities and players, but not hurt the user.
    Thanks for any help.
     
  2. Offline

    Skionz

    @Ward1246
    LivingEntity#damage()
    Just use a series of if statements to control the fl0w of the program. It should be pretty common sense.
     
  3. Offline

    pie_flavor

    Code:
    if (e instanceof Damageable) {
      Damageable e2 = (Damageable) e;
      e2.setHealth(e2.getHealth() - $damageamount);
    }
     
  4. Offline

    Ward1246

    That was fast! @pie_flavor That code has no errors in eclipse, but didn't damage anything in minecraft. The code is :
    if (player.isSneaking()) {
    for (Entity e : player.getNearbyEntities(3, 3, 3))
    if (e instanceof Damageable) {
    Damageable e2 = (Damageable) e;
    e2.setHealth(e2.getHealth() - 20);
    Let me know if i did something wrong.

    @Skionz How would i use that? Sorry, i am new to working with entities and altering them. Thank you both though.
     
  5. Offline

    Skionz

  6. Offline

    Ward1246

    My bad, i set the entities health to 20, there was a space between the - and the 20, fixed it thanks for the help! For the sake of knowledge i will still read that link you gave me. Wow, thanks for the quick fix both of you!
     
  7. Offline

    SuperOriginal

    @Ward1246 A space doesn't make a difference here..
     
    Last edited: Jan 28, 2015
  8. Offline

    Ward1246

    It did for this time. Look here for example. e2.setHealth(e2.getHealth() - 20); that space between the '-' and the '20' makes the damage "-" (this is what it looks like it does at least). Anyway when we remove the space e2.setHealth(e2.getHealth() -20); it makes the damage "-20". This one time it did for me at least, try both if you don't believe me the one with the space did nothing, then the one without did damage.
     
  9. Offline

    SuperOriginal

    @Ward1246 That makes no sense. You're subtracting 20 either way.

    "The term white space refers to characters of a file that you can't see — syntactically insignificant spaces, tabs, and line breaks. In Java, white space does not matter." Quoted directly out of a Java book.
     
    Last edited: Jan 28, 2015
    Krizeh likes this.
  10. Offline

    Ward1246

    In this case it didn't work the first time. I'm glad it worked, and i am sure some way some how there can be some cases where a space matters. Either way, no need to get in an argument over it, without lying it truly did not work the first time with the space. If you don't believe me that's ok, but i know it did not work the first time.
     
  11. Offline

    pie_flavor

    @SuperOriginal Explain to me how this space is not necessary.
    Code:
    Player p;
     
  12. Offline

    Ward1246

    [Now it seems as if it only kills certain mobs, some natural spawned ones don't die some unnatural spawned ones don't die either, i think it depends on the entities health] Is there a way i can fix this new error? The code above only kills mobs i spawn with spawn eggs or /summon, and does not kill passive mobs it seems. There is a crash report and i think it's because the mobs heath is out of range, or maybe i could test for certain entities, and do something with them. (I will mark this as unresolved so it isn't ignored as much)
    Crash report (open)

    [19:13:12 ERROR]: Could not pass event PlayerMoveEvent to Exoskeletons v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:486) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :235) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInFlying.a(SourceFile:137) [cr
    aftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.PacketPlayInPositionLook.handle(SourceFi
    le:20) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:157
    ) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:6
    67) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    60) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    58) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :469) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.IllegalArgumentException: Health must be between 0 and 4.0
    at org.bukkit.craftbukkit.v1_7_R3.entity.CraftLivingEntity.setHealth(Cra
    ftLivingEntity.java:81) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b309
    2jnks]
    at me..Exoskeletons.Exoskeleton.onPlayerMove(Exoskeleton.java:88)
    ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _72]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
    java:57) ~[?:1.7.0_72]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
    sorImpl.java:43) ~[?:1.7.0_72]
    at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_72]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
     
    Last edited: Jan 28, 2015
  13. Offline

    pie_flavor

    @Ward1246
    Passive mobs do not have 20 health. They have a lot less.
    If you're just trying to kill 'em, just do setHealth(0).
     
  14. Offline

    Ward1246

    I would like to just kill them up to 20 health, if there is none, isn't there a way i could test their health? like this maybe

    if (e2.getHealth() == 1)
    e2.setHealth(e2.getMaxHealth() -1);

    I tried that but nothing happens, i made the code go all the way to 21
    Code (open)

    if (player.isSneaking()) {
    for (Entity e : player.getNearbyEntities(3, 3, 3))
    if (e instanceof Damageable) {
    Damageable e2 = (Damageable) e;
    e2.setHealth(e2.getHealth() -20);
    if (e2.getHealth() == 1)
    e2.setHealth(e2.getMaxHealth() -1);

    if (e2.getHealth() == 2)
    e2.setHealth(e2.getMaxHealth() -2);

    if (e2.getHealth() == 3)
    e2.setHealth(e2.getMaxHealth() -3);

    if (e2.getHealth() == 4)
    e2.setHealth(e2.getMaxHealth() -4);

    if (e2.getHealth() == 5)
    e2.setHealth(e2.getMaxHealth() -5);

    if (e2.getHealth() == 6)
    e2.setHealth(e2.getMaxHealth() -6);

    if (e2.getHealth() == 7)
    e2.setHealth(e2.getMaxHealth() -7);

    if (e2.getHealth() == 8)
    e2.setHealth(e2.getMaxHealth() -8);

    if (e2.getHealth() == 9)
    e2.setHealth(e2.getMaxHealth() -2);

    if (e2.getHealth() == 10)
    e2.setHealth(e2.getMaxHealth() -10);

    if (e2.getHealth() == 11)
    e2.setHealth(e2.getMaxHealth() -11);

    if (e2.getHealth() == 12)
    e2.setHealth(e2.getMaxHealth() -12);

    if (e2.getHealth() == 13)
    e2.setHealth(e2.getMaxHealth() -13);

    if (e2.getHealth() == 14)
    e2.setHealth(e2.getMaxHealth() -14);

    if (e2.getHealth() == 15)
    e2.setHealth(e2.getMaxHealth() -15);

    if (e2.getHealth() == 16)
    e2.setHealth(e2.getMaxHealth() -16);

    if (e2.getHealth() == 17)
    e2.setHealth(e2.getMaxHealth() -17);

    if (e2.getHealth() == 18)
    e2.setHealth(e2.getMaxHealth() -18);

    if (e2.getHealth() == 19)
    e2.setHealth(e2.getMaxHealth() -19);

    if (e2.getHealth() == 20)
    e2.setHealth(e2.getMaxHealth() -20);

    if (e2.getHealth() >= 21)
    e2.setHealth(e2.getMaxHealth() -20);

    I would like to know what i did wrong there/how i could make it work. If i could do that it would be great, if not i will just set their health to 0. Thank you again for all the help.
     
  15. Offline

    pie_flavor

    @Ward1246 That's a lot of code.
    Code:
    if (e2.getMaxHealth() <= 20) {
      e2.setHealth(0);
    } else {
      e2.setHealth(e2.getMaxHealth() - 20);
    }
     
  16. Offline

    Ward1246

    @pie_flavor I can see where this is going, but it says that e2.setHealth(0); is deprecated, and seems to do nothing. Sorry if i am troubling you. If there is any solution ill try that, or if necessary i could use that huge code i had from before.
     
    Last edited: Jan 28, 2015
  17. Offline

    1Rogue

    I have no idea why you aren't just doing:

    Code:java
    1. Entity e = /* entity that caused damage, such as a player */;
    2. Damageable d = /* your damageable */;
    3. d.damage(20D, e);
     
  18. Offline

    Ward1246

    Sorry, there was a problem and it made the damage appear to do nothing (The damage event wasn't getting called , i was missing a bracket and so i never could test the cod) Thank you all for the help, and this is now 100% solved.
     
  19. Offline

    SuperOriginal

    When did I say that wasn't necessary?
     
Thread Status:
Not open for further replies.

Share This Page