Skip to content

Commit

Permalink
Ensure seasons are synced correctly on server login. Closes #428
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 16, 2024
1 parent 0acb313 commit 9fddac5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
54 changes: 25 additions & 29 deletions src/main/java/sereneseasons/handler/season/SeasonHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,28 @@
import net.minecraft.resources.ResourceKey;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Mth;
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;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.EntityJoinLevelEvent;
import net.minecraftforge.event.level.LevelEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.LogicalSide;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.network.PacketDistributor;
import sereneseasons.api.SSGameRules;
import sereneseasons.api.season.ISeasonState;
import sereneseasons.api.season.Season;
import sereneseasons.api.season.SeasonChangedEvent;
import sereneseasons.api.season.SeasonHelper;
import sereneseasons.config.ServerConfig;
import sereneseasons.core.SereneSeasons;
import sereneseasons.handler.PacketHandler;
import sereneseasons.init.ModTags;
import sereneseasons.network.message.MessageSyncSeasonCycle;
Expand All @@ -39,13 +40,14 @@
import java.util.HashMap;
import java.util.function.Supplier;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE)
public class SeasonHandler implements SeasonHelper.ISeasonDataProvider
{
public static final HashMap<Level, Long> lastDayTimes = new HashMap<>();
public static final HashMap<Level, Integer> updateTicks = new HashMap<>();

@SubscribeEvent
public void onWorldTick(TickEvent.LevelTickEvent event)
public static void onWorldTick(TickEvent.LevelTickEvent event)
{
Level level = event.level;

Expand Down Expand Up @@ -83,48 +85,42 @@ public void onWorldTick(TickEvent.LevelTickEvent event)
updateTicks.put(level, ticks + 1);
savedData.setDirty();
}

@SubscribeEvent
public static void onWorldLoaded(LevelEvent.Load event)
{
clientSeasonCycleTicks.clear();
}

@SubscribeEvent
public void onPlayerLogin(PlayerEvent.PlayerLoggedInEvent event)
public static void onJoinLevel(EntityJoinLevelEvent event)
{
Player player = event.getEntity();
Level world = player.level();

sendSeasonUpdate(world);
if (!(event.getEntity() instanceof ServerPlayer))
return;

ServerPlayer player = (ServerPlayer)event.getEntity();
Level level = player.level();

SeasonSavedData savedData = getSeasonSavedData(level);
PacketHandler.HANDLER.send(new MessageSyncSeasonCycle(level.dimension(), savedData.seasonCycleTicks), PacketDistributor.PLAYER.with(player));
}

private Season.SubSeason lastSeason = null;
private static Season.SubSeason lastSeason = null;
public static final HashMap<ResourceKey<Level>, Integer> clientSeasonCycleTicks = new HashMap<>();
public static SeasonTime getClientSeasonTime() {
Integer i = clientSeasonCycleTicks.get(Minecraft.getInstance().level.dimension());
Integer i = clientSeasonCycleTicks.getOrDefault(Minecraft.getInstance().level.dimension(), 0);
return new SeasonTime(i == null ? 0 : i);
}

@SubscribeEvent
public void onClientTick(TickEvent.ClientTickEvent event)
public static void onClientTick(TickEvent.ClientTickEvent event)
{
Player player = Minecraft.getInstance().player;

//Only do this when in the world
if (Minecraft.getInstance().player == null) return;
ResourceKey<Level> dimension = Minecraft.getInstance().player.level().dimension();
if (player == null) return;
ResourceKey<Level> dimension = player.level().dimension();

if (event.phase == TickEvent.Phase.END && ServerConfig.isDimensionWhitelisted(dimension))
{
clientSeasonCycleTicks.compute(dimension, (k, v) -> v == null ? 0 : v + 1);

//Keep ticking as we're synchronized with the server only every second
if (clientSeasonCycleTicks.get(dimension) > SeasonTime.ZERO.getCycleDuration())
{
clientSeasonCycleTicks.put(dimension, 0);
}
clientSeasonCycleTicks.compute(dimension, (k, v) -> v == null ? 0 : (v + 1) % SeasonTime.ZERO.getCycleDuration());

SeasonTime calendar = new SeasonTime(clientSeasonCycleTicks.get(dimension));

if (calendar.getSubSeason() != lastSeason)
{
Minecraft.getInstance().levelRenderer.allChanged();
Expand All @@ -140,7 +136,7 @@ public static void sendSeasonUpdate(Level level)
if (level.isClientSide)
return;

SeasonSavedData savedData = getSeasonSavedData(level);
SeasonSavedData savedData = getSeasonSavedData(level);

// NOTE: The previous tick time is not necessary the current tick time - 1. This is why we have to store it in a map.
SeasonTime newTime = new SeasonTime(savedData.seasonCycleTicks);
Expand Down Expand Up @@ -213,7 +209,7 @@ public ISeasonState getServerSeasonState(Level world)
@Override
public ISeasonState getClientSeasonState()
{
Integer i = clientSeasonCycleTicks.get(Minecraft.getInstance().level.dimension());
Integer i = clientSeasonCycleTicks.getOrDefault(Minecraft.getInstance().level.dimension(), 0);
return new SeasonTime(i == null ? 0 : i);
}

Expand Down
3 changes: 0 additions & 3 deletions src/main/java/sereneseasons/init/ModHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ public static void init()
PacketHandler.init();

//Handlers for functionality related to seasons
MinecraftForge.EVENT_BUS.register(SEASON_HANDLER);
SeasonHelper.dataProvider = SEASON_HANDLER;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static void handle(final MessageSyncSeasonCycle packet, CustomPayloadEven

if (playerDimension.equals(packet.dimension))
{
SeasonHandler.clientSeasonCycleTicks.replace(playerDimension, packet.seasonCycleTicks);
SeasonHandler.clientSeasonCycleTicks.put(playerDimension, packet.seasonCycleTicks);
}
});
context.setPacketHandled(true);
Expand Down

0 comments on commit 9fddac5

Please sign in to comment.