重构击杀记录类型

This commit is contained in:
17146 2024-05-20 17:34:02 +08:00
parent a08c226448
commit fb2316effa
6 changed files with 58 additions and 35 deletions

View file

@ -91,6 +91,6 @@ public class TargetMod {
addNetworkMessage(FireModeMessage.class, FireModeMessage::buffer, FireModeMessage::new, FireModeMessage::handler);
addNetworkMessage(GunRecycleGuiButtonMessage.class, GunRecycleGuiButtonMessage::buffer, GunRecycleGuiButtonMessage::new, GunRecycleGuiButtonMessage::handler);
addNetworkMessage(ReloadMessage.class, ReloadMessage::buffer, ReloadMessage::new, ReloadMessage::handler);
addNetworkMessage(PlayerKillMessage.class, PlayerKillMessage::encode, PlayerKillMessage::decode, PlayerKillMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
addNetworkMessage(PlayerGunKillMessage.class, PlayerGunKillMessage::encode, PlayerGunKillMessage::decode, PlayerGunKillMessage::handler, Optional.of(NetworkDirection.PLAY_TO_CLIENT));
}
}

View file

@ -50,17 +50,33 @@ public class KillMessageOverlay {
int attackerNameWidth = font.width(attackerName);
int targetNameWidth = font.width(targetName);
int nameW = record.headshot ? w - targetNameWidth - 68 - attackerNameWidth : w - targetNameWidth - 50 - attackerNameWidth;
// 击杀提示是右对齐的这里从右向左渲染
// 渲染被击杀者名称
event.getGuiGraphics().drawString(
Minecraft.getInstance().font,
attackerName,
nameW,
targetName,
w - targetNameWidth - 10f,
h,
record.attacker.getTeamColor(),
record.target.getTeamColor(),
false
);
// 渲染爆头图标
if (record.headshot) {
event.getGuiGraphics().blit(HEADSHOT,
w - targetNameWidth - 28,
h - 2,
0,
0,
12,
12,
12,
12
);
}
// 如果是枪械击杀则渲染枪械图标
if (record.stack.getItem() instanceof GunItem gunItem) {
ResourceLocation resourceLocation = gunItem.getGunIcon();
@ -78,26 +94,17 @@ public class KillMessageOverlay {
);
}
if (record.headshot) {
event.getGuiGraphics().blit(HEADSHOT,
w - targetNameWidth - 28,
h - 2,
0,
0,
12,
12,
12,
12
);
}
// 渲染击杀者名称
int nameW = record.headshot ? w - targetNameWidth - 68 - attackerNameWidth : w - targetNameWidth - 50 - attackerNameWidth;
event.getGuiGraphics().drawString(
Minecraft.getInstance().font,
targetName,
w - targetNameWidth - 10f,
attackerName,
nameW,
h,
record.target.getTeamColor(),
record.attacker.getTeamColor(),
false
);
}
}

View file

@ -8,7 +8,7 @@ import net.mcreator.target.init.TargetModSounds;
import net.mcreator.target.init.TargetModTags;
import net.mcreator.target.item.gun.GunItem;
import net.mcreator.target.network.TargetModVariables;
import net.mcreator.target.network.message.PlayerKillMessage;
import net.mcreator.target.network.message.PlayerGunKillMessage;
import net.mcreator.target.tools.SoundTool;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.protocol.game.ClientboundStopSoundPacket;
@ -18,6 +18,7 @@ import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.damagesource.DamageSource;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.damagesource.DamageTypes;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
@ -217,11 +218,13 @@ public class LivingEntityEventHandler {
return;
}
ResourceKey<DamageType> damageTypeResourceKey = source.typeHolder().unwrapKey().isPresent() ? source.typeHolder().unwrapKey().get() : DamageTypes.GENERIC;
if (source.getDirectEntity() instanceof ServerPlayer player) {
if (source.is(TargetModDamageTypes.GUN_FIRE) || source.is(TargetModDamageTypes.ARROW_IN_KNEE)) {
TargetMod.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new PlayerKillMessage(player.getId(), entity.getId(), false));
} else if (source.is(TargetModDamageTypes.GUN_FIRE_HEADSHOT) || source.is(TargetModDamageTypes.ARROW_IN_BRAIN)) {
TargetMod.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new PlayerKillMessage(player.getId(), entity.getId(), true));
if (source.is(TargetModDamageTypes.GUN_FIRE_HEADSHOT) || source.is(TargetModDamageTypes.ARROW_IN_BRAIN)) {
TargetMod.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new PlayerGunKillMessage(player.getId(), entity.getId(), true, damageTypeResourceKey));
} else {
TargetMod.PACKET_HANDLER.send(PacketDistributor.ALL.noArg(), new PlayerGunKillMessage(player.getId(), entity.getId(), false, damageTypeResourceKey));
}
}
}

View file

@ -4,6 +4,8 @@ import net.mcreator.target.event.KillMessageHandler;
import net.mcreator.target.network.message.GunsDataMessage;
import net.mcreator.target.tools.GunsTool;
import net.mcreator.target.tools.PlayerKillRecord;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.fml.LogicalSide;
@ -19,9 +21,9 @@ public class ClientPacketHandler {
}
}
public static void handlePlayerKillMessage(Player attacker, Entity target, boolean headshot, Supplier<NetworkEvent.Context> ctx) {
public static void handlePlayerKillMessage(Player attacker, Entity target, boolean headshot, ResourceKey<DamageType> damageType, Supplier<NetworkEvent.Context> ctx) {
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
KillMessageHandler.QUEUE.offer(new PlayerKillRecord(attacker, target, attacker.getMainHandItem(), headshot));
KillMessageHandler.QUEUE.offer(new PlayerKillRecord(attacker, target, attacker.getMainHandItem(), headshot, damageType));
}
}
}

View file

@ -3,7 +3,10 @@ package net.mcreator.target.network.message;
import net.mcreator.target.network.ClientPacketHandler;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraftforge.api.distmarker.Dist;
@ -12,31 +15,35 @@ import net.minecraftforge.network.NetworkEvent;
import java.util.function.Supplier;
public class PlayerKillMessage {
public class PlayerGunKillMessage {
public final int attackerId;
public final int targetId;
public final boolean headshot;
public final ResourceKey<DamageType> damageType;
public PlayerKillMessage(int attackerId, int targetId, boolean headshot) {
public PlayerGunKillMessage(int attackerId, int targetId, boolean headshot, ResourceKey<DamageType> damageType) {
this.attackerId = attackerId;
this.targetId = targetId;
this.headshot = headshot;
this.damageType = damageType;
}
public static void encode(PlayerKillMessage message, FriendlyByteBuf buffer) {
public static void encode(PlayerGunKillMessage message, FriendlyByteBuf buffer) {
buffer.writeInt(message.attackerId);
buffer.writeInt(message.targetId);
buffer.writeBoolean(message.headshot);
buffer.writeResourceKey(message.damageType);
}
public static PlayerKillMessage decode(FriendlyByteBuf buffer) {
public static PlayerGunKillMessage decode(FriendlyByteBuf buffer) {
int attackerId = buffer.readInt();
int targetId = buffer.readInt();
boolean headshot = buffer.readBoolean();
return new PlayerKillMessage(attackerId, targetId, headshot);
ResourceKey<DamageType> damageType = buffer.readResourceKey(Registries.DAMAGE_TYPE);
return new PlayerGunKillMessage(attackerId, targetId, headshot, damageType);
}
public static void handler(PlayerKillMessage message, Supplier<NetworkEvent.Context> ctx) {
public static void handler(PlayerGunKillMessage message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
ClientLevel level = Minecraft.getInstance().level;
if (level != null) {
@ -44,7 +51,7 @@ public class PlayerKillMessage {
Entity target = level.getEntity(message.targetId);
if (player != null && target != null) {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPacketHandler.handlePlayerKillMessage(player, target, message.headshot, ctx));
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPacketHandler.handlePlayerKillMessage(player, target, message.headshot, message.damageType, ctx));
}
}
});

View file

@ -1,5 +1,7 @@
package net.mcreator.target.tools;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.damagesource.DamageType;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
@ -10,12 +12,14 @@ public class PlayerKillRecord {
public ItemStack stack;
public boolean headshot;
public int tick;
public ResourceKey<DamageType> damageType;
public PlayerKillRecord(Player attacker, Entity target, ItemStack stack, boolean headshot) {
public PlayerKillRecord(Player attacker, Entity target, ItemStack stack, boolean headshot, ResourceKey<DamageType> damageType) {
this.attacker = attacker;
this.target = target;
this.stack = stack;
this.headshot = headshot;
this.tick = 0;
this.damageType = damageType;
}
}