Skip to content

Commit

Permalink
Combine standard season ticking and TimeSkipHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 15, 2024
1 parent 6c7800a commit 0acb313
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 97 deletions.
2 changes: 1 addition & 1 deletion src/main/java/sereneseasons/block/SeasonSensorBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
70 changes: 39 additions & 31 deletions src/main/java/sereneseasons/handler/season/SeasonHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -39,41 +41,53 @@

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)
{
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
Expand All @@ -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)
{
Expand Down
63 changes: 0 additions & 63 deletions src/main/java/sereneseasons/handler/season/TimeSkipHandler.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/sereneseasons/init/ModBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 0acb313

Please sign in to comment.