重构taser电极实体类

This commit is contained in:
17146 2024-05-20 14:40:25 +08:00
parent c08b3a170b
commit 1333c877bc
3 changed files with 36 additions and 96 deletions

View file

@ -2,105 +2,51 @@ package net.mcreator.target.entity;
import net.mcreator.target.headshot.BoundingBoxManager; import net.mcreator.target.headshot.BoundingBoxManager;
import net.mcreator.target.headshot.IHeadshotBox; import net.mcreator.target.headshot.IHeadshotBox;
import net.mcreator.target.init.TargetModEntities; import net.mcreator.target.init.*;
import net.mcreator.target.init.TargetModMobEffects;
import net.mcreator.target.init.TargetModSounds;
import net.mcreator.target.network.TargetModVariables; import net.mcreator.target.network.TargetModVariables;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.sounds.SoundSource; import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow; import net.minecraft.world.entity.projectile.ThrowableItemProjectile;
import net.minecraft.world.entity.projectile.ItemSupplier; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.EntityHitResult; import net.minecraft.world.phys.EntityHitResult;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.network.NetworkHooks; import net.minecraftforge.network.NetworkHooks;
import net.minecraftforge.network.PlayMessages;
import java.util.Optional; import java.util.Optional;
// TODO 父类改为Projectile public class TaserBulletProjectileEntity extends ThrowableItemProjectile {
@OnlyIn(value = Dist.CLIENT, _interface = ItemSupplier.class) private float damage = 5f;
public class TaserBulletProjectileEntity extends AbstractArrow implements ItemSupplier {
public static final ItemStack PROJECTILE_ITEM = new ItemStack(Blocks.AIR);
public TaserBulletProjectileEntity(PlayMessages.SpawnEntity packet, Level world) {
super(TargetModEntities.TASER_BULLET_PROJECTILE.get(), world);
}
public TaserBulletProjectileEntity(EntityType<? extends TaserBulletProjectileEntity> type, Level world) { public TaserBulletProjectileEntity(EntityType<? extends TaserBulletProjectileEntity> type, Level world) {
super(type, world); super(type, world);
} }
public TaserBulletProjectileEntity(EntityType<? extends TaserBulletProjectileEntity> type, double x, double y, double z, Level world) {
super(type, x, y, z, world);
}
public TaserBulletProjectileEntity(EntityType<? extends TaserBulletProjectileEntity> type, LivingEntity entity, Level world) { public TaserBulletProjectileEntity(EntityType<? extends TaserBulletProjectileEntity> type, LivingEntity entity, Level world) {
super(type, entity, world); super(type, entity, world);
} }
public TaserBulletProjectileEntity(LivingEntity entity, Level level, float damage) {
super(TargetModEntities.TASER_BULLET_PROJECTILE.get(), entity, level);
this.damage = damage;
}
@Override @Override
public Packet<ClientGamePacketListener> getAddEntityPacket() { public Packet<ClientGamePacketListener> getAddEntityPacket() {
return NetworkHooks.getEntitySpawningPacket(this); return NetworkHooks.getEntitySpawningPacket(this);
} }
@Override @Override
@OnlyIn(Dist.CLIENT) protected Item getDefaultItem() {
public ItemStack getItem() { return TargetModItems.TASER_ELECTRODE.get();
return PROJECTILE_ITEM;
}
@Override
protected ItemStack getPickupItem() {
return PROJECTILE_ITEM;
}
@Override
protected void doPostHurtEffects(LivingEntity entity) {
super.doPostHurtEffects(entity);
entity.setArrowCount(entity.getArrowCount() - 1);
}
public static TaserBulletProjectileEntity shoot(Level world, LivingEntity entity, RandomSource random, float power, double damage, int knockback) {
TaserBulletProjectileEntity taserBullet = new TaserBulletProjectileEntity(TargetModEntities.TASER_BULLET_PROJECTILE.get(), entity, world);
taserBullet.shoot(entity.getViewVector(1).x, entity.getViewVector(1).y, entity.getViewVector(1).z, power * 2, 0);
taserBullet.setSilent(true);
taserBullet.setCritArrow(false);
taserBullet.setBaseDamage(damage);
taserBullet.setKnockback(knockback);
world.addFreshEntity(taserBullet);
return taserBullet;
}
public static TaserBulletProjectileEntity shoot(LivingEntity entity, LivingEntity target) {
TaserBulletProjectileEntity taserBullet = new TaserBulletProjectileEntity(TargetModEntities.TASER_BULLET_PROJECTILE.get(), entity, entity.level());
double dx = target.getX() - entity.getX();
double dy = target.getY() + target.getEyeHeight() - 1.1;
double dz = target.getZ() - entity.getZ();
taserBullet.shoot(dx, dy - taserBullet.getY() + Math.hypot(dx, dz) * 0.2F, dz, 1f * 2, 12.0F);
taserBullet.setSilent(true);
taserBullet.setBaseDamage(5);
taserBullet.setKnockback(5);
taserBullet.setCritArrow(false);
entity.level().addFreshEntity(taserBullet);
return taserBullet;
}
public static TaserBulletProjectileEntity shoot(Level world, LivingEntity entity, RandomSource source) {
return shoot(world, entity, source, 1f, 5, 5);
} }
@Override @Override
@ -140,7 +86,6 @@ public class TaserBulletProjectileEntity extends AbstractArrow implements ItemSu
headshot = true; headshot = true;
} }
if (headshot && this.getOwner() instanceof LivingEntity living) { if (headshot && this.getOwner() instanceof LivingEntity living) {
setBaseDamage(getBaseDamage() * 1.5f);
living.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { living.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.headIndicator = 25; capability.headIndicator = 25;
capability.syncPlayerVariables(living); capability.syncPlayerVariables(living);
@ -152,7 +97,6 @@ public class TaserBulletProjectileEntity extends AbstractArrow implements ItemSu
} }
} }
} }
super.onHitEntity(result);
if (this.getOwner() instanceof LivingEntity source) { if (this.getOwner() instanceof LivingEntity source) {
CompoundTag tag = source.getMainHandItem().getOrCreateTag(); CompoundTag tag = source.getMainHandItem().getOrCreateTag();
@ -160,8 +104,15 @@ public class TaserBulletProjectileEntity extends AbstractArrow implements ItemSu
} }
if (entity instanceof Player player && !player.isCreative()) { if (entity instanceof Player player && !player.isCreative()) {
if (!player.level().isClientSide()) if (!player.level().isClientSide()) {
player.addEffect(new MobEffectInstance(TargetModMobEffects.SHOCK.get(), 100, 0)); player.addEffect(new MobEffectInstance(TargetModMobEffects.SHOCK.get(), 100, 0));
}
}
if (headshot) {
entity.hurt(TargetModDamageTypes.causeGunFireHeadshotDamage(this.level().registryAccess(), this.getOwner()), this.damage * 1.5f);
} else {
entity.hurt(TargetModDamageTypes.causeGunFireDamage(this.level().registryAccess(), this.getOwner()), this.damage);
} }
this.discard(); this.discard();
@ -171,8 +122,7 @@ public class TaserBulletProjectileEntity extends AbstractArrow implements ItemSu
public void tick() { public void tick() {
super.tick(); super.tick();
this.getPersistentData().putInt("live", this.getPersistentData().getInt("live") + 1); if (this.tickCount == 5) {
if (this.getPersistentData().getInt("live") == 5) {
this.setDeltaMovement(new Vec3(0, 0, 0)); this.setDeltaMovement(new Vec3(0, 0, 0));
} }

View file

@ -27,7 +27,7 @@ public class TargetModEntities {
public static final RegistryObject<EntityType<ClaymoreEntity>> CLAYMORE = register("claymore", public static final RegistryObject<EntityType<ClaymoreEntity>> CLAYMORE = register("claymore",
EntityType.Builder.<ClaymoreEntity>of(ClaymoreEntity::new, MobCategory.CREATURE).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(3).setCustomClientFactory(ClaymoreEntity::new).fireImmune().sized(0.5f, 0.5f)); EntityType.Builder.<ClaymoreEntity>of(ClaymoreEntity::new, MobCategory.CREATURE).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(3).setCustomClientFactory(ClaymoreEntity::new).fireImmune().sized(0.5f, 0.5f));
public static final RegistryObject<EntityType<TaserBulletProjectileEntity>> TASER_BULLET_PROJECTILE = register("projectile_taser_bullet_projectile", public static final RegistryObject<EntityType<TaserBulletProjectileEntity>> TASER_BULLET_PROJECTILE = register("projectile_taser_bullet_projectile",
EntityType.Builder.<TaserBulletProjectileEntity>of(TaserBulletProjectileEntity::new, MobCategory.MISC).setCustomClientFactory(TaserBulletProjectileEntity::new).setShouldReceiveVelocityUpdates(true).setTrackingRange(64) EntityType.Builder.<TaserBulletProjectileEntity>of(TaserBulletProjectileEntity::new, MobCategory.MISC).setShouldReceiveVelocityUpdates(true).setTrackingRange(64)
.setUpdateInterval(1).sized(0.5f, 0.5f)); .setUpdateInterval(1).sized(0.5f, 0.5f));
public static final RegistryObject<EntityType<GunGrenadeEntity>> GUN_GRENADE = register("projectile_gun_grenade", public static final RegistryObject<EntityType<GunGrenadeEntity>> GUN_GRENADE = register("projectile_gun_grenade",
EntityType.Builder.<GunGrenadeEntity>of(GunGrenadeEntity::new, MobCategory.MISC).setCustomClientFactory(GunGrenadeEntity::new).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f)); EntityType.Builder.<GunGrenadeEntity>of(GunGrenadeEntity::new, MobCategory.MISC).setCustomClientFactory(GunGrenadeEntity::new).setShouldReceiveVelocityUpdates(true).setTrackingRange(64).setUpdateInterval(1).sized(0.5f, 0.5f));

View file

@ -2,7 +2,6 @@ package net.mcreator.target.procedures;
import net.mcreator.target.entity.TaserBulletProjectileEntity; import net.mcreator.target.entity.TaserBulletProjectileEntity;
import net.mcreator.target.init.TargetModAttributes; import net.mcreator.target.init.TargetModAttributes;
import net.mcreator.target.init.TargetModEntities;
import net.mcreator.target.init.TargetModItems; import net.mcreator.target.init.TargetModItems;
import net.mcreator.target.init.TargetModSounds; import net.mcreator.target.init.TargetModSounds;
import net.mcreator.target.network.TargetModVariables; import net.mcreator.target.network.TargetModVariables;
@ -12,19 +11,18 @@ import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.projectile.AbstractArrow;
import net.minecraft.world.entity.projectile.Projectile;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
// TODO 内联这个类
public class TaserfireProcedure { public class TaserfireProcedure {
public static void execute(Entity entity) { public static void execute(Entity entity) {
if (entity == null) return; if (entity == null) return;
if (entity instanceof Player player && !player.isSpectator()) { if (entity instanceof Player player && !player.isSpectator()) {
ItemStack usehand = player.getMainHandItem(); ItemStack stack = player.getMainHandItem();
if (usehand.getItem() == TargetModItems.TASER.get() && !usehand.getOrCreateTag().getBoolean("reloading")) { if (stack.getItem() == TargetModItems.TASER.get() && !stack.getOrCreateTag().getBoolean("reloading")) {
Player _plrCldCheck4 = (Player) entity; Player _plrCldCheck4 = (Player) entity;
if (!_plrCldCheck4.getCooldowns().isOnCooldown(usehand.getItem()) && usehand.getOrCreateTag().getInt("ammo") > 0) { if (!_plrCldCheck4.getCooldowns().isOnCooldown(stack.getItem()) && stack.getOrCreateTag().getInt("ammo") > 0) {
entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> { entity.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1; capability.recoilHorizon = Math.random() < 0.5 ? -1 : 1;
@ -32,32 +30,24 @@ public class TaserfireProcedure {
capability.firing = 1; capability.firing = 1;
capability.syncPlayerVariables(entity); capability.syncPlayerVariables(entity);
}); });
player.getCooldowns().addCooldown(usehand.getItem(), 5); player.getCooldowns().addCooldown(stack.getItem(), 5);
if (entity instanceof ServerPlayer serverPlayer) { if (entity instanceof ServerPlayer serverPlayer) {
SoundTool.playLocalSound(serverPlayer, TargetModSounds.TASER_FIRE_1P.get(), 1, 1); SoundTool.playLocalSound(serverPlayer, TargetModSounds.TASER_FIRE_1P.get(), 1, 1);
serverPlayer.level().playSound(null, serverPlayer.getOnPos(), TargetModSounds.TASER_FIRE_3P.get(), SoundSource.PLAYERS, 1, 1); serverPlayer.level().playSound(null, serverPlayer.getOnPos(), TargetModSounds.TASER_FIRE_3P.get(), SoundSource.PLAYERS, 1, 1);
} }
Level projectileLevel = entity.level(); Level level = entity.level();
if (!projectileLevel.isClientSide()) { if (!level.isClientSide()) {
Projectile _entityToSpawn = new Object() { TaserBulletProjectileEntity taserBulletProjectile = new TaserBulletProjectileEntity(player, level, (float) stack.getOrCreateTag().getDouble("damage"));
public Projectile getArrow(Level level, Entity shooter, float damage, int knockback) {
AbstractArrow entityToSpawn = new TaserBulletProjectileEntity(TargetModEntities.TASER_BULLET_PROJECTILE.get(), level); taserBulletProjectile.setPos(entity.getX(), entity.getEyeY() - 0.1, entity.getZ());
entityToSpawn.setOwner(shooter); taserBulletProjectile.shoot(entity.getLookAngle().x, entity.getLookAngle().y, entity.getLookAngle().z, (float) stack.getOrCreateTag().getDouble("velocity"),
entityToSpawn.setBaseDamage(damage);
entityToSpawn.setKnockback(knockback);
entityToSpawn.setSilent(true);
return entityToSpawn;
}
}.getArrow(projectileLevel, entity, (float) (usehand.getOrCreateTag().getDouble("damage") / usehand.getOrCreateTag().getDouble("velocity")), 0);
_entityToSpawn.setPos(entity.getX(), entity.getEyeY() - 0.1, entity.getZ());
_entityToSpawn.shoot(entity.getLookAngle().x, entity.getLookAngle().y, entity.getLookAngle().z, (float) usehand.getOrCreateTag().getDouble("velocity"),
(float) ((LivingEntity) entity).getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue()); (float) ((LivingEntity) entity).getAttribute(TargetModAttributes.SPREAD.get()).getBaseValue());
projectileLevel.addFreshEntity(_entityToSpawn); level.addFreshEntity(taserBulletProjectile);
} }
usehand.getOrCreateTag().putInt("fire_animation", 4); stack.getOrCreateTag().putInt("fire_animation", 4);
usehand.getOrCreateTag().putInt("ammo", (usehand.getOrCreateTag().getInt("ammo") - 1)); stack.getOrCreateTag().putInt("ammo", (stack.getOrCreateTag().getInt("ammo") - 1));
} }
} }
} }