| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.mojang.datafixers.DataFixer;
|
| 6 | import com.mojang.datafixers.util.Pair;
|
| 7 | import com.mojang.logging.LogUtils;
|
| 8 | import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
|
| 9 | import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
|
| 10 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 11 | import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
|
| 12 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 13 | import it.unimi.dsi.fastutil.objects.ObjectLinkedOpenHashSet;
|
| 14 | import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
|
| 15 | import it.unimi.dsi.fastutil.objects.Object2IntMap.Entry;
|
| 16 | import java.io.IOException;
|
| 17 | import java.io.Writer;
|
| 18 | import java.nio.file.Files;
|
| 19 | import java.nio.file.Path;
|
| 20 | import java.util.ArrayList;
|
| 21 | import java.util.Collection;
|
| 22 | import java.util.Comparator;
|
| 23 | import java.util.List;
|
| 24 | import java.util.Locale;
|
| 25 | import java.util.Objects;
|
| 26 | import java.util.Optional;
|
| 27 | import java.util.Set;
|
| 28 | import java.util.UUID;
|
| 29 | import java.util.concurrent.Executor;
|
| 30 | import java.util.concurrent.atomic.AtomicInteger;
|
| 31 | import java.util.function.BooleanSupplier;
|
| 32 | import java.util.function.Function;
|
| 33 | import java.util.function.Predicate;
|
| 34 | import java.util.stream.Collectors;
|
| 35 | import java.util.stream.Stream;
|
| 36 | import net.minecraft.CrashReport;
|
| 37 | import net.minecraft.CrashReportCategory;
|
| 38 | import net.minecraft.ReportType;
|
| 39 | import net.minecraft.core.BlockPos;
|
| 40 | import net.minecraft.core.Direction;
|
| 41 | import net.minecraft.core.Holder;
|
| 42 | import net.minecraft.core.HolderSet;
|
| 43 | import net.minecraft.core.SectionPos;
|
| 44 | import net.minecraft.core.particles.ExplosionParticleInfo;
|
| 45 | import net.minecraft.core.particles.ParticleOptions;
|
| 46 | import net.minecraft.core.registries.Registries;
|
| 47 | import net.minecraft.network.chat.Component;
|
| 48 | import net.minecraft.network.protocol.Packet;
|
| 49 | import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
|
| 50 | import net.minecraft.network.protocol.game.ClientboundBlockEventPacket;
|
| 51 | import net.minecraft.network.protocol.game.ClientboundDamageEventPacket;
|
| 52 | import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
|
| 53 | import net.minecraft.network.protocol.game.ClientboundExplodePacket;
|
| 54 | import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
|
| 55 | import net.minecraft.network.protocol.game.ClientboundLevelEventPacket;
|
| 56 | import net.minecraft.network.protocol.game.ClientboundLevelParticlesPacket;
|
| 57 | import net.minecraft.network.protocol.game.ClientboundSoundEntityPacket;
|
| 58 | import net.minecraft.network.protocol.game.ClientboundSoundPacket;
|
| 59 | import net.minecraft.resources.ResourceKey;
|
| 60 | import net.minecraft.server.MinecraftServer;
|
| 61 | import net.minecraft.server.ServerScoreboard;
|
| 62 | import net.minecraft.server.players.SleepStatus;
|
| 63 | import net.minecraft.server.waypoints.ServerWaypointManager;
|
| 64 | import net.minecraft.sounds.SoundEvent;
|
| 65 | import net.minecraft.sounds.SoundSource;
|
| 66 | import net.minecraft.tags.BlockTags;
|
| 67 | import net.minecraft.tags.TagKey;
|
| 68 | import net.minecraft.util.Continuation;
|
| 69 | import net.minecraft.util.CsvOutput;
|
| 70 | import net.minecraft.util.Mth;
|
| 71 | import net.minecraft.util.ProgressListener;
|
| 72 | import net.minecraft.util.Util;
|
| 73 | import net.minecraft.util.datafix.DataFixTypes;
|
| 74 | import net.minecraft.util.debug.DebugSubscriptions;
|
| 75 | import net.minecraft.util.debug.LevelDebugSynchronizers;
|
| 76 | import net.minecraft.util.profiling.Profiler;
|
| 77 | import net.minecraft.util.profiling.ProfilerFiller;
|
| 78 | import net.minecraft.util.random.WeightedList;
|
| 79 | import net.minecraft.util.valueproviders.IntProvider;
|
| 80 | import net.minecraft.util.valueproviders.UniformInt;
|
| 81 | import net.minecraft.world.DifficultyInstance;
|
| 82 | import net.minecraft.world.TickRateManager;
|
| 83 | import net.minecraft.world.attribute.EnvironmentAttributeSystem;
|
| 84 | import net.minecraft.world.attribute.EnvironmentAttributes;
|
| 85 | import net.minecraft.world.clock.ClockTimeMarkers;
|
| 86 | import net.minecraft.world.clock.ServerClockManager;
|
| 87 | import net.minecraft.world.clock.WorldClock;
|
| 88 | import net.minecraft.world.damagesource.DamageSource;
|
| 89 | import net.minecraft.world.entity.Entity;
|
| 90 | import net.minecraft.world.entity.EntityEvent;
|
| 91 | import net.minecraft.world.entity.EntitySpawnReason;
|
| 92 | import net.minecraft.world.entity.EntityTypes;
|
| 93 | import net.minecraft.world.entity.LightningBolt;
|
| 94 | import net.minecraft.world.entity.LivingEntity;
|
| 95 | import net.minecraft.world.entity.Mob;
|
| 96 | import net.minecraft.world.entity.MobCategory;
|
| 97 | import net.minecraft.world.entity.ReputationEventHandler;
|
| 98 | import net.minecraft.world.entity.ai.navigation.PathNavigation;
|
| 99 | import net.minecraft.world.entity.ai.village.ReputationEventType;
|
| 100 | import net.minecraft.world.entity.ai.village.poi.PoiManager;
|
| 101 | import net.minecraft.world.entity.ai.village.poi.PoiRecord;
|
| 102 | import net.minecraft.world.entity.ai.village.poi.PoiType;
|
| 103 | import net.minecraft.world.entity.ai.village.poi.PoiTypes;
|
| 104 | import net.minecraft.world.entity.animal.equine.SkeletonHorse;
|
| 105 | import net.minecraft.world.entity.boss.enderdragon.EnderDragon;
|
| 106 | import net.minecraft.world.entity.boss.enderdragon.EnderDragonPart;
|
| 107 | import net.minecraft.world.entity.player.Player;
|
| 108 | import net.minecraft.world.entity.raid.Raid;
|
| 109 | import net.minecraft.world.entity.raid.Raids;
|
| 110 | import net.minecraft.world.flag.FeatureFlagSet;
|
| 111 | import net.minecraft.world.item.crafting.RecipeManager;
|
| 112 | import net.minecraft.world.level.BlockEventData;
|
| 113 | import net.minecraft.world.level.ChunkPos;
|
| 114 | import net.minecraft.world.level.CustomSpawner;
|
| 115 | import net.minecraft.world.level.Explosion;
|
| 116 | import net.minecraft.world.level.ExplosionDamageCalculator;
|
| 117 | import net.minecraft.world.level.Level;
|
| 118 | import net.minecraft.world.level.MoonPhase;
|
| 119 | import net.minecraft.world.level.NaturalSpawner;
|
| 120 | import net.minecraft.world.level.ServerExplosion;
|
| 121 | import net.minecraft.world.level.StructureManager;
|
| 122 | import net.minecraft.world.level.WorldGenLevel;
|
| 123 | import net.minecraft.world.level.biome.Biome;
|
| 124 | import net.minecraft.world.level.biome.BiomeResolver;
|
| 125 | import net.minecraft.world.level.block.Block;
|
| 126 | import net.minecraft.world.level.block.Blocks;
|
| 127 | import net.minecraft.world.level.block.LevelEvent;
|
| 128 | import net.minecraft.world.level.block.SnowLayerBlock;
|
| 129 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 130 | import net.minecraft.world.level.block.entity.TickingBlockEntity;
|
| 131 | import net.minecraft.world.level.block.state.BlockState;
|
| 132 | import net.minecraft.world.level.border.WorldBorder;
|
| 133 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 134 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 135 | import net.minecraft.world.level.chunk.LevelChunk;
|
| 136 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 137 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 138 | import net.minecraft.world.level.chunk.storage.EntityStorage;
|
| 139 | import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
|
| 140 | import net.minecraft.world.level.chunk.storage.SimpleRegionStorage;
|
| 141 | import net.minecraft.world.level.dimension.DimensionType;
|
| 142 | import net.minecraft.world.level.dimension.LevelStem;
|
| 143 | import net.minecraft.world.level.dimension.end.EnderDragonFight;
|
| 144 | import net.minecraft.world.level.entity.EntityPersistentStorage;
|
| 145 | import net.minecraft.world.level.entity.EntityTickList;
|
| 146 | import net.minecraft.world.level.entity.EntityTypeTest;
|
| 147 | import net.minecraft.world.level.entity.LevelCallback;
|
| 148 | import net.minecraft.world.level.entity.LevelEntityGetter;
|
| 149 | import net.minecraft.world.level.entity.PersistentEntitySectionManager;
|
| 150 | import net.minecraft.world.level.gameevent.DynamicGameEventListener;
|
| 151 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 152 | import net.minecraft.world.level.gameevent.GameEventDispatcher;
|
| 153 | import net.minecraft.world.level.gamerules.GameRule;
|
| 154 | import net.minecraft.world.level.gamerules.GameRules;
|
| 155 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 156 | import net.minecraft.world.level.levelgen.WorldGenSettings;
|
| 157 | import net.minecraft.world.level.levelgen.WorldOptions;
|
| 158 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 159 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 160 | import net.minecraft.world.level.levelgen.structure.StructureCheck;
|
| 161 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 162 | import net.minecraft.world.level.material.Fluid;
|
| 163 | import net.minecraft.world.level.material.FluidState;
|
| 164 | import net.minecraft.world.level.pathfinder.PathTypeCache;
|
| 165 | import net.minecraft.world.level.portal.PortalForcer;
|
| 166 | import net.minecraft.world.level.redstone.ExperimentalRedstoneUtils;
|
| 167 | import net.minecraft.world.level.redstone.Orientation;
|
| 168 | import net.minecraft.world.level.saveddata.WeatherData;
|
| 169 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 170 | import net.minecraft.world.level.saveddata.maps.MapIndex;
|
| 171 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 172 | import net.minecraft.world.level.storage.LevelData;
|
| 173 | import net.minecraft.world.level.storage.LevelStorageSource;
|
| 174 | import net.minecraft.world.level.storage.SavedDataStorage;
|
| 175 | import net.minecraft.world.level.storage.ServerLevelData;
|
| 176 | import net.minecraft.world.phys.AABB;
|
| 177 | import net.minecraft.world.phys.Vec3;
|
| 178 | import net.minecraft.world.phys.shapes.BooleanOp;
|
| 179 | import net.minecraft.world.phys.shapes.Shapes;
|
| 180 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 181 | import net.minecraft.world.ticks.LevelTicks;
|
| 182 | import net.minecraft.world.waypoints.WaypointTransmitter;
|
| 183 | import org.jspecify.annotations.Nullable;
|
| 184 | import org.slf4j.Logger;
|
| 185 |
|
| 186 | /**
|
| 187 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 188 | */
|
| 189 | public class ServerLevel extends Level implements WorldGenLevel, ServerEntityGetter {
|
| 190 | public static final BlockPos END_SPAWN_POINT = new BlockPos(100, 50, 0);
|
| 191 | public static final IntProvider RAIN_DELAY = UniformInt.of(12000, 180000);
|
| 192 | public static final IntProvider RAIN_DURATION = UniformInt.of(12000, 24000);
|
| 193 | private static final IntProvider THUNDER_DELAY = UniformInt.of(12000, 180000);
|
| 194 | public static final IntProvider THUNDER_DURATION = UniformInt.of(3600, 15600);
|
| 195 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 196 | private static final int EMPTY_TIME_NO_TICK = 300;
|
| 197 | private static final int MAX_SCHEDULED_TICKS_PER_TICK = 65536;
|
| 198 | private static final AtomicInteger ENTITY_COUNTER = new AtomicInteger();
|
| 199 | private final List<ServerPlayer> players = Lists.newArrayList();
|
| 200 | private final ServerChunkCache chunkSource;
|
| 201 | private final MinecraftServer server;
|
| 202 | private final ServerLevelData serverLevelData;
|
| 203 | private final EntityTickList entityTickList = new EntityTickList();
|
| 204 | private final ServerWaypointManager waypointManager;
|
| 205 | private EnvironmentAttributeSystem environmentAttributes;
|
| 206 | private final PersistentEntitySectionManager<Entity> entityManager;
|
| 207 | private final GameEventDispatcher gameEventDispatcher;
|
| 208 | public boolean noSave;
|
| 209 | private final SleepStatus sleepStatus;
|
| 210 | private int emptyTime;
|
| 211 | private final PortalForcer portalForcer;
|
| 212 | private final LevelTicks<Block> blockTicks = new LevelTicks<>(this::isPositionTickingWithEntitiesLoaded);
|
| 213 | private final LevelTicks<Fluid> fluidTicks = new LevelTicks<>(this::isPositionTickingWithEntitiesLoaded);
|
| 214 | private final PathTypeCache pathTypesByPosCache = new PathTypeCache();
|
| 215 | private final Set<Mob> navigatingMobs = new ObjectOpenHashSet<>();
|
| 216 | private volatile boolean isUpdatingNavigations;
|
| 217 | protected final Raids raids;
|
| 218 | private final ObjectLinkedOpenHashSet<BlockEventData> blockEvents = new ObjectLinkedOpenHashSet<>();
|
| 219 | private final List<BlockEventData> blockEventsToReschedule = new ArrayList<>(64);
|
| 220 | private boolean handlingTick;
|
| 221 | private final List<CustomSpawner> customSpawners;
|
| 222 | private @Nullable EnderDragonFight dragonFight;
|
| 223 | private final Int2ObjectMap<EnderDragonPart> dragonParts = new Int2ObjectOpenHashMap<>();
|
| 224 | private final BiomeResolver uncachedBiomeResolver;
|
| 225 | private final StructureManager structureManager;
|
| 226 | private final StructureCheck structureCheck;
|
| 227 | private final boolean tickTime;
|
| 228 | private final LevelDebugSynchronizers debugSynchronizers = new LevelDebugSynchronizers(this);
|
| 229 |
|
| 230 | public ServerLevel(
|
| 231 | final MinecraftServer server,
|
| 232 | final Executor executor,
|
| 233 | final LevelStorageSource.LevelStorageAccess levelStorage,
|
| 234 | final ServerLevelData levelData,
|
| 235 | final ResourceKey<Level> dimension,
|
| 236 | final LevelStem levelStem,
|
| 237 | final boolean isDebug,
|
| 238 | final long biomeZoomSeed,
|
| 239 | final List<CustomSpawner> customSpawners,
|
| 240 | final boolean tickTime
|
| 241 | ) {
|
| 242 | super(levelData, dimension, server.registryAccess(), levelStem.type(), false, isDebug, biomeZoomSeed, server.getMaxChainedNeighborUpdates());
|
| 243 | this.tickTime = tickTime;
|
| 244 | this.server = server;
|
| 245 | this.customSpawners = customSpawners;
|
| 246 | this.serverLevelData = levelData;
|
| 247 | ChunkGenerator generator = levelStem.generator();
|
| 248 | boolean syncWrites = server.forceSynchronousWrites();
|
| 249 | DataFixer fixerUpper = server.getFixerUpper();
|
| 250 | EntityPersistentStorage<Entity> entityStorage = new EntityStorage(
|
| 251 | new SimpleRegionStorage(
|
| 252 | new RegionStorageInfo(levelStorage.getLevelId(), dimension, "entities"),
|
| 253 | levelStorage.getDimensionPath(dimension).resolve("entities"),
|
| 254 | fixerUpper,
|
| 255 | syncWrites,
|
| 256 | DataFixTypes.ENTITY_CHUNK
|
| 257 | ),
|
| 258 | this,
|
| 259 | server
|
| 260 | );
|
| 261 | this.entityManager = new PersistentEntitySectionManager<>(Entity.class, new ServerLevel.EntityCallbacks(), entityStorage);
|
| 262 | this.chunkSource = new ServerChunkCache(
|
| 263 | this,
|
| 264 | levelStorage,
|
| 265 | fixerUpper,
|
| 266 | server.getStructureTemplateManager(),
|
| 267 | executor,
|
| 268 | generator,
|
| 269 | server.getPlayerList().getViewDistance(),
|
| 270 | server.getPlayerList().getSimulationDistance(),
|
| 271 | syncWrites,
|
| 272 | this.entityManager::updateChunkStatus
|
| 273 | );
|
| 274 | this.chunkSource.getGeneratorState().ensureStructuresGenerated();
|
| 275 | this.portalForcer = new PortalForcer(this);
|
| 276 | if (this.canHaveWeather()) {
|
| 277 | this.prepareWeather(server.getWeatherData());
|
| 278 | }
|
| 279 |
|
| 280 | this.raids = this.getDataStorage().computeIfAbsent(Raids.TYPE);
|
| 281 | if (!server.isSingleplayer()) {
|
| 282 | levelData.setGameType(server.getDefaultGameType());
|
| 283 | }
|
| 284 |
|
| 285 | WorldGenSettings worldGenSettings = server.getWorldGenSettings();
|
| 286 | WorldOptions options = worldGenSettings.options();
|
| 287 | long seed = options.seed();
|
| 288 | this.structureCheck = new StructureCheck(
|
| 289 | this.chunkSource.chunkScanner(),
|
| 290 | this.registryAccess(),
|
| 291 | server.getStructureTemplateManager(),
|
| 292 | dimension,
|
| 293 | generator,
|
| 294 | this.chunkSource.randomState(),
|
| 295 | this,
|
| 296 | generator.getBiomeSource(),
|
| 297 | seed,
|
| 298 | fixerUpper
|
| 299 | );
|
| 300 | this.structureManager = new StructureManager(this, options, this.structureCheck);
|
| 301 | if (this.dimensionType().hasEnderDragonFight()) {
|
| 302 | this.dragonFight = this.getDataStorage().computeIfAbsent(EnderDragonFight.TYPE);
|
| 303 | this.dragonFight.init(this, seed, BlockPos.ZERO);
|
| 304 | }
|
| 305 |
|
| 306 | this.sleepStatus = new SleepStatus();
|
| 307 | this.gameEventDispatcher = new GameEventDispatcher(this);
|
| 308 | this.waypointManager = new ServerWaypointManager();
|
| 309 | this.environmentAttributes = EnvironmentAttributeSystem.builder().addDefaultLayers(this).build();
|
| 310 | this.uncachedBiomeResolver = this.getChunkSource().getGenerator().getBiomeSource().createUncachedResolver(this.getChunkSource().randomState());
|
| 311 | this.updateSkyBrightness();
|
| 312 | }
|
| 313 |
|
| 314 | @Override
|
| 315 | public int getNextEntityId() {
|
| 316 | int id = 0;
|
| 317 |
|
| 318 | while (id == 0 || this.chunkSource.hasEntityWithId(id)) {
|
| 319 | id = ENTITY_COUNTER.incrementAndGet();
|
| 320 | }
|
| 321 |
|
| 322 | return id;
|
| 323 | }
|
| 324 |
|
| 325 | @Deprecated
|
| 326 | @VisibleForTesting
|
| 327 | public void setDragonFight(final @Nullable EnderDragonFight fight) {
|
| 328 | this.dragonFight = fight;
|
| 329 | }
|
| 330 |
|
| 331 | @Override
|
| 332 | public Holder<Biome> getUncachedNoiseBiome(final int quartX, final int quartY, final int quartZ) {
|
| 333 | return this.uncachedBiomeResolver.getNoiseBiome(quartX, quartY, quartZ);
|
| 334 | }
|
| 335 |
|
| 336 | public BiomeResolver uncachedBiomeResolver() {
|
| 337 | return this.uncachedBiomeResolver;
|
| 338 | }
|
| 339 |
|
| 340 | public StructureManager structureManager() {
|
| 341 | return this.structureManager;
|
| 342 | }
|
| 343 |
|
| 344 | public ServerClockManager clockManager() {
|
| 345 | return this.server.clockManager();
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | public EnvironmentAttributeSystem environmentAttributes() {
|
| 350 | return this.environmentAttributes;
|
| 351 | }
|
| 352 |
|
| 353 | @Deprecated
|
| 354 | @VisibleForTesting
|
| 355 | public EnvironmentAttributeSystem setEnvironmentAttributes(final EnvironmentAttributeSystem environmentAttributes) {
|
| 356 | EnvironmentAttributeSystem previous = this.environmentAttributes;
|
| 357 | this.environmentAttributes = environmentAttributes;
|
| 358 | return previous;
|
| 359 | }
|
| 360 |
|
| 361 | public void tick(final BooleanSupplier haveTime) {
|
| 362 | ProfilerFiller profiler = Profiler.get();
|
| 363 | this.handlingTick = true;
|
| 364 | this.environmentAttributes().invalidateTickCache();
|
| 365 | TickRateManager tickRateManager = this.tickRateManager();
|
| 366 | boolean runs = tickRateManager.runsNormally();
|
| 367 | if (runs) {
|
| 368 | profiler.push("world border");
|
| 369 | this.getWorldBorder().tick();
|
| 370 | profiler.popPush("weather");
|
| 371 | this.advanceWeatherCycle();
|
| 372 | profiler.pop();
|
| 373 | }
|
| 374 |
|
| 375 | int percentage = this.getGameRules().get(GameRules.PLAYERS_SLEEPING_PERCENTAGE);
|
| 376 | if (this.sleepStatus.areEnoughSleeping(percentage) && this.sleepStatus.areEnoughDeepSleeping(percentage, this.players)) {
|
| 377 | Optional<Holder<WorldClock>> defaultClock = this.dimensionType().defaultClock();
|
| 378 | if (this.getGameRules().get(GameRules.ADVANCE_TIME) && defaultClock.isPresent()) {
|
| 379 | this.server.clockManager().moveToTimeMarker(defaultClock.get(), ClockTimeMarkers.WAKE_UP_FROM_SLEEP);
|
| 380 | }
|
| 381 |
|
| 382 | this.wakeUpAllPlayers();
|
| 383 | if (this.getGameRules().get(GameRules.ADVANCE_WEATHER) && this.isRaining()) {
|
| 384 | this.resetWeatherCycle();
|
| 385 | }
|
| 386 | }
|
| 387 |
|
| 388 | this.updateSkyBrightness();
|
| 389 | if (runs) {
|
| 390 | this.tickTime();
|
| 391 | }
|
| 392 |
|
| 393 | profiler.push("tickPending");
|
| 394 | if (!this.isDebug() && runs) {
|
| 395 | long tick = this.getGameTime();
|
| 396 | profiler.push("blockTicks");
|
| 397 | this.blockTicks.tick(tick, 65536, this::tickBlock);
|
| 398 | profiler.popPush("fluidTicks");
|
| 399 | this.fluidTicks.tick(tick, 65536, this::tickFluid);
|
| 400 | profiler.pop();
|
| 401 | }
|
| 402 |
|
| 403 | profiler.popPush("raid");
|
| 404 | if (runs) {
|
| 405 | this.raids.tick(this);
|
| 406 | }
|
| 407 |
|
| 408 | profiler.popPush("chunkSource");
|
| 409 | this.getChunkSource().tick(haveTime, true);
|
| 410 | profiler.popPush("blockEvents");
|
| 411 | if (runs) {
|
| 412 | this.runBlockEvents();
|
| 413 | }
|
| 414 |
|
| 415 | this.handlingTick = false;
|
| 416 | profiler.pop();
|
| 417 | boolean isActive = this.chunkSource.hasActiveTickets();
|
| 418 | if (isActive) {
|
| 419 | this.resetEmptyTime();
|
| 420 | }
|
| 421 |
|
| 422 | if (runs) {
|
| 423 | this.emptyTime++;
|
| 424 | }
|
| 425 |
|
| 426 | if (this.emptyTime < 300) {
|
| 427 | profiler.push("entities");
|
| 428 | if (this.dragonFight != null && runs) {
|
| 429 | profiler.push("dragonFight");
|
| 430 | this.dragonFight.tick();
|
| 431 | profiler.pop();
|
| 432 | }
|
| 433 |
|
| 434 | this.entityTickList.forEach(entity -> {
|
| 435 | if (!entity.isRemoved()) {
|
| 436 | if (!tickRateManager.isEntityFrozen(entity)) {
|
| 437 | profiler.push("checkDespawn");
|
| 438 | entity.checkDespawn();
|
| 439 | profiler.pop();
|
| 440 | if (entity instanceof ServerPlayer || this.chunkSource.chunkMap.getDistanceManager().inEntityTickingRange(entity.chunkPosition().pack())) {
|
| 441 | Entity vehicle = entity.getVehicle();
|
| 442 | if (vehicle != null) {
|
| 443 | if (!vehicle.isRemoved() && vehicle.hasPassenger(entity)) {
|
| 444 | return;
|
| 445 | }
|
| 446 |
|
| 447 | entity.stopRiding();
|
| 448 | }
|
| 449 |
|
| 450 | profiler.push("tick");
|
| 451 | this.guardEntityTick(this::tickNonPassenger, entity);
|
| 452 | profiler.pop();
|
| 453 | }
|
| 454 | }
|
| 455 | }
|
| 456 | });
|
| 457 | profiler.popPush("blockEntities");
|
| 458 | this.tickBlockEntities();
|
| 459 | profiler.pop();
|
| 460 | }
|
| 461 |
|
| 462 | profiler.push("entityManagement");
|
| 463 | this.entityManager.tick();
|
| 464 | profiler.pop();
|
| 465 | profiler.push("debugSynchronizers");
|
| 466 | if (this.debugSynchronizers.hasAnySubscriberFor(DebugSubscriptions.NEIGHBOR_UPDATES)) {
|
| 467 | this.neighborUpdater.setDebugListener(blockPos -> this.debugSynchronizers.broadcastEventToTracking(blockPos, DebugSubscriptions.NEIGHBOR_UPDATES, blockPos));
|
| 468 | } else {
|
| 469 | this.neighborUpdater.setDebugListener(null);
|
| 470 | }
|
| 471 |
|
| 472 | this.debugSynchronizers.tick(this.server.debugSubscribers());
|
| 473 | profiler.pop();
|
| 474 | this.chunkSource.randomState().garbageCollect();
|
| 475 | }
|
| 476 |
|
| 477 | @Override
|
| 478 | public boolean shouldTickBlocksAt(final long chunkPos) {
|
| 479 | return this.chunkSource.chunkMap.getDistanceManager().inBlockTickingRange(chunkPos);
|
| 480 | }
|
| 481 |
|
| 482 | protected void tickTime() {
|
| 483 | if (this.tickTime) {
|
| 484 | long time = this.levelData.getGameTime() + 1L;
|
| 485 | this.serverLevelData.setGameTime(time);
|
| 486 | Profiler.get().push("scheduledFunctions");
|
| 487 | this.server.getScheduledEvents().tick(this.server, time);
|
| 488 | Profiler.get().pop();
|
| 489 | }
|
| 490 | }
|
| 491 |
|
| 492 | public void tickCustomSpawners(final boolean spawnEnemies) {
|
| 493 | for (CustomSpawner spawner : this.customSpawners) {
|
| 494 | spawner.tick(this, spawnEnemies);
|
| 495 | }
|
| 496 | }
|
| 497 |
|
| 498 | private void wakeUpAllPlayers() {
|
| 499 | this.sleepStatus.removeAllSleepers();
|
| 500 | this.players.stream().filter(LivingEntity::isSleeping).collect(Collectors.toList()).forEach(player -> player.stopSleepInBed(false, false));
|
| 501 | }
|
| 502 |
|
| 503 | public void tickChunk(final LevelChunk chunk, final int tickSpeed) {
|
| 504 | ChunkPos chunkPos = chunk.getPos();
|
| 505 | int minX = chunkPos.getMinBlockX();
|
| 506 | int minZ = chunkPos.getMinBlockZ();
|
| 507 | ProfilerFiller profiler = Profiler.get();
|
| 508 | profiler.push("iceandsnow");
|
| 509 |
|
| 510 | for (int i = 0; i < tickSpeed; i++) {
|
| 511 | if (this.random.nextInt(48) == 0) {
|
| 512 | this.tickPrecipitation(this.getBlockRandomPos(minX, 0, minZ, 15));
|
| 513 | }
|
| 514 | }
|
| 515 |
|
| 516 | profiler.popPush("tickBlocks");
|
| 517 | if (tickSpeed > 0) {
|
| 518 | LevelChunkSection[] sections = chunk.getSections();
|
| 519 |
|
| 520 | for (int sectionIndex = 0; sectionIndex < sections.length; sectionIndex++) {
|
| 521 | LevelChunkSection section = sections[sectionIndex];
|
| 522 | if (section.isRandomlyTicking()) {
|
| 523 | int sectionY = chunk.getSectionYFromSectionIndex(sectionIndex);
|
| 524 | int minYInSection = SectionPos.sectionToBlockCoord(sectionY);
|
| 525 |
|
| 526 | for (int i = 0; i < tickSpeed; i++) {
|
| 527 | BlockPos pos = this.getBlockRandomPos(minX, minYInSection, minZ, 15);
|
| 528 | profiler.push("randomTick");
|
| 529 | BlockState blockState = section.getBlockState(pos.getX() - minX, pos.getY() - minYInSection, pos.getZ() - minZ);
|
| 530 | if (blockState.isRandomlyTicking()) {
|
| 531 | blockState.randomTick(this, pos, this.random);
|
| 532 | }
|
| 533 |
|
| 534 | FluidState fluidState = blockState.getFluidState();
|
| 535 | if (fluidState.isRandomlyTicking()) {
|
| 536 | fluidState.randomTick(this, pos, this.random);
|
| 537 | }
|
| 538 |
|
| 539 | profiler.pop();
|
| 540 | }
|
| 541 | }
|
| 542 | }
|
| 543 | }
|
| 544 |
|
| 545 | profiler.pop();
|
| 546 | }
|
| 547 |
|
| 548 | public void tickThunder(final LevelChunk chunk) {
|
| 549 | ChunkPos chunkPos = chunk.getPos();
|
| 550 | boolean raining = this.isRaining();
|
| 551 | int minX = chunkPos.getMinBlockX();
|
| 552 | int minZ = chunkPos.getMinBlockZ();
|
| 553 | ProfilerFiller profiler = Profiler.get();
|
| 554 | profiler.push("thunder");
|
| 555 | if (raining && this.isThundering() && this.random.nextInt(100000) == 0) {
|
| 556 | BlockPos pos = this.findLightningTargetAround(this.getBlockRandomPos(minX, 0, minZ, 15));
|
| 557 | if (this.isRainingAt(pos)) {
|
| 558 | DifficultyInstance difficulty = this.getCurrentDifficultyAt(pos);
|
| 559 | boolean isTrap = this.getGameRules().get(GameRules.SPAWN_MOBS)
|
| 560 | && this.random.nextDouble() < difficulty.getEffectiveDifficulty() * 0.01
|
| 561 | && !this.getBlockState(pos.below()).is(BlockTags.LIGHTNING_RODS);
|
| 562 | if (isTrap) {
|
| 563 | SkeletonHorse horse = EntityTypes.SKELETON_HORSE.create(this, EntitySpawnReason.EVENT);
|
| 564 | if (horse != null) {
|
| 565 | horse.setTrap(true);
|
| 566 | horse.setAge(0);
|
| 567 | horse.setPos(pos.getX(), pos.getY(), pos.getZ());
|
| 568 | this.addFreshEntity(horse);
|
| 569 | }
|
| 570 | }
|
| 571 |
|
| 572 | LightningBolt bolt = EntityTypes.LIGHTNING_BOLT.create(this, EntitySpawnReason.EVENT);
|
| 573 | if (bolt != null) {
|
| 574 | bolt.snapTo(Vec3.atBottomCenterOf(pos));
|
| 575 | bolt.setVisualOnly(isTrap);
|
| 576 | this.addFreshEntity(bolt);
|
| 577 | }
|
| 578 | }
|
| 579 | }
|
| 580 |
|
| 581 | profiler.pop();
|
| 582 | }
|
| 583 |
|
| 584 | @VisibleForTesting
|
| 585 | public void tickPrecipitation(final BlockPos pos) {
|
| 586 | BlockPos topPos = this.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, pos);
|
| 587 | BlockPos belowPos = topPos.below();
|
| 588 | Biome biome = this.getBiome(topPos).value();
|
| 589 | if (biome.shouldFreeze(this, belowPos)) {
|
| 590 | this.setBlockAndUpdate(belowPos, Blocks.ICE.defaultBlockState());
|
| 591 | }
|
| 592 |
|
| 593 | if (this.isRaining()) {
|
| 594 | int maxHeight = this.getGameRules().get(GameRules.MAX_SNOW_ACCUMULATION_HEIGHT);
|
| 595 | if (maxHeight > 0 && biome.shouldSnow(this, topPos)) {
|
| 596 | BlockState state = this.getBlockState(topPos);
|
| 597 | if (state.is(Blocks.SNOW)) {
|
| 598 | int currentLayers = state.getValue(SnowLayerBlock.LAYERS);
|
| 599 | if (currentLayers < Math.min(maxHeight, 8)) {
|
| 600 | BlockState newState = state.setValue(SnowLayerBlock.LAYERS, currentLayers + 1);
|
| 601 | Block.pushEntitiesUp(state, newState, this, topPos);
|
| 602 | this.setBlockAndUpdate(topPos, newState);
|
| 603 | }
|
| 604 | } else {
|
| 605 | this.setBlockAndUpdate(topPos, Blocks.SNOW.defaultBlockState());
|
| 606 | }
|
| 607 | }
|
| 608 |
|
| 609 | Biome.Precipitation precipitation = biome.getPrecipitationAt(belowPos, this.getSeaLevel());
|
| 610 | if (precipitation != Biome.Precipitation.NONE) {
|
| 611 | BlockState belowState = this.getBlockState(belowPos);
|
| 612 | belowState.getBlock().handlePrecipitation(belowState, this, belowPos, precipitation);
|
| 613 | }
|
| 614 | }
|
| 615 | }
|
| 616 |
|
| 617 | private Optional<BlockPos> findLightningRod(final BlockPos center) {
|
| 618 | Optional<BlockPos> nearbyLightningRod = this.getPoiManager()
|
| 619 | .findClosest(
|
| 620 | p -> p.is(PoiTypes.LIGHTNING_ROD),
|
| 621 | lightningRodPos -> lightningRodPos.getY() == this.getHeight(Heightmap.Types.WORLD_SURFACE, lightningRodPos.getX(), lightningRodPos.getZ()) - 1,
|
| 622 | center,
|
| 623 | 128,
|
| 624 | PoiManager.Occupancy.ANY
|
| 625 | );
|
| 626 | return nearbyLightningRod.map(blockPos -> blockPos.above(1));
|
| 627 | }
|
| 628 |
|
| 629 | protected BlockPos findLightningTargetAround(final BlockPos pos) {
|
| 630 | BlockPos center = this.getHeightmapPos(Heightmap.Types.MOTION_BLOCKING, pos);
|
| 631 | Optional<BlockPos> lightningRodTarget = this.findLightningRod(center);
|
| 632 | if (lightningRodTarget.isPresent()) {
|
| 633 | return lightningRodTarget.get();
|
| 634 | }
|
| 635 |
|
| 636 | AABB search = AABB.encapsulatingFullBlocks(center, center.atY(this.getMaxY() + 1)).inflate(3.0);
|
| 637 | List<LivingEntity> entities = this.getEntitiesOfClass(LivingEntity.class, search, input -> input.isAlive() && this.canSeeSky(input.blockPosition()));
|
| 638 | if (!entities.isEmpty()) {
|
| 639 | return entities.get(this.random.nextInt(entities.size())).blockPosition();
|
| 640 | }
|
| 641 |
|
| 642 | if (center.getY() == this.getMinY() - 1) {
|
| 643 | center = center.above(2);
|
| 644 | }
|
| 645 |
|
| 646 | return center;
|
| 647 | }
|
| 648 |
|
| 649 | public boolean isHandlingTick() {
|
| 650 | return this.handlingTick;
|
| 651 | }
|
| 652 |
|
| 653 | public boolean canSleepThroughNights() {
|
| 654 | return this.getGameRules().get(GameRules.PLAYERS_SLEEPING_PERCENTAGE) <= 100;
|
| 655 | }
|
| 656 |
|
| 657 | private void announceSleepStatus() {
|
| 658 | if (this.canSleepThroughNights()) {
|
| 659 | if (!this.getServer().isSingleplayer() || this.getServer().isPublished()) {
|
| 660 | int percentage = this.getGameRules().get(GameRules.PLAYERS_SLEEPING_PERCENTAGE);
|
| 661 | Component message;
|
| 662 | if (this.sleepStatus.areEnoughSleeping(percentage)) {
|
| 663 | message = Component.translatable("sleep.skipping_night");
|
| 664 | } else {
|
| 665 | message = Component.translatable("sleep.players_sleeping", this.sleepStatus.amountSleeping(), this.sleepStatus.sleepersNeeded(percentage));
|
| 666 | }
|
| 667 |
|
| 668 | for (ServerPlayer player : this.players) {
|
| 669 | player.sendOverlayMessage(message);
|
| 670 | }
|
| 671 | }
|
| 672 | }
|
| 673 | }
|
| 674 |
|
| 675 | public void updateSleepingPlayerList() {
|
| 676 | if (!this.players.isEmpty() && this.sleepStatus.update(this.players)) {
|
| 677 | this.announceSleepStatus();
|
| 678 | }
|
| 679 | }
|
| 680 |
|
| 681 | public ServerScoreboard getScoreboard() {
|
| 682 | return this.server.getScoreboard();
|
| 683 | }
|
| 684 |
|
| 685 | public ServerWaypointManager getWaypointManager() {
|
| 686 | return this.waypointManager;
|
| 687 | }
|
| 688 |
|
| 689 | @Override
|
| 690 | public DifficultyInstance getCurrentDifficultyAt(final BlockPos pos) {
|
| 691 | long localTime = 0L;
|
| 692 | float moonBrightness = 0.0F;
|
| 693 | ChunkAccess chunk = this.getChunk(SectionPos.blockToSectionCoord(pos.getX()), SectionPos.blockToSectionCoord(pos.getZ()), ChunkStatus.FULL, false);
|
| 694 | if (chunk != null) {
|
| 695 | localTime = chunk.getInhabitedTime();
|
| 696 | moonBrightness = this.getMoonBrightness(pos);
|
| 697 | }
|
| 698 |
|
| 699 | return new DifficultyInstance(this.getDifficulty(), this.getOverworldClockTime(), localTime, moonBrightness);
|
| 700 | }
|
| 701 |
|
| 702 | public float getMoonBrightness(final BlockPos pos) {
|
| 703 | MoonPhase moonPhase = this.environmentAttributes.getValue(EnvironmentAttributes.MOON_PHASE, pos);
|
| 704 | return DimensionType.MOON_BRIGHTNESS_PER_PHASE[moonPhase.index()];
|
| 705 | }
|
| 706 |
|
| 707 | private void prepareWeather(final WeatherData weatherData) {
|
| 708 | if (weatherData.isRaining()) {
|
| 709 | this.rainLevel = 1.0F;
|
| 710 | if (weatherData.isThundering()) {
|
| 711 | this.thunderLevel = 1.0F;
|
| 712 | }
|
| 713 | }
|
| 714 | }
|
| 715 |
|
| 716 | private void advanceWeatherCycle() {
|
| 717 | boolean wasRaining = this.isRaining();
|
| 718 | if (this.canHaveWeather()) {
|
| 719 | WeatherData weatherData = this.getWeatherData();
|
| 720 | if (this.getGameRules().get(GameRules.ADVANCE_WEATHER)) {
|
| 721 | int clearWeatherTime = weatherData.getClearWeatherTime();
|
| 722 | int thunderTime = weatherData.getThunderTime();
|
| 723 | int rainTime = weatherData.getRainTime();
|
| 724 | boolean thundering = weatherData.isThundering();
|
| 725 | boolean raining = weatherData.isRaining();
|
| 726 | if (clearWeatherTime > 0) {
|
| 727 | clearWeatherTime--;
|
| 728 | thunderTime = thundering ? 0 : 1;
|
| 729 | rainTime = raining ? 0 : 1;
|
| 730 | thundering = false;
|
| 731 | raining = false;
|
| 732 | } else {
|
| 733 | if (thunderTime > 0) {
|
| 734 | if (--thunderTime == 0) {
|
| 735 | thundering = !thundering;
|
| 736 | }
|
| 737 | } else if (thundering) {
|
| 738 | thunderTime = THUNDER_DURATION.sample(this.random);
|
| 739 | } else {
|
| 740 | thunderTime = THUNDER_DELAY.sample(this.random);
|
| 741 | }
|
| 742 |
|
| 743 | if (rainTime > 0) {
|
| 744 | if (--rainTime == 0) {
|
| 745 | raining = !raining;
|
| 746 | }
|
| 747 | } else if (raining) {
|
| 748 | rainTime = RAIN_DURATION.sample(this.random);
|
| 749 | } else {
|
| 750 | rainTime = RAIN_DELAY.sample(this.random);
|
| 751 | }
|
| 752 | }
|
| 753 |
|
| 754 | weatherData.setThunderTime(thunderTime);
|
| 755 | weatherData.setRainTime(rainTime);
|
| 756 | weatherData.setClearWeatherTime(clearWeatherTime);
|
| 757 | weatherData.setThundering(thundering);
|
| 758 | weatherData.setRaining(raining);
|
| 759 | }
|
| 760 |
|
| 761 | this.oThunderLevel = this.thunderLevel;
|
| 762 | if (weatherData.isThundering()) {
|
| 763 | this.thunderLevel += 0.01F;
|
| 764 | } else {
|
| 765 | this.thunderLevel -= 0.01F;
|
| 766 | }
|
| 767 |
|
| 768 | this.thunderLevel = Mth.clamp(this.thunderLevel, 0.0F, 1.0F);
|
| 769 | this.oRainLevel = this.rainLevel;
|
| 770 | if (weatherData.isRaining()) {
|
| 771 | this.rainLevel += 0.01F;
|
| 772 | } else {
|
| 773 | this.rainLevel -= 0.01F;
|
| 774 | }
|
| 775 |
|
| 776 | this.rainLevel = Mth.clamp(this.rainLevel, 0.0F, 1.0F);
|
| 777 | }
|
| 778 |
|
| 779 | if (this.oRainLevel != this.rainLevel) {
|
| 780 | this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.RAIN_LEVEL_CHANGE, this.rainLevel), this.dimension());
|
| 781 | }
|
| 782 |
|
| 783 | if (this.oThunderLevel != this.thunderLevel) {
|
| 784 | this.server
|
| 785 | .getPlayerList()
|
| 786 | .broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.THUNDER_LEVEL_CHANGE, this.thunderLevel), this.dimension());
|
| 787 | }
|
| 788 |
|
| 789 | if (wasRaining != this.isRaining()) {
|
| 790 | if (wasRaining) {
|
| 791 | this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.STOP_RAINING, 0.0F));
|
| 792 | } else {
|
| 793 | this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.START_RAINING, 0.0F));
|
| 794 | }
|
| 795 |
|
| 796 | this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.RAIN_LEVEL_CHANGE, this.rainLevel));
|
| 797 | this.server.getPlayerList().broadcastAll(new ClientboundGameEventPacket(ClientboundGameEventPacket.THUNDER_LEVEL_CHANGE, this.thunderLevel));
|
| 798 | }
|
| 799 | }
|
| 800 |
|
| 801 | @VisibleForTesting
|
| 802 | public void resetWeatherCycle() {
|
| 803 | WeatherData weatherData = this.getWeatherData();
|
| 804 | weatherData.setRainTime(0);
|
| 805 | weatherData.setRaining(false);
|
| 806 | weatherData.setThunderTime(0);
|
| 807 | weatherData.setThundering(false);
|
| 808 | }
|
| 809 |
|
| 810 | public void resetEmptyTime() {
|
| 811 | this.emptyTime = 0;
|
| 812 | }
|
| 813 |
|
| 814 | private void tickFluid(final BlockPos pos, final Fluid type) {
|
| 815 | BlockState blockState = this.getBlockState(pos);
|
| 816 | FluidState fluidState = blockState.getFluidState();
|
| 817 | if (fluidState.is(type)) {
|
| 818 | fluidState.tick(this, pos, blockState);
|
| 819 | }
|
| 820 | }
|
| 821 |
|
| 822 | private void tickBlock(final BlockPos pos, final Block type) {
|
| 823 | BlockState state = this.getBlockState(pos);
|
| 824 | if (state.is(type)) {
|
| 825 | state.tick(this, pos, this.random);
|
| 826 | }
|
| 827 | }
|
| 828 |
|
| 829 | public void tickNonPassenger(final Entity entity) {
|
| 830 | ProfilerFiller profiler = Profiler.get();
|
| 831 | profiler.push(entity.typeHolder()::getRegisteredName);
|
| 832 | profiler.incrementCounter("tickNonPassenger");
|
| 833 | entity.commonTick();
|
| 834 | entity.tick();
|
| 835 | profiler.pop();
|
| 836 |
|
| 837 | for (Entity passenger : entity.getPassengers()) {
|
| 838 | this.tickPassenger(entity, passenger);
|
| 839 | }
|
| 840 | }
|
| 841 |
|
| 842 | private void tickPassenger(final Entity vehicle, final Entity entity) {
|
| 843 | if (entity.isRemoved() || entity.getVehicle() != vehicle) {
|
| 844 | entity.stopRiding();
|
| 845 | } else if (entity instanceof Player || this.entityTickList.contains(entity)) {
|
| 846 | ProfilerFiller profiler = Profiler.get();
|
| 847 | profiler.push(entity.typeHolder()::getRegisteredName);
|
| 848 | profiler.incrementCounter("tickPassenger");
|
| 849 | entity.commonTick();
|
| 850 | entity.rideTick();
|
| 851 | profiler.pop();
|
| 852 |
|
| 853 | for (Entity passenger : entity.getPassengers()) {
|
| 854 | this.tickPassenger(entity, passenger);
|
| 855 | }
|
| 856 | }
|
| 857 | }
|
| 858 |
|
| 859 | public void updateNeighboursOnBlockSet(final BlockPos pos, final BlockState oldState) {
|
| 860 | BlockState blockState = this.getBlockState(pos);
|
| 861 | Block newBlock = blockState.getBlock();
|
| 862 | boolean blockChanged = !oldState.is(newBlock);
|
| 863 | if (blockChanged) {
|
| 864 | oldState.affectNeighborsAfterRemoval(this, pos, false);
|
| 865 | }
|
| 866 |
|
| 867 | this.updateNeighborsAt(pos, blockState.getBlock());
|
| 868 | if (blockState.hasAnalogOutputSignal()) {
|
| 869 | this.updateNeighbourForOutputSignal(pos, newBlock);
|
| 870 | }
|
| 871 | }
|
| 872 |
|
| 873 | @Override
|
| 874 | public boolean mayInteract(final Entity entity, final BlockPos pos) {
|
| 875 | return !(entity instanceof Player player && (this.server.isUnderSpawnProtection(this, pos, player) || !this.getWorldBorder().isWithinBounds(pos)));
|
| 876 | }
|
| 877 |
|
| 878 | public void save(final @Nullable ProgressListener progressListener, final boolean flush, final boolean noSave) {
|
| 879 | ServerChunkCache chunkSource = this.getChunkSource();
|
| 880 | if (!noSave) {
|
| 881 | if (progressListener != null) {
|
| 882 | progressListener.progressStartNoAbort(Component.translatable("menu.savingLevel"));
|
| 883 | }
|
| 884 |
|
| 885 | this.saveLevelData(flush);
|
| 886 | if (progressListener != null) {
|
| 887 | progressListener.progressStage(Component.translatable("menu.savingChunks"));
|
| 888 | }
|
| 889 |
|
| 890 | chunkSource.save(flush);
|
| 891 | if (flush) {
|
| 892 | this.entityManager.saveAll();
|
| 893 | } else {
|
| 894 | this.entityManager.autoSave();
|
| 895 | }
|
| 896 | }
|
| 897 | }
|
| 898 |
|
| 899 | private void saveLevelData(final boolean sync) {
|
| 900 | SavedDataStorage savedDataStorage = this.getChunkSource().getDataStorage();
|
| 901 | if (sync) {
|
| 902 | savedDataStorage.saveAndJoin();
|
| 903 | } else {
|
| 904 | savedDataStorage.scheduleSave();
|
| 905 | }
|
| 906 | }
|
| 907 |
|
| 908 | public <T extends Entity> List<? extends T> getEntities(final EntityTypeTest<Entity, T> type, final Predicate<? super T> selector) {
|
| 909 | List<T> result = Lists.newArrayList();
|
| 910 | this.getEntities(type, selector, result);
|
| 911 | return result;
|
| 912 | }
|
| 913 |
|
| 914 | public <T extends Entity> void getEntities(final EntityTypeTest<Entity, T> type, final Predicate<? super T> selector, final List<? super T> result) {
|
| 915 | this.getEntities(type, selector, result, Integer.MAX_VALUE);
|
| 916 | }
|
| 917 |
|
| 918 | public <T extends Entity> void getEntities(
|
| 919 | final EntityTypeTest<Entity, T> type, final Predicate<? super T> selector, final List<? super T> result, final int maxResults
|
| 920 | ) {
|
| 921 | this.getEntities().get(type, entity -> {
|
| 922 | if (selector.test(entity)) {
|
| 923 | result.add(entity);
|
| 924 | if (result.size() >= maxResults) {
|
| 925 | return Continuation.ABORT;
|
| 926 | }
|
| 927 | }
|
| 928 |
|
| 929 | return Continuation.CONTINUE;
|
| 930 | });
|
| 931 | }
|
| 932 |
|
| 933 | public List<? extends EnderDragon> getDragons() {
|
| 934 | return this.getEntities(EntityTypes.ENDER_DRAGON, LivingEntity::isAlive);
|
| 935 | }
|
| 936 |
|
| 937 | public List<ServerPlayer> getPlayers(final Predicate<? super ServerPlayer> selector) {
|
| 938 | return this.getPlayers(selector, Integer.MAX_VALUE);
|
| 939 | }
|
| 940 |
|
| 941 | public List<ServerPlayer> getPlayers(final Predicate<? super ServerPlayer> selector, final int maxResults) {
|
| 942 | List<ServerPlayer> result = Lists.newArrayList();
|
| 943 |
|
| 944 | for (ServerPlayer player : this.players) {
|
| 945 | if (selector.test(player)) {
|
| 946 | result.add(player);
|
| 947 | if (result.size() >= maxResults) {
|
| 948 | return result;
|
| 949 | }
|
| 950 | }
|
| 951 | }
|
| 952 |
|
| 953 | return result;
|
| 954 | }
|
| 955 |
|
| 956 | public @Nullable ServerPlayer getRandomPlayer() {
|
| 957 | List<ServerPlayer> players = this.getPlayers(LivingEntity::isAlive);
|
| 958 | return players.isEmpty() ? null : players.get(this.random.nextInt(players.size()));
|
| 959 | }
|
| 960 |
|
| 961 | @Override
|
| 962 | public boolean addFreshEntity(final Entity entity) {
|
| 963 | return this.addEntity(entity);
|
| 964 | }
|
| 965 |
|
| 966 | public boolean addWithUUID(final Entity entity) {
|
| 967 | return this.addEntity(entity);
|
| 968 | }
|
| 969 |
|
| 970 | public void addDuringTeleport(final Entity entity) {
|
| 971 | if (entity instanceof ServerPlayer player) {
|
| 972 | this.addPlayer(player);
|
| 973 | } else {
|
| 974 | this.addEntity(entity);
|
| 975 | }
|
| 976 | }
|
| 977 |
|
| 978 | public void addNewPlayer(final ServerPlayer player) {
|
| 979 | this.addPlayer(player);
|
| 980 | }
|
| 981 |
|
| 982 | public void addRespawnedPlayer(final ServerPlayer player) {
|
| 983 | this.addPlayer(player);
|
| 984 | }
|
| 985 |
|
| 986 | private void addPlayer(final ServerPlayer player) {
|
| 987 | Entity existing = this.getEntity(player.getUUID());
|
| 988 | if (existing != null) {
|
| 989 | LOGGER.warn("Force-added player with duplicate UUID {}", player.getUUID());
|
| 990 | existing.unRide();
|
| 991 | this.removePlayerImmediately((ServerPlayer)existing, Entity.RemovalReason.DISCARDED);
|
| 992 | }
|
| 993 |
|
| 994 | this.entityManager.addNewEntity(player);
|
| 995 | }
|
| 996 |
|
| 997 | private boolean addEntity(final Entity entity) {
|
| 998 | if (entity.isRemoved()) {
|
| 999 | LOGGER.warn("Tried to add entity {} but it was marked as removed already", entity.typeHolder().getRegisteredName());
|
| 1000 | return false;
|
| 1001 | } else {
|
| 1002 | return this.entityManager.addNewEntity(entity);
|
| 1003 | }
|
| 1004 | }
|
| 1005 |
|
| 1006 | public boolean tryAddFreshEntityWithPassengers(final Entity entity) {
|
| 1007 | if (entity.getSelfAndPassengers().map(Entity::getUUID).anyMatch(this.entityManager::isLoaded)) {
|
| 1008 | return false;
|
| 1009 | }
|
| 1010 |
|
| 1011 | this.addFreshEntityWithPassengers(entity);
|
| 1012 | return true;
|
| 1013 | }
|
| 1014 |
|
| 1015 | public void unload(final LevelChunk levelChunk) {
|
| 1016 | levelChunk.clearAllBlockEntities();
|
| 1017 | levelChunk.unregisterTickContainerFromLevel(this);
|
| 1018 | this.debugSynchronizers.dropChunk(levelChunk.getPos());
|
| 1019 | }
|
| 1020 |
|
| 1021 | public void removePlayerImmediately(final ServerPlayer player, final Entity.RemovalReason reason) {
|
| 1022 | player.remove(reason);
|
| 1023 | }
|
| 1024 |
|
| 1025 | @Override
|
| 1026 | public void destroyBlockProgress(final int id, final BlockPos blockPos, final int progress) {
|
| 1027 | for (ServerPlayer player : this.server.getPlayerList().getPlayers()) {
|
| 1028 | if (player.level() == this && player.getId() != id) {
|
| 1029 | double xd = blockPos.getX() - player.getX();
|
| 1030 | double yd = blockPos.getY() - player.getY();
|
| 1031 | double zd = blockPos.getZ() - player.getZ();
|
| 1032 | if (xd * xd + yd * yd + zd * zd < 1024.0) {
|
| 1033 | player.connection.send(new ClientboundBlockDestructionPacket(id, blockPos, progress));
|
| 1034 | }
|
| 1035 | }
|
| 1036 | }
|
| 1037 | }
|
| 1038 |
|
| 1039 | @Override
|
| 1040 | public void playSeededSound(
|
| 1041 | final @Nullable Entity except,
|
| 1042 | final double x,
|
| 1043 | final double y,
|
| 1044 | final double z,
|
| 1045 | final Holder<SoundEvent> sound,
|
| 1046 | final SoundSource source,
|
| 1047 | final float volume,
|
| 1048 | final float pitch,
|
| 1049 | final long seed
|
| 1050 | ) {
|
| 1051 | this.server
|
| 1052 | .getPlayerList()
|
| 1053 | .broadcast(
|
| 1054 | except instanceof Player player ? player : null,
|
| 1055 | x,
|
| 1056 | y,
|
| 1057 | z,
|
| 1058 | sound.value().getRange(volume),
|
| 1059 | this.dimension(),
|
| 1060 | new ClientboundSoundPacket(sound, source, x, y, z, volume, pitch, seed)
|
| 1061 | );
|
| 1062 | }
|
| 1063 |
|
| 1064 | @Override
|
| 1065 | public void playSeededSound(
|
| 1066 | final @Nullable Entity except,
|
| 1067 | final Entity sourceEntity,
|
| 1068 | final Holder<SoundEvent> sound,
|
| 1069 | final SoundSource source,
|
| 1070 | final float volume,
|
| 1071 | final float pitch,
|
| 1072 | final long seed
|
| 1073 | ) {
|
| 1074 | this.server
|
| 1075 | .getPlayerList()
|
| 1076 | .broadcast(
|
| 1077 | except instanceof Player player ? player : null,
|
| 1078 | sourceEntity.getX(),
|
| 1079 | sourceEntity.getY(),
|
| 1080 | sourceEntity.getZ(),
|
| 1081 | sound.value().getRange(volume),
|
| 1082 | this.dimension(),
|
| 1083 | new ClientboundSoundEntityPacket(sound, source, sourceEntity, volume, pitch, seed)
|
| 1084 | );
|
| 1085 | }
|
| 1086 |
|
| 1087 | @Override
|
| 1088 | public void globalLevelEvent(final @LevelEvent.Value int type, final BlockPos pos, final int data) {
|
| 1089 | if (this.getGameRules().get(GameRules.GLOBAL_SOUND_EVENTS)) {
|
| 1090 | this.server.getPlayerList().getPlayers().forEach(player -> {
|
| 1091 | Vec3 soundPos;
|
| 1092 | if (player.level() == this) {
|
| 1093 | Vec3 centerOfBlock = Vec3.atCenterOf(pos);
|
| 1094 | if (player.distanceToSqr(centerOfBlock) < Mth.square(32)) {
|
| 1095 | soundPos = centerOfBlock;
|
| 1096 | } else {
|
| 1097 | Vec3 directionToEvent = centerOfBlock.subtract(player.position()).normalize();
|
| 1098 | soundPos = player.position().add(directionToEvent.scale(32.0));
|
| 1099 | }
|
| 1100 | } else {
|
| 1101 | soundPos = player.position();
|
| 1102 | }
|
| 1103 |
|
| 1104 | player.connection.send(new ClientboundLevelEventPacket(type, BlockPos.containing(soundPos), data, true));
|
| 1105 | });
|
| 1106 | } else {
|
| 1107 | this.levelEvent(null, type, pos, data);
|
| 1108 | }
|
| 1109 | }
|
| 1110 |
|
| 1111 | @Override
|
| 1112 | public void levelEvent(final @Nullable Entity source, final @LevelEvent.Value int type, final BlockPos pos, final int data) {
|
| 1113 | this.server
|
| 1114 | .getPlayerList()
|
| 1115 | .broadcast(
|
| 1116 | source instanceof Player player ? player : null,
|
| 1117 | pos.getX(),
|
| 1118 | pos.getY(),
|
| 1119 | pos.getZ(),
|
| 1120 | 64.0,
|
| 1121 | this.dimension(),
|
| 1122 | new ClientboundLevelEventPacket(type, pos, data, false)
|
| 1123 | );
|
| 1124 | }
|
| 1125 |
|
| 1126 | public int getLogicalHeight() {
|
| 1127 | return this.dimensionType().logicalHeight();
|
| 1128 | }
|
| 1129 |
|
| 1130 | @Override
|
| 1131 | public void gameEvent(final Holder<GameEvent> gameEvent, final Vec3 position, final GameEvent.Context context) {
|
| 1132 | this.gameEventDispatcher.post(gameEvent, position, context);
|
| 1133 | }
|
| 1134 |
|
| 1135 | @Override
|
| 1136 | public void sendBlockUpdated(final BlockPos pos, final BlockState old, final BlockState current, final @Block.UpdateFlags int updateFlags) {
|
| 1137 | if (this.isUpdatingNavigations) {
|
| 1138 | String message = "recursive call to sendBlockUpdated";
|
| 1139 | Util.logAndPauseIfInIde("recursive call to sendBlockUpdated", new IllegalStateException("recursive call to sendBlockUpdated"));
|
| 1140 | }
|
| 1141 |
|
| 1142 | this.getChunkSource().blockChanged(pos);
|
| 1143 | this.pathTypesByPosCache.invalidate(pos);
|
| 1144 | VoxelShape oldShape = old.getCollisionShape(this, pos);
|
| 1145 | VoxelShape newShape = current.getCollisionShape(this, pos);
|
| 1146 | if (Shapes.joinIsNotEmpty(oldShape, newShape, BooleanOp.NOT_SAME)) {
|
| 1147 | List<PathNavigation> navigationsToUpdate = new ObjectArrayList<>();
|
| 1148 |
|
| 1149 | for (Mob navigatingMob : this.navigatingMobs) {
|
| 1150 | PathNavigation pathNavigation = navigatingMob.getNavigation();
|
| 1151 | if (pathNavigation.shouldRecomputePath(pos)) {
|
| 1152 | navigationsToUpdate.add(pathNavigation);
|
| 1153 | }
|
| 1154 | }
|
| 1155 |
|
| 1156 | try {
|
| 1157 | this.isUpdatingNavigations = true;
|
| 1158 |
|
| 1159 | for (PathNavigation navigation : navigationsToUpdate) {
|
| 1160 | navigation.recomputePath();
|
| 1161 | }
|
| 1162 | } finally {
|
| 1163 | this.isUpdatingNavigations = false;
|
| 1164 | }
|
| 1165 | }
|
| 1166 | }
|
| 1167 |
|
| 1168 | @Override
|
| 1169 | public void updateNeighborsAt(final BlockPos pos, final Block sourceBlock) {
|
| 1170 | this.updateNeighborsAt(pos, sourceBlock, ExperimentalRedstoneUtils.initialOrientation(this, null, null));
|
| 1171 | }
|
| 1172 |
|
| 1173 | @Override
|
| 1174 | public void updateNeighborsAt(final BlockPos pos, final Block sourceBlock, final @Nullable Orientation orientation) {
|
| 1175 | this.neighborUpdater.updateNeighborsAtExceptFromFacing(pos, sourceBlock, null, orientation);
|
| 1176 | }
|
| 1177 |
|
| 1178 | @Override
|
| 1179 | public void updateNeighborsAtExceptFromFacing(
|
| 1180 | final BlockPos pos, final Block blockObject, final Direction skipDirection, final @Nullable Orientation orientation
|
| 1181 | ) {
|
| 1182 | this.neighborUpdater.updateNeighborsAtExceptFromFacing(pos, blockObject, skipDirection, orientation);
|
| 1183 | }
|
| 1184 |
|
| 1185 | @Override
|
| 1186 | public void neighborChanged(final BlockPos pos, final Block changedBlock, final @Nullable Orientation orientation) {
|
| 1187 | this.neighborUpdater.neighborChanged(pos, changedBlock, orientation);
|
| 1188 | }
|
| 1189 |
|
| 1190 | @Override
|
| 1191 | public void neighborChanged(
|
| 1192 | final BlockState state, final BlockPos pos, final Block changedBlock, final @Nullable Orientation orientation, final boolean movedByPiston
|
| 1193 | ) {
|
| 1194 | this.neighborUpdater.neighborChanged(state, pos, changedBlock, orientation, movedByPiston);
|
| 1195 | }
|
| 1196 |
|
| 1197 | @Override
|
| 1198 | public void broadcastEntityEvent(final Entity entity, final @EntityEvent.Value byte event) {
|
| 1199 | this.getChunkSource().sendToTrackingPlayersAndSelf(entity, new ClientboundEntityEventPacket(entity, event));
|
| 1200 | }
|
| 1201 |
|
| 1202 | @Override
|
| 1203 | public void broadcastDamageEvent(final Entity entity, final DamageSource source) {
|
| 1204 | this.getChunkSource().sendToTrackingPlayersAndSelf(entity, new ClientboundDamageEventPacket(entity, source));
|
| 1205 | }
|
| 1206 |
|
| 1207 | public ServerChunkCache getChunkSource() {
|
| 1208 | return this.chunkSource;
|
| 1209 | }
|
| 1210 |
|
| 1211 | @Override
|
| 1212 | public void explode(
|
| 1213 | final @Nullable Entity source,
|
| 1214 | final @Nullable DamageSource damageSource,
|
| 1215 | final @Nullable ExplosionDamageCalculator damageCalculator,
|
| 1216 | final double x,
|
| 1217 | final double y,
|
| 1218 | final double z,
|
| 1219 | final float r,
|
| 1220 | final boolean fire,
|
| 1221 | final Level.ExplosionInteraction interactionType,
|
| 1222 | final ParticleOptions smallExplosionParticles,
|
| 1223 | final ParticleOptions largeExplosionParticles,
|
| 1224 | final WeightedList<ExplosionParticleInfo> blockParticles,
|
| 1225 | final Holder<SoundEvent> explosionSound
|
| 1226 | ) {
|
| 1227 | Explosion.BlockInteraction blockInteraction = switch (interactionType) {
|
| 1228 | case NONE -> Explosion.BlockInteraction.KEEP;
|
| 1229 | case BLOCK -> this.getDestroyType(GameRules.BLOCK_EXPLOSION_DROP_DECAY);
|
| 1230 | case MOB -> this.getGameRules().get(GameRules.MOB_GRIEFING) ? this.getDestroyType(GameRules.MOB_EXPLOSION_DROP_DECAY) : Explosion.BlockInteraction.KEEP;
|
| 1231 | case TNT -> this.getDestroyType(GameRules.TNT_EXPLOSION_DROP_DECAY);
|
| 1232 | case TRIGGER -> Explosion.BlockInteraction.TRIGGER_BLOCK;
|
| 1233 | };
|
| 1234 | Vec3 center = new Vec3(x, y, z);
|
| 1235 | ServerExplosion explosion = new ServerExplosion(this, source, damageSource, damageCalculator, center, r, fire, blockInteraction);
|
| 1236 | int blockCount = explosion.explode();
|
| 1237 | ParticleOptions explosionParticle = explosion.isSmall() ? smallExplosionParticles : largeExplosionParticles;
|
| 1238 |
|
| 1239 | for (ServerPlayer player : this.players) {
|
| 1240 | if (player.distanceToSqr(center) < 4096.0) {
|
| 1241 | Optional<Vec3> playerKnockback = Optional.ofNullable(explosion.getHitPlayers().get(player));
|
| 1242 | player.connection
|
| 1243 | .send(
|
| 1244 | new ClientboundExplodePacket(
|
| 1245 | center, r, blockCount, playerKnockback, explosionParticle, explosionSound, blockParticles, source == null || !source.isSilent()
|
| 1246 | )
|
| 1247 | );
|
| 1248 | }
|
| 1249 | }
|
| 1250 | }
|
| 1251 |
|
| 1252 | private Explosion.BlockInteraction getDestroyType(final GameRule<Boolean> gameRule) {
|
| 1253 | return this.getGameRules().get(gameRule) ? Explosion.BlockInteraction.DESTROY_WITH_DECAY : Explosion.BlockInteraction.DESTROY;
|
| 1254 | }
|
| 1255 |
|
| 1256 | @Override
|
| 1257 | public void blockEvent(final BlockPos pos, final Block block, final int b0, final int b1) {
|
| 1258 | this.blockEvents.add(new BlockEventData(pos, block, b0, b1));
|
| 1259 | }
|
| 1260 |
|
| 1261 | private void runBlockEvents() {
|
| 1262 | this.blockEventsToReschedule.clear();
|
| 1263 |
|
| 1264 | while (!this.blockEvents.isEmpty()) {
|
| 1265 | BlockEventData eventData = this.blockEvents.removeFirst();
|
| 1266 | if (this.shouldTickBlocksAt(eventData.pos())) {
|
| 1267 | if (this.doBlockEvent(eventData)) {
|
| 1268 | this.server
|
| 1269 | .getPlayerList()
|
| 1270 | .broadcast(
|
| 1271 | null,
|
| 1272 | eventData.pos().getX(),
|
| 1273 | eventData.pos().getY(),
|
| 1274 | eventData.pos().getZ(),
|
| 1275 | 64.0,
|
| 1276 | this.dimension(),
|
| 1277 | new ClientboundBlockEventPacket(eventData.pos(), eventData.block(), eventData.paramA(), eventData.paramB())
|
| 1278 | );
|
| 1279 | }
|
| 1280 | } else {
|
| 1281 | this.blockEventsToReschedule.add(eventData);
|
| 1282 | }
|
| 1283 | }
|
| 1284 |
|
| 1285 | this.blockEvents.addAll(this.blockEventsToReschedule);
|
| 1286 | }
|
| 1287 |
|
| 1288 | private boolean doBlockEvent(final BlockEventData eventData) {
|
| 1289 | BlockState state = this.getBlockState(eventData.pos());
|
| 1290 | return state.is(eventData.block()) ? state.triggerEvent(this, eventData.pos(), eventData.paramA(), eventData.paramB()) : false;
|
| 1291 | }
|
| 1292 |
|
| 1293 | public LevelTicks<Block> getBlockTicks() {
|
| 1294 | return this.blockTicks;
|
| 1295 | }
|
| 1296 |
|
| 1297 | public LevelTicks<Fluid> getFluidTicks() {
|
| 1298 | return this.fluidTicks;
|
| 1299 | }
|
| 1300 |
|
| 1301 | @Override
|
| 1302 | public MinecraftServer getServer() {
|
| 1303 | return this.server;
|
| 1304 | }
|
| 1305 |
|
| 1306 | public PortalForcer getPortalForcer() {
|
| 1307 | return this.portalForcer;
|
| 1308 | }
|
| 1309 |
|
| 1310 | public StructureTemplateManager getStructureTemplateManager() {
|
| 1311 | return this.server.getStructureTemplateManager();
|
| 1312 | }
|
| 1313 |
|
| 1314 | public <T extends ParticleOptions> int sendParticles(
|
| 1315 | final T particle,
|
| 1316 | final double x,
|
| 1317 | final double y,
|
| 1318 | final double z,
|
| 1319 | final int count,
|
| 1320 | final double xDist,
|
| 1321 | final double yDist,
|
| 1322 | final double zDist,
|
| 1323 | final double speed
|
| 1324 | ) {
|
| 1325 | return this.sendParticles(particle, false, false, x, y, z, count, xDist, yDist, zDist, speed, ClientboundLevelParticlesPacket.RandomizationType.DEFAULT);
|
| 1326 | }
|
| 1327 |
|
| 1328 | public <T extends ParticleOptions> int sendParticles(
|
| 1329 | final T particle,
|
| 1330 | final double x,
|
| 1331 | final double y,
|
| 1332 | final double z,
|
| 1333 | final int count,
|
| 1334 | final double xDist,
|
| 1335 | final double yDist,
|
| 1336 | final double zDist,
|
| 1337 | final double speed,
|
| 1338 | final ClientboundLevelParticlesPacket.RandomizationType randomizationType
|
| 1339 | ) {
|
| 1340 | return this.sendParticles(particle, false, false, x, y, z, count, xDist, yDist, zDist, speed, randomizationType);
|
| 1341 | }
|
| 1342 |
|
| 1343 | public <T extends ParticleOptions> int sendParticles(
|
| 1344 | final T particle,
|
| 1345 | final double x,
|
| 1346 | final double y,
|
| 1347 | final double z,
|
| 1348 | final int count,
|
| 1349 | final double xDist,
|
| 1350 | final double yDist,
|
| 1351 | final double zDist,
|
| 1352 | final double xSpeed,
|
| 1353 | final double ySpeed,
|
| 1354 | final double zSpeed
|
| 1355 | ) {
|
| 1356 | return this.sendParticles(
|
| 1357 | particle, false, false, x, y, z, count, xDist, yDist, zDist, xSpeed, ySpeed, zSpeed, ClientboundLevelParticlesPacket.RandomizationType.DEFAULT
|
| 1358 | );
|
| 1359 | }
|
| 1360 |
|
| 1361 | public <T extends ParticleOptions> int sendParticles(
|
| 1362 | final T particle,
|
| 1363 | final double x,
|
| 1364 | final double y,
|
| 1365 | final double z,
|
| 1366 | final int count,
|
| 1367 | final double xDist,
|
| 1368 | final double yDist,
|
| 1369 | final double zDist,
|
| 1370 | final double xSpeed,
|
| 1371 | final double ySpeed,
|
| 1372 | final double zSpeed,
|
| 1373 | final ClientboundLevelParticlesPacket.RandomizationType randomizationType
|
| 1374 | ) {
|
| 1375 | return this.sendParticles(particle, false, false, x, y, z, count, xDist, yDist, zDist, xSpeed, ySpeed, zSpeed, randomizationType);
|
| 1376 | }
|
| 1377 |
|
| 1378 | public <T extends ParticleOptions> int sendParticles(
|
| 1379 | final T particle,
|
| 1380 | final boolean overrideLimiter,
|
| 1381 | final boolean alwaysShow,
|
| 1382 | final double x,
|
| 1383 | final double y,
|
| 1384 | final double z,
|
| 1385 | final int count,
|
| 1386 | final double xDist,
|
| 1387 | final double yDist,
|
| 1388 | final double zDist,
|
| 1389 | final double speed
|
| 1390 | ) {
|
| 1391 | return this.sendParticles(
|
| 1392 | particle, overrideLimiter, alwaysShow, x, y, z, count, xDist, yDist, zDist, speed, speed, speed, ClientboundLevelParticlesPacket.RandomizationType.DEFAULT
|
| 1393 | );
|
| 1394 | }
|
| 1395 |
|
| 1396 | public <T extends ParticleOptions> int sendParticles(
|
| 1397 | final T particle,
|
| 1398 | final boolean overrideLimiter,
|
| 1399 | final boolean alwaysShow,
|
| 1400 | final double x,
|
| 1401 | final double y,
|
| 1402 | final double z,
|
| 1403 | final int count,
|
| 1404 | final double xDist,
|
| 1405 | final double yDist,
|
| 1406 | final double zDist,
|
| 1407 | final double speed,
|
| 1408 | final ClientboundLevelParticlesPacket.RandomizationType randomizationType
|
| 1409 | ) {
|
| 1410 | return this.sendParticles(particle, overrideLimiter, alwaysShow, x, y, z, count, xDist, yDist, zDist, speed, speed, speed, randomizationType);
|
| 1411 | }
|
| 1412 |
|
| 1413 | public <T extends ParticleOptions> int sendParticles(
|
| 1414 | final T particle,
|
| 1415 | final boolean overrideLimiter,
|
| 1416 | final boolean alwaysShow,
|
| 1417 | final double x,
|
| 1418 | final double y,
|
| 1419 | final double z,
|
| 1420 | final int count,
|
| 1421 | final double xDist,
|
| 1422 | final double yDist,
|
| 1423 | final double zDist,
|
| 1424 | final double xSpeed,
|
| 1425 | final double ySpeed,
|
| 1426 | final double zSpeed,
|
| 1427 | final ClientboundLevelParticlesPacket.RandomizationType randomizationType
|
| 1428 | ) {
|
| 1429 | ClientboundLevelParticlesPacket packet = new ClientboundLevelParticlesPacket(
|
| 1430 | particle,
|
| 1431 | overrideLimiter,
|
| 1432 | alwaysShow,
|
| 1433 | x,
|
| 1434 | y,
|
| 1435 | z,
|
| 1436 | (float)xDist,
|
| 1437 | (float)yDist,
|
| 1438 | (float)zDist,
|
| 1439 | (float)xSpeed,
|
| 1440 | (float)ySpeed,
|
| 1441 | (float)zSpeed,
|
| 1442 | count,
|
| 1443 | randomizationType
|
| 1444 | );
|
| 1445 | int result = 0;
|
| 1446 |
|
| 1447 | for (int i = 0; i < this.players.size(); i++) {
|
| 1448 | ServerPlayer player = this.players.get(i);
|
| 1449 | if (this.sendParticles(player, overrideLimiter, x, y, z, packet)) {
|
| 1450 | result++;
|
| 1451 | }
|
| 1452 | }
|
| 1453 |
|
| 1454 | return result;
|
| 1455 | }
|
| 1456 |
|
| 1457 | public <T extends ParticleOptions> boolean sendParticles(
|
| 1458 | final ServerPlayer player,
|
| 1459 | final T particle,
|
| 1460 | final boolean overrideLimiter,
|
| 1461 | final boolean alwaysShow,
|
| 1462 | final double x,
|
| 1463 | final double y,
|
| 1464 | final double z,
|
| 1465 | final int count,
|
| 1466 | final double xDist,
|
| 1467 | final double yDist,
|
| 1468 | final double zDist,
|
| 1469 | final double speed
|
| 1470 | ) {
|
| 1471 | Packet<?> packet = new ClientboundLevelParticlesPacket(
|
| 1472 | particle, overrideLimiter, alwaysShow, x, y, z, (float)xDist, (float)yDist, (float)zDist, (float)speed, count
|
| 1473 | );
|
| 1474 | return this.sendParticles(player, overrideLimiter, x, y, z, packet);
|
| 1475 | }
|
| 1476 |
|
| 1477 | /**
|
| 1478 | * Access widened by fabric-transitive-access-wideners-v1 to accessible
|
| 1479 | */
|
| 1480 | public final boolean sendParticles(
|
| 1481 | final ServerPlayer player, final boolean overrideLimiter, final double x, final double y, final double z, final Packet<?> packet
|
| 1482 | ) {
|
| 1483 | if (player.level() != this) {
|
| 1484 | return false;
|
| 1485 | } else {
|
| 1486 | BlockPos pos = player.blockPosition();
|
| 1487 | if (pos.closerToCenterThan(new Vec3(x, y, z), overrideLimiter ? 512.0 : 32.0)) {
|
| 1488 | player.connection.send(packet);
|
| 1489 | return true;
|
| 1490 | } else {
|
| 1491 | return false;
|
| 1492 | }
|
| 1493 | }
|
| 1494 | }
|
| 1495 |
|
| 1496 | @Override
|
| 1497 | public @Nullable Entity getEntity(final int id) {
|
| 1498 | return this.getEntities().get(id);
|
| 1499 | }
|
| 1500 |
|
| 1501 | @Override
|
| 1502 | public @Nullable Entity getEntityInAnyDimension(final UUID uuid) {
|
| 1503 | Entity entity = this.getEntity(uuid);
|
| 1504 | if (entity != null) {
|
| 1505 | return entity;
|
| 1506 | }
|
| 1507 |
|
| 1508 | for (ServerLevel otherLevel : this.getServer().getAllLevels()) {
|
| 1509 | if (otherLevel != this) {
|
| 1510 | Entity otherEntity = otherLevel.getEntity(uuid);
|
| 1511 | if (otherEntity != null) {
|
| 1512 | return otherEntity;
|
| 1513 | }
|
| 1514 | }
|
| 1515 | }
|
| 1516 |
|
| 1517 | return null;
|
| 1518 | }
|
| 1519 |
|
| 1520 | @Override
|
| 1521 | public @Nullable Player getPlayerInAnyDimension(final UUID uuid) {
|
| 1522 | return this.getServer().getPlayerList().getPlayer(uuid);
|
| 1523 | }
|
| 1524 |
|
| 1525 | @Deprecated
|
| 1526 | public @Nullable Entity getEntityOrPart(final int id) {
|
| 1527 | Entity entity = this.getEntities().get(id);
|
| 1528 | return entity != null ? entity : this.dragonParts.get(id);
|
| 1529 | }
|
| 1530 |
|
| 1531 | @Override
|
| 1532 | public Collection<EnderDragonPart> dragonParts() {
|
| 1533 | return this.dragonParts.values();
|
| 1534 | }
|
| 1535 |
|
| 1536 | public @Nullable BlockPos findNearestMapStructure(
|
| 1537 | final TagKey<Structure> structureTag, final BlockPos origin, final int maxSearchRadius, final boolean createReference
|
| 1538 | ) {
|
| 1539 | Optional<HolderSet.Named<Structure>> tag = this.registryAccess().lookupOrThrow(Registries.STRUCTURE).get(structureTag);
|
| 1540 | return tag.isEmpty() ? null : this.findNearestMapStructure(tag.get(), origin, maxSearchRadius, createReference);
|
| 1541 | }
|
| 1542 |
|
| 1543 | public @Nullable BlockPos findNearestMapStructure(
|
| 1544 | final HolderSet<Structure> structureTag, final BlockPos origin, final int maxSearchRadius, final boolean createReference
|
| 1545 | ) {
|
| 1546 | Pair<BlockPos, Holder<Structure>> result = this.getChunkSource()
|
| 1547 | .getGenerator()
|
| 1548 | .findNearestMapStructure(this, structureTag, origin, maxSearchRadius, createReference);
|
| 1549 | return result != null ? result.getFirst() : null;
|
| 1550 | }
|
| 1551 |
|
| 1552 | public @Nullable Pair<BlockPos, Holder<Biome>> findClosestBiome3d(
|
| 1553 | final Predicate<Holder<Biome>> biomeTest,
|
| 1554 | final BlockPos origin,
|
| 1555 | final int maxSearchRadius,
|
| 1556 | final int sampleResolutionHorizontal,
|
| 1557 | final int sampleResolutionVertical
|
| 1558 | ) {
|
| 1559 | return this.getChunkSource()
|
| 1560 | .getGenerator()
|
| 1561 | .getBiomeSource()
|
| 1562 | .findClosestBiome3d(origin, maxSearchRadius, sampleResolutionHorizontal, sampleResolutionVertical, biomeTest, this.getChunkSource().randomState(), this);
|
| 1563 | }
|
| 1564 |
|
| 1565 | @Override
|
| 1566 | public WorldBorder getWorldBorder() {
|
| 1567 | WorldBorder worldBorder = this.getDataStorage().computeIfAbsent(WorldBorder.TYPE);
|
| 1568 | worldBorder.applyInitialSettings(this.levelData.getGameTime());
|
| 1569 | return worldBorder;
|
| 1570 | }
|
| 1571 |
|
| 1572 | public RecipeManager recipeAccess() {
|
| 1573 | return this.server.getRecipeManager();
|
| 1574 | }
|
| 1575 |
|
| 1576 | @Override
|
| 1577 | public TickRateManager tickRateManager() {
|
| 1578 | return this.server.tickRateManager();
|
| 1579 | }
|
| 1580 |
|
| 1581 | @Override
|
| 1582 | public boolean noSave() {
|
| 1583 | return this.noSave;
|
| 1584 | }
|
| 1585 |
|
| 1586 | public SavedDataStorage getDataStorage() {
|
| 1587 | return this.getChunkSource().getDataStorage();
|
| 1588 | }
|
| 1589 |
|
| 1590 | @Override
|
| 1591 | public @Nullable MapItemSavedData getMapData(final MapId id) {
|
| 1592 | return this.getServer().getDataStorage().get(MapItemSavedData.type(id));
|
| 1593 | }
|
| 1594 |
|
| 1595 | public void setMapData(final MapId id, final MapItemSavedData data) {
|
| 1596 | this.getServer().getDataStorage().set(MapItemSavedData.type(id), data);
|
| 1597 | }
|
| 1598 |
|
| 1599 | public MapId getFreeMapId() {
|
| 1600 | return this.getServer().getDataStorage().computeIfAbsent(MapIndex.TYPE).getNextMapId();
|
| 1601 | }
|
| 1602 |
|
| 1603 | @Override
|
| 1604 | public void setRespawnData(final LevelData.RespawnData respawnData) {
|
| 1605 | this.getServer().setRespawnData(respawnData);
|
| 1606 | }
|
| 1607 |
|
| 1608 | @Override
|
| 1609 | public LevelData.RespawnData getRespawnData() {
|
| 1610 | return this.getServer().getRespawnData();
|
| 1611 | }
|
| 1612 |
|
| 1613 | public LongSet getForceLoadedChunks() {
|
| 1614 | return this.chunkSource.getForceLoadedChunks();
|
| 1615 | }
|
| 1616 |
|
| 1617 | public boolean setChunkForced(final int chunkX, final int chunkZ, final boolean forced) {
|
| 1618 | boolean updated = this.chunkSource.updateChunkForced(new ChunkPos(chunkX, chunkZ), forced);
|
| 1619 | if (forced && updated) {
|
| 1620 | this.getChunk(chunkX, chunkZ);
|
| 1621 | }
|
| 1622 |
|
| 1623 | return updated;
|
| 1624 | }
|
| 1625 |
|
| 1626 | @Override
|
| 1627 | public List<ServerPlayer> players() {
|
| 1628 | return this.players;
|
| 1629 | }
|
| 1630 |
|
| 1631 | @Override
|
| 1632 | public void updatePOIOnBlockStateChange(final BlockPos pos, final BlockState oldState, final BlockState newState) {
|
| 1633 | Optional<Holder<PoiType>> oldType = PoiTypes.forState(oldState);
|
| 1634 | Optional<Holder<PoiType>> newType = PoiTypes.forState(newState);
|
| 1635 | if (!Objects.equals(oldType, newType)) {
|
| 1636 | BlockPos immutable = pos.immutable();
|
| 1637 | oldType.ifPresent(poiType -> this.getServer().execute(() -> {
|
| 1638 | this.getPoiManager().remove(immutable);
|
| 1639 | this.debugSynchronizers.dropPoi(immutable);
|
| 1640 | }));
|
| 1641 | newType.ifPresent(poiType -> this.getServer().execute(() -> {
|
| 1642 | PoiRecord record = this.getPoiManager().add(immutable, (Holder<PoiType>)poiType);
|
| 1643 | if (record != null) {
|
| 1644 | this.debugSynchronizers.registerPoi(record);
|
| 1645 | }
|
| 1646 | }));
|
| 1647 | }
|
| 1648 | }
|
| 1649 |
|
| 1650 | public PoiManager getPoiManager() {
|
| 1651 | return this.getChunkSource().getPoiManager();
|
| 1652 | }
|
| 1653 |
|
| 1654 | public boolean isVillage(final BlockPos pos) {
|
| 1655 | return this.isCloseToVillage(pos, 1);
|
| 1656 | }
|
| 1657 |
|
| 1658 | public boolean isVillage(final SectionPos sectionPos) {
|
| 1659 | return this.isVillage(sectionPos.center());
|
| 1660 | }
|
| 1661 |
|
| 1662 | public boolean isCloseToVillage(final BlockPos pos, final int sectionDistance) {
|
| 1663 | return sectionDistance > 6 ? false : this.sectionsToVillage(SectionPos.of(pos)) <= sectionDistance;
|
| 1664 | }
|
| 1665 |
|
| 1666 | public int sectionsToVillage(final SectionPos pos) {
|
| 1667 | return this.getPoiManager().sectionsToVillage(pos);
|
| 1668 | }
|
| 1669 |
|
| 1670 | public Raids getRaids() {
|
| 1671 | return this.raids;
|
| 1672 | }
|
| 1673 |
|
| 1674 | public @Nullable Raid getRaidAt(final BlockPos pos) {
|
| 1675 | return this.raids.getNearbyRaid(pos, 9216);
|
| 1676 | }
|
| 1677 |
|
| 1678 | public boolean isRaided(final BlockPos pos) {
|
| 1679 | return this.getRaidAt(pos) != null;
|
| 1680 | }
|
| 1681 |
|
| 1682 | public void onReputationEvent(final ReputationEventType type, final Entity source, final ReputationEventHandler target) {
|
| 1683 | target.onReputationEventFrom(type, source);
|
| 1684 | }
|
| 1685 |
|
| 1686 | public void saveDebugReport(final Path rootDir) throws IOException {
|
| 1687 | ChunkMap chunkMap = this.getChunkSource().chunkMap;
|
| 1688 |
|
| 1689 | try (Writer output = Files.newBufferedWriter(rootDir.resolve("stats.txt"))) {
|
| 1690 | output.write(String.format(Locale.ROOT, "spawning_chunks: %d\n", chunkMap.getDistanceManager().getNaturalSpawnChunkCount()));
|
| 1691 | NaturalSpawner.SpawnState lastSpawnState = this.getChunkSource().getLastSpawnState();
|
| 1692 | if (lastSpawnState != null) {
|
| 1693 | for (Entry<MobCategory> entry : lastSpawnState.getMobCategoryCounts().object2IntEntrySet()) {
|
| 1694 | output.write(String.format(Locale.ROOT, "spawn_count.%s: %d\n", entry.getKey().getName(), entry.getIntValue()));
|
| 1695 | }
|
| 1696 | }
|
| 1697 |
|
| 1698 | output.write(String.format(Locale.ROOT, "entities: %s\n", this.entityManager.gatherStats()));
|
| 1699 | output.write(String.format(Locale.ROOT, "block_entity_tickers: %d\n", this.blockEntityTickers.size()));
|
| 1700 | output.write(String.format(Locale.ROOT, "block_ticks: %d\n", this.getBlockTicks().count()));
|
| 1701 | output.write(String.format(Locale.ROOT, "fluid_ticks: %d\n", this.getFluidTicks().count()));
|
| 1702 | output.write("distance_manager: " + chunkMap.getDistanceManager().getDebugStatus() + "\n");
|
| 1703 | output.write(String.format(Locale.ROOT, "pending_tasks: %d\n", this.getChunkSource().getPendingTasksCount()));
|
| 1704 | }
|
| 1705 |
|
| 1706 | CrashReport test = new CrashReport("Level dump", new Exception("dummy"));
|
| 1707 | this.fillReportDetails(test);
|
| 1708 |
|
| 1709 | try (Writer output = Files.newBufferedWriter(rootDir.resolve("example_crash.txt"))) {
|
| 1710 | output.write(test.getFriendlyReport(ReportType.TEST));
|
| 1711 | }
|
| 1712 |
|
| 1713 | Path chunks = rootDir.resolve("chunks.csv");
|
| 1714 |
|
| 1715 | try (Writer output = Files.newBufferedWriter(chunks)) {
|
| 1716 | chunkMap.dumpChunks(output);
|
| 1717 | }
|
| 1718 |
|
| 1719 | Path entityChunks = rootDir.resolve("entity_chunks.csv");
|
| 1720 |
|
| 1721 | try (Writer output = Files.newBufferedWriter(entityChunks)) {
|
| 1722 | this.entityManager.dumpSections(output);
|
| 1723 | }
|
| 1724 |
|
| 1725 | Path entities = rootDir.resolve("entities.csv");
|
| 1726 |
|
| 1727 | try (Writer output = Files.newBufferedWriter(entities)) {
|
| 1728 | dumpEntities(output, this.getEntities().getAll());
|
| 1729 | }
|
| 1730 |
|
| 1731 | Path blockEntities = rootDir.resolve("block_entities.csv");
|
| 1732 |
|
| 1733 | try (Writer output = Files.newBufferedWriter(blockEntities)) {
|
| 1734 | this.dumpBlockEntityTickers(output);
|
| 1735 | }
|
| 1736 | }
|
| 1737 |
|
| 1738 | private static void dumpEntities(final Writer output, final Iterable<Entity> entities) throws IOException {
|
| 1739 | CsvOutput csvOutput = CsvOutput.builder()
|
| 1740 | .addColumn("x")
|
| 1741 | .addColumn("y")
|
| 1742 | .addColumn("z")
|
| 1743 | .addColumn("uuid")
|
| 1744 | .addColumn("type")
|
| 1745 | .addColumn("alive")
|
| 1746 | .addColumn("display_name")
|
| 1747 | .addColumn("custom_name")
|
| 1748 | .build(output);
|
| 1749 |
|
| 1750 | for (Entity entity : entities) {
|
| 1751 | Component customName = entity.getCustomName();
|
| 1752 | Component displayName = entity.getDisplayName();
|
| 1753 | csvOutput.writeRow(
|
| 1754 | entity.getX(),
|
| 1755 | entity.getY(),
|
| 1756 | entity.getZ(),
|
| 1757 | entity.getUUID(),
|
| 1758 | entity.typeHolder().getRegisteredName(),
|
| 1759 | entity.isAlive(),
|
| 1760 | displayName.getString(),
|
| 1761 | customName != null ? customName.getString() : null
|
| 1762 | );
|
| 1763 | }
|
| 1764 | }
|
| 1765 |
|
| 1766 | private void dumpBlockEntityTickers(final Writer output) throws IOException {
|
| 1767 | CsvOutput csvOutput = CsvOutput.builder().addColumn("x").addColumn("y").addColumn("z").addColumn("type").build(output);
|
| 1768 |
|
| 1769 | for (TickingBlockEntity ticker : this.blockEntityTickers) {
|
| 1770 | BlockPos blockPos = ticker.getPos();
|
| 1771 | csvOutput.writeRow(blockPos.getX(), blockPos.getY(), blockPos.getZ(), ticker.getType());
|
| 1772 | }
|
| 1773 | }
|
| 1774 |
|
| 1775 | @VisibleForTesting
|
| 1776 | public void clearBlockEvents(final BoundingBox bb) {
|
| 1777 | this.blockEvents.removeIf(e -> bb.isInside(e.pos()));
|
| 1778 | }
|
| 1779 |
|
| 1780 | public Iterable<Entity> getAllEntities() {
|
| 1781 | return this.getEntities().getAll();
|
| 1782 | }
|
| 1783 |
|
| 1784 | @Override
|
| 1785 | public String toString() {
|
| 1786 | return "ServerLevel[" + this.serverLevelData.getLevelName() + "]";
|
| 1787 | }
|
| 1788 |
|
| 1789 | public boolean isFlat() {
|
| 1790 | return this.server.getWorldData().isFlatWorld();
|
| 1791 | }
|
| 1792 |
|
| 1793 | @Override
|
| 1794 | public long getSeed() {
|
| 1795 | return this.server.getWorldGenSettings().options().seed();
|
| 1796 | }
|
| 1797 |
|
| 1798 | public @Nullable EnderDragonFight getDragonFight() {
|
| 1799 | return this.dragonFight;
|
| 1800 | }
|
| 1801 |
|
| 1802 | public WeatherData getWeatherData() {
|
| 1803 | return this.server.getWeatherData();
|
| 1804 | }
|
| 1805 |
|
| 1806 | @Override
|
| 1807 | public ServerLevel getLevel() {
|
| 1808 | return this;
|
| 1809 | }
|
| 1810 |
|
| 1811 | @VisibleForTesting
|
| 1812 | public String getWatchdogStats() {
|
| 1813 | return String.format(
|
| 1814 | Locale.ROOT,
|
| 1815 | "players: %s, entities: %s [%s], block_entities: %d [%s], block_ticks: %d, fluid_ticks: %d, chunk_source: %s",
|
| 1816 | this.players.size(),
|
| 1817 | this.entityManager.gatherStats(),
|
| 1818 | getTypeCount(this.entityManager.getEntityGetter().getAll(), e -> e.typeHolder().getRegisteredName()),
|
| 1819 | this.blockEntityTickers.size(),
|
| 1820 | getTypeCount(this.blockEntityTickers, TickingBlockEntity::getType),
|
| 1821 | this.getBlockTicks().count(),
|
| 1822 | this.getFluidTicks().count(),
|
| 1823 | this.gatherChunkSourceStats()
|
| 1824 | );
|
| 1825 | }
|
| 1826 |
|
| 1827 | private static <T> String getTypeCount(final Iterable<T> values, final Function<T, String> typeGetter) {
|
| 1828 | try {
|
| 1829 | Object2IntOpenHashMap<String> countByType = new Object2IntOpenHashMap<>();
|
| 1830 |
|
| 1831 | for (T e : values) {
|
| 1832 | String type = typeGetter.apply(e);
|
| 1833 | countByType.addTo(type, 1);
|
| 1834 | }
|
| 1835 |
|
| 1836 | Comparator<Entry<String>> compareByCount = Comparator.comparingInt(Entry::getIntValue);
|
| 1837 | return countByType.object2IntEntrySet()
|
| 1838 | .stream()
|
| 1839 | .sorted(compareByCount.reversed())
|
| 1840 | .limit(5L)
|
| 1841 | .map(ex -> (String)ex.getKey() + ":" + ex.getIntValue())
|
| 1842 | .collect(Collectors.joining(","));
|
| 1843 | } catch (Exception e) {
|
| 1844 | return "";
|
| 1845 | }
|
| 1846 | }
|
| 1847 |
|
| 1848 | @Override
|
| 1849 | protected LevelEntityGetter<Entity> getEntities() {
|
| 1850 | return this.entityManager.getEntityGetter();
|
| 1851 | }
|
| 1852 |
|
| 1853 | public void addLegacyChunkEntities(final Stream<Entity> loaded) {
|
| 1854 | this.entityManager.addLegacyChunkEntities(loaded);
|
| 1855 | }
|
| 1856 |
|
| 1857 | public void addWorldGenChunkEntities(final Stream<Entity> loaded) {
|
| 1858 | this.entityManager.addWorldGenChunkEntities(loaded);
|
| 1859 | }
|
| 1860 |
|
| 1861 | public void startTickingChunk(final LevelChunk levelChunk) {
|
| 1862 | levelChunk.unpackTicks(this.getGameTime());
|
| 1863 | }
|
| 1864 |
|
| 1865 | public void onStructureStartsAvailable(final ChunkAccess chunk) {
|
| 1866 | this.server.execute(() -> this.structureCheck.onStructureLoad(chunk.getPos(), chunk.getAllStarts()));
|
| 1867 | }
|
| 1868 |
|
| 1869 | public PathTypeCache getPathTypeCache() {
|
| 1870 | return this.pathTypesByPosCache;
|
| 1871 | }
|
| 1872 |
|
| 1873 | public void waitForEntities(final ChunkPos centerChunk, final int radius) {
|
| 1874 | List<ChunkPos> chunks = ChunkPos.rangeClosed(centerChunk, radius).toList();
|
| 1875 | this.server.managedBlock(() -> {
|
| 1876 | this.entityManager.processPendingLoads();
|
| 1877 |
|
| 1878 | for (ChunkPos chunk : chunks) {
|
| 1879 | if (!this.areEntitiesLoaded(chunk.pack())) {
|
| 1880 | return false;
|
| 1881 | }
|
| 1882 | }
|
| 1883 |
|
| 1884 | return true;
|
| 1885 | });
|
| 1886 | }
|
| 1887 |
|
| 1888 | public boolean isSpawningMonsters() {
|
| 1889 | return this.getGameRules().get(GameRules.SPAWN_MOBS) && this.getGameRules().get(GameRules.SPAWN_MONSTERS);
|
| 1890 | }
|
| 1891 |
|
| 1892 | @Override
|
| 1893 | public void close() throws IOException {
|
| 1894 | super.close();
|
| 1895 | this.entityManager.close();
|
| 1896 | }
|
| 1897 |
|
| 1898 | @Override
|
| 1899 | public String gatherChunkSourceStats() {
|
| 1900 | return "Chunks[S] W: " + this.chunkSource.gatherStats() + " E: " + this.entityManager.gatherStats();
|
| 1901 | }
|
| 1902 |
|
| 1903 | public boolean areEntitiesLoaded(final long chunkKey) {
|
| 1904 | return this.entityManager.areEntitiesLoaded(chunkKey);
|
| 1905 | }
|
| 1906 |
|
| 1907 | public boolean isPositionTickingWithEntitiesLoaded(final long key) {
|
| 1908 | return this.areEntitiesLoaded(key) && this.chunkSource.isPositionTicking(key);
|
| 1909 | }
|
| 1910 |
|
| 1911 | public boolean isPositionEntityTicking(final BlockPos pos) {
|
| 1912 | return this.entityManager.canPositionTick(pos) && this.chunkSource.chunkMap.getDistanceManager().inEntityTickingRange(ChunkPos.pack(pos));
|
| 1913 | }
|
| 1914 |
|
| 1915 | public boolean areEntitiesActuallyLoadedAndTicking(final ChunkPos pos) {
|
| 1916 | return this.entityManager.isTicking(pos) && this.entityManager.areEntitiesLoaded(pos.pack());
|
| 1917 | }
|
| 1918 |
|
| 1919 | public boolean anyPlayerCloseEnoughForSpawning(final BlockPos pos) {
|
| 1920 | return this.anyPlayerCloseEnoughForSpawning(ChunkPos.containing(pos));
|
| 1921 | }
|
| 1922 |
|
| 1923 | public boolean anyPlayerCloseEnoughForSpawning(final ChunkPos pos) {
|
| 1924 | return this.chunkSource.chunkMap.anyPlayerCloseEnoughForSpawning(pos);
|
| 1925 | }
|
| 1926 |
|
| 1927 | public boolean canSpreadFireAround(final BlockPos pos) {
|
| 1928 | int spreadRadius = this.getGameRules().get(GameRules.FIRE_SPREAD_RADIUS_AROUND_PLAYER);
|
| 1929 | return spreadRadius == -1 || this.chunkSource.chunkMap.anyPlayerCloseEnoughTo(pos, spreadRadius);
|
| 1930 | }
|
| 1931 |
|
| 1932 | public boolean canSpawnEntitiesInChunk(final ChunkPos pos) {
|
| 1933 | return this.entityManager.canPositionTick(pos) && this.getWorldBorder().isWithinBounds(pos);
|
| 1934 | }
|
| 1935 |
|
| 1936 | @Override
|
| 1937 | public FeatureFlagSet enabledFeatures() {
|
| 1938 | return this.server.getWorldData().enabledFeatures();
|
| 1939 | }
|
| 1940 |
|
| 1941 | public GameRules getGameRules() {
|
| 1942 | return this.server.getGameRules();
|
| 1943 | }
|
| 1944 |
|
| 1945 | @Override
|
| 1946 | public CrashReportCategory fillReportDetails(final CrashReport report) {
|
| 1947 | CrashReportCategory category = super.fillReportDetails(report);
|
| 1948 | WeatherData weatherData = this.getWeatherData();
|
| 1949 | category.setDetail("Loaded entity count", () -> String.valueOf(this.entityManager.count()));
|
| 1950 | category.setDetail(
|
| 1951 | "Server weather",
|
| 1952 | () -> String.format(
|
| 1953 | Locale.ROOT,
|
| 1954 | "Rain time: %d (now: %b), thunder time: %d (now: %b)",
|
| 1955 | weatherData.getRainTime(),
|
| 1956 | this.isRaining(),
|
| 1957 | weatherData.getThunderTime(),
|
| 1958 | this.isThundering()
|
| 1959 | )
|
| 1960 | );
|
| 1961 | return category;
|
| 1962 | }
|
| 1963 |
|
| 1964 | @Override
|
| 1965 | public int getSeaLevel() {
|
| 1966 | return this.chunkSource.getGenerator().getSeaLevel();
|
| 1967 | }
|
| 1968 |
|
| 1969 | @Override
|
| 1970 | public void onBlockEntityAdded(final BlockEntity blockEntity) {
|
| 1971 | super.onBlockEntityAdded(blockEntity);
|
| 1972 | this.debugSynchronizers.registerBlockEntity(blockEntity);
|
| 1973 | }
|
| 1974 |
|
| 1975 | public LevelDebugSynchronizers debugSynchronizers() {
|
| 1976 | return this.debugSynchronizers;
|
| 1977 | }
|
| 1978 |
|
| 1979 | public boolean isAllowedToEnterPortal(final Level toLevel) {
|
| 1980 | return toLevel.dimension() == Level.NETHER ? this.getGameRules().get(GameRules.ALLOW_ENTERING_NETHER_USING_PORTALS) : true;
|
| 1981 | }
|
| 1982 |
|
| 1983 | public boolean isPvpAllowed() {
|
| 1984 | return this.getGameRules().get(GameRules.PVP);
|
| 1985 | }
|
| 1986 |
|
| 1987 | public boolean isCommandBlockEnabled() {
|
| 1988 | return this.getGameRules().get(GameRules.COMMAND_BLOCKS_WORK);
|
| 1989 | }
|
| 1990 |
|
| 1991 | public boolean isSpawnerBlockEnabled() {
|
| 1992 | return this.getGameRules().get(GameRules.SPAWNER_BLOCKS_WORK);
|
| 1993 | }
|
| 1994 |
|
| 1995 | private final class EntityCallbacks implements LevelCallback<Entity> {
|
| 1996 | public void onCreated(final Entity entity) {
|
| 1997 | if (entity instanceof WaypointTransmitter waypoint && waypoint.isTransmittingWaypoint()) {
|
| 1998 | ServerLevel.this.getWaypointManager().trackWaypoint(waypoint);
|
| 1999 | }
|
| 2000 | }
|
| 2001 |
|
| 2002 | public void onDestroyed(final Entity entity) {
|
| 2003 | if (entity instanceof WaypointTransmitter waypoint) {
|
| 2004 | ServerLevel.this.getWaypointManager().untrackWaypoint(waypoint);
|
| 2005 | }
|
| 2006 |
|
| 2007 | ServerLevel.this.getScoreboard().entityRemoved(entity);
|
| 2008 | }
|
| 2009 |
|
| 2010 | public void onTickingStart(final Entity entity) {
|
| 2011 | ServerLevel.this.entityTickList.add(entity);
|
| 2012 | }
|
| 2013 |
|
| 2014 | public void onTickingEnd(final Entity entity) {
|
| 2015 | ServerLevel.this.entityTickList.remove(entity);
|
| 2016 | }
|
| 2017 |
|
| 2018 | public void onTrackingStart(final Entity entity) {
|
| 2019 | ServerLevel.this.getChunkSource().addEntity(entity);
|
| 2020 | if (entity instanceof ServerPlayer player) {
|
| 2021 | ServerLevel.this.players.add(player);
|
| 2022 | if (player.isReceivingWaypoints()) {
|
| 2023 | ServerLevel.this.getWaypointManager().addPlayer(player);
|
| 2024 | }
|
| 2025 |
|
| 2026 | ServerLevel.this.updateSleepingPlayerList();
|
| 2027 | }
|
| 2028 |
|
| 2029 | if (entity instanceof WaypointTransmitter waypoint && waypoint.isTransmittingWaypoint()) {
|
| 2030 | ServerLevel.this.getWaypointManager().trackWaypoint(waypoint);
|
| 2031 | }
|
| 2032 |
|
| 2033 | if (entity instanceof Mob mob) {
|
| 2034 | if (ServerLevel.this.isUpdatingNavigations) {
|
| 2035 | String message = "onTrackingStart called during navigation iteration";
|
| 2036 | Util.logAndPauseIfInIde(
|
| 2037 | "onTrackingStart called during navigation iteration", new IllegalStateException("onTrackingStart called during navigation iteration")
|
| 2038 | );
|
| 2039 | }
|
| 2040 |
|
| 2041 | ServerLevel.this.navigatingMobs.add(mob);
|
| 2042 | }
|
| 2043 |
|
| 2044 | if (entity instanceof EnderDragon dragon) {
|
| 2045 | for (EnderDragonPart subEntity : dragon.getSubEntities()) {
|
| 2046 | ServerLevel.this.dragonParts.put(subEntity.getId(), subEntity);
|
| 2047 | }
|
| 2048 | }
|
| 2049 |
|
| 2050 | entity.updateDynamicGameEventListener(DynamicGameEventListener::add);
|
| 2051 | }
|
| 2052 |
|
| 2053 | public void onTrackingEnd(final Entity entity) {
|
| 2054 | ServerLevel.this.getChunkSource().removeEntity(entity);
|
| 2055 | if (entity instanceof ServerPlayer player) {
|
| 2056 | ServerLevel.this.players.remove(player);
|
| 2057 | ServerLevel.this.getWaypointManager().removePlayer(player);
|
| 2058 | ServerLevel.this.updateSleepingPlayerList();
|
| 2059 | }
|
| 2060 |
|
| 2061 | if (entity instanceof Mob mob) {
|
| 2062 | if (ServerLevel.this.isUpdatingNavigations) {
|
| 2063 | String message = "onTrackingStart called during navigation iteration";
|
| 2064 | Util.logAndPauseIfInIde(
|
| 2065 | "onTrackingStart called during navigation iteration", new IllegalStateException("onTrackingStart called during navigation iteration")
|
| 2066 | );
|
| 2067 | }
|
| 2068 |
|
| 2069 | ServerLevel.this.navigatingMobs.remove(mob);
|
| 2070 | }
|
| 2071 |
|
| 2072 | if (entity instanceof EnderDragon dragon) {
|
| 2073 | for (EnderDragonPart subEntity : dragon.getSubEntities()) {
|
| 2074 | ServerLevel.this.dragonParts.remove(subEntity.getId());
|
| 2075 | }
|
| 2076 | }
|
| 2077 |
|
| 2078 | entity.updateDynamicGameEventListener(DynamicGameEventListener::remove);
|
| 2079 | ServerLevel.this.debugSynchronizers.dropEntity(entity);
|
| 2080 | }
|
| 2081 |
|
| 2082 | public void onSectionChange(final Entity entity) {
|
| 2083 | entity.updateDynamicGameEventListener(DynamicGameEventListener::move);
|
| 2084 | }
|
| 2085 | }
|
| 2086 | }
|
| 2087 |
|