Skip to content

How to save data with the world in a Minecraft mod

SavedData is data tied to a world, not to a block or an entity. Make a class that extends SavedData (WorldSavedData on 1.16.5 and 1.12.2) with a save method (write to a CompoundTag) and a load method (read it back), tell the game how to create it (a SavedData.Factory from 1.20.3, a SavedDataType from 1.21.5), and fetch it from the level’s data storage by name with computeIfAbsent. It writes the file data/<name>.dat. Call setDirty() after every change, or the game does not save it. For data on a player or entity use an attachment; for data on a block use a block entity.

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. Write the SavedData class with its save and load methods and the way to create it.
  2. Fetch it from the overworld’s data storage by name.
  3. Change its fields, then mark it dirty (setDirty()).
  4. Join twice, restart the world and join again to see the count carry on.

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

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • ServerPlayConnectionEvents.JOIN

Before 1.20.3 saved data has no Factory: computeIfAbsent takes the load function and a supplier. It also loads and saves without a HolderLookup.Provider.

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
private int joins;
public static JoinCounterData load(CompoundTag tag) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(JoinCounterData::load, JoinCounterData::new, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.chat.Component;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
handler.getPlayer().sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(server).addJoin())));
}
}

In your main mod class, inside onInitialize:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
private int joins;
public static JoinCounterData load(CompoundTag tag) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(JoinCounterData::load, JoinCounterData::new, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(player.server).addJoin()));
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
public static final SavedData.Factory<JoinCounterData> FACTORY = new SavedData.Factory<>(JoinCounterData::new, JoinCounterData::load, null);
private int joins;
public static JoinCounterData load(CompoundTag tag) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(FACTORY, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(player.server).addJoin()));
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • ServerPlayConnectionEvents.JOIN

Before 1.20.5 saved data is read and written without a HolderLookup.Provider: load(CompoundTag) and save(CompoundTag).

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
public static final SavedData.Factory<JoinCounterData> FACTORY = new SavedData.Factory<>(JoinCounterData::new, JoinCounterData::load, null);
private int joins;
public static JoinCounterData load(CompoundTag tag) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(FACTORY, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.chat.Component;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
handler.getPlayer().sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(server).addJoin())));
}
}

In your main mod class, inside onInitialize:

JoinCounterJoin.register();

Fabric 1.20.5 to 1.21.1, Fabric 1.21.2 to 1.21.4

Section titled “Fabric 1.20.5 to 1.21.1, Fabric 1.21.2 to 1.21.4”

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • ServerPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
public static final SavedData.Factory<JoinCounterData> FACTORY = new SavedData.Factory<>(JoinCounterData::new, JoinCounterData::load, null);
private int joins;
public static JoinCounterData load(CompoundTag tag, HolderLookup.Provider registries) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(FACTORY, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag, HolderLookup.Provider registries) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.chat.Component;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
handler.getPlayer().sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(server).addJoin())));
}
}

In your main mod class, inside onInitialize:

JoinCounterJoin.register();

NeoForge 1.20.5 to 1.21.1, NeoForge 1.21.2 to 1.21.4

Section titled “NeoForge 1.20.5 to 1.21.1, NeoForge 1.21.2 to 1.21.4”

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
public static final SavedData.Factory<JoinCounterData> FACTORY = new SavedData.Factory<>(JoinCounterData::new, JoinCounterData::load, null);
private int joins;
public static JoinCounterData load(CompoundTag tag, HolderLookup.Provider registries) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(FACTORY, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag, HolderLookup.Provider registries) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(player.server).addJoin()));
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedDataType
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
// The codec is the save format: it reads the file and writes it back.
public static final Codec<JoinCounterData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.optionalFieldOf("joins", 0).forGetter(data -> data.joins)
).apply(instance, JoinCounterData::new));
public static final SavedDataType<JoinCounterData> TYPE = new SavedDataType<>("examplemod_join_counter", JoinCounterData::new, CODEC);
private int joins;
public JoinCounterData() {
this(0);
}
private JoinCounterData(int joins) {
this.joins = joins;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(TYPE);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(player.server).addJoin()));
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • ServerPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
// Since 1.21.5 saved data is described by a codec and a SavedDataType instead of a Factory with load and save methods.
public static final Codec<JoinCounterData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.optionalFieldOf("joins", 0).forGetter(data -> data.joins)).apply(instance, JoinCounterData::new));
public static final SavedDataType<JoinCounterData> TYPE = new SavedDataType<>("examplemod_join_counter", () -> new JoinCounterData(0), CODEC, null);
private int joins;
private JoinCounterData(int joins) {
this.joins = joins;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(TYPE);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.chat.Component;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
handler.getPlayer().sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(server).addJoin())));
}
}

In your main mod class, inside onInitialize:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedDataType
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
// The codec is the save format: it reads the file and writes it back.
public static final Codec<JoinCounterData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.optionalFieldOf("joins", 0).forGetter(data -> data.joins)
).apply(instance, JoinCounterData::new));
public static final SavedDataType<JoinCounterData> TYPE = new SavedDataType<>("examplemod_join_counter", JoinCounterData::new, CODEC);
private int joins;
public JoinCounterData() {
this(0);
}
private JoinCounterData(int joins) {
this.joins = joins;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(TYPE);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(player.getServer()).addJoin()));
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedDataType
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
// The codec is the save format: it reads the file and writes it back.
public static final Codec<JoinCounterData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.optionalFieldOf("joins", 0).forGetter(data -> data.joins)
).apply(instance, JoinCounterData::new));
public static final SavedDataType<JoinCounterData> TYPE = new SavedDataType<>("examplemod_join_counter", JoinCounterData::new, CODEC);
private int joins;
public JoinCounterData() {
this(0);
}
private JoinCounterData(int joins) {
this.joins = joins;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(TYPE);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(player.level().getServer()).addJoin()));
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedDataType
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.resources.Identifier;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
// Join Counter: a number saved with the world. Every join adds 1.
public class JoinCounterData extends SavedData {
// The codec is the save format: it reads the file and writes it back.
public static final Codec<JoinCounterData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.optionalFieldOf("joins", 0).forGetter(data -> data.joins)
).apply(instance, JoinCounterData::new));
public static final SavedDataType<JoinCounterData> TYPE = new SavedDataType<>(Identifier.fromNamespaceAndPath("examplemod", "join_counter"), JoinCounterData::new, CODEC);
private int joins;
public JoinCounterData() {
this(0);
}
private JoinCounterData(int joins) {
this.joins = joins;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(TYPE);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
NeoForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(player.level().getServer()).addJoin()));
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • ServerPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import com.example.examplemod.ExampleMod;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
import net.minecraft.world.level.saveddata.SavedDataType;
// Join Counter: a number saved with the world (in data/examplemod/join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
// Since 1.21.5 saved data is described by a codec and a SavedDataType instead of a Factory with load and save methods.
public static final Codec<JoinCounterData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.INT.optionalFieldOf("joins", 0).forGetter(data -> data.joins)).apply(instance, JoinCounterData::new));
public static final SavedDataType<JoinCounterData> TYPE = new SavedDataType<>(ExampleMod.id("join_counter"), () -> new JoinCounterData(0), CODEC, null);
private int joins;
private JoinCounterData(int joins) {
this.joins = joins;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(TYPE);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.data.JoinCounterData;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.chat.Component;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
handler.getPlayer().sendSystemMessage(Component.literal("Joins in this world: " + JoinCounterData.get(server).addJoin())));
}
}

In your main mod class, inside onInitialize:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • PlayerEvent.PlayerLoggedInEvent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
private int joins;
public static JoinCounterData load(CompoundTag tag) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(JoinCounterData::load, JoinCounterData::new, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import net.minecraft.Util;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.network.chat.Component;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayer player) {
player.sendMessage(new TextComponent("Joins in this world: " + JoinCounterData.get(player.server).addJoin()), Util.NIL_UUID);
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • ServerPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
private int joins;
public static JoinCounterData load(CompoundTag tag) {
JoinCounterData data = new JoinCounterData();
data.joins = tag.getInt("joins");
return data;
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(JoinCounterData::load, JoinCounterData::new, "examplemod_join_counter");
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import net.minecraft.Util;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.data.JoinCounterData;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.chat.Component;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
handler.getPlayer().sendMessage(new TextComponent("Joins in this world: " + JoinCounterData.get(server).addJoin()), Util.NIL_UUID));
}
}

In your main mod class, inside onInitialize:

JoinCounterJoin.register();

Uses:

  • SavedData
  • SavedData.Factory
  • DimensionDataStorage.computeIfAbsent
  • ServerPlayConnectionEvents.JOIN

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.saveddata.SavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends SavedData {
private static final String ID = "examplemod_join_counter";
private int joins;
public JoinCounterData() {
super(ID);
}
@Override
public void load(CompoundTag tag) {
joins = tag.getInt("joins");
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(JoinCounterData::new, ID);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import net.minecraft.Util;
import net.minecraft.network.chat.TextComponent;
import com.example.examplemod.data.JoinCounterData;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.minecraft.network.chat.Component;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's onInitialize.
public static void register() {
ServerPlayConnectionEvents.JOIN.register((handler, sender, server) ->
handler.player.sendMessage(new TextComponent("Joins in this world: " + JoinCounterData.get(server).addJoin()), Util.NIL_UUID));
}
}

In your main mod class, inside onInitialize:

JoinCounterJoin.register();

Uses:

  • WorldSavedData.save
  • WorldSavedData.load
  • WorldSavedData.setDirty
  • DimensionSavedDataManager.computeIfAbsent

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.storage.WorldSavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends WorldSavedData {
private static final String NAME = "examplemod_join_counter";
private int joins;
public JoinCounterData() {
super(NAME);
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(MinecraftServer server) {
return server.overworld().getDataStorage().computeIfAbsent(JoinCounterData::new, NAME);
}
public int addJoin() {
joins++;
setDirty();
return joins;
}
@Override
public void load(CompoundNBT tag) {
joins = tag.getInt("joins");
}
@Override
public CompoundNBT save(CompoundNBT tag) {
tag.putInt("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import net.minecraft.util.text.StringTextComponent;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.Util;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.player.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
// Called from the mod's constructor.
public static void register() {
MinecraftForge.EVENT_BUS.addListener(JoinCounterJoin::onLogin);
}
private static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
if (event.getEntity() instanceof ServerPlayerEntity) {
ServerPlayerEntity player = (ServerPlayerEntity) event.getEntity();
player.sendMessage(new StringTextComponent("Joins in this world: " + JoinCounterData.get(player.server).addJoin()), Util.NIL_UUID);
}
}
}

In your main mod class, inside <init>:

JoinCounterJoin.register();

Uses:

  • WorldSavedData.save
  • WorldSavedData.load
  • WorldSavedData.setDirty
  • DimensionSavedDataManager.computeIfAbsent

In 1.12.2 the class extends WorldSavedData with readFromNBT and writeToNBT, it is fetched with world.getMapStorage().getOrLoadData(class, name) (and added with setData when new), and markDirty() saves it.

src/main/java/com/example/examplemod/data/JoinCounterData.java

package com.example.examplemod.data;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraft.world.storage.MapStorage;
import net.minecraft.world.storage.WorldSavedData;
// Join Counter: a number saved with the world (in data/examplemod_join_counter.dat). Every join adds 1.
public class JoinCounterData extends WorldSavedData {
private static final String NAME = "examplemod_join_counter";
private int joins;
// 1.12.2 makes saved data through a constructor that takes the name.
public JoinCounterData(String name) {
super(name);
}
// The overworld's storage is the one place every dimension shares.
public static JoinCounterData get(World world) {
MapStorage storage = world.getMapStorage();
JoinCounterData data = (JoinCounterData) storage.getOrLoadData(JoinCounterData.class, NAME);
if (data == null) {
data = new JoinCounterData(NAME);
storage.setData(NAME, data);
}
return data;
}
public int addJoin() {
joins++;
markDirty();
return joins;
}
@Override
public void readFromNBT(NBTTagCompound tag) {
joins = tag.getInteger("joins");
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
tag.setInteger("joins", joins);
return tag;
}
}

src/main/java/com/example/examplemod/event/JoinCounterJoin.java

package com.example.examplemod.event;
import com.example.examplemod.ExampleMod;
import com.example.examplemod.data.JoinCounterData;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.gameevent.PlayerEvent;
// Counts every player join in JoinCounterData and tells the player the number.
@Mod.EventBusSubscriber(modid = ExampleMod.MOD_ID)
public final class JoinCounterJoin {
private JoinCounterJoin() {
}
@SubscribeEvent
public static void onLogin(PlayerEvent.PlayerLoggedInEvent event) {
event.player.sendMessage(new TextComponentString("Joins in this world: " + JoinCounterData.get(event.player.world).addJoin()));
}
}

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.