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.
- Override use and test that the level is a ServerLevel.
- Set the time of day to the tick you want.
- Set clear weather (setWeatherParameters(6000, 0, false, false) before 26.1).
- Add a cooldown.
For each loader and version
Section titled “For each loader and version”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.setDayTimeServerLevel.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.
Fabric and NeoForge 1.21.2 to 1.21.11
Section titled “Fabric and NeoForge 1.21.2 to 1.21.11”Uses:
ServerLevel.setDayTimeServerLevel.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.
Fabric and NeoForge 26.1 to 26.3
Section titled “Fabric and NeoForge 26.1 to 26.3”Uses:
ServerClockManager.moveToTimeMarkerClockTimeMarkers.DAYServerLevel.getWeatherDataWeatherData.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.
Forge 1.16.5
Section titled “Forge 1.16.5”Uses:
ServerWorld.setDayTimeServerWorld.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.
Fabric 1.16.5 to 1.20.3
Section titled “Fabric 1.16.5 to 1.20.3”Uses:
ServerLevel.setDayTimeServerLevel.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.
Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
WorldServer.setDayTimeWorldServer.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.
Sources
Section titled “Sources”- https://docs.neoforged.net/docs/1.21.1/items/interactions
- https://docs.fabricmc.net/1.21.1/develop/items/first-item
Related how-tos
Section titled “Related how-tos”- An item that explodes where you aim
- A tool that breaks a 3 by 3 area
- A wand that strikes lightning where you look
- An item that changes the block you click
- An item that fills the hunger bar
In Linen
Section titled “In Linen”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.