From 0acb3131c4d7442bb42e1eee02c752e1a612b8c9 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Tue, 16 Jan 2024 09:57:34 +1100 Subject: [PATCH] Combine standard season ticking and TimeSkipHandler --- .../block/SeasonSensorBlock.java | 2 +- .../entity}/SeasonSensorBlockEntity.java | 2 +- .../handler/season/SeasonHandler.java | 70 +++++++++++-------- .../handler/season/TimeSkipHandler.java | 63 ----------------- .../java/sereneseasons/init/ModBlocks.java | 2 +- 5 files changed, 42 insertions(+), 97 deletions(-) rename src/main/java/sereneseasons/{tileentity => block/entity}/SeasonSensorBlockEntity.java (94%) delete mode 100644 src/main/java/sereneseasons/handler/season/TimeSkipHandler.java diff --git a/src/main/java/sereneseasons/block/SeasonSensorBlock.java b/src/main/java/sereneseasons/block/SeasonSensorBlock.java index 96127b03..a29035c6 100644 --- a/src/main/java/sereneseasons/block/SeasonSensorBlock.java +++ b/src/main/java/sereneseasons/block/SeasonSensorBlock.java @@ -27,7 +27,7 @@ import sereneseasons.api.season.SeasonHelper; import sereneseasons.config.ServerConfig; import sereneseasons.season.SeasonTime; -import sereneseasons.tileentity.SeasonSensorBlockEntity; +import sereneseasons.block.entity.SeasonSensorBlockEntity; import javax.annotation.Nullable; diff --git a/src/main/java/sereneseasons/tileentity/SeasonSensorBlockEntity.java b/src/main/java/sereneseasons/block/entity/SeasonSensorBlockEntity.java similarity index 94% rename from src/main/java/sereneseasons/tileentity/SeasonSensorBlockEntity.java rename to src/main/java/sereneseasons/block/entity/SeasonSensorBlockEntity.java index 6a5f3160..aba21c05 100644 --- a/src/main/java/sereneseasons/tileentity/SeasonSensorBlockEntity.java +++ b/src/main/java/sereneseasons/block/entity/SeasonSensorBlockEntity.java @@ -2,7 +2,7 @@ * Copyright 2021, the Glitchfiend Team. * All rights reserved. ******************************************************************************/ -package sereneseasons.tileentity; +package sereneseasons.block.entity; import net.minecraft.core.BlockPos; import net.minecraft.world.level.block.state.BlockState; diff --git a/src/main/java/sereneseasons/handler/season/SeasonHandler.java b/src/main/java/sereneseasons/handler/season/SeasonHandler.java index f2723068..1c4f42f3 100644 --- a/src/main/java/sereneseasons/handler/season/SeasonHandler.java +++ b/src/main/java/sereneseasons/handler/season/SeasonHandler.java @@ -13,6 +13,7 @@ import net.minecraft.util.datafix.DataFixTypes; import net.minecraft.world.entity.player.Player; import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelAccessor; import net.minecraft.world.level.biome.Biome; import net.minecraft.world.level.saveddata.SavedData; import net.minecraft.world.level.storage.DimensionDataStorage; @@ -21,6 +22,7 @@ import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.level.LevelEvent; import net.minecraftforge.eventbus.api.SubscribeEvent; +import net.minecraftforge.fml.LogicalSide; import net.minecraftforge.network.PacketDistributor; import sereneseasons.api.SSGameRules; import sereneseasons.api.season.ISeasonState; @@ -39,41 +41,53 @@ public class SeasonHandler implements SeasonHelper.ISeasonDataProvider { + public static final HashMap lastDayTimes = new HashMap<>(); + public static final HashMap updateTicks = new HashMap<>(); + @SubscribeEvent public void onWorldTick(TickEvent.LevelTickEvent event) { - Level world = event.level; + Level level = event.level; - if (event.phase == TickEvent.Phase.END && !world.isClientSide) - { - if (!ServerConfig.progressSeasonWhileOffline.get()) - { - MinecraftServer server = world.getServer(); - if (server != null && server.getPlayerList().getPlayerCount() == 0) - return; - } + if (event.phase != TickEvent.Phase.START || level.isClientSide() || !ServerConfig.isDimensionWhitelisted(level.dimension())) + return; + + long dayTime = level.getDayTime(); + long lastDayTime = lastDayTimes.getOrDefault(level, dayTime); + lastDayTimes.put(level, dayTime); + + // Only tick seasons if the game rule is enabled + if (!level.getGameRules().getBoolean(SSGameRules.RULE_DOSEASONCYCLE)) + return; - // Only tick seasons if the game rule is enabled - if (!world.getGameRules().getBoolean(SSGameRules.RULE_DOSEASONCYCLE)) + if (!ServerConfig.progressSeasonWhileOffline.get()) + { + MinecraftServer server = level.getServer(); + if (server != null && server.getPlayerList().getPlayerCount() == 0) return; - - SeasonSavedData savedData = getSeasonSavedData(world); + } - // Clamp season cycle ticks to prevent a bad state occurring - savedData.seasonCycleTicks = Mth.clamp(savedData.seasonCycleTicks, 0, SeasonTime.ZERO.getCycleDuration()); + long difference = dayTime - lastDayTime; + if (difference == 0) + return; - if (++savedData.seasonCycleTicks > SeasonTime.ZERO.getCycleDuration()) - { - savedData.seasonCycleTicks = 0; - } - - if (savedData.seasonCycleTicks % 20 == 0) - { - sendSeasonUpdate(world); - } + SeasonSavedData savedData = getSeasonSavedData(level); + savedData.seasonCycleTicks = Mth.positiveModulo(savedData.seasonCycleTicks + (int)difference, SeasonTime.ZERO.getCycleDuration()); - savedData.setDirty(); + int ticks = updateTicks.getOrDefault(level, 0); + if (ticks >= 20) + { + sendSeasonUpdate(level); + ticks %= 20; } + updateTicks.put(level, ticks + 1); + savedData.setDirty(); + } + + @SubscribeEvent + public static void onWorldLoaded(LevelEvent.Load event) + { + clientSeasonCycleTicks.clear(); } @SubscribeEvent @@ -92,12 +106,6 @@ public static SeasonTime getClientSeasonTime() { return new SeasonTime(i == null ? 0 : i); } - @SubscribeEvent - public void onWorldLoaded(LevelEvent.Load event) - { - clientSeasonCycleTicks.clear(); - } - @SubscribeEvent public void onClientTick(TickEvent.ClientTickEvent event) { diff --git a/src/main/java/sereneseasons/handler/season/TimeSkipHandler.java b/src/main/java/sereneseasons/handler/season/TimeSkipHandler.java deleted file mode 100644 index 9e25e014..00000000 --- a/src/main/java/sereneseasons/handler/season/TimeSkipHandler.java +++ /dev/null @@ -1,63 +0,0 @@ -/******************************************************************************* - * Copyright 2021, the Glitchfiend Team. - * All rights reserved. - ******************************************************************************/ -package sereneseasons.handler.season; - -import net.minecraft.resources.ResourceKey; -import net.minecraft.server.level.ServerLevel; -import net.minecraft.world.level.Level; -import net.minecraftforge.event.TickEvent; -import net.minecraftforge.event.level.LevelEvent; -import net.minecraftforge.eventbus.api.SubscribeEvent; -import net.minecraftforge.fml.LogicalSide; -import net.minecraftforge.fml.common.Mod; -import sereneseasons.season.SeasonSavedData; - -import java.util.HashMap; - -@Mod.EventBusSubscriber -public class TimeSkipHandler -{ - public static final HashMap, Long> lastDayTimes = new HashMap<>(); - - @SubscribeEvent - public static void onWorldLoaded(LevelEvent.Load event) - { - lastDayTimes.clear(); - } - - @SubscribeEvent - public static void onWorldTick(TickEvent.LevelTickEvent event) - { - if (event.phase == TickEvent.Phase.START && event.side == LogicalSide.SERVER) - { - ServerLevel world = (ServerLevel)event.level; - long dayTime = world.getLevelData().getDayTime(); - - if (!lastDayTimes.containsKey(world.dimension())) - lastDayTimes.put(world.dimension(), dayTime); - - long lastDayTime = lastDayTimes.get(world.dimension()); - long difference = dayTime - lastDayTime; - - if (difference < 0) - { - difference += 24000L; - } - - // Time has skipped, skip the season time too - if (difference > 1) - { - SeasonSavedData seasonData = SeasonHandler.getSeasonSavedData(world); - seasonData.seasonCycleTicks += difference; - seasonData.setDirty(); - SeasonHandler.sendSeasonUpdate(world); - // Really this should be uncommented, but apparently other mods do bullshit things that cause this to get spammed. - // SereneSeasons.LOGGER.info("Season time skipped by " + difference + " in " + world.dimension().location().toString()); - } - - lastDayTimes.put(world.dimension(), dayTime); - } - } -} diff --git a/src/main/java/sereneseasons/init/ModBlocks.java b/src/main/java/sereneseasons/init/ModBlocks.java index 4a0a4751..d01c4332 100644 --- a/src/main/java/sereneseasons/init/ModBlocks.java +++ b/src/main/java/sereneseasons/init/ModBlocks.java @@ -10,7 +10,7 @@ import sereneseasons.api.SSBlocks; import sereneseasons.block.SeasonSensorBlock; import sereneseasons.core.SereneSeasons; -import sereneseasons.tileentity.SeasonSensorBlockEntity; +import sereneseasons.block.entity.SeasonSensorBlockEntity; import java.util.List; import java.util.function.Supplier;