Solved Merge Two Seperate Events

Discussion in 'Plugin Development' started by RUDD33, Oct 15, 2013.

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

    RUDD33

    So I am trying to figure out a way to merge the two events posted below so that it checks if the "Spider Eye" is in the player's hot bar before the poison effect is added to the player. If the "Spider Eye" is not in the inventory's hot bar, then I don't want the arrow to poison the player.

    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void damageListener(EntityDamageEvent e) {
    3. if (e.isCancelled()) return;
    4.  
    5. if (e instanceof EntityDamageByEntityEvent) {
    6. EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent)e;
    7.  
    8. if (evt.getEntity() instanceof LivingEntity) {
    9. LivingEntity ent = (LivingEntity)evt.getEntity();
    10. Entity dam = evt.getDamager();
    11.  
    12. if (dam instanceof Arrow) {
    13. ent.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 80, 1));
    14. }
    15. }
    16. }
    17. }
    18. @EventHandler
    19. public void onPlayerInteract(PlayerInteractEvent event) {
    20. Player player = event.getPlayer();
    21.  
    22. for(int i = 0; i < 9; i++){
    23. ItemStack item = player.getInventory().getItem(i);
    24. if(item.getType().equals(Material.SPIDER_EYE) && item.getAmount() == 1) {
    25.  
    26. }
    27. }
    28. }
     
  2. Offline

    thecrystalflame

    not to sure on what you mean but why not just check if the player has a spiders eye in there hotbar from the EntityDamageEvent
     
  3. Offline

    RUDD33

    So what would be the bet way to check for the player, before checking the inventory, while inside of EntityDamageEvent. I can't use the getPlayer() inside of the EntityDamageEvent without adding the cast OfflinePlayer to the evt (Shown in the code below).
    Code:java
    1. Player player = ((OfflinePlayer) evt).getPlayer();
     
  4. Offline

    thecrystalflame

    you should be able to do
    Code:java
    1.  
    2. if (event.getEntity() instanceof Player) {
    3. Player player = (Player)event.getEntity();
    4. }
    5.  
     
  5. Offline

    RUDD33

    Well there aren't any errors in the codes, but the poison doesn't seem to be working now. I tested it by added a player.sendMessage("") instead of the poison, but that didn't work either. Any idea why it wouldn't execute the code now?

    Here is my current code -
    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void damageListener(EntityDamageEvent e) {
    3. if (e.isCancelled()) return;
    4.  
    5. if (e instanceof EntityDamageByEntityEvent) {
    6. EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent)e;
    7.  
    8. if (evt.getEntity() instanceof LivingEntity) {
    9. LivingEntity ent = (LivingEntity)evt.getEntity();
    10. Entity dam = evt.getDamager();
    11.  
    12. if (evt.getEntity() instanceof Player) {
    13. Player player = (Player)evt.getEntity();
    14. for(int i = 0; i < 9; i++){
    15. ItemStack item = player.getInventory().getItem(i);
    16. if(item.getType().equals(Material.SPIDER_EYE) && item.getAmount() == 1) {
    17.  
    18. if (dam instanceof Arrow) {
    19. ent.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 80, 1));
    20. }
    21. }
    22. }
    23. }
    24. }
    25. }
     
  6. Offline

    thecrystalflame

    try removing the if(evt.getEntity() instanceof LivingEntity) statement and set the damager in the instancof player one instead
     
  7. Offline

    RUDD33

    No that didn't seem to work. I removed the check inventory code to test if it was the issue, but the poison effect no longer worked after removing that statement and setting the damager under the instanceof player statement.
     
  8. Offline

    thecrystalflame

    ok try adding in debug lines to single out the cause of the problem
     
  9. Offline

    RUDD33

    There doesn't seem to be an issue with the code, but when I add the inventory check code:
    Code:java
    1. if (evt.getEntity() instanceof Player) {
    2. Player player = (Player)evt.getEntity();
    3. for(int i = 0; i < 9; i++){
    4. ItemStack item = player.getInventory().getItem(i);
    5. if(item.getType().equals(Material.SPIDER_EYE) && item.getAmount() == 1) {

    it doesn't run any of the following commands after the next if statement such as the poison.
    Code:java
    1. if (dam instanceof Arrow) {
    2. ent.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 80, 1));
     
  10. Offline

    thecrystalflame

    ok try changing the ent.addPotio............ to player.addPotionEffect now that you have the player stored correctly
     
  11. Offline

    RUDD33

    If I do that, I have to remove
    Code:java
    1. LivingEntity ent = (LivingEntity)evt.getEntity();

    because ent is no longer being used and would that remove the poison from being added to mobs? Which is fine since with that code it is only added to creepers for some unknown reason. If it does, I'll get a person to help me test it in a minute. If changing that should still affect the creeper then it is not working.

    Alright so I figured out that the poison still works after deleting that statement and changing it to a player. Now I see an error code in the cmd.exe on the line
    Code:java
    1. if(item.getType().equals(Material.SPIDER_EYE) && item.getAmount() == 1) {

    Each time the person is shot, the error pops up saying it could not pass event EntityDamageByEntityEvent to "AssassinsMain" (Name of Plugin) and it was caused by line 50 which is the line above.

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

    thecrystalflame

    can you show me the stacktrace of this error?
     
  13. Offline

    RUDD33

    How do I do that?
     
  14. Offline

    thecrystalflame

    right click on the cmd top border thing, edit > mark. select the error, then edit >copy
     
  15. Offline

    RUDD33

    Hmmm. The error disappeared after restarting the server and it has not returned, but it still won't execute commands after the Inventory Check coding is added. If I delete the section, then the poison works. Here is my current code.
    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void damageListener(EntityDamageEvent e) {
    3. if (e.isCancelled()) return;
    4.  
    5. if (e instanceof EntityDamageByEntityEvent) {
    6. EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent)e;
    7.  
    8. if (evt.getEntity() instanceof Player) {
    9. Player player = (Player)evt.getEntity();
    10. Entity dam = evt.getDamager();
    11.  
    12. for(int i = 0; i < 9; i++){
    13. ItemStack item = player.getInventory().getItem(i);
    14. if(item.getType().equals(Material.SPIDER_EYE) && item.getAmount() == 1) {
    15.  
    16. if (dam instanceof Arrow) {
    17. player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 80, 1));
    18. }
    19. }
    20. }
    21. }
    22. }
    23. }
    24. }
     
  16. Offline

    thecrystalflame

    so you are testing this with a single spiders eye in your inventory hotbar correct?
     
  17. Offline

    RUDD33

    Correct
     
  18. Offline

    remremrem

    Make sure that evt.getDamager() is actually returning an instance of Arrow.
     
  19. Offline

    RUDD33

    I just got the error again. Here it is -
    Code:
    22:08:41 [SEVERE] Could not pass event EntityDamageByEntityEvent to AssassinsMai
    n v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callEvent(Craf
    tEventFactory.java:93)
            at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.callEntityDama
    geEvent(CraftEventFactory.java:381)
            at org.bukkit.craftbukkit.v1_6_R3.event.CraftEventFactory.handleEntityDa
    mageEvent(CraftEventFactory.java:406)
            at net.minecraft.server.v1_6_R3.EntityLiving.damageEntity(EntityLiving.j
    ava:637)
            at net.minecraft.server.v1_6_R3.EntityHuman.damageEntity(EntityHuman.jav
    a:714)
            at net.minecraft.server.v1_6_R3.EntityPlayer.damageEntity(EntityPlayer.j
    ava:383)
            at net.minecraft.server.v1_6_R3.EntityArrow.l_(EntityArrow.java:229)
            at net.minecraft.server.v1_6_R3.World.entityJoinedWorld(World.java:1354)
     
            at net.minecraft.server.v1_6_R3.World.playerJoinedWorld(World.java:1335)
     
            at net.minecraft.server.v1_6_R3.World.tickEntities(World.java:1223)
            at net.minecraft.server.v1_6_R3.WorldServer.tickEntities(WorldServer.jav
    a:480)
            at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:5
    74)
            at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:2
    27)
            at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:4
    88)
            at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java
    :421)
            at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:5
    83)
    Caused by: java.lang.NullPointerException
            at assassins.main.AssassinsMain.damageListener(AssassinsMain.java:50)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
     
  20. Offline

    thecrystalflame

    which line is line 50?
     
  21. Offline

    RUDD33


    Code:java
    1. if(item.getType().equals(Material.SPIDER_EYE) && item.getAmount() == 1) {
     
  22. Offline

    thecrystalflame

    i would assume this is because of possible blank spaces in your inventory, try doing an if (item != null) { statement surrounding that
     
  23. Offline

    RUDD33

    No error in the cmd.exe, but the poison will not work unless the the inventory check is removed.
    Here is my current code:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void damageListener(EntityDamageEvent e) {
    3. if (e.isCancelled()) return;
    4.  
    5. if (e instanceof EntityDamageByEntityEvent) {
    6. EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent)e;
    7.  
    8. if (evt.getEntity() instanceof Player) {
    9. Player player = (Player)evt.getEntity();
    10. Entity dam = evt.getDamager();
    11.  
    12. for(int i = 0; i < 9; i++){
    13. ItemStack item = player.getInventory().getItem(i);
    14. if (item != null) {
    15. if(item.getType().equals(Material.SPIDER_EYE) && item.getAmount() == 1) {
    16.  
    17. if (dam instanceof Arrow) {
    18. player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 80, 1));
    19. }
    20. }
    21. }
    22. }
    23. }
    24. }
    25. }
    26. }


    Alright I was able to get what I wanted by changing how I would do the code. Instead of checking the hot bar, i just decided to check one position instead because this method would suffice. Here is the code:
    Code:java
    1. @EventHandler(priority = EventPriority.HIGH)
    2. public void damageListener(EntityDamageEvent e) {
    3. if (e.isCancelled()) return;
    4.  
    5. if (e instanceof EntityDamageByEntityEvent) {
    6. EntityDamageByEntityEvent evt = (EntityDamageByEntityEvent)e;
    7.  
    8. if (evt.getEntity() instanceof Player) {
    9. Player player = (Player)evt.getEntity();
    10. Entity dam = evt.getDamager();
    11.  
    12. if(player.getItemInHand().getType() == Material.BOW){
    13. if (player.getInventory().getItem(8).equals(Material.SPIDER_EYE)){
    14. }
    15.  
    16. if (dam instanceof Arrow) {
    17. player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 80, 1));
    18. }
    19. }
    20. }
    21. }

    Now even though it doesn't affect anything, I get the same error from before I added the null. Where could I add the null code again to make it so the same error doesn't appear.
    Note: I can no longer use "if (item != null) { " because I removed the line of code that allowed "item" to be processed.
    Other than this, the plugin now works fine. Thank you very much for all your help because I had no idea what to do before. Thanks again!

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

    thecrystalflame

    above the if statement where you get item at slot 8 and check for it being a spider eye add
    Code:java
    1.  
    2. if (player.getInventory().getItem(8) != null) {
    3.  
     
  25. Offline

    RUDD33

    Yep, that did the trick. Thanks for all the help. I'll let you know if I have any future questions.
     
Thread Status:
Not open for further replies.

Share This Page