345 lines
13 KiB
Java
345 lines
13 KiB
Java
package net.mcreator.target.entity;
|
|
|
|
import net.mcreator.target.init.TargetModEntities;
|
|
import net.mcreator.target.init.TargetModItems;
|
|
import net.mcreator.target.init.TargetModSounds;
|
|
import net.mcreator.target.network.TargetModVariables;
|
|
import net.mcreator.target.procedures.Target1DangShiTiGengXinKeShiProcedure;
|
|
import net.minecraft.commands.CommandSource;
|
|
import net.minecraft.commands.CommandSourceStack;
|
|
import net.minecraft.commands.arguments.EntityAnchorArgument;
|
|
import net.minecraft.core.BlockPos;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.network.chat.Component;
|
|
import net.minecraft.network.protocol.Packet;
|
|
import net.minecraft.network.protocol.game.ClientGamePacketListener;
|
|
import net.minecraft.network.syncher.EntityDataAccessor;
|
|
import net.minecraft.network.syncher.EntityDataSerializers;
|
|
import net.minecraft.network.syncher.SynchedEntityData;
|
|
import net.minecraft.server.level.ServerLevel;
|
|
import net.minecraft.sounds.SoundSource;
|
|
import net.minecraft.world.DifficultyInstance;
|
|
import net.minecraft.world.InteractionHand;
|
|
import net.minecraft.world.InteractionResult;
|
|
import net.minecraft.world.damagesource.DamageSource;
|
|
import net.minecraft.world.damagesource.DamageTypes;
|
|
import net.minecraft.world.entity.*;
|
|
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
|
import net.minecraft.world.entity.ai.attributes.Attributes;
|
|
import net.minecraft.world.entity.ai.control.FlyingMoveControl;
|
|
import net.minecraft.world.entity.ai.navigation.FlyingPathNavigation;
|
|
import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
|
import net.minecraft.world.entity.player.Player;
|
|
import net.minecraft.world.entity.projectile.ThrownPotion;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.Level;
|
|
import net.minecraft.world.level.ServerLevelAccessor;
|
|
import net.minecraft.world.level.block.state.BlockState;
|
|
import net.minecraft.world.phys.Vec3;
|
|
import net.minecraftforge.event.entity.living.LivingDeathEvent;
|
|
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
|
import net.minecraftforge.fml.common.Mod;
|
|
import net.minecraftforge.network.NetworkHooks;
|
|
import net.minecraftforge.network.PlayMessages;
|
|
import software.bernie.geckolib.animatable.GeoEntity;
|
|
import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache;
|
|
import software.bernie.geckolib.core.animation.AnimatableManager;
|
|
import software.bernie.geckolib.core.animation.AnimationController;
|
|
import software.bernie.geckolib.core.animation.AnimationState;
|
|
import software.bernie.geckolib.core.animation.RawAnimation;
|
|
import software.bernie.geckolib.core.object.PlayState;
|
|
import software.bernie.geckolib.util.GeckoLibUtil;
|
|
|
|
import javax.annotation.Nullable;
|
|
|
|
@Mod.EventBusSubscriber
|
|
public class Target1Entity extends PathfinderMob implements GeoEntity, AnimatedEntity {
|
|
public static final EntityDataAccessor<Boolean> SHOOT = SynchedEntityData.defineId(Target1Entity.class, EntityDataSerializers.BOOLEAN);
|
|
public static final EntityDataAccessor<String> ANIMATION = SynchedEntityData.defineId(Target1Entity.class, EntityDataSerializers.STRING);
|
|
public static final EntityDataAccessor<String> TEXTURE = SynchedEntityData.defineId(Target1Entity.class, EntityDataSerializers.STRING);
|
|
private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this);
|
|
|
|
public String animationProcedure = "empty";
|
|
|
|
public Target1Entity(PlayMessages.SpawnEntity packet, Level world) {
|
|
this(TargetModEntities.TARGET_1.get(), world);
|
|
}
|
|
|
|
public Target1Entity(EntityType<Target1Entity> type, Level world) {
|
|
super(type, world);
|
|
xpReward = 0;
|
|
setNoAi(true);
|
|
setPersistenceRequired();
|
|
this.moveControl = new FlyingMoveControl(this, 10, true);
|
|
}
|
|
|
|
@Override
|
|
protected void defineSynchedData() {
|
|
super.defineSynchedData();
|
|
this.entityData.define(SHOOT, false);
|
|
this.entityData.define(ANIMATION, "undefined");
|
|
this.entityData.define(TEXTURE, "target");
|
|
}
|
|
|
|
public void setTexture(String texture) {
|
|
this.entityData.set(TEXTURE, texture);
|
|
}
|
|
|
|
public String getTexture() {
|
|
return this.entityData.get(TEXTURE);
|
|
}
|
|
|
|
@Override
|
|
public Packet<ClientGamePacketListener> getAddEntityPacket() {
|
|
return NetworkHooks.getEntitySpawningPacket(this);
|
|
}
|
|
|
|
@Override
|
|
protected PathNavigation createNavigation(Level world) {
|
|
return new FlyingPathNavigation(this, world);
|
|
}
|
|
|
|
@Override
|
|
public MobType getMobType() {
|
|
return super.getMobType();
|
|
}
|
|
|
|
@Override
|
|
public boolean removeWhenFarAway(double distanceToClosestPlayer) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean causeFallDamage(float l, float d, DamageSource source) {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean hurt(DamageSource source, float amount) {
|
|
if (!this.level().isClientSide()) {
|
|
this.level().playSound(null, BlockPos.containing(this.getX(), this.getY(), this.getZ()), TargetModSounds.HIT.get(), SoundSource.BLOCKS, 2, 1);
|
|
} else {
|
|
this.level().playLocalSound(this.getX(), this.getY(), this.getZ(), TargetModSounds.HIT.get(), SoundSource.BLOCKS, 2, 1, false);
|
|
}
|
|
|
|
if (source.is(DamageTypes.IN_FIRE))
|
|
return false;
|
|
if (source.getDirectEntity() instanceof ThrownPotion || source.getDirectEntity() instanceof AreaEffectCloud)
|
|
return false;
|
|
if (source.is(DamageTypes.FALL))
|
|
return false;
|
|
if (source.is(DamageTypes.CACTUS))
|
|
return false;
|
|
if (source.is(DamageTypes.DROWN))
|
|
return false;
|
|
if (source.is(DamageTypes.LIGHTNING_BOLT))
|
|
return false;
|
|
if (source.is(DamageTypes.FALLING_ANVIL))
|
|
return false;
|
|
if (source.is(DamageTypes.DRAGON_BREATH))
|
|
return false;
|
|
if (source.is(DamageTypes.WITHER))
|
|
return false;
|
|
if (source.is(DamageTypes.WITHER_SKULL))
|
|
return false;
|
|
return super.hurt(source, amount);
|
|
}
|
|
|
|
@Override
|
|
public SpawnGroupData finalizeSpawn(ServerLevelAccessor world, DifficultyInstance difficulty, MobSpawnType reason, @Nullable SpawnGroupData livingdata, @Nullable CompoundTag tag) {
|
|
SpawnGroupData data = super.finalizeSpawn(world, difficulty, reason, livingdata, tag);
|
|
|
|
this.setYRot(0);
|
|
this.setXRot(0);
|
|
this.setYBodyRot(this.getYRot());
|
|
this.setYHeadRot(this.getYRot());
|
|
this.yRotO = this.getYRot();
|
|
this.xRotO = this.getXRot();
|
|
|
|
return data;
|
|
}
|
|
|
|
@Override
|
|
public void addAdditionalSaveData(CompoundTag compound) {
|
|
super.addAdditionalSaveData(compound);
|
|
compound.putString("Texture", this.getTexture());
|
|
}
|
|
|
|
@Override
|
|
public void readAdditionalSaveData(CompoundTag compound) {
|
|
super.readAdditionalSaveData(compound);
|
|
if (compound.contains("Texture"))
|
|
this.setTexture(compound.getString("Texture"));
|
|
}
|
|
|
|
@Override
|
|
public InteractionResult mobInteract(Player player, InteractionHand hand) {
|
|
InteractionResult result = InteractionResult.sidedSuccess(this.level().isClientSide());
|
|
super.mobInteract(player, hand);
|
|
|
|
if (player.isShiftKeyDown()) {
|
|
if (!this.level().isClientSide()) {
|
|
this.discard();
|
|
}
|
|
|
|
player.addItem(new ItemStack(TargetModItems.TARGET_DEPLOYER.get()));
|
|
} else {
|
|
if (!(player.getCapability(TargetModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new TargetModVariables.PlayerVariables())).zooming) {
|
|
this.lookAt(EntityAnchorArgument.Anchor.EYES, new Vec3((player.getX()), this.getY(), (player.getZ())));
|
|
|
|
this.setYRot(this.getYRot());
|
|
this.setXRot(0);
|
|
this.setYBodyRot(this.getYRot());
|
|
this.setYHeadRot(this.getYRot());
|
|
this.yRotO = this.getYRot();
|
|
this.xRotO = this.getXRot();
|
|
|
|
this.getPersistentData().putDouble("targetdown", 0);
|
|
}
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
@Override
|
|
public void baseTick() {
|
|
super.baseTick();
|
|
Target1DangShiTiGengXinKeShiProcedure.execute(this);
|
|
this.refreshDimensions();
|
|
}
|
|
|
|
@Override
|
|
public EntityDimensions getDimensions(Pose p_33597_) {
|
|
float num;
|
|
if (this.getPersistentData().getDouble("targetdown") > 0) {
|
|
num = 0.1f;
|
|
} else {
|
|
num = 1f;
|
|
}
|
|
|
|
return super.getDimensions(p_33597_).scale(num);
|
|
}
|
|
|
|
|
|
@Override
|
|
public boolean isPushable() {
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
protected void doPush(Entity entityIn) {
|
|
}
|
|
|
|
@Override
|
|
protected void pushEntities() {
|
|
}
|
|
|
|
@Override
|
|
protected void checkFallDamage(double y, boolean onGroundIn, BlockState state, BlockPos pos) {
|
|
}
|
|
|
|
@Override
|
|
public void setNoGravity(boolean ignored) {
|
|
super.setNoGravity(true);
|
|
}
|
|
|
|
@Override
|
|
public void aiStep() {
|
|
super.aiStep();
|
|
this.updateSwingTime();
|
|
this.setNoGravity(true);
|
|
}
|
|
|
|
public static void init() {
|
|
}
|
|
|
|
public static AttributeSupplier.Builder createAttributes() {
|
|
return Mob.createMobAttributes()
|
|
.add(Attributes.MOVEMENT_SPEED, 0)
|
|
.add(Attributes.MAX_HEALTH, 40)
|
|
.add(Attributes.ARMOR, 0)
|
|
.add(Attributes.ATTACK_DAMAGE, 0)
|
|
.add(Attributes.FOLLOW_RANGE, 16)
|
|
.add(Attributes.KNOCKBACK_RESISTANCE, 10)
|
|
.add(Attributes.FLYING_SPEED, 0);
|
|
}
|
|
|
|
private PlayState movementPredicate(AnimationState<Target1Entity> event) {
|
|
if (this.animationProcedure.equals("empty")) {
|
|
return event.setAndContinue(RawAnimation.begin().thenLoop("animation.target.idle"));
|
|
}
|
|
return PlayState.STOP;
|
|
}
|
|
|
|
private PlayState procedurePredicate(AnimationState<Target1Entity> event) {
|
|
if (!animationProcedure.equals("empty") && event.getController().getAnimationState() == AnimationController.State.STOPPED) {
|
|
event.getController().setAnimation(RawAnimation.begin().thenPlay(this.animationProcedure));
|
|
if (event.getController().getAnimationState() == AnimationController.State.STOPPED) {
|
|
this.animationProcedure = "empty";
|
|
event.getController().forceAnimationReset();
|
|
}
|
|
} else if (animationProcedure.equals("empty")) {
|
|
return PlayState.STOP;
|
|
}
|
|
return PlayState.CONTINUE;
|
|
}
|
|
|
|
@Override
|
|
protected void tickDeath() {
|
|
++this.deathTime;
|
|
if (this.deathTime == 100) {
|
|
this.remove(Target1Entity.RemovalReason.KILLED);
|
|
this.dropExperience();
|
|
}
|
|
}
|
|
|
|
public String getSyncedAnimation() {
|
|
return this.entityData.get(ANIMATION);
|
|
}
|
|
|
|
public void setAnimation(String animation) {
|
|
this.entityData.set(ANIMATION, animation);
|
|
}
|
|
|
|
@Override
|
|
public void setAnimationProcedure(String procedure) {
|
|
this.animationProcedure = procedure;
|
|
}
|
|
|
|
@Override
|
|
public void registerControllers(AnimatableManager.ControllerRegistrar data) {
|
|
data.add(new AnimationController<>(this, "movement", 0, this::movementPredicate));
|
|
data.add(new AnimationController<>(this, "procedure", 0, this::procedurePredicate));
|
|
}
|
|
|
|
@Override
|
|
public AnimatableInstanceCache getAnimatableInstanceCache() {
|
|
return this.cache;
|
|
}
|
|
|
|
@SubscribeEvent
|
|
public static void onTarget1Down(LivingDeathEvent event) {
|
|
if (event.getEntity() == null) return;
|
|
|
|
var entity = event.getEntity();
|
|
var sourceEntity = event.getSource().getEntity();
|
|
|
|
if (entity == null || sourceEntity == null) return;
|
|
|
|
if (entity instanceof Target1Entity target1) {
|
|
target1.setHealth(target1.getMaxHealth());
|
|
|
|
// TODO 修改为正确的音效播放方法
|
|
// sourceEntity.level().playSound(null, sourceEntity.blockPosition(), TargetModSounds.TARGETDOWN.get(), SoundSource.PLAYERS, 100, 1);
|
|
if (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:targetdown player @s ~ ~ ~ 100 1");
|
|
}
|
|
|
|
if (sourceEntity instanceof Player player)
|
|
player.displayClientMessage(Component.literal(("Target Down " + new java.text.DecimalFormat("##.#").format((entity.position()).distanceTo((sourceEntity.position()))) + "M")), true);
|
|
entity.getPersistentData().putDouble("targetdown", 201);
|
|
event.setCanceled(true);
|
|
}
|
|
}
|
|
}
|