Solved Striking Lightning at Opposing player

Discussion in 'Spigot Plugin Help' started by MarkIsCool, Jun 4, 2018.

  1. I'm not sure if I did this right.

    Right now, I'm trying to check if the damager has a certain item, and if they do, strike lightning at the location of the player they just hit. So basically just 1 player who has a certain axe, strike lightning at the opposing player when they hit them. I'm having a little trouble so anything would help :D

    Code (Text):

       public void onPlayerHit(EntityDamageByEntityEvent e) {

           if (e.getEntityType() == EntityType.PLAYER) {
               Player damager = (Player) e.getDamager();
               Player playerWhoGotHit = (Player) e.getEntity();
               if (damager.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase("axe of zeus")) {
                   Location l = playerWhoGotHit.getLocation();
                   World w = playerWhoGotHit.getLocation().getWorld();
  2. I just solved it.
  3. Strahan


    Glad to hear you solved it. I'd suggest rather than stacking .getDisplayName() on top of the .getItemMeta() that you first check if it has item meta. If it does not, you're gonna clutter your log with exceptions. Also don't use deprecated stuff if you can avoid it; instead of damager.getItemInHand() use damager.getInventory().getItemInMainHand(). Lastly, I'd use ChatColor.stripColor() so if you decide in the future to make the name colorful it won't break your functionality.
    • Useful Useful x 1
  4. That was exactly my problem. I had "Axe of Zeus" with ChatColor.AQUA in another class.
    So when I checked it with this method, it did not go through because Axe of Zeus had ChatColor. So thanks for letting me know about ChatColor.stripColor().

    Although I don't really understand what you mean by "dont use deprecated stuff."
    You're telling me to use

    rather than

    What's the significance of using one over the other?
  5. Strahan


    One is deprecated, the other not. Deprecated means it's marked for future removal from the API. Could be next version, could be many many versions. Best to avoid using them because when the version comes around that removes it, your plugin breaks.

    In this case, it was deprecated because when they came out with the ability to hold things in your off-hand they had to make a way to reference specifically which hand you want to check.
    • Useful Useful x 1
  6. Optic_Fusion1

    Resource Staff

    in some cases (because md_5 loves backwards compatibility a little to much tbh) it might never get removed
    • Like Like x 1
  7. You explained that very well. Thank you! :D