#item entity health

1 messages · Page 1 of 1 (latest)

cursive mulch
#

Let's talk here

#

Are you using maven in your project?

forest prism
#

Yes

cursive mulch
#

And simply replace 1.18.2 with 1.19 everywhere

#

Hello alex 🙂

hollow wadi
cursive mulch
#

Slightly offtopic but the expansion works now after your update

hollow wadi
#

perfect!

#

thanks!

cursive mulch
#

also I saw your comment about it being hard to find if CombatLogX killed a player. I'll see what I can do about that

forest prism
#

Thanks

cursive mulch
forest prism
#

Well I have never used build tools and I don't have git I need to figure out all that before

cursive mulch
#

BuildTools does everything for you

#

Download the jar and run java -jar BuildTools.jar --rev 1.19 --remapped

#

That's it

#

Git bash is optional

forest prism
#

where do I have to run that ? in the plugin folder ?

cursive mulch
#

in the folder with the BuildTools jar

#

I recommend just making a new folder for that

forest prism
#

okay

#

done

cursive mulch
#

Did you add everything to your pom?

forest prism
#

I'm confused on what I should add, does this replace the spigot that I already have ?

cursive mulch
#

Yes

#

if something isn't working feel free to send your pom and I'll take a look

forest prism
#

Thanks a lot

forest prism
#

Ok looks like my pom is working

#

what do I have to do to get the nms version of the item entity ?

hollow wadi
#
((CraftItem)item).getHandle();
#

gives you an NMS entity

#

where "item" is a bukkit Item

forest prism
#

oh nms entities don't have health either

#

that's crazy to think that all entities have armor slots

#

but not all have health

#

how does that make sense

#

@cursive mulch can you help me please ?

cursive mulch
#

There's a method to get armor slots but it returns an empty list by default

forest prism
#

oh

#

do you know how to get the health ?

cursive mulch
#

health field in ItemEntity

#
        Item item = null; //Bukkit Item Entity
        ItemEntity nmsItem = (ItemEntity) ((CraftItem) item).getHandle();

        try {
            Field privateField = ItemEntity.class.getDeclaredField("health");
            privateField.setAccessible(true);
            int health = privateField.getInt(nmsItem); //get
            privateField.setInt(nmsItem, health); //set
        } catch(NoSuchFieldException | IllegalAccessException e) {
            //TODO Handle
        }

@forest prism

#

You can store that field somewhere so it doesn't do a look up each time

hollow wadi
#

why do you need the "health" of an item anyway?

forest prism
#

java.lang.NoClassDefFoundError: org/bukkit/craftbukkit/v1_19_R1/entity/CraftItem

forest prism
cursive mulch
hollow wadi
#

EntityType.DROPPED_ITEM

#

no need to check any "health" or sth

forest prism
hollow wadi
#

erm

#

I meant

#

EntityDamageEvent

#

EntityDeathEvent works too

forest prism
#

entity death event is only called for living entities

#

and entity damage event is what i'm using but it's called each time the item takes damage not when it dies

#

so I want to check if damage > health in entitydamageevent

hollow wadi
#

oh yeah it was the damage event

#

damage event definitely works

forest prism
#

yes

#

but need health in it

#
    @EventHandler
    public void dragonEggDestroyed(EntityDamageEvent event) {
        if(event.getEntity() instanceof Item) {
            Item item = (Item) event.getEntity();
            if(item.getItemStack().getType()==Material.DRAGON_EGG) {
                Entity ci = ((CraftItem)item).getHandle();
                try {
                    Field f = Entity.class.getDeclaredField("health");
                    f.setAccessible(true);
                    int health = f.getInt(ci);
                    if(health<=event.getDamage()) {
                        Bukkit.broadcastMessage("§cDragon egg destroyed.");
                    }
                } catch (NoSuchFieldException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SecurityException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }```
That's my code @hollow wadi
hollow wadi
forest prism
#

I did that

#

but for some reasons it's called twice on certain damage types @hollow wadi

#

if you throw it in fire you will get dropped item died message twice

hollow wadi
#

hm that is weird. yeah then you'll have to use that reflection stuff

#

and be sure to cache the field instead of getting it every time

forest prism
#

I think it's because items can take damage several times in a single tick

hollow wadi
#

very ugly solution:

#
    private final Set<Entity> trackedEntities = new HashSet<>();

    @EventHandler
    public void onDamage(EntityDamageEvent event) {
        if(event.getEntityType() == EntityType.DROPPED_ITEM) {
            if(trackedEntities.contains(event.getEntity())) {
                return;
            }
            trackedEntities.add(event.getEntity());
            Bukkit.getScheduler().runTask(this, () -> {
                if(event.getEntity().isDead()) {
                    Bukkit.broadcastMessage("Dropped item died: " + event.getEntity());
                }
                trackedEntities.remove(event.getEntity());
            });
        }
    }
cursive mulch
#

^^ this is probably better than messing around with reflection

forest prism
#

I thought about that then thought it was ugly and wanted a better solution

#

but now that I see how complicated it is

#

it seems good

#

thanks a lot

#

also what does Bukkit.getScheduler().runTask does

#

how often does it run that and when does it stop

#

oh it's just like runtasklater(this, () -> {}, 1); ?

hollow wadi
#

only once

hollow wadi
#

yes

forest prism
#

Thanks