Skip to content

Commit c5f6ae1

Browse files
committed
Fix maxLoops bug and removed check for recording file
Refactored TimeLoop.init() Fixed startTimeOfDay Add client side check for sleeping so the event doesn't trigger twice Bump version to 1.7.9
1 parent 68ca926 commit c5f6ae1

9 files changed

Lines changed: 31 additions & 44 deletions

File tree

common/src/main/java/com/vltno/timeloop/TimeLoop.java

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@
44
import com.google.gson.JsonObject;
55
import com.google.gson.JsonParser;
66
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7+
import net.minecraft.ChatFormatting;
78
import net.minecraft.commands.CommandSourceStack;
89
import net.minecraft.commands.Commands;
910
import net.minecraft.core.BlockPos;
1011
import net.minecraft.core.HolderLookup;
1112
import net.minecraft.nbt.CompoundTag;
1213
import net.minecraft.nbt.ListTag;
14+
import net.minecraft.network.chat.Component;
15+
import net.minecraft.network.chat.Style;
1316
import net.minecraft.server.MinecraftServer;
1417
import net.minecraft.server.level.ServerLevel;
18+
import net.minecraft.server.level.ServerPlayer;
1519
import net.minecraft.world.entity.player.Player;
1620
import net.minecraft.world.item.ItemStack;
21+
import net.minecraft.world.level.storage.LevelResource;
1722
import net.minecraft.world.phys.Vec3;
1823
import org.slf4j.Logger;
1924
import org.slf4j.LoggerFactory;
@@ -60,11 +65,7 @@ public class TimeLoop {
6065
// Get the world folder path for config/recording loading
6166
public static Path worldFolder;
6267

63-
public static boolean isDedicatedServer;
64-
65-
public static void init(boolean isDedicatedServer) {
66-
TimeLoop.isDedicatedServer = isDedicatedServer;
67-
68+
public static void init() {
6869
loopBossBar = new LoopBossBar();
6970

7071
LOOP_LOGGER.info("Initializing TimeLoop mod (Common)");
@@ -102,8 +103,13 @@ public static void executeCommand(String command) {
102103
* Runs the next iteration of the loop.
103104
*/
104105
public static void runLoopIteration() {
105-
if (loopIteration + 1 >= maxLoops) {
106+
if (!isLooping) {
107+
LOOP_LOGGER.warn("Tried to iterate but not looping!");
108+
return;
109+
}
110+
if (loopIteration + 1 >= maxLoops && maxLoops != 0) {
106111
stopLoop();
112+
return;
107113
}
108114
LOOP_LOGGER.info("Starting iteration {} of loop", loopIteration);
109115
saveRecordings();
@@ -152,7 +158,6 @@ public static void runLoopIteration() {
152158
player.teleportTo(spawnPosition.getX(), spawnPosition.getY(), spawnPosition.getZ());
153159
}
154160
}
155-
156161
String playerSceneName = loopSceneManager.getPlayerSceneName(playerName);
157162
TimeLoop.executeCommand(String.format("mocap playback start .%s %s skin_from_player %s", playerSceneName, playerNickname, playerSkin));
158163
});
@@ -186,7 +191,8 @@ public static void startLoop() {
186191

187192
playerData.setStartPosition(server.getPlayerList().getPlayerByName(playerName).position());
188193
});
189-
194+
195+
startTimeOfDay = serverLevel.getDayTime();
190196
isLooping = true;
191197
config.isLooping = true;
192198
tickCounter = 0;
@@ -220,9 +226,7 @@ public static void saveRecordings() {
220226
LOOP_LOGGER.info("Processing recording for player: {}", playerName);
221227
executeCommand(String.format("mocap recording stop -+mc.%s.1", playerName));
222228
executeCommand(String.format("mocap recording save %s -+mc.%s.1", recordingName.toLowerCase(), playerName));
223-
if (recordingFileExists(recordingName)) {
224-
executeCommand(String.format("mocap scenes add_to %s %s", playerSceneName, recordingName.toLowerCase()));
225-
}
229+
executeCommand(String.format("mocap scenes add_to %s %s", playerSceneName, recordingName.toLowerCase()));
226230
});
227231
}
228232

@@ -244,25 +248,6 @@ public static void stopLoop() {
244248
}
245249
}
246250

247-
/**
248-
* Checks if a recording file with the specified name exists in the predefined recordings directory.
249-
* The method returns a boolean indicating the existence of the file.
250-
*
251-
* @param recordingName The name of the recording file to check, without the file extension.
252-
* @return true if the recording file exists, false otherwise.
253-
*/
254-
private static boolean recordingFileExists(String recordingName) {
255-
// Build the complete path for the recording directory using the absolute world path
256-
Path recordingDir = worldFolder.resolve("mocap_files").resolve("recordings");
257-
Path recordingFile = recordingDir.resolve(recordingName.toLowerCase() + ".mcmocap_rec");
258-
259-
boolean exists = recordingFile.toFile().exists();
260-
if (!exists) {
261-
LOOP_LOGGER.error("Expected recording file does not exist: {}", recordingFile.toAbsolutePath());
262-
}
263-
return exists;
264-
}
265-
266251
public static void modifyPlayerAttributes(String targetPlayerName, String newPlayerNickname, String newSkin) {
267252
String playerSceneName = loopSceneManager.getPlayerSceneName(targetPlayerName);
268253
executeCommand(String.format("mocap scenes modify .%s %s player_skin skin_from_player %s", playerSceneName, newPlayerNickname, newSkin));

common/src/main/java/com/vltno/timeloop/events/EntitySleepEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
public class EntitySleepEvent {
99
public static void onStopSleeping(LivingEntity entity) {
10+
if (entity.level().isClientSide()) { return; }
1011
if ((entity.getType() == EntityType.PLAYER) && (TimeLoop.loopType == LoopTypes.SLEEP) ) {
1112
TimeLoop.LOOP_LOGGER.info("Player slept, looping.");
1213
TimeLoop.runLoopIteration();

common/src/main/java/com/vltno/timeloop/events/LifecycleEvent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static void onServerStopping()
6767
TimeLoop.stopLoop();
6868
TimeLoop.config.isLooping = true;
6969

70-
if(TimeLoop.worldFolder != null) {
70+
if (TimeLoop.worldFolder != null) {
7171
TimeLoop.config.save();
7272
}
7373
} else {

common/src/main/java/com/vltno/timeloop/events/PlayerEvent.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
public class PlayerEvent {
88
public static void afterRespawn() {
9-
if (TimeLoop.loopType != LoopTypes.DEATH) { return; }
10-
TimeLoop.runLoopIteration();
9+
if (TimeLoop.loopType == LoopTypes.DEATH) {
10+
TimeLoop.LOOP_LOGGER.info("RESPAWNED!!!!");
11+
TimeLoop.runLoopIteration();
12+
}
1113
}
1214
}

fabric/src/main/java/com/vltno/timeloop/fabric/TimeLoopFabric.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22

33
import com.vltno.timeloop.*;
44
import com.vltno.timeloop.fabric.events.*;
5-
import net.fabricmc.api.EnvType;
65
import net.fabricmc.api.ModInitializer;
76
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
87
import net.fabricmc.fabric.api.entity.event.v1.EntitySleepEvents;
98
import net.fabricmc.fabric.api.entity.event.v1.ServerPlayerEvents;
109
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
1110
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
1211
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
13-
import net.fabricmc.loader.api.FabricLoader;
1412
import org.slf4j.Logger;
1513
import org.slf4j.LoggerFactory;
1614

1715

1816
public class TimeLoopFabric implements ModInitializer {
1917
public static final Logger LOOP_LOGGER = LoggerFactory.getLogger("TimeLoop");
20-
public static final boolean isDedicatedServer = FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER;
2118

2219
@Override
2320
public void onInitialize() {
2421
LOOP_LOGGER.info("Initializing TimeLoop mod (Fabric)");
2522

26-
TimeLoop.init(isDedicatedServer);
23+
TimeLoop.init();
2724

2825
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
2926
// Only register commands on the logical server
@@ -32,7 +29,10 @@ public void onInitialize() {
3229
}
3330
});
3431

35-
EntitySleepEvents.STOP_SLEEPING.register(EntitySleepFabricEvent::onStopSleeping);
32+
EntitySleepEvents.STOP_SLEEPING.register((entity, blockPos) -> {
33+
if (entity.level().isClientSide()) { return; }
34+
EntitySleepFabricEvent.onStopSleeping(entity, blockPos);
35+
});
3636

3737
ServerLifecycleEvents.SERVER_STARTED.register(LifecycleFabricEvent::onServerStart);
3838

fabric/src/main/java/com/vltno/timeloop/fabric/events/EntitySleepFabricEvent.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
public class EntitySleepFabricEvent {
88
public static void onStopSleeping(LivingEntity entity, BlockPos sleepingPos) {
9+
if (entity.level().isClientSide()) { return; }
910
EntitySleepEvent.onStopSleeping(entity);
1011
}
1112
}

fabric/src/main/resources/fabric.mod.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"main": [
1818
"com.vltno.timeloop.fabric.TimeLoopFabric"
1919
]
20-
},
20+
},
2121
"depends": {
2222
"minecraft": "${minecraft_version}",
2323
"fabricloader": ">=${fabric_loader_version}",

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Mod Properties
22
mod_id=timeloop
3-
mod_version=1.7.8
3+
mod_version=1.7.9
44
mod_name=Time Loop
55
mod_description=A set of commands that 'loops' time by using the Motion Capture Mod. Motion Capture mod by mt1006. Inspired by Tombino. Original datapack by Penguin Mafia.
66
mod_author_1=Luigi

neoforge/src/main/java/com/vltno/timeloop/neoforge/TimeLoopNeoForge.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,11 @@
2525
public class TimeLoopNeoForge {
2626

2727
public static final Logger LOOP_LOGGER = LoggerFactory.getLogger("TimeLoop");
28-
29-
public static final boolean isLogicalServer = !FMLEnvironment.dist.isClient();
3028

3129
public TimeLoopNeoForge(IEventBus modEventBus) {
3230
LOOP_LOGGER.info("Initializing TimeLoop mod (NeoForge)");
3331

34-
TimeLoop.init(isLogicalServer);
32+
TimeLoop.init();
3533

3634
// Register gameplay event listeners to the NeoForge EVENT bus
3735
NeoForge.EVENT_BUS.register(this);
@@ -52,9 +50,9 @@ public void onEndServerTick(ServerTickEvent.Post event) {
5250
TickNeoForgeEvent.onEndServerTick(event.getServer());
5351
}
5452

55-
// Player Wake Up handler (approximation for Stop Sleeping)
5653
@SubscribeEvent
5754
public void onPlayerWakeUp(PlayerWakeUpEvent event) {
55+
if (event.getEntity().level().isClientSide()) { return; }
5856
EntitySleepNeoForgeEvent.onStopSleeping(event.getEntity());
5957
}
6058

0 commit comments

Comments
 (0)