| 1 | package net.minecraft.world.entity.npc.wanderingtrader;
|
| 2 |
|
| 3 | import java.util.EnumSet;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.sounds.SoundEvent;
|
| 7 | import net.minecraft.sounds.SoundEvents;
|
| 8 | import net.minecraft.stats.Stats;
|
| 9 | import net.minecraft.world.InteractionHand;
|
| 10 | import net.minecraft.world.InteractionResult;
|
| 11 | import net.minecraft.world.damagesource.DamageSource;
|
| 12 | import net.minecraft.world.entity.AgeableMob;
|
| 13 | import net.minecraft.world.entity.EntityType;
|
| 14 | import net.minecraft.world.entity.ExperienceOrb;
|
| 15 | import net.minecraft.world.entity.Mob;
|
| 16 | import net.minecraft.world.entity.ai.goal.AvoidEntityGoal;
|
| 17 | import net.minecraft.world.entity.ai.goal.FloatGoal;
|
| 18 | import net.minecraft.world.entity.ai.goal.Goal;
|
| 19 | import net.minecraft.world.entity.ai.goal.InteractGoal;
|
| 20 | import net.minecraft.world.entity.ai.goal.LookAtPlayerGoal;
|
| 21 | import net.minecraft.world.entity.ai.goal.LookAtTradingPlayerGoal;
|
| 22 | import net.minecraft.world.entity.ai.goal.MoveTowardsRestrictionGoal;
|
| 23 | import net.minecraft.world.entity.ai.goal.PanicGoal;
|
| 24 | import net.minecraft.world.entity.ai.goal.TradeWithPlayerGoal;
|
| 25 | import net.minecraft.world.entity.ai.goal.UseItemGoal;
|
| 26 | import net.minecraft.world.entity.ai.goal.WaterAvoidingRandomStrollGoal;
|
| 27 | import net.minecraft.world.entity.monster.Vex;
|
| 28 | import net.minecraft.world.entity.monster.Zoglin;
|
| 29 | import net.minecraft.world.entity.monster.illager.Evoker;
|
| 30 | import net.minecraft.world.entity.monster.illager.Illusioner;
|
| 31 | import net.minecraft.world.entity.monster.illager.Pillager;
|
| 32 | import net.minecraft.world.entity.monster.illager.Vindicator;
|
| 33 | import net.minecraft.world.entity.monster.zombie.Zombie;
|
| 34 | import net.minecraft.world.entity.npc.villager.AbstractVillager;
|
| 35 | import net.minecraft.world.entity.player.Player;
|
| 36 | import net.minecraft.world.item.ItemStack;
|
| 37 | import net.minecraft.world.item.Items;
|
| 38 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 39 | import net.minecraft.world.item.alchemy.Potions;
|
| 40 | import net.minecraft.world.item.component.Consumable;
|
| 41 | import net.minecraft.world.item.trading.MerchantOffer;
|
| 42 | import net.minecraft.world.item.trading.MerchantOffers;
|
| 43 | import net.minecraft.world.item.trading.TradeSets;
|
| 44 | import net.minecraft.world.level.Level;
|
| 45 | import net.minecraft.world.level.storage.ValueInput;
|
| 46 | import net.minecraft.world.level.storage.ValueOutput;
|
| 47 | import net.minecraft.world.phys.Vec3;
|
| 48 | import org.jspecify.annotations.Nullable;
|
| 49 |
|
| 50 | public class WanderingTrader extends AbstractVillager implements Consumable.OverrideConsumeSound {
|
| 51 | private static final int DEFAULT_DESPAWN_DELAY = 0;
|
| 52 | private @Nullable BlockPos wanderTarget;
|
| 53 | private int despawnDelay = 0;
|
| 54 |
|
| 55 | public WanderingTrader(final EntityType<? extends WanderingTrader> type, final Level level) {
|
| 56 | super(type, level);
|
| 57 | }
|
| 58 |
|
| 59 | @Override
|
| 60 | protected void registerGoals() {
|
| 61 | this.goalSelector.addGoal(0, new FloatGoal(this));
|
| 62 | this.goalSelector
|
| 63 | .addGoal(
|
| 64 | 0,
|
| 65 | new UseItemGoal<>(
|
| 66 | this,
|
| 67 | PotionContents.createItemStack(Items.POTION, Potions.INVISIBILITY),
|
| 68 | SoundEvents.WANDERING_TRADER_DISAPPEARED,
|
| 69 | e -> this.level().isDarkOutside() && !e.isInvisible()
|
| 70 | )
|
| 71 | );
|
| 72 | this.goalSelector
|
| 73 | .addGoal(
|
| 74 | 0,
|
| 75 | new UseItemGoal<>(this, new ItemStack(Items.MILK_BUCKET), SoundEvents.WANDERING_TRADER_REAPPEARED, e -> this.level().isBrightOutside() && e.isInvisible())
|
| 76 | );
|
| 77 | this.goalSelector.addGoal(1, new TradeWithPlayerGoal(this));
|
| 78 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Zombie.class, 8.0F, 0.5, 0.5));
|
| 79 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Evoker.class, 12.0F, 0.5, 0.5));
|
| 80 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Vindicator.class, 8.0F, 0.5, 0.5));
|
| 81 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Vex.class, 8.0F, 0.5, 0.5));
|
| 82 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Pillager.class, 15.0F, 0.5, 0.5));
|
| 83 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Illusioner.class, 12.0F, 0.5, 0.5));
|
| 84 | this.goalSelector.addGoal(1, new AvoidEntityGoal<>(this, Zoglin.class, 10.0F, 0.5, 0.5));
|
| 85 | this.goalSelector.addGoal(1, new PanicGoal(this, 0.5));
|
| 86 | this.goalSelector.addGoal(1, new LookAtTradingPlayerGoal(this));
|
| 87 | this.goalSelector.addGoal(2, new WanderingTrader.WanderToPositionGoal(this, 2.0, 0.35));
|
| 88 | this.goalSelector.addGoal(4, new MoveTowardsRestrictionGoal(this, 0.35));
|
| 89 | this.goalSelector.addGoal(8, new WaterAvoidingRandomStrollGoal(this, 0.35));
|
| 90 | this.goalSelector.addGoal(9, new InteractGoal(this, Player.class, 3.0F, 1.0F));
|
| 91 | this.goalSelector.addGoal(10, new LookAtPlayerGoal(this, Mob.class, 8.0F));
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public @Nullable AgeableMob getBreedOffspring(final ServerLevel level, final AgeableMob partner) {
|
| 96 | return null;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public boolean showProgressBar() {
|
| 101 | return false;
|
| 102 | }
|
| 103 |
|
| 104 | @Override
|
| 105 | public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
|
| 106 | ItemStack itemStack = player.getItemInHand(hand);
|
| 107 | if (!itemStack.is(Items.VILLAGER_SPAWN_EGG) && this.isAlive() && !this.isTrading() && !this.isBaby()) {
|
| 108 | if (hand == InteractionHand.MAIN_HAND) {
|
| 109 | player.awardStat(Stats.TALKED_TO_VILLAGER);
|
| 110 | }
|
| 111 |
|
| 112 | if (!this.level().isClientSide()) {
|
| 113 | if (this.getOffers().isEmpty()) {
|
| 114 | return InteractionResult.CONSUME;
|
| 115 | }
|
| 116 |
|
| 117 | this.setTradingPlayer(player);
|
| 118 | this.openTradingScreen(player, this.getDisplayName(), 1);
|
| 119 | }
|
| 120 |
|
| 121 | return InteractionResult.SUCCESS_SERVER;
|
| 122 | } else {
|
| 123 | return super.mobInteract(player, hand);
|
| 124 | }
|
| 125 | }
|
| 126 |
|
| 127 | @Override
|
| 128 | protected void updateTrades(final ServerLevel level) {
|
| 129 | MerchantOffers offers = this.getOffers();
|
| 130 | this.addOffersFromTradeSet(level, offers, TradeSets.WANDERING_TRADER_BUYING);
|
| 131 | this.addOffersFromTradeSet(level, offers, TradeSets.WANDERING_TRADER_UNCOMMON);
|
| 132 | this.addOffersFromTradeSet(level, offers, TradeSets.WANDERING_TRADER_COMMON);
|
| 133 | }
|
| 134 |
|
| 135 | @Override
|
| 136 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 137 | super.addAdditionalSaveData(output);
|
| 138 | output.putInt("DespawnDelay", this.despawnDelay);
|
| 139 | output.storeNullable("wander_target", BlockPos.CODEC, this.wanderTarget);
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 144 | super.readAdditionalSaveData(input);
|
| 145 | this.despawnDelay = input.getIntOr("DespawnDelay", 0);
|
| 146 | this.wanderTarget = input.read("wander_target", BlockPos.CODEC).orElse(null);
|
| 147 | this.setAge(Math.max(0, this.getAge()));
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public boolean removeWhenFarAway(final double distSqr) {
|
| 152 | return false;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | protected void rewardTradeXp(final MerchantOffer offer) {
|
| 157 | if (offer.shouldRewardExp()) {
|
| 158 | int popXp = 3 + this.random.nextInt(4);
|
| 159 | this.level().addFreshEntity(new ExperienceOrb(this.level(), this.getX(), this.getY() + 0.5, this.getZ(), popXp));
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | protected SoundEvent getAmbientSound() {
|
| 165 | return this.isTrading() ? SoundEvents.WANDERING_TRADER_TRADE : SoundEvents.WANDERING_TRADER_AMBIENT;
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | protected SoundEvent getHurtSound(final DamageSource source) {
|
| 170 | return SoundEvents.WANDERING_TRADER_HURT;
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | protected SoundEvent getDeathSound() {
|
| 175 | return SoundEvents.WANDERING_TRADER_DEATH;
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public SoundEvent getConsumeSound(final ItemStack itemStack) {
|
| 180 | return itemStack.is(Items.MILK_BUCKET) ? SoundEvents.WANDERING_TRADER_DRINK_MILK : SoundEvents.WANDERING_TRADER_DRINK_POTION;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | protected SoundEvent getTradeUpdatedSound(final boolean validTrade) {
|
| 185 | return validTrade ? SoundEvents.WANDERING_TRADER_YES : SoundEvents.WANDERING_TRADER_NO;
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public SoundEvent getNotifyTradeSound() {
|
| 190 | return SoundEvents.WANDERING_TRADER_YES;
|
| 191 | }
|
| 192 |
|
| 193 | public void setDespawnDelay(final int despawnDelay) {
|
| 194 | this.despawnDelay = despawnDelay;
|
| 195 | }
|
| 196 |
|
| 197 | public int getDespawnDelay() {
|
| 198 | return this.despawnDelay;
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public void aiStep() {
|
| 203 | super.aiStep();
|
| 204 | if (!this.level().isClientSide()) {
|
| 205 | this.maybeDespawn();
|
| 206 | }
|
| 207 | }
|
| 208 |
|
| 209 | private void maybeDespawn() {
|
| 210 | if (this.despawnDelay > 0 && !this.isTrading() && --this.despawnDelay == 0) {
|
| 211 | this.discard();
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | public void setWanderTarget(final @Nullable BlockPos pos) {
|
| 216 | this.wanderTarget = pos;
|
| 217 | }
|
| 218 |
|
| 219 | private @Nullable BlockPos getWanderTarget() {
|
| 220 | return this.wanderTarget;
|
| 221 | }
|
| 222 |
|
| 223 | private class WanderToPositionGoal extends Goal {
|
| 224 | private final WanderingTrader trader;
|
| 225 | private final double stopDistance;
|
| 226 | private final double speedModifier;
|
| 227 |
|
| 228 | public WanderToPositionGoal(final WanderingTrader trader, final double stopDistance, final double speedModifier) {
|
| 229 | this.trader = trader;
|
| 230 | this.stopDistance = stopDistance;
|
| 231 | this.speedModifier = speedModifier;
|
| 232 | this.setFlags(EnumSet.of(Goal.Flag.MOVE));
|
| 233 | }
|
| 234 |
|
| 235 | @Override
|
| 236 | public void stop() {
|
| 237 | this.trader.setWanderTarget(null);
|
| 238 | WanderingTrader.this.navigation.stop();
|
| 239 | }
|
| 240 |
|
| 241 | @Override
|
| 242 | public boolean canUse() {
|
| 243 | BlockPos wanderPosition = this.trader.getWanderTarget();
|
| 244 | return wanderPosition != null && this.isTooFarAway(wanderPosition, this.stopDistance);
|
| 245 | }
|
| 246 |
|
| 247 | @Override
|
| 248 | public void tick() {
|
| 249 | BlockPos wanderPosition = this.trader.getWanderTarget();
|
| 250 | if (wanderPosition != null && WanderingTrader.this.navigation.isDone()) {
|
| 251 | if (this.isTooFarAway(wanderPosition, 10.0)) {
|
| 252 | Vec3 dir = new Vec3(wanderPosition.getX() - this.trader.getX(), wanderPosition.getY() - this.trader.getY(), wanderPosition.getZ() - this.trader.getZ())
|
| 253 | .normalize();
|
| 254 | Vec3 targetPos = dir.scale(10.0).add(this.trader.getX(), this.trader.getY(), this.trader.getZ());
|
| 255 | WanderingTrader.this.navigation.moveTo(targetPos.x, targetPos.y, targetPos.z, this.speedModifier);
|
| 256 | } else {
|
| 257 | WanderingTrader.this.navigation.moveTo(wanderPosition.getX(), wanderPosition.getY(), wanderPosition.getZ(), this.speedModifier);
|
| 258 | }
|
| 259 | }
|
| 260 | }
|
| 261 |
|
| 262 | private boolean isTooFarAway(final BlockPos pos, final double distance) {
|
| 263 | return !pos.closerToCenterThan(this.trader.position(), distance);
|
| 264 | }
|
| 265 | }
|
| 266 | }
|
| 267 |
|