마인크래프트
다른 몹이 나를 공격하면, 그 몹을 공격하는 좀비 코딩
바리새인
2025. 3. 19. 23:50
@EventHandler
public void onPlayerAttack(EntityDamageByEntityEvent event) {
// Only care if a player attacked something
if (!(event.getDamager() instanceof Player player)) return;
if (!(event.getEntity() instanceof LivingEntity target)) return;
// Loop through nearby entities to find the player's guardians
for (Entity entity : player.getWorld().getNearbyEntities(player.getLocation(), 20, 20, 20)) {
if (entity instanceof Zombie z && z.hasMetadata("owner")) {
String ownerId = z.getMetadata("owner").get(0).asString();
if (ownerId.equals(player.getUniqueId().toString())) {
// Don’t allow the guardian to attack its owner or another guardian
if (target instanceof Player p && p.getUniqueId().equals(player.getUniqueId())) continue;
if (target instanceof Zombie otherZ && otherZ.hasMetadata("owner")) continue;
z.setTarget(target);
}
}
}
}