| 1 | package net.minecraft.world.entity.npc.wanderingtrader;
|
| 2 |
|
| 3 | import java.util.Optional;
|
| 4 | import net.minecraft.core.BlockPos;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.tags.BiomeTags;
|
| 7 | import net.minecraft.util.Continuation;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.util.RandomSource;
|
| 10 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 11 | import net.minecraft.world.entity.EntityTypes;
|
| 12 | import net.minecraft.world.entity.SpawnPlacementType;
|
| 13 | import net.minecraft.world.entity.SpawnPlacements;
|
| 14 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 15 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 16 | import net.minecraft.world.entity.animal.equine.TraderLlama;
|
| 17 | import net.minecraft.world.entity.player.Player;
|
| 18 | import net.minecraft.world.level.CustomSpawner;
|
| 19 | import net.minecraft.world.level.LevelReader;
|
| 20 | import net.minecraft.world.level.gamerules.GameRules;
|
| 21 | import net.minecraft.world.level.saveddata.WanderingTraderData;
|
| 22 | import net.minecraft.world.level.storage.SavedDataStorage;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public class WanderingTraderSpawner implements CustomSpawner {
|
| 26 | private static final int DEFAULT_TICK_DELAY = 1200;
|
| 27 | public static final int DEFAULT_SPAWN_DELAY = 24000;
|
| 28 | public static final int MIN_SPAWN_CHANCE = 25;
|
| 29 | private static final int MAX_SPAWN_CHANCE = 75;
|
| 30 | private static final int SPAWN_CHANCE_INCREASE = 25;
|
| 31 | private static final int SPAWN_ONE_IN_X_CHANCE = 10;
|
| 32 | private static final int NUMBER_OF_SPAWN_ATTEMPTS = 10;
|
| 33 | private final RandomSource random = RandomSource.create();
|
| 34 | private final SavedDataStorage savedDataStorage;
|
| 35 | private int tickDelay;
|
| 36 | private @Nullable WanderingTraderData traderData;
|
| 37 |
|
| 38 | public WanderingTraderSpawner(final SavedDataStorage savedDataStorage) {
|
| 39 | this.savedDataStorage = savedDataStorage;
|
| 40 | this.tickDelay = 1200;
|
| 41 | this.traderData = null;
|
| 42 | }
|
| 43 |
|
| 44 | @Override
|
| 45 | public void tick(final ServerLevel level, final boolean spawnEnemies) {
|
| 46 | if (level.getGameRules().get(GameRules.SPAWN_WANDERING_TRADERS)) {
|
| 47 | if (--this.tickDelay <= 0) {
|
| 48 | this.tickDelay = 1200;
|
| 49 | WanderingTraderData data = this.getTraderData();
|
| 50 | int spawnDelay = data.spawnDelay() - 1200;
|
| 51 | data.setSpawnDelay(spawnDelay);
|
| 52 | if (spawnDelay <= 0) {
|
| 53 | data.setSpawnDelay(24000);
|
| 54 | int chanceToSpawn = data.spawnChance();
|
| 55 | int newSpawnChance = Mth.clamp(chanceToSpawn + 25, 25, 75);
|
| 56 | data.setSpawnChance(newSpawnChance);
|
| 57 | if (this.random.nextInt(100) <= chanceToSpawn) {
|
| 58 | if (this.spawn(level)) {
|
| 59 | data.setSpawnChance(25);
|
| 60 | }
|
| 61 | }
|
| 62 | }
|
| 63 | }
|
| 64 | }
|
| 65 | }
|
| 66 |
|
| 67 | private WanderingTraderData getTraderData() {
|
| 68 | if (this.traderData == null) {
|
| 69 | this.traderData = this.savedDataStorage.computeIfAbsent(WanderingTraderData.TYPE);
|
| 70 | }
|
| 71 |
|
| 72 | return this.traderData;
|
| 73 | }
|
| 74 |
|
| 75 | private boolean spawn(final ServerLevel level) {
|
| 76 | Player player = level.getRandomPlayer();
|
| 77 | if (player == null) {
|
| 78 | return true;
|
| 79 | }
|
| 80 |
|
| 81 | if (this.random.nextInt(10) != 0) {
|
| 82 | return false;
|
| 83 | }
|
| 84 |
|
| 85 | BlockPos playerPos = player.blockPosition();
|
| 86 | int radius = 48;
|
| 87 | PoiManager poiManager = level.getPoiManager();
|
| 88 | Optional<BlockPos> poiPos = poiManager.find(p -> p.is(PoiTypes.MEETING), p -> true, playerPos, 48, PoiManager.Occupancy.ANY);
|
| 89 | BlockPos referencePos = poiPos.orElse(playerPos);
|
| 90 | BlockPos spawnPosition = this.findSpawnPositionNear(level, referencePos, 48);
|
| 91 | if (spawnPosition != null && this.hasEnoughSpace(level, spawnPosition)) {
|
| 92 | if (level.getBiome(spawnPosition).is(BiomeTags.WITHOUT_WANDERING_TRADER_SPAWNS)) {
|
| 93 | return false;
|
| 94 | }
|
| 95 |
|
| 96 | WanderingTrader trader = EntityTypes.WANDERING_TRADER.spawn(level, spawnPosition, EntitySpawnReason.EVENT);
|
| 97 | if (trader != null) {
|
| 98 | for (int i = 0; i < 2; i++) {
|
| 99 | this.tryToSpawnLlamaFor(level, trader, 4);
|
| 100 | }
|
| 101 |
|
| 102 | trader.setDespawnDelay(48000);
|
| 103 | trader.setWanderTarget(referencePos);
|
| 104 | trader.setHomeTo(referencePos, 16);
|
| 105 | return true;
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | return false;
|
| 110 | }
|
| 111 |
|
| 112 | private void tryToSpawnLlamaFor(final ServerLevel level, final WanderingTrader trader, final int radius) {
|
| 113 | BlockPos spawnPosition = this.findSpawnPositionNear(level, trader.blockPosition(), radius);
|
| 114 | if (spawnPosition != null) {
|
| 115 | TraderLlama llama = EntityTypes.TRADER_LLAMA.spawn(level, spawnPosition, EntitySpawnReason.EVENT);
|
| 116 | if (llama != null) {
|
| 117 | llama.setLeashedTo(trader, true);
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | private @Nullable BlockPos findSpawnPositionNear(final LevelReader level, final BlockPos referencePosition, final int radius) {
|
| 123 | BlockPos spawnPosition = null;
|
| 124 | SpawnPlacementType wanderingTraderSpawnType = SpawnPlacements.getPlacementType(EntityTypes.WANDERING_TRADER);
|
| 125 |
|
| 126 | for (int i = 0; i < 10; i++) {
|
| 127 | int xPosition = referencePosition.getX() + this.random.nextInt(radius * 2) - radius;
|
| 128 | int zPosition = referencePosition.getZ() + this.random.nextInt(radius * 2) - radius;
|
| 129 | int yPosition = level.getHeight(SpawnPlacements.getHeightmapType(EntityTypes.WANDERING_TRADER), xPosition, zPosition);
|
| 130 | BlockPos spawnPos = new BlockPos(xPosition, yPosition, zPosition);
|
| 131 | if (wanderingTraderSpawnType.isSpawnPositionOk(level, spawnPos, EntityTypes.WANDERING_TRADER)) {
|
| 132 | spawnPosition = spawnPos;
|
| 133 | break;
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | return spawnPosition;
|
| 138 | }
|
| 139 |
|
| 140 | private boolean hasEnoughSpace(final LevelReader level, final BlockPos spawnPos) {
|
| 141 | return !level.findBlocksIn(spawnPos, spawnPos.offset(1, 2, 1))
|
| 142 | .forEachUntil((pos, state) -> Continuation.continueIf(state.getCollisionShape(level, pos).isEmpty()));
|
| 143 | }
|
| 144 | }
|
| 145 |
|