블록을 깨면 나를 따라오지만, 공격하지 않는 좀비 코딩

2025. 3. 19. 23:04마인크래프트

    @EventHandler
    public void onBlockBreak(BlockBreakEvent event) {
        Player player = event.getPlayer();
        Location blockLocation = event.getBlock().getLocation();
        World world = blockLocation.getWorld();

        // Spawn a friendly zombie
        Zombie zombie = (Zombie) world.spawn(blockLocation.add(0, 1, 0), Zombie.class);
        zombie.setCustomName(player.getName() + "'s Friend");
        zombie.setCustomNameVisible(true);
        zombie.setAdult();
        zombie.setCollidable(false);
        zombie.setCanPickupItems(false);
        zombie.setRemoveWhenFarAway(false);

        // ✅ Disable Zombie's Damage Ability
        zombie.getAttribute(Attribute.GENERIC_ATTACK_DAMAGE).setBaseValue(0);
        zombie.setTarget(null); // Ensure no auto-targeting

        // Store zombie reference
        friendlyZombies.put(player.getUniqueId(), zombie);

        // Make the zombie follow the player
        new BukkitRunnable() {
            @Override
            public void run() {
                if (!zombie.isValid() || player.isDead()) {
                    this.cancel();
                    friendlyZombies.remove(player.getUniqueId());
                    return;
                }

                // Follow the player
                zombie.setTarget(player);
            }
        }.runTaskTimer(this, 0L, 20L);
    }

    @EventHandler
    public void onZombieTarget(EntityTargetLivingEntityEvent event) {
        if (event.getEntity() instanceof Zombie && event.getTarget() instanceof Player) {
            Zombie zombie = (Zombie) event.getEntity();
            if (friendlyZombies.containsValue(zombie)) {
                event.setCancelled(true); // Cancel attack behavior
                zombie.setTarget(null);   // Ensure it doesn't target anything
            }
        }
    }

    @EventHandler
    public void onZombieTargetChange(EntityTargetEvent event) {
        if (event.getEntity() instanceof Zombie && event.getTarget() instanceof Player) {
            Zombie zombie = (Zombie) event.getEntity();
            if (friendlyZombies.containsValue(zombie)) {
                event.setCancelled(true); // Cancel targeting
                zombie.setTarget(null);   // Ensure targeting is reset
            }
        }
    }