#Null check

1 messages · Page 1 of 1 (latest)

strong pivot
#

How would i create a null check for the following code or else i get a console spam whenever i try to shoot a vanilla bow as it has no lore


    @EventHandler
    public void onShoot(EntityShootBowEvent event) {
        if(event.getProjectile() instanceof Arrow){
            if(event.getEntity() instanceof Player){

                if(event.getBow() != null && event.getBow().getItemMeta() != null && event.getBow().getItemMeta().getLore() != null
                && event.getBow().getItemMeta().getLore().contains("§6Item Ability: Three Shot")){

                    Arrow arrow = (Arrow) event.getProjectile();

                    Arrow arrow1 = event.getEntity().getWorld().spawn(event.getEntity().getEyeLocation(), Arrow.class);
                    arrow1.setDamage(arrow1.getDamage() * 2);
                    arrow1.setShooter(event.getEntity());
                    arrow1.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(12)));

                    Arrow arrow2 = event.getEntity().getWorld().spawn(event.getEntity().getEyeLocation(), Arrow.class);
                    arrow2.setDamage(arrow1.getDamage() * 2);
                    arrow2.setShooter(event.getEntity());
                    arrow2.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(-12)));
                }
            }
        }
    }
}```
#
org.bukkit.event.EventException: null```
lethal relic
#

send the rest of the error

strong pivot
#

okay 1 sec

#
        at me.oxid.myfirstplugin.items.HomingBow.onShoot(HomingBow.java:28) ~[?:?]
        at jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[?:?]
        at java.lang.reflect.Method.invoke(Method.java:578) ~[?:?]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-api-1.20.1-R0.1-SNAPSHOT.jar:?]```
#

the bit inbetween is just the spam

lethal relic
#

weird

strong pivot
#

i understand hopw im getting the error

#

cause vanilla bows dont have lore

#

so upon a lore check no lore is found but i dont know how i would do a null check for this

lethal relic
#

you already have one, so I don't understand why it still throws an error 😅

#

try ```java
@EventHandler
public void onShoot(EntityShootBowEvent event) {
if (!(event.getProjectile() instanceof Arrow) || !(event.getEntity() instanceof Player)) return;
ItemStack bow = event.getBow();
List<String> lore;
if (bow == null || bow.getItemMeta() == null || (lore = bow.getItemMeta().getLore()) == null || !lore.contains("§6Item Ability: Three Shot")) return;

    Arrow arrow = (Arrow) event.getProjectile();

    Arrow arrow1 = event.getEntity().getWorld().spawn(event.getEntity().getEyeLocation(), Arrow.class);
    arrow1.setDamage(arrow1.getDamage() * 2);
    arrow1.setShooter(event.getEntity());
    arrow1.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(12)));

    Arrow arrow2 = event.getEntity().getWorld().spawn(event.getEntity().getEyeLocation(), Arrow.class);
    arrow2.setDamage(arrow1.getDamage() * 2);
    arrow2.setShooter(event.getEntity());
    arrow2.setVelocity(arrow.getVelocity().rotateAroundY(Math.toRadians(-12)));
}
strong pivot
#

oml

#

sorry i sent the wrong code

lethal relic
#

o

strong pivot
#
    public void onShoot(EntityShootBowEvent event) {
        Entity arrow = event.getProjectile();
        Player player = (Player) event.getEntity();

        if(player.getInventory().getItemInMainHand().getItemMeta().getLore().contains("§7I mean ...")){

            new BukkitRunnable(){
                
                public void run(){

                    if(arrow.isOnGround() || arrow.isDead()){
                        cancel();
                    }
                    else{
                        List<Entity> nearest = arrow.getNearbyEntities(5,5,5);

                        for(Entity target : nearest){
                            if(player.hasLineOfSight(target) && target instanceof LivingEntity && !target.isDead() && target != player){
                                arrow.setVelocity(target.getLocation().toVector().subtract(arrow.getLocation().toVector()));
                            }
                        }
                    }
                }
            }.runTaskTimer(plugin, 0l,1L);
        }
    }
}```
#

which doesnt have a null check

#

i was looking at the wrong thing thw whole time

lethal relic
#

check if the item meta isn't null, and then check if the lore isn't null

strong pivot
#

if(event.getBow() != null && event.getBow().getItemMeta() != null && event.getBow().getItemMeta().getLore() != null && event.getBow().getItemMeta().getLore().contains("§7I mean ...")){

lethal relic
#

yeah similar to this

#

but with the right item instead of the e.getBow()

strong pivot
#

its a bow

lethal relic
#

yeah but you're not getting the item from and event

#

you're getting it from the player

#

you're using player.getInventory().getItemInMainHand() instead of e.getBow() in the first code you sent

strong pivot
#

ohhhhh oh

strong pivot
#
                player.getInventory().getItemInMainHand().getItemMeta().getLore().contains("§7I mean ...")) {```
#

yh this works ty

strong pivot
#

Can i add a seperate issue here or should i make a new post