/**
   * Entity damaged by another entity event. Cancels the event and sends damager a message if either
   * damager or attacked player have not enabled PvP
   *
   * @param event event being handled
   */
  @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
  public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
    if (event.getEntity() instanceof Player) {
      final Player attackedPlayer = (Player) event.getEntity();
      final Player attacker;

      if (event.getDamager() instanceof Player) {
        attacker = (Player) event.getDamager();
      } else if (event.getDamager() instanceof Projectile) {
        if (((Projectile) event.getDamager()).getShooter() instanceof Player) {
          attacker = (Player) ((Projectile) event.getDamager()).getShooter();
        } else {
          return;
        }
      } else {
        return;
      }

      final World world = attacker.getWorld();
      final Localisation localisation = plugin.getLocalisation();
      if (PVPConfigUtils.getPlayerStatus(attacker, world, plugin)) {
        if (PVPConfigUtils.getPlayerStatus(attackedPlayer, world, plugin)) {
          LastPVPActionTimeConfigUtils.update(attacker, world, plugin);
          if (DebugConfigUtils.getDebugEnabled(plugin)) {
            attacker.sendMessage(
                localisation.get(
                    LocalisationEntry.DEBUG_ATTACKED_PLAYER,
                    new Object[] {attackedPlayer.getDisplayName(), event.getDamage()}));
            attackedPlayer.sendMessage(
                localisation.get(
                    LocalisationEntry.DEBUG_ATTACKED_BY_PLAYER,
                    new Object[] {attacker.getDisplayName(), event.getDamage()}));
          }
        } else {
          event.setCancelled(true);
          attacker.sendMessage(
              localisation.get(
                  LocalisationEntry.MSG_ATTACK_CANCELLED_PLAYER_DOES_NOT_HAVE_PVP_ENABLED,
                  new Object[] {attackedPlayer.getDisplayName()}));
        }
      } else {
        event.setCancelled(true);
        attacker.sendMessage(
            localisation.get(LocalisationEntry.MSG_ATTACK_CANCELLED_PVP_NOT_ENABLED));
      }

      // Stop arrows bouncing back, possibly hitting you.
      if ((event.isCancelled()) && (event.getDamager() instanceof Projectile)) {
        ((Projectile) event.getDamager()).setBounce(false);
      }
    }
  }
Ejemplo n.º 2
0
  /**
   * Sets name tag for players. If a player has PVP enabled, their name is prefixed by the value
   * specified in the config
   *
   * @param event event being handled
   */
  @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
  public void onNameTag(AsyncPlayerReceiveNameTagEvent event) {
    final Player playerSeen = event.getNamedPlayer();
    final Object[] formatObjects = {playerSeen.getDisplayName()};

    if (PVPConfigUtils.getPlayerStatus(playerSeen, playerSeen.getWorld(), plugin)) {
      try {
        event.setTag(
            String.format(
                ColorUtils.addColor(
                        PrefixConfigUtils.getPlayerPrefix(
                            playerSeen, playerSeen.getWorld(), plugin))
                    + "%s",
                formatObjects));
      } catch (IllegalFormatException ex) {
        plugin.getLogger().log(Level.WARNING, "{0}Invalid prefix in config", ChatColor.RED);
        plugin.getLogger().log(Level.WARNING, "{0}Player tags not changed", ChatColor.RED);
        plugin
            .getLogger()
            .log(Level.WARNING, "{0}Edit or update your config to resolve", ChatColor.RED);
      }
    }
  }