| 1 | package net.minecraft.world.entity.animal.golem;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 5 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 6 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.util.Mth;
|
| 12 | import net.minecraft.world.InteractionHand;
|
| 13 | import net.minecraft.world.InteractionResult;
|
| 14 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 15 | import net.minecraft.world.damagesource.DamageSource;
|
| 16 | import net.minecraft.world.entity.EntityType;
|
| 17 | import net.minecraft.world.entity.LivingEntity;
|
| 18 | import net.minecraft.world.entity.Mob;
|
| 19 | import net.minecraft.world.entity.Shearable;
|
| 20 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 21 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 22 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 23 | import net.minecraft.world.entity.ai.goal.RandomLookAroundGoal;
|
| 24 | import net.minecraft.world.entity.ai.goal.RangedAttackGoal;
|
| 25 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 26 | import net.minecraft.world.entity.ai.goal.target.NearestAttackableTargetGoal;
|
| 27 | import net.minecraft.world.entity.monster.Enemy;
|
| 28 | import net.minecraft.world.entity.monster.RangedAttackMob;
|
| 29 | import net.minecraft.world.entity.player.Player;
|
| 30 | import net.minecraft.world.entity.projectile.Projectile;
|
| 31 | import net.minecraft.world.entity.projectile.throwableitemprojectile.Snowball;
|
| 32 | import net.minecraft.world.item.ItemStack;
|
| 33 | import net.minecraft.world.item.Items;
|
| 34 | import net.minecraft.world.level.Level;
|
| 35 | import net.minecraft.world.level.block.Blocks;
|
| 36 | import net.minecraft.world.level.block.state.BlockState;
|
| 37 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 38 | import net.minecraft.world.level.gamerules.GameRules;
|
| 39 | import net.minecraft.world.level.storage.ValueInput;
|
| 40 | import net.minecraft.world.level.storage.ValueOutput;
|
| 41 | import net.minecraft.world.level.storage.loot.BuiltInLootTables;
|
| 42 | import net.minecraft.world.phys.Vec3;
|
| 43 | import org.jspecify.annotations.Nullable;
|
| 44 |
|
| 45 | public class SnowGolem extends AbstractGolem implements RangedAttackMob, Shearable {
|
| 46 | private static final EntityDataAccessor<Byte> DATA_PUMPKIN_ID = SynchedEntityData.defineId(SnowGolem.class, EntityDataSerializers.BYTE);
|
| 47 | private static final byte PUMPKIN_FLAG = 16;
|
| 48 | private static final boolean DEFAULT_PUMPKIN = true;
|
| 49 |
|
| 50 | public SnowGolem(final EntityType<? extends SnowGolem> type, final Level level) {
|
| 51 | super(type, level);
|
| 52 | }
|
| 53 |
|
| 54 | @Override
|
| 55 | protected void registerGoals() {
|
| 56 | this.goalSelector.addGoal(1, new RangedAttackGoal(this, 1.25, 20, 10.0F));
|
| 57 | this.goalSelector.addGoal(2, new WaterAvoidingRandomStrollGoal(this, 1.0, 1.0000001E-5F));
|
| 58 | this.goalSelector.addGoal(3, new LookAtPlayerGoal(this, Player.class, 6.0F));
|
| 59 | this.goalSelector.addGoal(4, new RandomLookAroundGoal(this));
|
| 60 | this.targetSelector.addGoal(1, new NearestAttackableTargetGoal<>(this, Mob.class, 10, true, false, (target, level) -> target instanceof Enemy));
|
| 61 | }
|
| 62 |
|
| 63 | public static AttributeSupplier.Builder createAttributes() {
|
| 64 | return Mob.createMobAttributes().add(Attributes.MAX_HEALTH, 4.0).add(Attributes.MOVEMENT_SPEED, 0.2F);
|
| 65 | }
|
| 66 |
|
| 67 | @Override
|
| 68 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 69 | super.defineSynchedData(entityData);
|
| 70 | entityData.define(DATA_PUMPKIN_ID, (byte)16);
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 75 | super.addAdditionalSaveData(output);
|
| 76 | output.putBoolean("Pumpkin", this.hasPumpkin());
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 81 | super.readAdditionalSaveData(input);
|
| 82 | this.setPumpkin(input.getBooleanOr("Pumpkin", true));
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public boolean isSensitiveToWater() {
|
| 87 | return true;
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public void aiStep() {
|
| 92 | super.aiStep();
|
| 93 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 94 | if (serverLevel.environmentAttributes().getValue(EnvironmentAttributes.SNOW_GOLEM_MELTS, this.position())) {
|
| 95 | this.hurtServer(serverLevel, this.damageSources().onFire(), 1.0F);
|
| 96 | }
|
| 97 |
|
| 98 | if (!serverLevel.getGameRules().get(GameRules.MOB_GRIEFING)) {
|
| 99 | return;
|
| 100 | }
|
| 101 |
|
| 102 | BlockState snow = Blocks.SNOW.defaultBlockState();
|
| 103 |
|
| 104 | for (int i = 0; i < 4; i++) {
|
| 105 | int xx = Mth.floor(this.getX() + (i % 2 * 2 - 1) * 0.25F);
|
| 106 | int yy = Mth.floor(this.getY());
|
| 107 | int zz = Mth.floor(this.getZ() + (i / 2 % 2 * 2 - 1) * 0.25F);
|
| 108 | BlockPos snowPos = new BlockPos(xx, yy, zz);
|
| 109 | if (this.level().getBlockState(snowPos).isAir() && snow.canSurvive(this.level(), snowPos)) {
|
| 110 | this.level().setBlockAndUpdate(snowPos, snow);
|
| 111 | this.level().gameEvent(GameEvent.BLOCK_PLACE, snowPos, GameEvent.Context.of(this, snow));
|
| 112 | }
|
| 113 | }
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | public void performRangedAttack(final LivingEntity target, final float power) {
|
| 119 | double xd = target.getX() - this.getX();
|
| 120 | double yd = target.getEyeY() - 1.1F;
|
| 121 | double zd = target.getZ() - this.getZ();
|
| 122 | double yo = Math.sqrt(xd * xd + zd * zd) * 0.2F;
|
| 123 | if (this.level() instanceof ServerLevel serverLevel) {
|
| 124 | ItemStack itemStack = new ItemStack(Items.SNOWBALL);
|
| 125 | Projectile.spawnProjectile(
|
| 126 | new Snowball(serverLevel, this, itemStack),
|
| 127 | serverLevel,
|
| 128 | itemStack,
|
| 129 | projectile -> projectile.shoot(xd, yd + yo - projectile.getY(), zd, 1.6F, this.rangedAttackUncertainty(serverLevel))
|
| 130 | );
|
| 131 | }
|
| 132 |
|
| 133 | this.playSound(SoundEvents.SNOW_GOLEM_SHOOT, 1.0F, 0.4F / (this.getRandom().nextFloat() * 0.4F + 0.8F));
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public float rangedAttackUncertainty(final Level level) {
|
| 138 | return 12.0F;
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | protected InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 143 | ItemStack itemStack = player.getItemInHand(hand);
|
| 144 | if (itemStack.is(Items.SHEARS) && this.readyForShearing()) {
|
| 145 | if (this.level() instanceof ServerLevel level) {
|
| 146 | this.shear(level, SoundSource.PLAYERS, itemStack);
|
| 147 | this.gameEvent(GameEvent.SHEAR, player);
|
| 148 | itemStack.hurtAndBreak(1, player, hand.asEquipmentSlot());
|
| 149 | }
|
| 150 |
|
| 151 | return InteractionResult.SUCCESS;
|
| 152 | } else {
|
| 153 | return InteractionResult.PASS;
|
| 154 | }
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public void shear(final ServerLevel level, final SoundSource soundSource, final ItemStack tool) {
|
| 159 | level.playSound(null, this, SoundEvents.SNOW_GOLEM_SHEAR, soundSource, 1.0F, 1.0F);
|
| 160 | this.setPumpkin(false);
|
| 161 | this.dropFromShearingLootTable(level, BuiltInLootTables.SHEAR_SNOW_GOLEM, tool, (l, drop) -> this.spawnAtLocation(l, drop, this.getEyeHeight()));
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public boolean readyForShearing() {
|
| 166 | return this.hasPumpkin();
|
| 167 | }
|
| 168 |
|
| 169 | public boolean hasPumpkin() {
|
| 170 | return (this.entityData.get(DATA_PUMPKIN_ID) & 16) != 0;
|
| 171 | }
|
| 172 |
|
| 173 | public void setPumpkin(final boolean pumpkin) {
|
| 174 | byte current = this.entityData.get(DATA_PUMPKIN_ID);
|
| 175 | if (pumpkin) {
|
| 176 | this.entityData.set(DATA_PUMPKIN_ID, (byte)(current | 16));
|
| 177 | } else {
|
| 178 | this.entityData.set(DATA_PUMPKIN_ID, (byte)(current & -17));
|
| 179 | }
|
| 180 | }
|
| 181 |
|
| 182 | @Override
|
| 183 | protected @Nullable SoundEvent getAmbientSound() {
|
| 184 | return SoundEvents.SNOW_GOLEM_AMBIENT;
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | protected @Nullable SoundEvent getHurtSound(final DamageSource source) {
|
| 189 | return SoundEvents.SNOW_GOLEM_HURT;
|
| 190 | }
|
| 191 |
|
| 192 | @Override
|
| 193 | protected @Nullable SoundEvent getDeathSound() {
|
| 194 | return SoundEvents.SNOW_GOLEM_DEATH;
|
| 195 | }
|
| 196 |
|
| 197 | @Override
|
| 198 | public Vec3 getLeashOffset() {
|
| 199 | return new Vec3(0.0, 0.75F * this.getEyeHeight(), this.getBbWidth() * 0.4F);
|
| 200 | }
|
| 201 | }
|
| 202 |
|