Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Executing code every tick and saving global data


Recommended Posts

Hello! I am looking to have a global time variable that ticks up with the normal gameTick but skips forward when you use the time command or skip the night by sleeping, in order to simulate the time that has passed. Quite confident with the logical part of it, but I have a few questions regarding how to save/sync and properly update every tick:

  1. What is the proper way to run code every tick? I've looked at listening for TickEvent or ClientTickEvent and ServerTickEvent. Would it be sensible to use them?
  2. How do I save global data on the server? The documentation is somewhat useful, but it's outdated and I haven't found a way to save global data.
  3. How do I conveniently send time data from server to client? Should I use my own packets? Can I include the numbers when sending initial world data to the client?
  4. Can I count on World#getGameTime and World#getDayTime to stay in sync between server and client? If so, I should be able to just execute the same code every tick on both client and server to keep them in sync.
  5. Can I count on World#getGameTime and World#getDayTime to be equal across different worlds on the server? From my testing, they seem to be the same regardless of which world you call the method from.

Thanks in advance for any answers to my questions!

Link to post
Share on other sites

1,2: It depends exactly what you are after. I would recommend using WorldTickEvent, and saving the time in a capability attached to the world. If you want to track each dimension independently, use the specific world#getGameTime, otherwise just use the overworld (using specific worlds the time will only increment when the world is loaded).

1&2: I would suggest listening to ServerTickEvent (with Phase END), and keeping the data attached to a capability attached to the Overworld. you can get the time using overworld#getGameTime. To keep track of time sleeping, subscribe to SleepFinishedTimeEvent (at EventPriority.LOWEST to run after all other listeners) and add the time difference.

3: packets, it depends what you need on the client. If it is just for rendering in a GUI, you could use a containerscreen to sync it, otherwise I believe you will need packets (sent whenever the offset between gametime and your gametime + sleeptime changes.

4: Yes, but the sleep event is only fired on the server, so you will have to manually sync your offset.

5: Yes, all worlds other than the Overworld use a DerivedWorldInfo, which just passes both times through from the overworld info. (I didn't realise this until I just looked).

Link to post
Share on other sites

Just a thought, instead having your own timer, you can have an "extra time" where when the night skips you add it by xx value...or something similar, and when you need that specific value you just get it by adding it with the current game time.

Edited by poopoodice
Link to post
Share on other sites

@Alpvax

Thanks for your answers!

For the record, I'm not listening for any sleep events. Since AFAIK it's not fired if time is set by a command, I'm using a more general approach.

@poopoodice

Thanks for your reply! I am indeed looking to use an offset variable. It's a good point that I shouldn't have to store any other time variable, and I'll look into that.

Link to post
Share on other sites
On 7/16/2021 at 3:00 PM, Anrza said:

@Alpvax

Thanks for your answers!

For the record, I'm not listening for any sleep events. Since AFAIK it's not fired if time is set by a command, I'm using a more general approach.

@poopoodice

Thanks for your reply! I am indeed looking to use an offset variable. It's a good point that I shouldn't have to store any other time variable, and I'll look into that.

The `dayTime` value is set by the command, so you could listen for the command event, check the daytime before, subtract it from the daytime after (wait 1 tick to get the "after" time) and add that difference to your offset if you wanted. It depends how accurate you want to be.

Alternatively, when your offset changes, you could check the difference between `gameTime` and `dayTime` (you would then have to track a second offset to check when it changes, but it might give you a bit more accuracy).

 

On 7/15/2021 at 3:26 PM, Alpvax said:

offset between gametime and your gametime + sleeptime

I meant that you should just save the offset, as poopoodice suggested

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • Switched to JDK 16. Now have this: Exception in thread "main" java.lang.module.ResolutionException: Module authlib reads more than one module named cpw.mods.securejarhandler     at java.base/java.lang.module.Resolver.resolveFail(Resolver.java:908)     at java.base/java.lang.module.Resolver.checkExportSuppliers(Resolver.java:729)     at java.base/java.lang.module.Resolver.finish(Resolver.java:379)     at java.base/java.lang.module.Configuration.<init>(Configuration.java:140)     at java.base/java.lang.module.Configuration.resolveAndBind(Configuration.java:494)     at java.base/java.lang.module.Configuration.resolveAndBind(Configuration.java:298)     at cpw.mods.bootstraplauncher@0.1.15/cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:77) Can't find much real info on what this means, let alone relating it to what I'm trying to do. Once again, help appreciated.
    • I Installed the latest version of forge and it still keeps crashing.  
    • i am looking for a mod that shows all the chunks that have been loaded or a mod that shows all of the chunks that are currently loaded for 1.17.1
    • Heya. I have been trying to figure out why using /tp keeps crashing me when Im using forge 1.16.5. the crash report is below. please help me out. ---- Minecraft Crash Report ---- // Don't do that. Time: 7/25/21 5:11 PM Description: Exception generating new chunk java.lang.IllegalArgumentException: bound must be positive     at java.util.Random.nextInt(Random.java:388) ~[?:1.8.0_51] {}     at net.minecraft.world.gen.feature.structure.Structure.func_236392_a_(Structure.java:202) ~[?:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}     at net.minecraft.world.gen.feature.structure.Structure.func_242785_a(Structure.java:221) ~[?:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}     at net.minecraft.world.gen.feature.StructureFeature.func_242771_a(SourceFile:35) ~[?:?] {re:classloading}     at net.minecraft.world.gen.ChunkGenerator.func_242705_a(SourceFile:278) ~[?:?] {re:mixin,re:classloading,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorAccessor,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorMixin,pl:mixin:A}     at net.minecraft.world.gen.ChunkGenerator.func_242707_a(SourceFile:267) ~[?:?] {re:mixin,re:classloading,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorAccessor,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorMixin,pl:mixin:A}     at net.minecraft.world.chunk.ChunkStatus.lambda$static$2(ChunkStatus.java:41) ~[?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.world.chunk.ChunkStatus$$Lambda$2265/387194988.doWork(Unknown Source) ~[?:?] {}     at net.minecraft.world.chunk.ChunkStatus.func_223198_a(ChunkStatus.java:198) ~[?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.world.server.ChunkManager.lambda$null$18(ChunkManager.java:524) ~[?:?] {re:classloading}     at net.minecraft.world.server.ChunkManager$$Lambda$6523/918777125.apply(Unknown Source) ~[?:?] {}     at com.mojang.datafixers.util.Either$Left.map(Either.java:38) ~[datafixerupper-4.0.26.jar:?] {re:classloading,re:classloading}     at net.minecraft.world.server.ChunkManager.lambda$chunkGenerate$20(ChunkManager.java:522) ~[?:?] {re:classloading}     at net.minecraft.world.server.ChunkManager$$Lambda$6520/530773096.apply(Unknown Source) ~[?:?] {}     at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:952) ~[?:1.8.0_51] {}     at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926) ~[?:1.8.0_51] {}     at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:442) ~[?:1.8.0_51] {}     at net.minecraft.world.chunk.ChunkTaskPriorityQueueSorter.func_219083_b(SourceFile:58) ~[?:?] {re:classloading}     at net.minecraft.world.chunk.ChunkTaskPriorityQueueSorter$$Lambda$6464/1211192729.run(Unknown Source) ~[?:?] {}     at net.minecraft.util.concurrent.DelegatedTaskExecutor.func_213148_e(SourceFile:94) ~[?:?] {re:classloading}     at net.minecraft.util.concurrent.DelegatedTaskExecutor.func_213145_a(SourceFile:137) ~[?:?] {re:classloading}     at net.minecraft.util.concurrent.DelegatedTaskExecutor.run(SourceFile:105) ~[?:?] {re:classloading}     at java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402) ~[?:1.8.0_51] {}     at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) ~[?:1.8.0_51] {}     at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056) ~[?:1.8.0_51] {}     at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1689) ~[?:1.8.0_51] {}     at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157) ~[?:1.8.0_51] {} A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Server thread Stacktrace:     at java.util.Random.nextInt(Random.java:388) ~[?:1.8.0_51] {}     at net.minecraft.world.gen.feature.structure.Structure.func_236392_a_(Structure.java:202) ~[?:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}     at net.minecraft.world.gen.feature.structure.Structure.func_242785_a(Structure.java:221) ~[?:?] {re:classloading,pl:accesstransformer:B,re:mixin,pl:accesstransformer:B}     at net.minecraft.world.gen.feature.StructureFeature.func_242771_a(SourceFile:35) ~[?:?] {re:classloading}     at net.minecraft.world.gen.ChunkGenerator.func_242705_a(SourceFile:278) ~[?:?] {re:mixin,re:classloading,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorAccessor,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorMixin,pl:mixin:A}     at net.minecraft.world.gen.ChunkGenerator.func_242707_a(SourceFile:267) ~[?:?] {re:mixin,re:classloading,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorAccessor,pl:mixin:APP:repurposed_structures.mixins.json:ChunkGeneratorMixin,pl:mixin:A}     at net.minecraft.world.chunk.ChunkStatus.lambda$static$2(ChunkStatus.java:41) ~[?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.world.chunk.ChunkStatus$$Lambda$2265/387194988.doWork(Unknown Source) ~[?:?] {}     at net.minecraft.world.chunk.ChunkStatus.func_223198_a(ChunkStatus.java:198) ~[?:?] {re:classloading,pl:accesstransformer:B} -- Chunk to be generated -- Details:     Location: 170,-183     Position hash: -785979014998     Generator: net.minecraft.world.gen.NoiseChunkGenerator@68c16e02 Stacktrace:     at net.minecraft.world.server.ChunkManager.lambda$null$18(ChunkManager.java:524) ~[?:?] {re:classloading}     at net.minecraft.world.server.ChunkManager$$Lambda$6523/918777125.apply(Unknown Source) ~[?:?] {}     at com.mojang.datafixers.util.Either$Left.map(Either.java:38) ~[datafixerupper-4.0.26.jar:?] {re:classloading,re:classloading,re:classloading}     at net.minecraft.world.server.ChunkManager.lambda$chunkGenerate$20(ChunkManager.java:522) ~[?:?] {re:classloading}     at net.minecraft.world.server.ChunkManager$$Lambda$6520/530773096.apply(Unknown Source) ~[?:?] {}     at java.util.concurrent.CompletableFuture.uniCompose(CompletableFuture.java:952) ~[?:1.8.0_51] {}     at java.util.concurrent.CompletableFuture$UniCompose.tryFire(CompletableFuture.java:926) ~[?:1.8.0_51] {}     at java.util.concurrent.CompletableFuture$Completion.run(CompletableFuture.java:442) ~[?:1.8.0_51] {}     at net.minecraft.world.chunk.ChunkTaskPriorityQueueSorter.func_219083_b(SourceFile:58) ~[?:?] {re:classloading}     at net.minecraft.world.chunk.ChunkTaskPriorityQueueSorter$$Lambda$6464/1211192729.run(Unknown Source) ~[?:?] {}     at net.minecraft.util.concurrent.DelegatedTaskExecutor.func_213148_e(SourceFile:94) ~[?:?] {re:classloading} -- Affected level -- Details:     All players: 1 total; [ServerPlayerEntity['KrystalTheFox_'/132, l='ServerLevel[Modded Survival]', x=2748.50, y=77.00, z=-2911.50]]     Chunk stats: ServerChunkCache: 3347     Level dimension: minecraft:overworld     Level spawn location: World: (51,64,-90), Chunk: (at 3,4,6 in 3,-6; contains blocks 48,0,-96 to 63,255,-81), Region: (0,-1; contains chunks 0,-32 to 31,-1, blocks 0,0,-512 to 511,255,-1)     Level time: 42412 game time, 227 day time     Level name: Modded Survival     Level game mode: Game mode: survival (ID 0). Hardcore: false. Cheats: false     Level weather: Rain time: 120492 (now: false), thunder time: 52965 (now: false)     Known server brands: forge     Level was modded: true     Level storage version: 0x04ABD - Anvil Stacktrace:     at net.minecraft.server.MinecraftServer.func_71190_q(MinecraftServer.java:854) ~[?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.server.MinecraftServer.func_71217_p(MinecraftServer.java:787) ~[?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.server.integrated.IntegratedServer.func_71217_p(IntegratedServer.java:78) ~[?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.server.MinecraftServer.func_240802_v_(MinecraftServer.java:642) [?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.server.MinecraftServer.lambda$startServer$0(MinecraftServer.java:232) [?:?] {re:classloading,pl:accesstransformer:B}     at net.minecraft.server.MinecraftServer$$Lambda$6334/1896438195.run(Unknown Source) [?:?] {}     at java.lang.Thread.run(Thread.java:745) [?:1.8.0_51] {} -- System Details -- Details:     Minecraft Version: 1.16.5     Minecraft Version ID: 1.16.5     Operating System: Windows 10 (amd64) version 10.0     Java Version: 1.8.0_51, Oracle Corporation     Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation     Memory: 1148904608 bytes (1095 MB) / 2147483648 bytes (2048 MB) up to 2147483648 bytes (2048 MB)     CPUs: 4     JVM Flags: 9 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -Xss1M -Xmx2G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M     ModLauncher: 8.0.9+86+master.3cf110c     ModLauncher launch target: fmlclient     ModLauncher naming: srg     ModLauncher services:          /mixin-0.8.2.jar mixin PLUGINSERVICE          /eventbus-4.0.0.jar eventbus PLUGINSERVICE          /forge-1.16.5-36.0.45.jar object_holder_definalize PLUGINSERVICE          /forge-1.16.5-36.0.45.jar runtime_enum_extender PLUGINSERVICE          /accesstransformers-3.0.1.jar accesstransformer PLUGINSERVICE          /forge-1.16.5-36.0.45.jar capability_inject_definalize PLUGINSERVICE          /forge-1.16.5-36.0.45.jar runtimedistcleaner PLUGINSERVICE          /mixin-0.8.2.jar mixin TRANSFORMATIONSERVICE          /forge-1.16.5-36.0.45.jar fml TRANSFORMATIONSERVICE      FML: 36.0     Forge: net.minecraftforge:36.0.45     FML Language Providers:          javafml@36.0         minecraft@1     Mod List:          forge-1.16.5-36.0.45-client.jar                   |Minecraft                     |minecraft                     |1.16.5              |DONE      |NOSIGNATURE         dungeons_mobs-1.16.5-1.0.5.jar                    |Dungeons Mobs                 |dungeons_mobs                 |1.0.5               |DONE      |NOSIGNATURE         repurposed_structures-1.16.5-2.6.6-forge.jar      |Repurposed Structures         |repurposed_structures         |1.16.5-2.6.6-forge  |DONE      |NOSIGNATURE         illagers_plus-1.16.4v1.5.jar                      |Illagers+                     |illagers_plus                 |1.16.4v1.5          |DONE      |NOSIGNATURE         enchantwithmob-1.16.5-1.4.6.jar                   |Enchant With Mob              |enchantwithmob                |1.16.5-1.4.6        |DONE      |NOSIGNATURE         Ma-Enchants-1.16.5-3.6.0 (1).jar                  |Ma Enchants                   |ma-enchants                   |1.16.5-3.6.0        |DONE      |NOSIGNATURE         forge-1.16.5-36.0.45-universal.jar                |Forge                         |forge                         |36.0.45             |DONE      |22:af:21:d8:19:82:7f:93:94:fe:2b:ac:b7:e4:41:57:68:39:87:b1:a7:5c:c6:44:f9:25:74:21:14:f5:0d:90         torohealth-1.16.4-forge-4.jar                     |ToroHealth                    |torohealth                    |NONE                |DONE      |NOSIGNATURE         extragolems-11.3.0.jar                            |Extra Golems                  |golems                        |11.3.0              |DONE      |NOSIGNATURE         Deeper+in+The+Caves+Remake+1.1.jar                |Deeper In The Caves Remake    |stacked_dimensions_warden     |1.0.0               |DONE      |NOSIGNATURE         MobBattleMod-1.16.2-2.3.4 (2).jar                 |Mob Battle Mod                |mobbattle                     |1.16.2-2.3.4        |DONE      |NOSIGNATURE         supertools-1.16.5-4.5.jar                         |Vanilla+ Tools                |supertools                    |version             |DONE      |NOSIGNATURE     Crash Report UUID: 2888177d-2bf6-4908-99f1-1b7b6465ec05     Player Count: 1 / 8; [ServerPlayerEntity['KrystalTheFox_'/132, l='ServerLevel[Modded Survival]', x=2748.50, y=77.00, z=-2911.50]]     Data Packs: vanilla, mod:dungeons_mobs (incompatible), mod:repurposed_structures, mod:illagers_plus, mod:enchantwithmob, mod:ma-enchants (incompatible), mod:forge, mod:torohealth, mod:golems (incompatible), mod:stacked_dimensions_warden, mod:mobbattle (incompatible), mod:supertools (incompatible)     Type: Integrated Server (map_client.txt)     Is Modded: Definitely; Client brand changed to 'forge'
    • I installed a Forge server, ran the normal .jar file, accepted the EULA, tried to run the forge.jar file but it doesn't give me any folders (like the mods or config) folder. Already looked through the forum here but there weren't any help. I also updated to the newest Java version but it doesn't help.
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.