Skip to content

Saved data and reloaders

StopeSavedData keeps a mod’s state per dimension in the world save, EntityData keeps small values on single entities, and DataReloaders runs a mod’s code each time the server loads its data packs. Use saved data for state the world changes (wear on paths), entity data for a counter that belongs to one mob, and a reload handler for rules a data pack or config folder supplies.

Type Summary Status Since Side Thread
DataFile A file found by DataReloaders#registerFiles. Experimental 2.0.0 server any
DataReloaders Server data-pack reload handlers. Experimental 1.0.0 both setup
EntityData Small int values a mod stores on an entity under its own key, saved with the entity. Experimental 2.6.0 server server
StopeSavedData Per-dimension saved data without the version-specific vanilla signature. Experimental 1.0.0 server server

Saved data is a vanilla SavedData subclass; StopeSavedData#getOrCreate finds each dimension’s instance:

Count the steps on each block in the world save
public final class PathWear extends SavedData {
private static final String DATA_VERSION = "dataVersion";
private static final String POSITIONS = "positions";
private static final String STEPS = "steps";
private final Long2IntOpenHashMap steps = new Long2IntOpenHashMap();
public static PathWear get(ServerLevel level) {
return StopeSavedData.getOrCreate(level, "mymod_path_wear", PathWear::new, tag -> {
PathWear wear = new PathWear();
long[] positions = tag.getLongArray(POSITIONS);
int[] counts = tag.getIntArray(STEPS);
for (int i = 0; i < counts.length; i++) wear.steps.put(positions[i], counts[i]);
return wear;
});
}
public int step(BlockPos pos) {
setDirty();
return steps.addTo(pos.asLong(), 1) + 1;
}
public void clear(BlockPos pos) {
if (steps.remove(pos.asLong()) != 0) setDirty();
}
@Override
public CompoundTag save(CompoundTag tag) {
tag.putInt(DATA_VERSION, 1);
// The key set and the values iterate in the same order, so the two arrays line up.
tag.putLongArray(POSITIONS, steps.keySet().toLongArray());
tag.putIntArray(STEPS, steps.values().toIntArray());
return tag;
}
}

Experimental · Since 2.6.0 · Side: server · Thread: server

With FEATHER_TIMER, a ResourceLocation constant built with id("feather_timer"), the mod calls this once per tick for each adult chicken it tracks:

Count down a timer saved on each chicken
static void tickChicken(ServerLevel level, Chicken chicken) {
int left = EntityData.getInt(chicken, FEATHER_TIMER, -1);
if (left < 0) {
// No saved timer yet: a new chicken, or one from a world saved without the mod.
left = 12000 + level.getRandom().nextInt(12000);
} else if (left == 0) {
chicken.spawnAtLocation(Items.FEATHER);
left = 12000 + level.getRandom().nextInt(12000);
} else {
left--;
}
EntityData.putInt(chicken, FEATHER_TIMER, left);
}

DataFile: Experimental · Since 2.0.0 · Side: server · Thread: any

DataReloaders: Experimental · Since 1.0.0 · Side: both · Thread: setup

A handler that reads JSON rule files uses DataReloaders#registerFiles, which lists the files of a data-pack directory and a config folder for it. Its timing, thread, and duplicate-ID rule are those of DataReloaders#register, whose Javadoc states them. With wearRules, a static Map<Block, Block> field the mod’s footfall listener reads, and LOGGER, the mod’s SLF4J logger:

Read rule files from data packs and the config folder
DataReloaders.registerFiles(id("wear_rules"), "wear_rules", "mymod/wear_rules", files -> {
Map<Block, Block> rules = new HashMap<>();
for (DataFile file : files) {
try (Reader reader = file.openReader()) {
JsonObject rule = JsonParser.parseReader(reader).getAsJsonObject();
ResourceLocation fromId = Ids.parse(GsonHelper.getAsString(rule, "from"));
ResourceLocation toId = Ids.parse(GsonHelper.getAsString(rule, "to"));
Block from = RegistryLookups.block(fromId)
.orElseThrow(() -> new IllegalArgumentException("Unknown block ID " + fromId));
Block to = RegistryLookups.block(toId)
.orElseThrow(() -> new IllegalArgumentException("Unknown block ID " + toId));
// Files come in load order, so a later file for the same block wins.
rules.put(from, to);
} catch (IOException | RuntimeException e) {
LOGGER.warn("Skipped wear rule {}: {}", file.location(), e.getMessage());
}
}
wearRules = Map.copyOf(rules);
});
  • A data-pack file and a config-folder file can share an ID: the data-pack file data/<namespace>/wear_rules/config/grass.json and the config file grass.json are both <namespace>:config/grass when the handler’s namespace is <namespace>.
  • Both files of such a pair are listed, the data-pack file first: config-folder files come after every data-pack file.
  • A handler that applies the list in order lets a config file override a data-pack file.