Make mobs passive in the event that a command is activated

Discussion in 'Plugin Development' started by Plootonix, Jun 23, 2020.

Thread Status:
Not open for further replies.
  1. Offline

    Plootonix

    I'm have previous experience with Java, but I'm brand new to plugin development. I'm trying to create a block of code such that, if a player activates a command known as the "cloaking device" they turn invisible and cannot be targeted by monsters for a short period of time. I have the invisible part down, but I'm having trouble with the not getting attacked by monsters part.

    Here's the cloaking device code snippet:
    Code:
            if (label.equalsIgnoreCase("cloakingdevice")) {
                if(!(sender instanceof Player)) {
                    return true;
                }
               
                Player player = (Player) sender;
               
                if (cooldowns.containsKey(player.getName())) {
                    //player is in hashmap
                    if (cooldowns.get(player.getName()) > System.currentTimeMillis()) {
                        //still have time left in cooldown
                        long timeLeft = (cooldowns.get(player.getName()) - System.currentTimeMillis()) / 1000;
                        player.sendMessage(ChatColor.AQUA + "Ability will be ready in " + "" + ChatColor.GOLD + "" + timeLeft + " second(s)");
                        return true;
                    }
                }
               
                cooldowns.put(player.getName(), System.currentTimeMillis() + (120 * 1000));
               
                player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 1200, 2));
                player.sendMessage(ChatColor.GREEN + "Cloaking device activated!");
                cdActive.add(player.getName()); //problematic line?
               
               
                return true;
               
            }
    Here's the event handler for making the mobs passive (Note: It works when I take out the player object declaration and checking if player is in the list of players with cloaking device active):
    Code:
        @EventHandler()
        public void onEntityTarget(EntityTargetEvent event) {
           
            if (event.getTarget() instanceof Player) {
                Player player = (Player) event.getTarget();  //problematic
                if (cdActive.contains(player.getName())) {  //problematic
                    event.setCancelled(true);
                    return;
                }
    
            }
    
        }
    It's not throwing any errors in the server console so I'm not sure what the issue is. The code is also partially incomplete, but since I like to code one block at a time, I wanted to get this figured out before I continued. Any suggestions would be greatly appreciated!
     
  2. Offline

    Machine Maker

    Well, I'd probably use EntityTargetLivingEntityEvent, since that reduces the amount of times your code is run. I'd add some debug lines that print to the console to check if the event is getting called (make sure its registered) and if your conditional checking if the player is un that list is correct.
     
  3. Offline

    Plootonix

    Got rid of the array and changed event handler to:
    Code:
        @EventHandler()
        public void entityTarget(EntityTargetEvent event) {
            if (!(event.getTarget() instanceof Player))
                return;
            Player player = (Player) event.getTarget();
           
            if (!(player.hasPotionEffect(PotionEffectType.INVISIBILITY)))
                return;
           
           
            if (player.hasPotionEffect(PotionEffectType.INVISIBILITY)) {
                event.setCancelled(true);
            }
           
        }
    Works for all mobs that were not originally targeting the player, but any mobs that were targeting the player before the cloaking device command was used still target the player. Is there a way to cancel the targeting process while the player is being targeted? I've tried a couple of different things but none seem to work. Still no error messages in the server cmd.
     
Thread Status:
Not open for further replies.

Share This Page