移动伤害指示器渲染代码
This commit is contained in:
parent
72e4b0ac12
commit
a0b8fdc6c9
4 changed files with 77 additions and 126 deletions
|
@ -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);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -60,6 +60,7 @@ public class PlayerEventHandler {
|
||||||
handleSpecialWeaponAmmo(player);
|
handleSpecialWeaponAmmo(player);
|
||||||
handleChangeFireRate(player);
|
handleChangeFireRate(player);
|
||||||
handleDistantRange(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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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();
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Add table
Reference in a new issue