L2JMobius

C6 Disable hitting NPCs

G-hamsteR · 9 · 7284

Offline G-hamsteR

  • Viscount
  • *****
    • Posts: 335
Hello,

Players can hit NPC and cause damage. Even though NPCs can't die, they can still drain HP from them using skills.

Ideally, they shouldn't be hit, since it creates lag for everyone in towns, but I think l2off allowed npc hitting.


Online Mobius

  • Distinguished King
  • *****
    • Posts: 16114
Try this.

Code: [Select]
Index: java/org/l2jmobius/gameserver/model/actor/Creature.java
===================================================================
--- java/org/l2jmobius/gameserver/model/actor/Creature.java (revision 7541)
+++ java/org/l2jmobius/gameserver/model/actor/Creature.java (working copy)
@@ -9070,7 +9070,7 @@
  */
  public void reduceCurrentHp(double i, Creature attacker, boolean awake)
  {
- if ((this instanceof NpcInstance) && Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()))
+ if (isNpc() && (Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()) || (attacker.isPlayer() && !isAttackable())))
  {
  return;
  }


Online Mobius

  • Distinguished King
  • *****
    • Posts: 16114
Also removed those ugly i parameters.
Code: [Select]
Index: java/org/l2jmobius/gameserver/model/actor/Creature.java
===================================================================
--- java/org/l2jmobius/gameserver/model/actor/Creature.java (revision 7541)
+++ java/org/l2jmobius/gameserver/model/actor/Creature.java (working copy)
@@ -9054,23 +9054,23 @@
 
  /**
  * Reduce current hp.
- * @param i the i
+ * @param amount the amount
  * @param attacker the attacker
  */
- public void reduceCurrentHp(double i, Creature attacker)
+ public void reduceCurrentHp(double amount, Creature attacker)
  {
- reduceCurrentHp(i, attacker, true);
+ reduceCurrentHp(amount, attacker, true);
  }
 
  /**
  * Reduce current hp.
- * @param i the i
+ * @param amount the amount
  * @param attacker the attacker
  * @param awake the awake
  */
- public void reduceCurrentHp(double i, Creature attacker, boolean awake)
+ public void reduceCurrentHp(double amount, Creature attacker, boolean awake)
  {
- if ((this instanceof NpcInstance) && Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()))
+ if (isNpc() && (Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()) || (attacker.isPlayer() && !isAttackable())))
  {
  return;
  }
@@ -9077,33 +9077,33 @@
 
  if (Config.CHAMPION_ENABLE && _champion && (Config.CHAMPION_HP != 0))
  {
- getStatus().reduceHp(i / Config.CHAMPION_HP, attacker, awake);
+ getStatus().reduceHp(amount / Config.CHAMPION_HP, attacker, awake);
  }
  else if (_advanceFlag)
  {
- getStatus().reduceHp(i / _advanceMultiplier, attacker, awake);
+ getStatus().reduceHp(amount / _advanceMultiplier, attacker, awake);
  }
  else if (_isUnkillable)
  {
  final double hpToReduce = getStatus().getCurrentHp() - 1;
- if (i > getStatus().getCurrentHp())
+ if (amount > getStatus().getCurrentHp())
  {
  getStatus().reduceHp(hpToReduce, attacker, awake);
  }
  else
  {
- getStatus().reduceHp(i, attacker, awake);
+ getStatus().reduceHp(amount, attacker, awake);
  }
  }
  else
  {
- getStatus().reduceHp(i, attacker, awake);
+ getStatus().reduceHp(amount, attacker, awake);
  }
  }
 
  private long _nextReducingHPByOverTime = -1;
 
- public void reduceCurrentHpByDamOverTime(double i, Creature attacker, boolean awake, int period)
+ public void reduceCurrentHpByDamOverTime(double amount, Creature attacker, boolean awake, int period)
  {
  if (_nextReducingHPByOverTime > System.currentTimeMillis())
  {
@@ -9111,12 +9111,12 @@
  }
 
  _nextReducingHPByOverTime = System.currentTimeMillis() + (period * 1000);
- reduceCurrentHp(i, attacker, awake);
+ reduceCurrentHp(amount, attacker, awake);
  }
 
  private long _nextReducingMPByOverTime = -1;
 
- public void reduceCurrentMpByDamOverTime(double i, int period)
+ public void reduceCurrentMpByDamOverTime(double amount, int period)
  {
  if (_nextReducingMPByOverTime > System.currentTimeMillis())
  {
@@ -9124,7 +9124,7 @@
  }
 
  _nextReducingMPByOverTime = System.currentTimeMillis() + (period * 1000);
- reduceCurrentMp(i);
+ reduceCurrentMp(amount);
  }
 
  /**


Offline G-hamsteR

  • Viscount
  • *****
    • Posts: 335
For some reason, it's not working. I will try the previous patch that you posted and send you feedback :)


Offline G-hamsteR

  • Viscount
  • *****
    • Posts: 335
I added this check on the code and all NPCs are attackable.

Code: [Select]
if (isAttackable())
{
attacker.getActingPlayer().sendMessage("Is attackable");
}

This is why the code doesn't work.


Online Mobius

  • Distinguished King
  • *****
    • Posts: 16114
This is only for the drain skills.
Code: [Select]
Index: java/org/l2jmobius/gameserver/model/actor/Creature.java
===================================================================
--- java/org/l2jmobius/gameserver/model/actor/Creature.java (revision 7541)
+++ java/org/l2jmobius/gameserver/model/actor/Creature.java (working copy)
@@ -9054,23 +9054,23 @@
 
  /**
  * Reduce current hp.
- * @param i the i
+ * @param amount the amount
  * @param attacker the attacker
  */
- public void reduceCurrentHp(double i, Creature attacker)
+ public void reduceCurrentHp(double amount, Creature attacker)
  {
- reduceCurrentHp(i, attacker, true);
+ reduceCurrentHp(amount, attacker, true);
  }
 
  /**
  * Reduce current hp.
- * @param i the i
+ * @param amount the amount
  * @param attacker the attacker
  * @param awake the awake
  */
- public void reduceCurrentHp(double i, Creature attacker, boolean awake)
+ public void reduceCurrentHp(double amount, Creature attacker, boolean awake)
  {
- if ((this instanceof NpcInstance) && Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()))
+ if (isNpc() && Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()))
  {
  return;
  }
@@ -9077,33 +9077,33 @@
 
  if (Config.CHAMPION_ENABLE && _champion && (Config.CHAMPION_HP != 0))
  {
- getStatus().reduceHp(i / Config.CHAMPION_HP, attacker, awake);
+ getStatus().reduceHp(amount / Config.CHAMPION_HP, attacker, awake);
  }
  else if (_advanceFlag)
  {
- getStatus().reduceHp(i / _advanceMultiplier, attacker, awake);
+ getStatus().reduceHp(amount / _advanceMultiplier, attacker, awake);
  }
  else if (_isUnkillable)
  {
  final double hpToReduce = getStatus().getCurrentHp() - 1;
- if (i > getStatus().getCurrentHp())
+ if (amount > getStatus().getCurrentHp())
  {
  getStatus().reduceHp(hpToReduce, attacker, awake);
  }
  else
  {
- getStatus().reduceHp(i, attacker, awake);
+ getStatus().reduceHp(amount, attacker, awake);
  }
  }
  else
  {
- getStatus().reduceHp(i, attacker, awake);
+ getStatus().reduceHp(amount, attacker, awake);
  }
  }
 
  private long _nextReducingHPByOverTime = -1;
 
- public void reduceCurrentHpByDamOverTime(double i, Creature attacker, boolean awake, int period)
+ public void reduceCurrentHpByDamOverTime(double amount, Creature attacker, boolean awake, int period)
  {
  if (_nextReducingHPByOverTime > System.currentTimeMillis())
  {
@@ -9111,12 +9111,12 @@
  }
 
  _nextReducingHPByOverTime = System.currentTimeMillis() + (period * 1000);
- reduceCurrentHp(i, attacker, awake);
+ reduceCurrentHp(amount, attacker, awake);
  }
 
  private long _nextReducingMPByOverTime = -1;
 
- public void reduceCurrentMpByDamOverTime(double i, int period)
+ public void reduceCurrentMpByDamOverTime(double amount, int period)
  {
  if (_nextReducingMPByOverTime > System.currentTimeMillis())
  {
@@ -9124,7 +9124,7 @@
  }
 
  _nextReducingMPByOverTime = System.currentTimeMillis() + (period * 1000);
- reduceCurrentMp(i);
+ reduceCurrentMp(amount);
  }
 
  /**
Index: java/org/l2jmobius/gameserver/model/skills/handlers/SkillDrain.java
===================================================================
--- java/org/l2jmobius/gameserver/model/skills/handlers/SkillDrain.java (revision 7541)
+++ java/org/l2jmobius/gameserver/model/skills/handlers/SkillDrain.java (working copy)
@@ -57,6 +57,11 @@
  for (WorldObject target2 : targets)
  {
  final Creature target = (Creature) target2;
+ if (creature.isPlayable() && !target.isAttackable())
+ {
+ continue;
+ }
+
  if (target.isAlikeDead() && (getTargetType() != SkillTargetType.TARGET_CORPSE_MOB))
  {
  continue;
@@ -63,7 +68,7 @@
  }
 
  // Like L2OFF no effect on invul object except Npcs
- if ((creature != target) && (target.isInvul() && !(target instanceof NpcInstance)))
+ if ((creature != target) && (target.isInvul() && !target.isNpc()))
  {
  continue; // No effect on invulnerable chars unless they cast it themselves.
  }


Offline G-hamsteR

  • Viscount
  • *****
    • Posts: 335
This didn't work, but the following seems to do the job. isAttackable returns true on every creature that you can attack, including all NPCs. However, all NPCs have the isInvul as true. So I changed isAttackable to isInvul. Please tell me if it creates any problems somewhere else, otherwise it's safe to commit :)

Code: [Select]
Index: java/org/l2jmobius/gameserver/model/actor/Creature.java
===================================================================
--- java/org/l2jmobius/gameserver/model/actor/Creature.java (revision 7541)
+++ java/org/l2jmobius/gameserver/model/actor/Creature.java (working copy)
@@ -9054,23 +9054,23 @@
 
  /**
  * Reduce current hp.
- * @param i the i
+ * @param amount the amount
  * @param attacker the attacker
  */
- public void reduceCurrentHp(double i, Creature attacker)
+ public void reduceCurrentHp(double amount, Creature attacker)
  {
- reduceCurrentHp(i, attacker, true);
+ reduceCurrentHp(amount, attacker, true);
  }
 
  /**
  * Reduce current hp.
- * @param i the i
+ * @param amount the amount
  * @param attacker the attacker
  * @param awake the awake
  */
- public void reduceCurrentHp(double i, Creature attacker, boolean awake)
+ public void reduceCurrentHp(double amount, Creature attacker, boolean awake)
  {
- if ((this instanceof NpcInstance) && Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()))
+ if (isNpc() && Config.INVUL_NPC_LIST.contains(((NpcInstance) this).getNpcId()))
  {
  return;
  }
@@ -9077,33 +9077,33 @@
 
  if (Config.CHAMPION_ENABLE && _champion && (Config.CHAMPION_HP != 0))
  {
- getStatus().reduceHp(i / Config.CHAMPION_HP, attacker, awake);
+ getStatus().reduceHp(amount / Config.CHAMPION_HP, attacker, awake);
  }
  else if (_advanceFlag)
  {
- getStatus().reduceHp(i / _advanceMultiplier, attacker, awake);
+ getStatus().reduceHp(amount / _advanceMultiplier, attacker, awake);
  }
  else if (_isUnkillable)
  {
  final double hpToReduce = getStatus().getCurrentHp() - 1;
- if (i > getStatus().getCurrentHp())
+ if (amount > getStatus().getCurrentHp())
  {
  getStatus().reduceHp(hpToReduce, attacker, awake);
  }
  else
  {
- getStatus().reduceHp(i, attacker, awake);
+ getStatus().reduceHp(amount, attacker, awake);
  }
  }
  else
  {
- getStatus().reduceHp(i, attacker, awake);
+ getStatus().reduceHp(amount, attacker, awake);
  }
  }
 
  private long _nextReducingHPByOverTime = -1;
 
- public void reduceCurrentHpByDamOverTime(double i, Creature attacker, boolean awake, int period)
+ public void reduceCurrentHpByDamOverTime(double amount, Creature attacker, boolean awake, int period)
  {
  if (_nextReducingHPByOverTime > System.currentTimeMillis())
  {
@@ -9111,12 +9111,12 @@
  }
 
  _nextReducingHPByOverTime = System.currentTimeMillis() + (period * 1000);
- reduceCurrentHp(i, attacker, awake);
+ reduceCurrentHp(amount, attacker, awake);
  }
 
  private long _nextReducingMPByOverTime = -1;
 
- public void reduceCurrentMpByDamOverTime(double i, int period)
+ public void reduceCurrentMpByDamOverTime(double amount, int period)
  {
  if (_nextReducingMPByOverTime > System.currentTimeMillis())
  {
@@ -9124,7 +9124,7 @@
  }
 
  _nextReducingMPByOverTime = System.currentTimeMillis() + (period * 1000);
- reduceCurrentMp(i);
+ reduceCurrentMp(amount);
  }
 
  /**
Index: java/org/l2jmobius/gameserver/model/skills/handlers/SkillDrain.java
===================================================================
--- java/org/l2jmobius/gameserver/model/skills/handlers/SkillDrain.java (revision 7541)
+++ java/org/l2jmobius/gameserver/model/skills/handlers/SkillDrain.java (working copy)
@@ -57,6 +57,11 @@
  for (WorldObject target2 : targets)
  {
  final Creature target = (Creature) target2;
+ if (creature.isPlayable() && target.isInvul())
+ {
+ continue;
+ }
+
  if (target.isAlikeDead() && (getTargetType() != SkillTargetType.TARGET_CORPSE_MOB))
  {
  continue;
@@ -63,7 +68,7 @@
  }
 
  // Like L2OFF no effect on invul object except Npcs
- if ((creature != target) && (target.isInvul() && !(target instanceof NpcInstance)))
+ if ((creature != target) && (target.isInvul() && !target.isNpc()))
  {
  continue; // No effect on invulnerable chars unless they cast it themselves.
  }


Offline G-hamsteR

  • Viscount
  • *****
    • Posts: 335
I agree with you, but hitting NPCs is allowed in retail.

Drain only? aren't they supposed to hit the npcs because they lag the server? Imagine 200 players at a time using skills or hitting the effects of the shots in giran town :o