Jump to content

manually setting time [best practice, advice]


sworn

Recommended Posts

Hello.

I'm starting this mod for testing.

I was reading that executing commands with ICommandManager is a bad practice.

 

My mod does only one thing, slower the timer (increasing day/night length).

It's actually working. But I have some questions...

 

  1. Is there a better way to stop the game timer and change it manually instead of execute the game command?
     
  2. When I run the the mod on a server, or just "open lan", I start to see tons of "Server: Added 1 to the time". Is there a way to prevent that?

     
Spoiler

@Mod.EventBusSubscriber
public class Events {
    public static boolean isSet = false;
    public static int tickCounter = 0; 
    
    @SubscribeEvent
    public static void onTick(TickEvent.WorldTickEvent event) {
        tickCounter++;
        if (tickCounter < 70) return;
        
        try {
            tickCounter = 0;
            
            if (event == null) return;
            
            World world = event.world;
            if (world == null) return;
            
            execCommand(world, "/time add 1");
        }catch(Exception ex) {
            System.out.println("swn => event [onTick] fail!");
            System.out.println("       " + ex.getMessage());
        }
    }
    
    @SubscribeEvent
    public static void onWorldLoaded(WorldEvent.Load event)    {
        try {
            if (isSet) return;
            
            if (event == null) return;
            
            World world = event.getWorld();
            if (world == null) return;
            
            execCommand(world, "/gamerule doDaylightCycle false");
        }catch(Exception ex) {
            System.out.println("swn => event [onWorldLoaded] fail!");
            System.out.println("       " + ex.getMessage());
        }
    }
    
    private static void execCommand(World world, String command) {
        if (world.provider.getDimension() != 0) return;
        if (world.isRemote) return;
    
        MinecraftServer server = world.getMinecraftServer();
        ICommandManager cmd = server.getCommandManager();
        cmd.executeCommand(server, command);
    }
}

 

Edited by sworn
Link to comment
Share on other sites

To quote myself from a similar post

On 2017-04-01 at 0:17 PM, Matryoshika said:

You need to create a custom WorldProvider, and override calculateCelestialAngle.
Then you need to register said WorldProvider to the wanted dimension(s), by calling DimensionManager#unregisterDimension(id) and then re-registering it with DimensionManager#registerDimension(id, DimensionType#register(...))

Though I would iterate over the worlds, and check their WorldProvider's first, and only change if the WorldProvider is an instanceof the overworld's WorldProvider, as some dimensions have "frozen" time, etc.

calculateCelestialAngle is the method Minecraft uses to figure out time for the WorldProvider's associated dimension.
If you override it, you can make it state whatever time you want.

Edited by Matryoshika

Also previously known as eAndPi.

"Pi, is there a station coming up where we can board your train of thought?" -Kronnn

Published Mods: Underworld

Handy links: Vic_'s Forge events Own WIP Tutorials.

Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.