Help With nbt/entity names

Discussion in 'Plugin Development' started by WellThatsAkwrd, Nov 29, 2019.

  1. Offline

    WellThatsAkwrd

    Im making a grenade and I want the grenade to only work on specfic snowballs I.e and named/nbt snowball.
    And im stuck and cant find any resources.

    Any resouces or help?

    package me.lucioriley.revamp.events;

    import org.bukkit.Location;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;

    public class Grenade implements Listener {

    @EventHandler
    public void onProjectileHit(ProjectileHitEvent e) {
    if(e.getEntity().getShooter() instanceof Player) {
    if(e.getEntityType().equals(EntityType.SNOWBALL)) {
    if(e.getHitBlock() == null) {
    Location loc = e.getHitEntity().getLocation();
    e.getHitEntity().getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 3, false, false);
    } else if (e.getHitEntity() == null) {
    Location loc = e.getHitBlock().getLocation();
    e.getHitBlock().getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 3, false, false);

    }

    }

    }

    }

    }
     
  2. Offline

    KarimAKL

    @WellThatsAkwrd I just answered another question that’s pretty similar to this one, maybe my answer to that question helps you?
     
  3. Offline

    WellThatsAkwrd

    Thanks that helps a bit... I've edited the code a little but i can quiet understand what you were talking about in that answer... May your elaborate. Sorry.
     
    Last edited: Nov 29, 2019
  4. Offline

    KarimAKL

    @WellThatsAkwrd I haven't tested this so, it might not work. :7 But i was thinking about something like this (let me know if there's something you don't understand, i'll try explaining):
    Code:Java
    1. private List<UUID> entities = new ArrayList<>();
    2.  
    3. @EventHandler
    4. private void onLaunch(ProjectileLaunchEvent event) {
    5. Projectile entity = event.getEntity();
    6. ProjectileSource shooter = entity.getShooter();
    7. if (!(shooter instanceof Player)) return; // This'll return false if the shooter is null so, there's no need for a null check
    8. Player player = (Player) shooter;
    9. ItemStack item = player.getInventory().getItemInMainHand(); // The javadocs says that this won't return null so, there's no need for a null check
    10. if (/*item has NBT tag (i won't write NMS without an IDE lol)*/) {
    11. entities.add(entity.getUniqueId());
    12. }
    13. }
    14.  
    15. @EventHandler
    16. private void onHit(ProjectileHitEvent event) {
    17. Projectile entity = event.getEntity();
    18. if (!entities.contains(entity.getUniqueId())) return;
    19. entities.remove(entity.getUniqueId());
    20. // Do your thing here
    21. }

    Note that i wrote this in the forums (without an IDE), and that i haven't tested this. (i'm thinking that the player's item in hand (in the ProjectileLaunchEvent) might be AIR (because it might be called after the item is removed))
    Let me know how it goes.
     
  5. Offline

    WellThatsAkwrd

    Thanks i will try it out..

    Edit: Everything is going on find but. i dont now how to refrence the item meta in this specific line of code:

    if(/*item has NBT tag (i won't write NMS without an IDE lol)*/) {
    entities.add(entity.getUniqueId());
    }

    Sorry i very new to spigot devlopment..

    Can you expain what i have to do And here is the whole class:

    Code:Java

    package me.lucioriley.revamp.events;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.UUID;

    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.event.entity.ProjectileLaunchEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.projectiles.ProjectileSource;

    public class Grenade implements Listener {

    public List<UUID> entities = new ArrayList<UUID>();


    @EventHandler
    public void OnJoin(PlayerJoinEvent e) {
    Player player = e.getPlayer();
    ItemStack Grenade = new ItemStack(Material.SNOWBALL, 1);
    ItemMeta GrenadeMeta = Grenade.getItemMeta();
    GrenadeMeta.setDisplayName("Grenade");
    Grenade.setItemMeta(GrenadeMeta);
    player.getInventory().addItem(Grenade);

    }

    @EventHandler
    public void onLaunch(ProjectileLaunchEvent event) {
    Projectile entity = event.getEntity();
    ProjectileSource shooter = entity.getShooter();
    if (!(shooter instanceof Player)) return;
    Player player = (Player) shooter;
    ItemStack item = player.getInventory().getItemInMainHand();
    if(item.hasItemMeta()) {
    entities.add(entity.getUniqueId());
    }
    }

    @EventHandler
    public void onHit(ProjectileHitEvent event) {
    Projectile entity = event.getEntity();
    if (!entities.contains(entity.getUniqueId())) return;
    entities.remove(entity.getUniqueId()); }
    public void onProjectileHit(ProjectileHitEvent e) {
    if(e.getEntity().getShooter() instanceof Player) {
    if(e.getEntityType().equals(EntityType.SNOWBALL)) {
    if(e.getHitBlock() == null) {
    Location loc = e.getHitEntity().getLocation();
    e.getHitEntity().getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 3, false, false);
    } else if (e.getHitEntity() == null) {
    Location loc = e.getHitBlock().getLocation();
    e.getHitBlock().getWorld().createExplosion(loc.getX(), loc.getY(), loc.getZ(), 3, false, false);

    }

    }

    }
    }

    }
     
    Last edited: Nov 30, 2019
  6. Offline

    KarimAKL

    Use a search engine (if you still can't find anything, you can ask here).
    I don't recommend using NBT tags when you're new to the API (it requires NMS); instead, you can use the item's ItemMeta.
    Use the javadocs and look for ItemStack, then look at it's methods ("look for getItemMeta()").
    Latest Javadocs: https://hub.spigotmc.org/javadocs/spigot/index.html?overview-summary.html
    Again, if you still can't find anything, ask here.

    Do you know Java? If not, i'd recommend learning it before any API.

    Use [Syntax=Java]Code here[/Syntax]
     
  7. Offline

    WellThatsAkwrd

    @KarimAKL Thanks for the info, yes I know java.
     
    KarimAKL likes this.

Share This Page