Stop item break on cactus

Discussion in 'Plugin Development' started by keeper317, Oct 7, 2014.

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

    keeper317

    I am looking for away to stop an item drop from breaking on a cactus.
     
  2. Offline

    Gerov

    keeper317 ItemDespawnEvent maybe? I am not completely sure if that will work, but you can try it.
     
  3. Offline

    keeper317

    No that didnt work
     
  4. Offline

    teej107

  5. Offline

    keeper317

  6. Offline

    teej107

    keeper317 Of coarse it won't work alone. You need to check for it in an EntityDamageEvent. Can I see your code with it?
     
  7. Offline

    keeper317

    This is the class with it.
    Code:java
    1. public class FallDamage implements Listener
    2. {
    3. @EventHandler
    4. public void onFallDamage(EntityDamageEvent e)
    5. {
    6. if(!(e.getEntity() instanceof Player))
    7. return;
    8. if(e.getCause() == DamageCause.FALL)
    9. e.setCancelled(true);
    10. if(!(e.getEntity() instanceof Item))
    11. return;
    12.  
    13. if(e.getCause() == DamageCause.FIRE || e.getCause() == DamageCause.FIRE_TICK || e.getCause() == DamageCause.CONTACT)
    14. e.setCancelled(true);
    15. }
    16. }
    17.  

    I know this event is set up correctly as fall damage works properly.
     
  8. Offline

    teej107

    Your first if statement is blocking the rest of the code from happening. An Item isn't a Player so of coarse your first if statement will be true which will forcefully end the method.
     
  9. Offline

    keeper317

    Yea I realized that as soon as i sent it.
    This is the new code:
    Code:java
    1. public class FallDamage implements Listener
    2. {
    3. @EventHandler
    4. public void onFallDamage(EntityDamageEvent e)
    5. {
    6. if(!(e.getEntity() instanceof Player) && !(e.getEntity() instanceof Item))
    7. return;
    8. if(e.getCause() == DamageCause.FALL)
    9. e.setCancelled(true);
    10. if(e.getEntity() instanceof Item && (e.getCause() == DamageCause.FIRE || e.getCause() == DamageCause.FIRE_TICK || e.getCause() == DamageCause.CONTACT))
    11. e.setCancelled(true);
    12. Bukkit.broadcastMessage("Entity is: " + e.getEntityType().toString());
    13. }
    14. }

    It still doesnt work for items and when i throw an item on a cactus it still gets removed. Is there a way to just sort through a players inv and teleport the item to a different spot. This is to prevent an item from being destroyed during the course of a minigame.
     
  10. Offline

    BillyBobJoe168

    why not just do
    Code:java
    1. if (e.getEntity() instanceof Item) {
    2. //do your stuff here
    3. }
    4.  

    Your code only will run if the entity IS a player AND an item. Not sure why you did that :p

    Oh yeah and also I don't think items despawning when dropping on a cactus is considered fall damage.
     
    XsergeiX, Code0 and Caprei like this.
  11. Offline

    keeper317

    The first line is return which exits the method. it only runs if the entity is not a player and not an item. items on cactus should be contact not fall damage. fall damage is for players if you read the next line you would have seen that.
     
  12. Offline

    teej107

    Did you register your event?
     
  13. Offline

    NonameSL



    Why are you calling if(e.getEntity() instanceof Item) twice? Your current code checks for fall damage in the item, and items can't have fall damage...
    You can just do:
    Code:java
    1. if(e.getEntity() instanceof Player && e.getCause().equals(DamageCause.FALL){
    2. //Player involved code here, now you can do whatever you want with the player!
    3. e.setCancelled(true);
    4. }else if(e.getEntity() instanceof Item && (e.getCause().equals(DamageCause.CONTACT) || e.getCause().toString().contains("FIRE")/*Easier way instead of checkiing for both fire and fire ticks*/)){
    5. //Item involved code here, Now you can do whatever you want with the item!
    6. e.setCancelled(true);
    7. }
     
  14. Offline

    keeper317

    yes fall damage works the way it should.

    if with the code written that way it does not work

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

    BillyBobJoe168

    then how about trying to write it in a way that does work?
     
  16. Offline

    CaptainUniverse

    I'm pretty sure when an item gets despawned by cactus you use EntityDeathEvent considering an item on the ground is an entity.
     
    Code0 likes this.
  17. Offline

    keeper317

    How about you go fuck yourself
    Isn't that item still taking damage
     
  18. Offline

    CaptainUniverse

    no im pretty sure the item is killed when it touches a cactus not damaged keeper317
     
  19. Offline

    BillyBobJoe168

    ...
    Items don't take damage.
     
  20. Offline

    keeper317

    Ok but death is only called when an entity's health is lower than zero. ergo it has taken damage.
     
  21. Offline

    BillyBobJoe168

    Correct me if I'm wrong. Aren't you asking about dropped items? Dropped items don't have health...
     
  22. Offline

    teej107

    Code0 likes this.
  23. Offline

    keeper317

    Yes im talking about dropped items. Ok but you cant cancel the death event.
    What I need is either a way to prevent dropped items from dieing or when a player dies stopping the items in there inv from dropping at there death point and dropping at a predetermined location that is known to be safe.
     
  24. Offline

    FabeGabeMC

    That's against the rules. Please refrain from yelling out/saying obscenities and telling people to do you know.
    That breaks two rules.
     
    XsergeiX likes this.
  25. Actually, if you look at the code, that's not really the way it works. The first if statement is basically a "if entity is a player or an item", not an "if entity is player and item". Admittedly, these should logically be two different methods, or a more spaced out method, but essentially it's checking fall damage for players, or fire/contact damage for items.
     
  26. Offline

    keeper317

    Based on the rules that you have posted I am in no way violating the rules.
     
  27. Offline

    FabeGabeMC

  28. Offline

    BillyBobJoe168

    Its is "and" because it has && not ||
     
  29. Offline

    keeper317

    I have read the rules and based on the rules phrasing I in no way violate them. My post was not excessive use of profanity and it is not racist, discrimination or a threat in anyway. A personal attack cannot be defined with any one definition and therefore cannot be justification for any repercussions.

    yes but ! is not. the code checks to see if it is not a player and not an item. I have sense changed it due to the poor logical flow. However it will likely be changed back due to the new code not working.

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

    teej107

    keeper317
    though that comment wasn't needed at all. If the OP knew how to write it in a way that works, he would've done it and solved this thread or not even create it in the first place. If you wanted to be an ass about it, you could say that just about in about every thread.

    Try this, here some pseudo code.
    Code:java
    1. If entity is Player and damagecause is from fall,
    2. cancel event.
    3.  
    4. If entity is Item and damagecause is from contact OR fire
    5. cancel event.
    6.  
     
Thread Status:
Not open for further replies.

Share This Page