diff --git a/src/main/java/net/mcreator/target/event/LivingEntityEventHandler.java b/src/main/java/net/mcreator/target/event/LivingEntityEventHandler.java new file mode 100644 index 000000000..a265490aa --- /dev/null +++ b/src/main/java/net/mcreator/target/event/LivingEntityEventHandler.java @@ -0,0 +1,42 @@ +package net.mcreator.target.event; + +import net.mcreator.target.network.TargetModVariables; +import net.minecraft.commands.CommandSource; +import net.minecraft.commands.CommandSourceStack; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.world.damagesource.DamageTypes; +import net.minecraft.world.entity.player.Player; +import net.minecraftforge.event.entity.living.LivingHurtEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.common.Mod; + +@Mod.EventBusSubscriber +public class LivingEntityEventHandler { + @SubscribeEvent + public static void onEntityAttacked(LivingHurtEvent event) { + renderDamageIndicator(event); + } + + private static void renderDamageIndicator(LivingHurtEvent event) { + if (event == null || event.getEntity() == null) return; + var damagesource = event.getSource(); + var sourceEntity = event.getEntity(); + + if (damagesource == null || sourceEntity == null) return; + + if (sourceEntity instanceof Player && (damagesource.is(DamageTypes.EXPLOSION) || damagesource.is(DamageTypes.PLAYER_EXPLOSION) || damagesource.is(ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation("target:mine"))))) { + if (sourceEntity.getServer() != null) { + // TODO 修改为正确的音效播放方法 + sourceEntity.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, sourceEntity.position(), sourceEntity.getRotationVector(), sourceEntity.level() instanceof ServerLevel ? (ServerLevel) sourceEntity.level() : null, 4, + sourceEntity.getName().getString(), sourceEntity.getDisplayName(), sourceEntity.level().getServer(), sourceEntity), "playsound target:indication voice @a ~ ~ ~ 1 1"); + } + sourceEntity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + capability.hitIndicator = 25; + capability.syncPlayerVariables(sourceEntity); + }); + } + } +} diff --git a/src/main/java/net/mcreator/target/event/PlayerEventHandler.java b/src/main/java/net/mcreator/target/event/PlayerEventHandler.java index dc1f33741..8026bb0e1 100644 --- a/src/main/java/net/mcreator/target/event/PlayerEventHandler.java +++ b/src/main/java/net/mcreator/target/event/PlayerEventHandler.java @@ -60,6 +60,7 @@ public class PlayerEventHandler { handleSpecialWeaponAmmo(player); handleChangeFireRate(player); handleDistantRange(player); + renderDamageIndicator(player); } } @@ -315,4 +316,38 @@ public class PlayerEventHandler { } } } + + + private static void renderDamageIndicator(Player player) { + double[] recoilTimer = {0}; + double totalTime = 10; + int sleepTime = 2; + double recoilDuration = totalTime / sleepTime; + Runnable recoilRunnable = () -> { + while (recoilTimer[0] < recoilDuration) { + if (player == null) return; + + player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { + var headIndicator = player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> c.headIndicator).orElse(0d); + var hitIndicator = player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> c.headIndicator).orElse(0d); + var killIndicator = player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).map(c -> c.killIndicator).orElse(0d); + + capability.headIndicator = Math.max(0, headIndicator - 1); + capability.hitIndicator = Math.max(0, hitIndicator - 1); + capability.killIndicator = Math.max(0, killIndicator - 1); + + capability.syncPlayerVariables(player); + }); + + recoilTimer[0]++; + try { + Thread.sleep(sleepTime); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }; + Thread recoilThread = new Thread(recoilRunnable); + recoilThread.start(); + } } diff --git a/src/main/java/net/mcreator/target/procedures/Hitindication2Procedure.java b/src/main/java/net/mcreator/target/procedures/Hitindication2Procedure.java deleted file mode 100644 index 4ffe3c9a8..000000000 --- a/src/main/java/net/mcreator/target/procedures/Hitindication2Procedure.java +++ /dev/null @@ -1,53 +0,0 @@ -package net.mcreator.target.procedures; - -import net.mcreator.target.network.TargetModVariables; -import net.minecraft.commands.CommandSource; -import net.minecraft.commands.CommandSourceStack; -import net.minecraft.core.registries.Registries; -import net.minecraft.resources.ResourceKey; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.world.damagesource.DamageSource; -import net.minecraft.world.damagesource.DamageTypes; -import net.minecraft.world.entity.Entity; -import net.minecraft.world.entity.player.Player; -import net.minecraftforge.event.entity.living.LivingHurtEvent; -import net.minecraftforge.eventbus.api.Event; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.common.Mod; - -import javax.annotation.Nullable; - -@Mod.EventBusSubscriber -public class Hitindication2Procedure { - @SubscribeEvent - public static void onEntityAttacked(LivingHurtEvent event) { - if (event != null && event.getEntity() != null) { - execute(event, event.getSource(), event.getSource().getEntity()); - } - } - - public static void execute(DamageSource damagesource, Entity sourceentity) { - execute(null, damagesource, sourceentity); - } - - private static void execute(@Nullable Event event, DamageSource damagesource, Entity sourceentity) { - if (damagesource == null || sourceentity == null) - return; - if (sourceentity instanceof Player && (damagesource.is(DamageTypes.EXPLOSION) || damagesource.is(DamageTypes.PLAYER_EXPLOSION) || damagesource.is(ResourceKey.create(Registries.DAMAGE_TYPE, new ResourceLocation("target:mine"))))) { - { - if (!sourceentity.level().isClientSide() && sourceentity.getServer() != null) { - sourceentity.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, sourceentity.position(), sourceentity.getRotationVector(), sourceentity.level() instanceof ServerLevel ? (ServerLevel) sourceentity.level() : null, 4, - sourceentity.getName().getString(), sourceentity.getDisplayName(), sourceentity.level().getServer(), sourceentity), "playsound target:indication voice @a ~ ~ ~ 1 1"); - } - } - { - double _setval = 25; - sourceentity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.hitIndicator = _setval; - capability.syncPlayerVariables(sourceentity); - }); - } - } - } -} diff --git a/src/main/java/net/mcreator/target/procedures/HitindicationProcedure.java b/src/main/java/net/mcreator/target/procedures/HitindicationProcedure.java deleted file mode 100644 index c45db5547..000000000 --- a/src/main/java/net/mcreator/target/procedures/HitindicationProcedure.java +++ /dev/null @@ -1,73 +0,0 @@ -package net.mcreator.target.procedures; - -import net.mcreator.target.network.TargetModVariables; -import net.minecraft.world.entity.Entity; -import net.minecraftforge.event.TickEvent; -import net.minecraftforge.eventbus.api.Event; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.common.Mod; - -import javax.annotation.Nullable; - -@Mod.EventBusSubscriber -public class HitindicationProcedure { - @SubscribeEvent - public static void onPlayerTick(TickEvent.PlayerTickEvent event) { - if (event.phase == TickEvent.Phase.END) { - execute(event, event.player); - } - } - - public static void execute(Entity entity) { - execute(null, entity); - } - - private static void execute(@Nullable Event event, Entity entity) { - - double[] recoilTimer = {0}; - double totalTime = 10; - int sleepTime = 2; - double recoilDuration = totalTime / sleepTime; - Runnable recoilRunnable = () -> { - while (recoilTimer[0] < recoilDuration) { - if (entity == null) - return; - if ((entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).hitIndicator > 0) { - { - double _setval = (entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).hitIndicator - 1; - entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.hitIndicator = _setval; - capability.syncPlayerVariables(entity); - }); - } - } - if ((entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).headIndicator > 0) { - { - double _setval = (entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).headIndicator - 1; - entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.headIndicator = _setval; - capability.syncPlayerVariables(entity); - }); - } - } - if ((entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).killIndicator > 0) { - { - double _setval = (entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).killIndicator - 1; - entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { - capability.killIndicator = _setval; - capability.syncPlayerVariables(entity); - }); - } - } - recoilTimer[0]++; - try { - Thread.sleep(sleepTime); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - }; - Thread recoilThread = new Thread(recoilRunnable); - recoilThread.start(); - } -}