java 如何检测玩家的死亡以及是什么杀死了它- Fabric MC 1.20.4

eiee3dmh  于 2024-01-05  发布在  Java
关注(0)|答案(1)|浏览(246)

我试过使用mixin,但它似乎根本无法运行,以下是我的代码,

  1. import net.minecraft.entity.damage.DamageSource;
  2. import net.minecraft.entity.player.PlayerEntity;
  3. import net.spamta.pvpoverhaul.PvPOverhaul;
  4. import org.spongepowered.asm.mixin.Mixin;
  5. import org.spongepowered.asm.mixin.injection.At;
  6. import org.spongepowered.asm.mixin.injection.Inject;
  7. import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
  8. import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
  9. import net.minecraft.server.MinecraftServer;
  10. import net.minecraft.server.network.ServerPlayerEntity;
  11. import net.minecraft.text.Text;
  12. @Mixin(PlayerEntity.class)
  13. public class GearKeeper {
  14. @Inject(method="onDeath", at=@At("TAIL"))
  15. private void died(DamageSource damageSource, CallbackInfo info){
  16. PvPOverhaul.LOGGER.info(damageSource.getName().toString());
  17. PlayerEntity player = (PlayerEntity) (Object) this;
  18. sendDeathMessageToAllPlayers(player);
  19. }
  20. private void sendDeathMessageToAllPlayers(PlayerEntity player) {
  21. MinecraftServer server = player.getServer();
  22. if (server != null) {
  23. server.getPlayerManager().getPlayerList().forEach(serverPlayer -> {
  24. serverPlayer.sendMessage(Text.literal(player.getName().toString() + " has perished!"), false);
  25. });
  26. }
  27. }
  28. }

字符串
尝试检测是否有玩家被杀,并期望它说“Piglin Brute”或当时杀死我的任何东西,但它最终没有记录任何东西。

3pmvbmvn

3pmvbmvn1#

  1. import net.minecraft.entity.damage.DamageSource;
  2. import net.minecraft.entity.player.PlayerEntity;
  3. import net.spamta.pvpoverhaul.PvPOverhaul;
  4. import org.spongepowered.asm.mixin.Mixin;
  5. import org.spongepowered.asm.mixin.injection.At;
  6. import org.spongepowered.asm.mixin.injection.Inject;
  7. import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
  8. import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
  9. import net.minecraft.server.MinecraftServer;
  10. import net.minecraft.server.network.ServerPlayerEntity;
  11. import net.minecraft.text.Text;
  12. @Mixin(PlayerEntity.class)
  13. public class GearKeeper {
  14. @Inject(method="onDeath", at=@At("TAIL"))
  15. private void died(DamageSource damageSource, CallbackInfo info){
  16. PvPOverhaul.LOGGER.info(damageSource.getName().toString());
  17. PlayerEntity player = (PlayerEntity) (Object) this;
  18. sendDeathMessageToAllPlayers(player);
  19. }
  20. private void sendDeathMessageToAllPlayers(PlayerEntity player) {
  21. MinecraftServer server = player.getServer();
  22. if (server != null) {
  23. server.getPlayerManager().getPlayerList().forEach(serverPlayer -> {
  24. serverPlayer.sendMessage(Text.literal(player.getName().toString() + " has
  25. perished!"), false);
  26. });
  27. }
  28. }
  29. }

字符串
您还应该在resources文件夹中创建一个名为pvpoverhaul.mixins.json的mixin配置文件

  1. {
  2. "required": 1,
  3. "package": "net.spamta.pvpoverhaul.mixin",
  4. "compatibilityLevel": "JAVA_16",
  5. "mixins": [
  6. "GearKeeper"
  7. ]
  8. }


如果您已经正确设置了所有内容,那么mixin现在应该已经加载,并且deed方法中的代码应该在玩家死亡时执行。然后damageSource.getName().toString()应该记录杀死玩家的东西的名称。
希望这个答案能奏效

展开查看全部

相关问题