添加粒子工具类

This commit is contained in:
17146 2024-05-19 22:59:48 +08:00
parent cdd6d457ee
commit dbe0c91f89
5 changed files with 66 additions and 49 deletions

View file

@ -5,6 +5,7 @@ import net.mcreator.target.headshot.IHeadshotBox;
import net.mcreator.target.init.*;
import net.mcreator.target.network.TargetModVariables;
import net.mcreator.target.tools.ExtendedEntityRayTraceResult;
import net.mcreator.target.tools.ParticleTool;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Holder;
@ -258,11 +259,10 @@ public class ProjectileEntity extends Entity implements IEntityAdditionalSpawnDa
protected void onHitBlock(Vec3 location) {
if (this.level() instanceof ServerLevel serverLevel) {
if (this.beast) {
serverLevel.sendParticles(ParticleTypes.END_ROD, location.x, location.y, location.z, 15, 0.1, 0.1, 0.1, 0.05);
ParticleTool.sendParticle(serverLevel, ParticleTypes.END_ROD, location.x, location.y, location.z, 15, 0.1, 0.1, 0.1, 0.05, true);
} else {
serverLevel.sendParticles(TargetModParticleTypes.BULLET_HOLE.get(), location.x, location.y, location.z, 1, 0, 0, 0, 0);
serverLevel.sendParticles(ParticleTypes.SMOKE, location.x, location.y, location.z, 3, 0, 0.1, 0, 0.01);
serverLevel.sendParticles(TargetModParticleTypes.BULLET_HOLE.get(), location.x, location.y, location.z, 1, 0, 0, 0, 0);
ParticleTool.sendParticle(serverLevel, TargetModParticleTypes.BULLET_HOLE.get(), location.x, location.y, location.z, 1, 0, 0, 0, 0, true);
ParticleTool.sendParticle(serverLevel, ParticleTypes.SMOKE, location.x, location.y, location.z, 3, 0, 0.1, 0, 0.01, true);
this.discard();
}

View file

@ -13,6 +13,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
@ -173,11 +174,7 @@ public class GunEventHandler {
}
if (player.getMainHandItem().getItem() == TargetModItems.SENTINEL.get()) {
if ((player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).zooming) {
stack.getOrCreateTag().putBoolean("zoom_fire", true);
} else {
stack.getOrCreateTag().putBoolean("zoom_fire", false);
}
stack.getOrCreateTag().putBoolean("zoom_fire", (player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).zooming);
if (stack.getOrCreateTag().getDouble("power") > 20) {
stack.getOrCreateTag().putDouble("power", (stack.getOrCreateTag().getDouble("power") - 20));
} else {
@ -232,7 +229,7 @@ public class GunEventHandler {
SoundEvent sound3p = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "v_fire_3p"));
if (sound3p != null) {
player.playSound(sound3p, 4f, 1f);
player.level().playSound(null, player.getOnPos(), sound3p, SoundSource.PLAYERS, 4f, 1f);
}
SoundEvent soundFar = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_charge_far"));
@ -252,7 +249,7 @@ public class GunEventHandler {
SoundEvent sound3p = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_fire_3p"));
if (sound3p != null) {
player.playSound(sound3p, 4f, 1f);
player.level().playSound(null, player.getOnPos(), sound3p, SoundSource.PLAYERS, 4f, 1f);
}
SoundEvent soundFar = ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation(TargetMod.MODID, name + "_far"));

View file

@ -7,10 +7,8 @@ import net.mcreator.target.init.TargetModItems;
import net.mcreator.target.init.TargetModSounds;
import net.mcreator.target.network.TargetModVariables;
import net.mcreator.target.tools.SoundTool;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
@ -24,8 +22,9 @@ public class TaserfireProcedure {
if (entity == null) return;
if (entity instanceof Player player && !player.isSpectator()) {
ItemStack usehand = player.getMainHandItem();
if (usehand.getItem() == TargetModItems.TASER.get() && !usehand.getOrCreateTag().getBoolean("reloading") && !(entity instanceof Player _plrCldCheck4 && _plrCldCheck4.getCooldowns().isOnCooldown(usehand.getItem()))
&& usehand.getOrCreateTag().getInt("ammo") > 0) {
if (usehand.getItem() == TargetModItems.TASER.get() && !usehand.getOrCreateTag().getBoolean("reloading")) {
Player _plrCldCheck4 = (Player) entity;
if (!_plrCldCheck4.getCooldowns().isOnCooldown(usehand.getItem()) && usehand.getOrCreateTag().getInt("ammo") > 0) {
entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1;
@ -37,11 +36,7 @@ public class TaserfireProcedure {
if (entity instanceof ServerPlayer serverPlayer) {
SoundTool.playLocalSound(serverPlayer, TargetModSounds.TASER_FIRE_1P.get(), 1, 1);
serverPlayer.playSound(TargetModSounds.TASER_FIRE_3P.get(), 1, 1);
// TODO 重写stopsound
entity.getServer().getCommands().performPrefixedCommand(new CommandSourceStack(CommandSource.NULL, entity.position(), entity.getRotationVector(), entity.level() instanceof ServerLevel ? (ServerLevel) entity.level() : null, 4,
entity.getName().getString(), entity.getDisplayName(), entity.level().getServer(), entity), "stopsound @s player target:taser_fire_3p");
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), TargetModSounds.TASER_FIRE_3P.get(), SoundSource.PLAYERS, 1, 1);
}
Level projectileLevel = entity.level();
@ -66,4 +61,5 @@ public class TaserfireProcedure {
}
}
}
}
}

View file

@ -0,0 +1,14 @@
package net.mcreator.target.tools;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
public class ParticleTool {
public static <T extends ParticleOptions> void sendParticle(ServerLevel level, T particle, double x, double y, double z, int count,
double xOffset, double yOffset, double zOffset, double speed, boolean force) {
for (ServerPlayer serverPlayer : level.players()) {
level.sendParticles(serverPlayer, particle, force, x, y, z, count, xOffset, yOffset, zOffset, speed);
}
}
}

View file

@ -2,6 +2,8 @@ package net.mcreator.target.tools;
import net.minecraft.core.Holder;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
import net.minecraft.network.protocol.game.ClientboundStopSoundPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
@ -30,4 +32,12 @@ public class SoundTool {
player.connection.send(new ClientboundSoundPacket(new Holder.Direct<>(sound),
source, player.getX(), player.getY(), player.getZ(), volume, pitch, player.level().random.nextLong()));
}
public static void stopSound(ServerPlayer player, ResourceLocation sound) {
stopSound(player, sound, SoundSource.PLAYERS);
}
public static void stopSound(ServerPlayer player, ResourceLocation sound, SoundSource source) {
player.connection.send(new ClientboundStopSoundPacket(sound, source));
}
}