No-destroying item frame?

Hello, i create my server but the players can break itemframe… and i don’t know how can i cancelled it?

Its an entity. Changing the final damage of the appropriate DamageEntityEvent to 0 should work.

but how can i see if it’s THIS?
i try
if(victim instanceof ItemFrame){} and if(victim instanceof Painting){}
but it doen’t work

It’s better to just set the base damage to 0.

Try doing the following when listening for a DamageEntityEvent:


@Listener
public void onDamage(DamageEntityEvent event) {
  this.logger.info("The targeted entity was: " + event.getTargetEntity());
}

And yes, it has been implemented since last week, just the issue hasn’t been updated.

1 Like

i try but it does nothing…
but when i try destructentityevent, i have a result, but i can’t cancel it…
how can i do this?

So it seems that currently, the DamageEntityEvent is not fully implemented for all Entity(ies), only Living entities. We’ll get this working soon enough.

1 Like

Is it already working?

no, i don’t thinking, i try today again, and i don’t have it, …
the snow ball can break it, the players too, …
if you have any solution, can you give me it?
plz

…still not working for itemframes?

Can i check if the Entity is a instanceof hanging?

if (e.getTargetEntity() instanceof Hanging) {
...
}

Now, the thread is old but…

The event is, sadly, still not implemented in the latest stable version of SpongeForge for the recommended version of Forge, as of July 17, 2018. But good news is, there is a workaround.

For what I know, on ItemFrame placement, Sponge fires AffectEntityEvent and on any interaction, i.e. clicking on entities, Sponge fires InteractEntityEvent. So, we can listen for the latter and cancel it if:

  • The mouse button pressed is the left one.
  • The target Entity is an ItemFrame.
  • The ItemFrame is empty, since the name of the thread is “No-destroying item frame?” and ItemFrame only destroys on left-click if it’s empty.

And the resulting code is:

@Listener
public void onEntityInteract(InteractEntityEvent.Primary event) {
    if(event.getTargetEntity() instanceof ItemFrame && ((EntityItemFrame) event.getTargetEntity()).getDisplayedItem().isEmpty())
        event.setCancelled(true);
}

But left-clicking the item frame is not the only way to remove it. You can remove it with snowballs and stuff. Well, this can be easily avoided using CollideEntityEvent.

@Listener
public void onEntityCollide(CollideEntityEvent event) {
    for(Entity entity : event.getEntities())
        if(entity instanceof ItemFrame) {
            event.setCancelled(true);
            break;
        }
}

And of course, you shouldn’t do it for any and all item frames, so you will probably need to add some other conditions yourself.

Also, in the above code, I cast Sponge’s Entity to Forge’s EntityItemFrame, to get the held ItemStack, but this shouldn’t be a problem, since the classes I use are Minecraft’s classes anyway, thus you shouldn’t be needing Forge to run the plugin.

Why? Because I couldn’t find how it’s done with Sponge. Maybe it has something to do with NBT, but I’m new to Sponge and, again, I don’t know how to do this with it.

If you know how to, I would be glad if you shared it.

P.S. I’ve just realized it would be good to make some kind of tutorial on how to set up gradle with SpongeForge and Forge both and put a link somewhere here… Maybe I will do it later. But it isn’t so much of a challenge, really.