tanyaofei/minecraft-fakeplayer

FakePlayer does not receive knockback when hit by players

Closed this issue · 9 comments

Describe the bug / 描述你的 bug 情况
When a FakePlayer is hit by another player, the FakePlayer does not receive knockback.

To Reproduce / 如何触发
Steps to reproduce the behavior: / 触发的步骤

  1. Create a FakePlayer using the plugin.
  2. Hit the FakePlayer with any weapon or by hand.
  3. Observe that the FakePlayer does not receive any knockback.

Server information / 服务器信息

  • Software / 核心: PaperMc 1.21.1
  • Java: 21
  • Version / 版本: 0.3.9

Additional context / 更多信息
Only the plugin and the required dependencies are installed, with no other plugins present. No errors appear in the server logs.

I guess you have OP privilege. So, when you spawn the fakeplayer, this fakeplayer maybe is godmode or other gamemode.

The issue doesn't seem to be related to OP privileges or a specific gamemode like godmode. After reviewing the Citizens plugin code, I found a solution to the knockback issue for FakePlayers. The problem occurs because knockback is usually handled by the client for player entities, but since FakePlayers don’t have a client, this logic needs to be manually handled on the server.

In Citizens, they resolved this by manually handling the knockback server-side. Here’s how they implemented it:

  @Override
  public boolean hurt(DamageSource damagesource, float f) {
      // knock back velocity is cancelled and sent to client for handling when
      // the entity is a player. there is no client so make this happen
      // manually.
      boolean damaged = super.hurt(damagesource, f);
      if (damaged && hurtMarked) {
          hurtMarked = false;
          Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), () -> EntityHumanNPC.this.hurtMarked = true);
      }
      return damaged;
  }

Source: https://github.com/CitizensDev/Citizens2/blob/0f701a3c4078ab8adac30365180fae34babab411/v1_21_R1/src/main/java/net/citizensnpcs/nms/v1_21_R1/entity/EntityHumanNPC.java#L248-L259

参阅README
默认情况下,假玩家处于无敌模式。玩家需要手动关闭无敌模式 /fp config set invulnerable false 才能吸引**性。关闭后,他们会收到饥饿和健康效果。您可能需要使用 res 或 信标 来确保假玩家的饥饿和健康。
所以假人处于无敌模式,你应当使用 指令将玩家修改为允许被攻击的模式
See README
By default, fake players are in invincible mode. Players need to manually turn off invincible mode with /fp config set invulnerable false to attract aggression. After turning it off, they will receive hunger and health effects. You may need to use res or beacon to ensure the fake player's hunger 和 health。
So the fakeplayer is in invincible mode, you should use the command to modify the fakeplayer to a mode that allows being attacked or knockback. @ @BoomPixel

参阅README 默认情况下,假玩家处于无敌模式。玩家需要手动关闭无敌模式 /fp config set invulnerable false 才能吸引**性。关闭后,他们会收到饥饿和健康效果。您可能需要使用 res 或 信标 来确保假玩家的饥饿和健康。 所以假人处于无敌模式,你应当使用 指令将玩家修改为允许被攻击的模式 See README By default, fake players are in invincible mode. Players need to manually turn off invincible mode with /fp config set invulnerable false to attract aggression. After turning it off, they will receive hunger and health effects. You may need to use res or beacon to ensure the fake player's hunger 和 health。 So the fakeplayer is in invincible mode, you should use the command to modify the fakeplayer to a mode that allows being attacked or knockback. @ @BoomPixel

已经关闭了无敌模式,可正常受到爆炸造成的击退,但不能受到玩家攻击所造成的击退,可造成正常伤害

knockback is a clientside behavior, so they are not knocked back when attacked by other players. i am looking for a way to simulate knockback to maintain the same behavior

That's weird, But why are you giving FakePlayer knockback (to play as badminton? LOL)? I use this plugin mainly to give redstone machines random ticks.

所以能否添加一个开关来使FakePlayer受到玩家击退,我正在研究一些有趣的装置

The issue doesn't seem to be related to OP privileges or a specific gamemode like godmode. After reviewing the Citizens plugin code, I found a solution to the knockback issue for FakePlayers. The problem occurs because knockback is usually handled by the client for player entities, but since FakePlayers don’t have a client, this logic needs to be manually handled on the server.

In Citizens, they resolved this by manually handling the knockback server-side. Here’s how they implemented it:

  @Override
  public boolean hurt(DamageSource damagesource, float f) {
      // knock back velocity is cancelled and sent to client for handling when
      // the entity is a player. there is no client so make this happen
      // manually.
      boolean damaged = super.hurt(damagesource, f);
      if (damaged && hurtMarked) {
          hurtMarked = false;
          Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), () -> EntityHumanNPC.this.hurtMarked = true);
      }
      return damaged;
  }

Source: https://github.com/CitizensDev/Citizens2/blob/0f701a3c4078ab8adac30365180fae34babab411/v1_21_R1/src/main/java/net/citizensnpcs/nms/v1_21_R1/entity/EntityHumanNPC.java#L248-L259

This helps me a lot, this bug was fix at 0.3.10