Minecraft 1.16 | Java] Entities can hit me, but they can't?

Ba
1

@EventHandler
public void onEntityDamageByEntity (EntityDamageByEntityEvent e) {
Entity p = e.getEntity ();
if (! Main.allowedPlayer.contains (p)) {
e.setCancelled (true);
Bukkit.broadcastMessage ("HIT");
}
}

With

Player p = (Player) e.getEntity ();

it doesn't work either.

So how can I get the player in

if (! Main.allowedPlayer.contains (p)) {

retrieve without

at Listeners.AntiBuild_Interacts.onEntityDamageByEntity (AntiBuild_Interacts.java:

to get?

cl

The error message that you gave us doesn't bring us much, since almost everything is missing. You don't know why the exception is thrown and not in which line.

But I suspect that it is a ClassCastException because you don't have a check anywhere.

What is Main.allowedPlayers? An (array) list with player?

Then we already have a problem. The EntityDamageByEntity event is triggered as soon as an entity is damaged - at first it doesn't matter which entity type. That means, you first have to check whether the entity that is taking damage is also a player.

That means, you first have to do such an if:

if (e.getEntity () instanceof Player) {
// The entity that gets harmed is a player, go ahead.
// Here is your actual code
}