Poisoned Arrows (Arrows and potions)

Discussion in 'Plugin Development' started by valon750, Jan 3, 2013.

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

    valon750

    *goes off to learn about HashSets*

    Hmm, so I've had a look around at other posts, but I'm having trouble being able to relate it to the arrow :/

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

    fireblast709

    Code:
    // Init
    HashSet<UUID> poisonI;
    // Add
    poisonI.add(arrow.getUniqueId());
    // If it is a poison I arrow
    poisonI.contains(arrow.getUniqueId());
     
  3. Offline

    valon750

    Oh wow that was completely different to what I saw in the other persons post, probably why he was asking for help with it.

    So I should put this with the onEnable? Then when it comes to hitting the enemy, have the.. EntityDamageByEntity go off in the player listener.java, which checks if it's a poison I arrow?, then if so, apply the potion effect to the entity?
     
  4. Offline

    fireblast709

    In the class body:
    Code:java
    1. HashSet<UUID> poisonI = new HashSet<UUID>();

    In ProjectileLaunchEvent:
    Code:java
    1. if(it is a poison I arrow)
    2. {
    3. poisonI.add(event.getEntity().getUniqueId());
    4. }

    In EntityDamageByEntityEvent:
    Code:java
    1. if(damager instanceof Arrow)
    2. {
    3. if(poisonI.contains(damager.getUniqueId()))
    4. {
    5. damaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, ticks, 0));
    6. }
    7. }
     
  5. Offline

    valon750


    Alrighty then!

    Code:
        HashSet<UUID> poisonI = new HashSet<UUID>();
        public PotionPoint plugin;
        public PlayerListener(PotionPoint instance){
            plugin = instance;
        }
     
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onArrowShoot(ProjectileLaunchEvent e){
            if(poisonI.contains(arrow.getUniqueId())){
                poisonI.add(e.getEntity().getUniqueId());
            }
            }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onMobHit(EntityDamageByEntityEvent e){
            if(damager instanceof Arrow){
                if(poisonI.contains(damager.getUniqueId()))
                {
                    damaged.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
                }
            }
        }
       
    }
    Now, I'm getting issues that "arrow can't be resolved", first "damager" can't be resolved to a variable, then the other damager and damaged can't be resolved.

    Eh, I assume it's because it's nearly 1am, but I really can't figure out how to get rid of those issues...

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

    fireblast709

    you should get those from the events ;3. Like event.getDamager() from EntityDamageByEntityEvent
     
  7. Offline

    valon750

    I see, I attempted:

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onMobHit(EntityDamageByEntityEvent e){
            Damager damager = e.getDamager();
    And that gets rid of the issue with "damager", but now "Damager" can't be resolved to a type :S

    This has usually worked for me in the past, I guess I just can't think straight at this time :D

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

    fireblast709

    then go to bed... Entity damager = event.getDamager();
     
  9. Offline

    valon750

    Ah right, I'll just add in that for damager and damaged, oh! there only seems to be getDamager and getDamage, I assumed the damaged would be getDamage?

    Ah, no it can't as that's for an integer, any ideas?

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

    lol768

    GetEntity
     
  11. Offline

    valon750

    ooooooooh

    What would I do about addPotionEffect? As that says that it "is undefined for the type Entity"
     
  12. Offline

    lol768

    Cast it to a player after checking it is an instanceof Player.
     
  13. Offline

    valon750

    Cast it? What do you mean?
     
  14. Offline

    fireblast709

    valon750 don't cast it to Player (example below, how to cast). It can be any entity. If you want your arrows to only work on Players, use
    Code:
    if(event.getEntity() instanceof Player)
    {
        Player damaged = (Player) event.getEntity();
    
    But if you want it to work on all mobs(living entities, LivingEntity... see the connection?)
    Code:
    if(event.getEntity() instanceof LivingEntity) // it lives
    {
        LivingEntity damagee = (LivingEntity) event.getEntity()
    as the addPotionEffect() is a method of the LivingEntity interface, it is possible to allow them on all sub classes (like monsters, animals).

    The principle of casting is to convert an object to another type of the same 'family'. Assume
    Code:
    class A{}
    class B extends A{}
    class C extends A{}
    Now look at the structure. We have class A, B and C. In which B and C extend A.
    You can cast B and C easily to A by writing
    Code:
    B b = new B();
    C c = new C();
    A ab = (A)b;
    A ac = (A)c;
    Not that this has much effect, but for the sake of the example. Now what if we have an A, which is actually a B or C. We can cast A to B or C, but we do not know what it is. And if we cast it to B and it is C, this will throw a nasty ClassCastException (as C does not extend B). Here you would have to implement a if-else block
    Code:
    A a = new B(); // create a B. Btw this is legit to do, as B extends A
    if(a instanceof C) // results into false, as it is an instance of B
    {
        C c = (C)a;
    }
    else if(a instanceof B) // results into true
    {
        B b = (B)a;
    }
    Now the question remains: why not just use B and C, and never use A. Well it is the same as minecraft entities. If we want to create a List of them all, we cannot make a List<Sheep> and store Cows there. Therefore we create a List<Animal> that can store Sheep, Cows AND Chickens... Nice huh.

    If any questions remain, feel free to ask them
     
  15. Offline

    valon750

    Wow, sorry but that hardly made any sense to me at all... :(

    I attempted to sort out the damagee and all that, ended up with this:

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onMobHit(EntityDamageByEntityEvent e){
            Entity damager = e.getDamager();
            LivingEntity damagee = (LivingEntity) e.getEntity();
            if(e.getEntity() instanceof LivingEntity) // it lives
            {                   
                    damagee.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
                }
            }
        }
    But obviously "damager" isn't used so that's throwing up errors
     
  16. Offline

    fireblast709

    It should not throw up errors... anyway you need the damager later to check if the arrow was a poisonous one
     
  17. Offline

    valon750

    Hmm...

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onArrowShoot(ProjectileLaunchEvent e){
            if(poisonI.contains(arrow.getUniqueId())){
                poisonI.add(e.getEntity().getUniqueId());
            }
            }
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onMobHit(EntityDamageByEntityEvent e){
            Entity damager = e.getDamager();
            LivingEntity damagee = (LivingEntity) e.getEntity();
            if(e.getEntity() instanceof LivingEntity) // it lives
            {                   
                    damagee.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 200, 0));
                }
            }
        }
    Says here that arrow can't be resolved, and that damager is never used :S
     
  18. Offline

    fireblast709

    damager never used is an warning in your IDE, not an error. Also instead of arrow use the entity from the event
    Code:
    /@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onArrowShoot(ProjectileLaunchEvent e)
    {
        if(e.getEntity() instanceof Arrow)
        {
            if(/*insert poison arrow logic check*/)
            {
                poisonI.add(e.getEntity().getUniqueId());
            }
        }
    }
     
  19. Offline

    valon750

    Well no it's not an error but it's a squiggly line, and no one likes squiggly lines :3

    This still has the issue with arrow not being resolved:

    Code:
        @EventHandler(priority = EventPriority.HIGHEST)
        public void onArrowShoot(ProjectileLaunchEvent e){
            if(e.getEntity() instanceof Arrow) {
            if(poisonI.contains(arrow.getUniqueId())){
                poisonI.add(e.getEntity().getUniqueId());
            }
            }
            }
    What am I doing wrong?
     
  20. Offline

    fireblast709

    read the line where I put in the UniqueId() ;3 (in my last posted code, the one you quoted)
     
  21. Offline

    valon750

    Hmm, you mean the logic check? Because the when I look at the e.getEntity().getUniqueId()); it looks exactly the same as what I've got.

    I'm not sure on what the logic check is, so I assumed it was "if(e.getEntity() instanceof Arrow) {"

    I'm such a newblet ;_;
     
  22. Offline

    fireblast709

    the logic check is atm unknown to me aswell. I will look into it. It probably will have to be something like checking what arrow will get shot (using minecraft logic)
     
  23. Offline

    valon750

    That'd be great thanks, I really appreciate all the help you give to me, as well as everyone else on this site :3

    Hmm, would it be something along the lines of having to search for the items lore? Obviously I'm not amazing at this stuff so it's rather difficult to think of what it could be :D But hey, I'm learning more and more every day :3

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

    fireblast709

    valon750 I created arrow registering code, which differs between normal arrow and arrows you specified using lore (they cannot contain enchantments!)
    Code:java
    1. private HashMap<String, String> usingArrow = new HashMap<String, String>();
    2.  
    3. @EventHandler
    4. public void onInteract(PlayerInteractEvent e)
    5. {
    6. ItemStack hand = e.getPlayer().getItemInHand();
    7. if(hand == null || hand.getType() != Material.BOW)
    8. {
    9. return;
    10. }
    11. if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
    12. {
    13. registerUsingArrow(e.getPlayer());
    14. }
    15. }
    16.  
    17. @EventHandler
    18. public void onInteract(PlayerInteractEntityEvent e)
    19. {
    20. ItemStack hand = e.getPlayer().getItemInHand();
    21. if(hand == null || hand.getType() != Material.BOW)
    22. {
    23. return;
    24. }
    25. registerUsingArrow(e.getPlayer());
    26. }
    27.  
    28. private void registerUsingArrow(Player player)
    29. {
    30. ItemStack[] iss = player.getInventory().getContents();
    31. for(ItemStack is : iss)
    32. {
    33. if(is == null)
    34. {
    35. continue;
    36. }
    37. if(is.getType() == Material.ARROW)
    38. {
    39. List<String> lore = is.hasLore() ? is.getLore() : new ArrayList<String>();
    40. if(lore.size() < 1 || !is.getEnchantments().isEmpty())
    41. {
    42. usingArrow.put(player.getName(), "NORMAL");
    43. }
    44. else if(is.getEnchantments().isEmpty())
    45. {
    46. usingArrow.put(player.getName(), lore.get(0));
    47. }
    48. return;
    49. }
    50. }
    51. }
    52.  
     
  25. Offline

    valon750

    Oh wow, you have been busy haven't you :S

    So I can place that any where in the playerlistener.java I have?
     
  26. Offline

    fireblast709

  27. Offline

    valon750


    Only issue I seem to be having is....:
    Code:
    List<String> lore = is.hasLore() ? is.getLore()
    saying they they're undefined for the type ItemStack.

    Then obviously there's the issue I'm having with arrow not being resolved with ProjectileLaunchEvent, but one thing at a time :3

    Also, sorry for the slow replies, I got a rubiks cube today and well, I'm sure you can understand :D

    I should be more precise, hasLore and getLore are the things being underlined as being undefined.

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

    fireblast709

    Valon sorry made a mistake there
    Code:
    List<String> lore = is.getItemMeta().hasLore() ? is.getItemMeta().getLore() : new ArrayList<String>();
     
  29. Offline

    valon750

    Ah there we go,

    Right then, so this should register that if the player is using a bow then it checks to see if it has a lore?
     
  30. Offline

    fireblast709

    this should register when the player tries to shoot with a bow, what arrow it will pick. And the type (either the lore or "NORMAL") will be put into a HashMap. If you get a ProjectileLaunchEvent later, and the hashmap.get(playername) will give "Poison I", this means they shot a poison arrow
     
Thread Status:
Not open for further replies.

Share This Page