Skip to content

Instantly share code, notes, and snippets.

@eatmyvenom
Created July 21, 2020 03:25
Show Gist options
  • Save eatmyvenom/63496c6503a190b636323e1a38e55778 to your computer and use it in GitHub Desktop.
Save eatmyvenom/63496c6503a190b636323e1a38e55778 to your computer and use it in GitHub Desktop.
public void tick(BooleanSupplier booleanSupplier) {
boolean hasAnythingToTick;
Profiler profiler = this.getProfiler();
this.inBlockTick = true;
// tick the world border
profiler.push("world border");
this.getWorldBorder().tick();
// tick the weather
profiler.swap("weather");
boolean wasRaining = this.isRaining();
if (this.getDimension().hasSkyLight()) {
if (this.getGameRules().getBoolean(GameRules.DO_WEATHER_CYCLE)) {
// set some variables for the next bits of code
int clearWeatherTime = this.field_24456.getClearWeatherTime();
int thunderTime = this.field_24456.getThunderTime();
int rainTime = this.field_24456.getRainTime();
boolean isThundering = this.properties.isThundering();
boolean isRaining = this.properties.isRaining();
// reset weather if needed
if (clearWeatherTime > 0) {
--clearWeatherTime;
thunderTime = isThundering ? 0 : 1;
rainTime = isRaining ? 0 : 1;
isThundering = false;
isRaining = false;
// reset thundering if needed
} else {
if (thunderTime > 0) {
if (--thunderTime == 0) {
isThundering = !isThundering;
}
} else {
thunderTime = isThundering ? this.random.nextInt(12000) + 3600 : this.random.nextInt(168000) + 12000;
}
//reset rain if needed
if (rainTime > 0) {
if (--rainTime == 0) {
isRaining = !isRaining;
}
} else {
rainTime = isRaining ? this.random.nextInt(12000) + 12000 : this.random.nextInt(168000) + 12000;
}
}
// set new weather data
this.field_24456.setThunderTime(thunderTime);
this.field_24456.setRainTime(rainTime);
this.field_24456.setClearWeatherTime(clearWeatherTime);
this.field_24456.setThundering(isThundering);
this.field_24456.setRaining(isRaining);
}
// set all gradients
// thunder gradient
this.thunderGradientPrev = this.thunderGradient;
this.thunderGradient = this.properties.isThundering() ? (float)((double)this.thunderGradient + 0.01) : (float)((double)this.thunderGradient - 0.01);
this.thunderGradient = MathHelper.clamp(this.thunderGradient, 0.0f, 1.0f);
// rain gradient
this.rainGradientPrev = this.rainGradient;
this.rainGradient = this.properties.isRaining() ? (float)((double)this.rainGradient + 0.01) : (float)((double)this.rainGradient - 0.01);
this.rainGradient = MathHelper.clamp(this.rainGradient, 0.0f, 1.0f);
}
// check and send changes to the client
if (this.rainGradientPrev != this.rainGradient) {
// send packet for rain changes
this.server.getPlayerManager().sendToDimension(new GameStateChangeS2CPacket(GameStateChangeS2CPacket.RAIN_GRADIENT_CHANGED, this.rainGradient), this.getRegistryKey());
}
if (this.thunderGradientPrev != this.thunderGradient) {
// send packet for thunder changes
this.server.getPlayerManager().sendToDimension(new GameStateChangeS2CPacket(GameStateChangeS2CPacket.THUNDER_GRADIENT_CHANGED, this.thunderGradient), this.getRegistryKey());
}
if (wasRaining != this.isRaining()) {
// send packets to indicate changes to all weather data
if (wasRaining) {
this.server.getPlayerManager().sendToAll(new GameStateChangeS2CPacket(GameStateChangeS2CPacket.RAIN_STOPPED, 0.0f));
} else {
this.server.getPlayerManager().sendToAll(new GameStateChangeS2CPacket(GameStateChangeS2CPacket.RAIN_STARTED, 0.0f));
}
this.server.getPlayerManager().sendToAll(new GameStateChangeS2CPacket(GameStateChangeS2CPacket.RAIN_GRADIENT_CHANGED, this.rainGradient));
this.server.getPlayerManager().sendToAll(new GameStateChangeS2CPacket(GameStateChangeS2CPacket.THUNDER_GRADIENT_CHANGED, this.thunderGradient));
}
// calculate weather stopping due to player sleeping
if (this.allPlayersSleeping && this.players.stream().noneMatch(arg -> !arg.isSpectator() && !arg.isSleepingLongEnough())) {
this.allPlayersSleeping = false;
if (this.getGameRules().getBoolean(GameRules.DO_DAYLIGHT_CYCLE)) {
// some shit not important
long l = this.properties.getTimeOfDay() + 24000L;
this.method_29199(l - l % 24000L);
}
// wake up player if it reset weather
this.wakeSleepingPlayers();
// check if the weather cycle gets ticked
if (this.getGameRules().getBoolean(GameRules.DO_WEATHER_CYCLE)) {
// do the weather reset
this.resetWeather();
}
}
// set the skylight level for the world because of night mobspawning
this.calculateAmbientDarkness();
// advance world time
this.tickTime();
// tick chunks
profiler.swap("chunkSource");
this.getChunkManager().tick(booleanSupplier);
// tick blocks then fluids
profiler.swap("tickPending");
if (!this.isDebugWorld()) {
// do block ticks
this.blockTickScheduler.tick();
// do fluid ticks
this.fluidTickScheduler.tick();
}
// tick raids and wandering traders
profiler.swap("raid");
this.raidManager.tick();
// process all block events
profiler.swap("blockEvents");
this.processSyncedBlockEvents();
this.inBlockTick = false;
// tick all entities
profiler.swap("entities");
hasAnythingToTick = !this.players.isEmpty() || !this.getForcedChunks().isEmpty();
if (hasAnythingToTick) {
this.resetIdleTimeout();
}
// make sure there is stuff to do on the server
if (hasAnythingToTick || this.idleTimeout++ < 300) {
Entity entityRequiredForLoad;
// tick the dragon fight stages and similar
if (this.enderDragonFight != null) {
this.enderDragonFight.tick();
}
this.inEntityTick = true;
// get all entites to tick
ObjectIterator entityIterator = this.entitiesById.int2ObjectEntrySet().iterator();
// loop through entites and tick them
while (entityIterator.hasNext()) {
// get current entry in the list being iterated
Int2ObjectMap.Entry currentIteratorEntry = (Int2ObjectMap.Entry)entityIterator.next();
// get the entity to be ticked
Entity currentEntity = (Entity)currentIteratorEntry.getValue();
// get the vehicle of the entity to be ticked
Entity currentEntitiesVehicle = currentEntity.getVehicle();
// remove entity if it shouldn't spawn
if (!this.server.shouldSpawnAnimals() && (currentEntity instanceof AnimalEntity || currentEntity instanceof WaterCreatureEntity)) {
currentEntity.remove();
}
if (!this.server.shouldSpawnNpcs() && currentEntity instanceof Npc) {
currentEntity.remove();
}
// check if entity should be despawned this tick
profiler.push("checkDespawn");
if (!currentEntity.removed) {
currentEntity.checkDespawn();
}
profiler.pop();
// dismount entity if it just had vehicle broken
if (currentEntitiesVehicle != null) {
if (!currentEntitiesVehicle.removed && currentEntitiesVehicle.hasPassenger(currentEntity)) continue;
currentEntity.stopRiding();
}
// tick the actual entity as long as it isn't about to
// despawn and isn't a part of the ender dragon
profiler.push("tick");
if (!currentEntity.removed && !(currentEntity instanceof EnderDragonPart)) {
this.tickEntity(this::tickEntity, currentEntity);
}
profiler.pop();
// remove entity from world (despawn)
profiler.push("remove");
if (currentEntity.removed) {
this.removeEntityFromChunk(currentEntity);
// doesnt remove the actual iterator but rather the current entry so it no longer is ticked
entityIterator.remove();
this.unloadEntity(currentEntity);
}
profiler.pop();
}
this.inEntityTick = false;
// tick special case entites for some reason
while ((entityRequiredForLoad = this.entitiesToLoad.poll()) != null) {
this.loadEntityUnchecked(entityRequiredForLoad);
}
// tick all block entites
this.tickBlockEntities();
}
profiler.pop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment