Solved Help with creating CombatLog

Discussion in 'Plugin Development' started by idkG0D, Feb 18, 2019.

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

    idkG0D

    I want to create a simple combatLog to stop player commands when he is in pvp like as...

    • If the event detects that the player has hit another player, both are added to an ArrayList / HashMap.

    • If this ArrayList or HashMap has these players, they will not be able to use commands for a certain amount of time.

    • This time is set after they leave combat, meaning if they are no longer hitting each other, a cooldown begins. At the end of this cooldown, the event checks whether one of them hit someone during the cooldown, or not.
    • If the player hits someone, the cooldown is ended, and the player will be kept in that ArrayList or HashMap, starting again soon after he stops hitting players.
    • If he does not hit anyone during this cooldown, he is removed from that ArrayList / HashMap and will be able to use commands again.

    So far I tried to start something but I have no idea how to do this event, can anyone give me a tip?

    Code:
    @EventHandler
        public void underPvp(EntityDamageByEntityEvent e){
            Player p = (Player) e.getEntity();
            Player d = (Player) e.getDamager();
    
            if(DamageEvents.pvpON.contains(p.getName())){
                if(e.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK){
                    if(e.getDamager() instanceof Player){
                        if(VersusEvents.lutando.containsKey(p)){
                            return;
                        }
                        if(!underpvp.contains(p.getName())) {
                            underpvp.add(p.getName());
                            p.sendMessage("§aVocê entrou em pvp com o jogador §f"+d.getName());
                        }
                        if(!underpvp.contains(d.getName())){
                            underpvp.add(d.getName());
                            d.sendMessage("§aVocê entrou em pvp com o jogador §f"+p.getName());
                        }
                    }
                }
            }
        }
     
  2. Offline

    mehboss

    I would personally do something like this:
    Code:
            public ArrayList<Player> combatlogged = new ArrayList<>();
         
            public void onDamage (EntityDamageByEntityEvent e) {
             
                if (e.getEntity() instanceof Player && e.getDamager() instanceof Player) {
                    Player attacked = (Player) e.getEntity();
                    Player attacker = (Player) e.getDamager();
                 
                    combatlogged.add(attacked);
                    combatlogged.add(attacker);
                 
                    // create delayed task and remove from array after x seconds
                }
            }
         
            @EventHandler
            public void onCommand (PlayerCommandPreprocessEvent e) {
                if (combatlogged.contains(e.getPlayer())) {
                    e.setCancelled(true);
                }
            }
    And to answer your question, you can use the PlayerCommandPreprocessEvent. However, if you wanted it for ANYTIME a player gets damaged regardless of what way (mob, fire, etc) the code would be a little bit different.
     
    idkG0D likes this.
Thread Status:
Not open for further replies.

Share This Page