添加新的伤害类型

This commit is contained in:
17146 2024-05-19 01:08:45 +08:00
parent 9e0e39a2fd
commit 305bd29bba
15 changed files with 99 additions and 24 deletions

View file

@ -350,7 +350,7 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
capability.syncPlayerVariables(shooter); capability.syncPlayerVariables(shooter);
}); });
entity.hurt(TargetModDamageTypes.causeGunFireDamage(this.level().registryAccess(), this.shooter), this.damage * this.headShot); entity.hurt(TargetModDamageTypes.causeGunFireHeadshotDamage(this.level().registryAccess(), this.shooter), this.damage * this.headShot);
} else { } else {
if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) { if (!this.shooter.level().isClientSide() && this.shooter instanceof ServerPlayer player) {
var holder = Holder.direct(TargetModSounds.INDICATION.get()); var holder = Holder.direct(TargetModSounds.INDICATION.get());

View file

@ -0,0 +1,29 @@
package net.mcreator.target.event;
import net.mcreator.target.tools.PlayerKillRecord;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import java.util.ArrayDeque;
import java.util.Queue;
@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class KillMessageHandler {
public static Queue<PlayerKillRecord> QUEUE = new ArrayDeque<>();
@SubscribeEvent
public static void onClientTick(TickEvent.ClientTickEvent event) {
if (event.phase == TickEvent.Phase.END) {
for (PlayerKillRecord record : QUEUE) {
record.tick++;
if (record.tick >= 100) {
QUEUE.poll();
}
}
}
}
}

View file

@ -73,7 +73,7 @@ public class LivingEntityEventHandler {
stack.getOrCreateTag().putDouble("damagetotal", stack.getOrCreateTag().getDouble("damagetotal") + damage); stack.getOrCreateTag().putDouble("damagetotal", stack.getOrCreateTag().getDouble("damagetotal") + damage);
} }
if (damagesource.is(TargetModDamageTypes.GUNFIRE)) { if (damagesource.is(TargetModDamageTypes.GUN_FIRE)) {
double distance = entity.position().distanceTo(sourceentity.position()); double distance = entity.position().distanceTo(sourceentity.position());
if (stack.is(TargetModTags.Items.SHOTGUN) || stack.getItem() == TargetModItems.BOCEK.get()) { if (stack.is(TargetModTags.Items.SHOTGUN) || stack.getItem() == TargetModItems.BOCEK.get()) {
@ -225,8 +225,10 @@ public class LivingEntityEventHandler {
DamageSource source = event.getSource(); DamageSource source = event.getSource();
if (source.getDirectEntity() instanceof ServerPlayer player) { if (source.getDirectEntity() instanceof ServerPlayer player) {
if (source.is(TargetModDamageTypes.GUNFIRE)) { if (source.is(TargetModDamageTypes.GUN_FIRE)) {
TargetMod.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerKillMessage(player.getId(), entity.getId())); TargetMod.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerKillMessage(player.getId(), entity.getId(), false));
} else if (source.is(TargetModDamageTypes.GUN_FIRE_HEADSHOT)) {
TargetMod.PACKET_HANDLER.send(PacketDistributor.PLAYER.with(() -> player), new PlayerKillMessage(player.getId(), entity.getId(), true));
} }
} }
} }

View file

@ -16,13 +16,18 @@ import javax.annotation.Nullable;
@SuppressWarnings("OptionalGetWithoutIsPresent") @SuppressWarnings("OptionalGetWithoutIsPresent")
public class TargetModDamageTypes { public class TargetModDamageTypes {
public static final ResourceKey<DamageType> GUNFIRE = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "gunfire")); public static final ResourceKey<DamageType> GUN_FIRE = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "gunfire"));
public static final ResourceKey<DamageType> GUN_FIRE_HEADSHOT = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "gunfire_headshot"));
public static final ResourceKey<DamageType> ARROW_IN_BRAIN = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "arrow_in_brain")); public static final ResourceKey<DamageType> ARROW_IN_BRAIN = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "arrow_in_brain"));
public static final ResourceKey<DamageType> MINE = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "mine")); public static final ResourceKey<DamageType> MINE = ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation(TargetMod.MODID, "mine"));
public static DamageSource causeGunFireDamage(RegistryAccess registryAccess, @Nullable Entity entity) { public static DamageSource causeGunFireDamage(RegistryAccess registryAccess, @Nullable Entity entity) {
return new DamageMessages(registryAccess.registry(Registries.DAMAGE_TYPE).get().getHolderOrThrow(GUNFIRE), entity); return new DamageMessages(registryAccess.registry(Registries.DAMAGE_TYPE).get().getHolderOrThrow(GUN_FIRE), entity);
}
public static DamageSource causeGunFireHeadshotDamage(RegistryAccess registryAccess, @Nullable Entity entity) {
return new DamageMessages(registryAccess.registry(Registries.DAMAGE_TYPE).get().getHolderOrThrow(GUN_FIRE_HEADSHOT), entity);
} }
public static DamageSource causeArrowInBrainDamage(RegistryAccess registryAccess, @Nullable Entity entity) { public static DamageSource causeArrowInBrainDamage(RegistryAccess registryAccess, @Nullable Entity entity) {

View file

@ -22,7 +22,7 @@ public class LivingEntityMixin {
@ModifyArg(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;knockback(DDD)V"), index = 0) @ModifyArg(method = "hurt", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/LivingEntity;knockback(DDD)V"), index = 0)
private double modifyApplyKnockbackArgs(double original) { private double modifyApplyKnockbackArgs(double original) {
if (this.target$source.is(TargetModDamageTypes.GUNFIRE)) { if (this.target$source.is(TargetModDamageTypes.GUN_FIRE)) {
return 0.1 * original; return 0.1 * original;
} }
return original; return original;

View file

@ -1,7 +1,9 @@
package net.mcreator.target.network; package net.mcreator.target.network;
import net.mcreator.target.event.KillMessageHandler;
import net.mcreator.target.network.message.GunsDataMessage; import net.mcreator.target.network.message.GunsDataMessage;
import net.mcreator.target.tools.GunsTool; import net.mcreator.target.tools.GunsTool;
import net.mcreator.target.tools.PlayerKillRecord;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.fml.LogicalSide;
@ -17,9 +19,9 @@ public class ClientPacketHandler {
} }
} }
public static void handlePlayerKillMessage(Player attacker, Entity target, Supplier<NetworkEvent.Context> ctx) { public static void handlePlayerKillMessage(Player attacker, Entity target, boolean headshot, Supplier<NetworkEvent.Context> ctx) {
if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) { if (ctx.get().getDirection().getReceptionSide() == LogicalSide.CLIENT) {
System.out.println(attacker + " killed " + target); KillMessageHandler.QUEUE.offer(new PlayerKillRecord(attacker, target, attacker.getMainHandItem(), headshot));
} }
} }
} }

View file

@ -15,21 +15,25 @@ import java.util.function.Supplier;
public class PlayerKillMessage { public class PlayerKillMessage {
public final int attackerId; public final int attackerId;
public final int targetId; public final int targetId;
public final boolean headshot;
public PlayerKillMessage(int attackerId, int targetId) { public PlayerKillMessage(int attackerId, int targetId, boolean headshot) {
this.attackerId = attackerId; this.attackerId = attackerId;
this.targetId = targetId; this.targetId = targetId;
this.headshot = headshot;
} }
public static void encode(PlayerKillMessage message, FriendlyByteBuf buffer) { public static void encode(PlayerKillMessage message, FriendlyByteBuf buffer) {
buffer.writeInt(message.attackerId); buffer.writeInt(message.attackerId);
buffer.writeInt(message.targetId); buffer.writeInt(message.targetId);
buffer.writeBoolean(message.headshot);
} }
public static PlayerKillMessage decode(FriendlyByteBuf buffer) { public static PlayerKillMessage decode(FriendlyByteBuf buffer) {
int attackerId = buffer.readInt(); int attackerId = buffer.readInt();
int targetId = buffer.readInt(); int targetId = buffer.readInt();
return new PlayerKillMessage(attackerId, targetId); boolean headshot = buffer.readBoolean();
return new PlayerKillMessage(attackerId, targetId, headshot);
} }
public static void handler(PlayerKillMessage message, Supplier<NetworkEvent.Context> ctx) { public static void handler(PlayerKillMessage message, Supplier<NetworkEvent.Context> ctx) {
@ -40,7 +44,7 @@ public class PlayerKillMessage {
Entity target = level.getEntity(message.targetId); Entity target = level.getEntity(message.targetId);
if (player != null && target != null) { if (player != null && target != null) {
DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPacketHandler.handlePlayerKillMessage(player, target, ctx)); DistExecutor.unsafeRunWhenOn(Dist.CLIENT, () -> () -> ClientPacketHandler.handlePlayerKillMessage(player, target, message.headshot, ctx));
} }
} }
}); });

View file

@ -0,0 +1,21 @@
package net.mcreator.target.tools;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
public class PlayerKillRecord {
public Player attacker;
public Entity target;
public ItemStack stack;
public boolean headshot;
public int tick;
public PlayerKillRecord(Player attacker, Entity target, ItemStack stack, boolean headshot) {
this.attacker = attacker;
this.target = target;
this.stack = stack;
this.headshot = headshot;
this.tick = 0;
}
}

View file

@ -152,12 +152,13 @@
"death.attack.gunfire": "%1$s was shoot by %2$s", "death.attack.gunfire": "%1$s was shoot by %2$s",
"death.attack.gunfire.entity": "%1$s was shoot by %2$s", "death.attack.gunfire.entity": "%1$s was shoot by %2$s",
"death.attack.gunfire.item": "%1$s was shoot by %2$s", "death.attack.gunfire.item": "%1$s was shoot by %2$s using %3$s",
"death.attack.gunfire.player": "%1$s was shoot by %2$s", "death.attack.gunfire_headshot": "%1$s was headshot by %2$s",
"death.attack.gunfire_headshot.entity": "%1$s was headshot by %2$s",
"death.attack.gunfire_headshot.item": "%1$s was headshot by %2$s using %3$s",
"death.attack.arrow_in_brain": "%2$s's arrow shot into %1$s's brain", "death.attack.arrow_in_brain": "%2$s's arrow shot into %1$s's brain",
"death.attack.arrow_in_brain.item": "An arrow shot into %1$s's brain,murderer is %2$s used %3$s", "death.attack.arrow_in_brain.item": "An arrow shot into %1$s's brain,murderer is %2$s used %3$s",
"death.attack.arrow_in_brain.player": "An arrow shot into %1$s's brain whilst trying to escape %2$s", "death.attack.arrow_in_brain.entity": "An arrow shot into %1$s's brain whilst trying to escape %2$s",
"death.attack.beast_gun": "%1$s was killed by %2$s using BEAST guns", "death.attack.beast_gun": "%1$s was killed by %2$s using BEAST guns",
"Shell Estimated Range": "Estimated Range:", "Shell Estimated Range": "Estimated Range:",

View file

@ -153,10 +153,12 @@
"death.attack.gunfire": "%1$s被%2$s射爆了", "death.attack.gunfire": "%1$s被%2$s射爆了",
"death.attack.gunfire.entity": "%1$s被%2$s射爆了", "death.attack.gunfire.entity": "%1$s被%2$s射爆了",
"death.attack.gunfire.item": "%1$s被%2$s用%3$s射爆了", "death.attack.gunfire.item": "%1$s被%2$s用%3$s射爆了",
"death.attack.gunfire.player": "%1$s在逃离%2$s的时候被射爆了", "death.attack.gunfire_headshot": "%1$s被%2$s射爆了脑袋",
"death.attack.arrow_in_brain": "%1$s的脑子进矢了,凶手是%2$s", "death.attack.gunfire_headshot.entity": "%1$s被%2$s射爆了脑袋",
"death.attack.arrow_in_brain.item": "%1$s的脑子进矢了,凶手%2$s使用了%3$s", "death.attack.gunfire_headshot.item": "%1$s被%2$s用%3$s射爆了脑袋",
"death.attack.arrow_in_brain.player": "%1$s在逃离%2$s的时候脑子进矢了", "death.attack.arrow_in_brain": "%1$s的脑子进矢了凶手是%2$s",
"death.attack.arrow_in_brain.item": "%1$s的脑子进矢了凶手%2$s使用了%3$s",
"death.attack.arrow_in_brain.entity": "%1$s在逃离%2$s的时候脑子进矢了",
"death.attack.beast_gun": "%1$s被%2$s用BEAST枪械臭炸了", "death.attack.beast_gun": "%1$s被%2$s用BEAST枪械臭炸了",
"Shell Estimated Range": "炮弹预估射程:", "Shell Estimated Range": "炮弹预估射程:",

View file

@ -1,6 +1,7 @@
{ {
"replace": false, "replace": false,
"values": [ "values": [
"target:gunfire" "target:gunfire",
"target:gunfire_headshot"
] ]
} }

View file

@ -1,6 +1,7 @@
{ {
"replace": false, "replace": false,
"values": [ "values": [
"target:gunfire" "target:gunfire",
"target:gunfire_headshot"
] ]
} }

View file

@ -1,6 +1,7 @@
{ {
"replace": false, "replace": false,
"values": [ "values": [
"target:gunfire" "target:gunfire",
"target:gunfire_headshot"
] ]
} }

View file

@ -1,6 +1,7 @@
{ {
"replace": false, "replace": false,
"values": [ "values": [
"target:gunfire" "target:gunfire",
"target:gunfire_headshot"
] ]
} }

View file

@ -0,0 +1,5 @@
{
"exhaustion": 0,
"message_id": "gunfire_headshot",
"scaling": "never"
}