Solved Getting snowball thrown entity ID

Discussion in 'Spigot Plugin Development' started by Poofyjerry, Feb 23, 2020.

  1. How can I get the ID of a snowball that is thrown from a player?

    CODE WHERE I NEED TO EXTRACT THE IDs:
    Code (Text):

      @EventHandler
      public static void onInteract(PlayerInteractEvent e) {

        if (e.getAction().equals(Action.RIGHT_CLICK_AIR)
          || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
          ItemStack item = e.getItem();
          if (item == null) {
            return;
          }
          if (item.getType().equals(Material.GOLDEN_SHOVEL)) {
            if (item.getItemMeta().getDisplayName().equals(ChatColor.GOLD + "LIL SHOTTY")) {
              Player player = e.getPlayer();
              player.performCommand("pfall");
              player.launchProjectile(Snowball.class);
              player.launchProjectile(Snowball.class);
              player.launchProjectile(Snowball.class);
              player.launchProjectile(Snowball.class);
    //PUT ALL 4 OF THESE IDs IN ARRAYLIST
            }
          }

          if (item.getType().equals(Material.SNOWBALL)) {
            if (item.getItemMeta().getDisplayName().equals(ChatColor.GOLD + "SHOTTY SNOWBALLS")) {
    //PUT SNOWBALL ID THROWN HERE INTO ARRAYLIST
         
            }
          }
        }
      }
     
    CODE WHERE I CHECK THE EXTRACTED IDs WITH THE SNOWBALL THAT LANDS:
    Code (Text):

      @EventHandler
      public void onCollison(ProjectileHitEvent e) {

        boolean isBlock = false;
        Block blockHit = null;

        // if block is hit do nothing
        Entity entity = e.getHitEntity();
        if (entity == null) {
          blockHit = e.getHitBlock();
        }

        if (blockHit != null) {
          isBlock = true;
        }

        if (!isBlock) {
          }
        } else if (isBlock) {
          if (e.getEntity().getType() == EntityType.SNOWBALL) {
            Block relative = null;
            BlockFace face = e.getHitBlockFace();
            if (face == null) {
              return;
            }
            if (face == BlockFace.UP) {
              relative = blockHit.getRelative(0, 1, 0);
            } else if (face == BlockFace.NORTH) {
              relative = blockHit.getRelative(0, 0, -1);
            } else if (face == BlockFace.EAST) {
              relative = blockHit.getRelative(1, 0, 0);
            } else if (face == BlockFace.WEST) {
              relative = blockHit.getRelative(-1, 0, 0);
            } else if (face == BlockFace.SOUTH) {
              relative = blockHit.getRelative(0, 0, 1);
            } else if (face == BlockFace.DOWN) {
              relative = blockHit.getRelative(0, -1, 0);
            }
            if (relative == null) {
              return;
            }
            relative.setType(Material.ICE);
          }
        } else {
          return;
        }
      }
    }
     
    **this code places an ice block on the face of a block where a snowball hit only if the snowball was shot with the LIL SHOTTY or SHOTTY SNOWBALLS

    Basically, my idea is to have an array list store the entity ids of the snowballs that were thrown when using the "LIL SHOTTY" or when a player throws a snowball with the display name of "SHOTTY SNOWBALLS".

    Then in the second piece of code I would like to have a for loop check if the snowballs that landed match any of the snowball entity IDs I stored earlier. I was successful in doing this for a bow and arrow using the EnitityShootBowEvent and extracting the arrow ID that way, however there doesn't seem to be a specific event I can use when throwing snowballs. Where can I pull the IDs from?
     
    #1 Poofyjerry, Feb 23, 2020
    Last edited: Mar 18, 2020
  2. "player.launchProjectile()" returns an instance of the launched snowball, which is a subtype of Entity, which means you can just call ".getEntityId()" on it.
     
    • Agree Agree x 2
  3. Thank you it worked, sorry for late response