Skip to content

How to make an item that sets the time and weather (Minecraft mod)

A ServerLevel holds the time and the weather. Before 26.1, setDayTime(1000) sets the time of day (0 is sunrise, 6000 noon, 13000 dusk, 18000 midnight) and setWeatherParameters(clearTime, rainTime, raining, thundering) sets the weather, with times in ticks. On 26.1 and newer the time is a world clock and the weather is a saved data object. These exist only on the server level, so check with instanceof.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Override use and test that the level is a ServerLevel.
  2. Set the time of day to the tick you want.
  3. Set clear weather (setWeatherParameters(6000, 0, false, false) before 26.1).
  4. Add a cooldown.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.17.1 to 1.20.1

Section titled “Fabric and NeoForge 1.20.4 to 1.21.1, NeoForge 1.20.1 to 1.20.2, Forge 1.17.1 to 1.20.1”

Uses:

  • ServerLevel.setDayTime
  • ServerLevel.setWeatherParameters

src/main/java/com/example/examplemod/item/SunCharmItem.java

package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Sun Charm: right-click to make it morning and clear the weather for five minutes.
public class SunCharmItem extends Item {
public SunCharmItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel) {
serverLevel.setDayTime(1000);
serverLevel.setWeatherParameters(6000, 0, false, false);
player.getCooldowns().addCooldown(this, 100);
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

It’s registered like any other item, as SunCharmItem: see a new item.

Uses:

  • ServerLevel.setDayTime
  • ServerLevel.setWeatherParameters

src/main/java/com/example/examplemod/item/SunCharmItem.java

package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EntitySpawnReason;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Sun Charm: right-click to make it morning and clear the weather for five minutes.
public class SunCharmItem extends Item {
public SunCharmItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel) {
serverLevel.setDayTime(1000);
serverLevel.setWeatherParameters(6000, 0, false, false);
player.getCooldowns().addCooldown(player.getItemInHand(hand), 100);
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other item, as SunCharmItem: see a new item.

Uses:

  • ServerClockManager.moveToTimeMarker
  • ClockTimeMarkers.DAY
  • ServerLevel.getWeatherData
  • WeatherData.setClearWeatherTime

Since 26.1 the time of day is a world clock (moveToTimeMarker) and the weather is a WeatherData saved-data object; setDayTime and setWeatherParameters are gone.

src/main/java/com/example/examplemod/item/SunCharmItem.java

package com.example.examplemod.item;
import net.minecraft.core.registries.Registries;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.clock.ClockTimeMarkers;
import net.minecraft.world.clock.WorldClocks;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.saveddata.WeatherData;
// Sun Charm: right-click to make it day and clear the weather for five minutes.
public class SunCharmItem extends Item {
public SunCharmItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResult use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel serverLevel) {
var clock = serverLevel.registryAccess().lookupOrThrow(Registries.WORLD_CLOCK).getOrThrow(WorldClocks.OVERWORLD);
serverLevel.clockManager().moveToTimeMarker(clock, ClockTimeMarkers.DAY);
WeatherData weather = serverLevel.getWeatherData();
weather.setClearWeatherTime(6000);
weather.setRaining(false);
weather.setThundering(false);
player.getCooldowns().addCooldown(player.getItemInHand(hand), 100);
}
return InteractionResult.SUCCESS;
}
}

It’s registered like any other item, as SunCharmItem: see a new item.

Uses:

  • ServerWorld.setDayTime
  • ServerWorld.setWeatherParameters

src/main/java/com/example/examplemod/item/SunCharmItem.java

package com.example.examplemod.item;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.util.Hand;
import net.minecraft.util.ActionResult;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
// Sun Charm: right-click to make it morning and clear the weather for five minutes.
public class SunCharmItem extends Item {
public SunCharmItem(Item.Properties properties) {
super(properties);
}
@Override
public ActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
if (!world.isClientSide()) {
ServerWorld serverWorld = (ServerWorld) world;
serverWorld.setDayTime(1000);
serverWorld.setWeatherParameters(6000, 0, false, false);
player.getCooldowns().addCooldown(this, 100);
}
return ActionResult.sidedSuccess(player.getItemInHand(hand), world.isClientSide());
}
}

It’s registered like any other item, as SunCharmItem: see a new item.

Uses:

  • ServerLevel.setDayTime
  • ServerLevel.setWeatherParameters

src/main/java/com/example/examplemod/item/SunCharmItem.java

package com.example.examplemod.item;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.MobSpawnType;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
// Sun Charm: right-click to make it morning and clear the weather for five minutes.
public class SunCharmItem extends Item {
public SunCharmItem(Item.Properties properties) {
super(properties);
}
@Override
public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand hand) {
if (level instanceof ServerLevel) {
ServerLevel serverLevel = (ServerLevel) level;
serverLevel.setDayTime(1000);
serverLevel.setWeatherParameters(6000, 0, false, false);
player.getCooldowns().addCooldown(this, 100);
}
return InteractionResultHolder.sidedSuccess(player.getItemInHand(hand), level.isClientSide());
}
}

It’s registered like any other item, as SunCharmItem: see a new item.

Uses:

  • WorldServer.setDayTime
  • WorldServer.setWeatherParameters

In 1.12.2 the world is a WorldServer: world.setWorldTime sets the time, and the weather is set on world.getWorldInfo() (setCleanWeatherTime, setRainTime, setThunderTime, setRaining, setThundering).

src/main/java/com/example/examplemod/item/SunCharmItem.java

package com.example.examplemod.item;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.world.World;
import net.minecraft.world.storage.WorldInfo;
// Sun Charm: right-click to make it morning and clear the weather for five minutes.
public class SunCharmItem extends Item {
@Override
public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, EnumHand hand) {
if (!world.isRemote) {
WorldInfo info = world.getWorldInfo();
world.setWorldTime(1000);
info.setCleanWeatherTime(6000);
info.setRainTime(0);
info.setThunderTime(0);
info.setRaining(false);
info.setThundering(false);
player.getCooldownTracker().setCooldown(this, 100);
}
return new ActionResult<>(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}
}

It’s registered like any other item, as SunCharmItem: see a new item.

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.