-
Notifications
You must be signed in to change notification settings - Fork 1
Tracked Data
- Tracked Data is the internal name used for tracking data pertaining to some in game object. We can attach our TrackedData to entities, players, levels(worlds), block entities, and chunks! For each type of tracked data we have a special implementation for each of the following. You also have the choice of synchronizing TrackedData to clients automatically by extending specialized classes. Let's begin covering each of these impls!
- If you're looking to track entity data on the server side only, you will need to create a new class that extends
ServerEntityTrackedData
:
import dev.corgitaco.dataanchor.data.registry.TrackedDataKey;
import dev.corgitaco.dataanchor.data.type.entity.EntityTrackedData;
import dev.corgitaco.dataanchor.data.type.entity.ServerEntityTrackedData;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.Nullable;
public class ExampleServerTrackedEntityData extends ServerEntityTrackedData {
public ExampleServerTrackedEntityData(TrackedDataKey<? extends EntityTrackedData> trackedDataKey, Entity entity) {
super(trackedDataKey, entity);
}
@Override
public @Nullable CompoundTag save() {
return null;
}
@Override
public void load(CompoundTag tag) {
}
}
Once you extend the class you will be required to override a save() and load() method. Save may return null and load may remain empty, if your tracked data is not saved to disk.
For our example, we will track an integer, save it to disk, load it from disk, and create our getters and setters:
package dev.corgitaco.dataanchor.test.data;
import dev.corgitaco.dataanchor.data.registry.TrackedDataKey;
import dev.corgitaco.dataanchor.data.type.entity.EntityTrackedData;
import dev.corgitaco.dataanchor.data.type.entity.ServerEntityTrackedData;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.entity.Entity;
import org.jetbrains.annotations.Nullable;
public class ExampleServerTrackedEntityData extends ServerEntityTrackedData {
private int trackedInt = 0;
public ExampleServerTrackedEntityData(TrackedDataKey<? extends EntityTrackedData> trackedDataKey, Entity entity) {
super(trackedDataKey, entity);
}
@Override
public @Nullable CompoundTag save() {
CompoundTag compoundTag = new CompoundTag();
compoundTag.putInt("trackedInt", this.trackedInt);
return compoundTag;
}
@Override
public void load(CompoundTag tag) {
if (tag.contains("trackedInt")) {
this.trackedInt = tag.getInt("trackedInt");
}
}
public void setTrackedInt(int trackedInt) {
this.trackedInt = trackedInt;
}
public int getTrackedInt() {
return trackedInt;
}
}
Once we've created our trackedInt variable, we can now now register our ExampleServerTrackedEntityData class to the TrackedDataRegistry for entities! This will allow us to access our ExampleServerTrackedEntityData for any entity of our choice! Your ExampleServerTrackedEntityData class may be registered anytime before your entity is loaded in the world if needed though it is highly advised to register your Tracked data to the registry ASAP during mod initialization
In your mod's main class do the following:
public static final TrackedDataKey<ExampleServerTrackedEntityData> EXAMPLE_SERVER_TRACKED_ENTITY_DATA = TrackedDataRegistries.ENTITY.register(new ResourceLocation("modid", "example_server_tracked_entity_data"), ExampleServerTrackedEntityData.class, ExampleServerTrackedEntityData::new);
This will create a TrackedDataKey
that we can use to access our ExampleServerTrackedEntityData
!
With our key created, wherever Minecraft provides us with an Entity we can use that to access our ExampleServerTrackedEntityData
. To do so, you would do the following"
...
Optional<ExampleServerTrackedEntityData> entityTrackedData = TrackedDataRegistries.ENTITY.getContainer(entity).get(EXAMPLE_SERVER_TRACKED_ENTITY_DATA);
...
From here, if the data is present we can set our TrackedInt variable by doing:
...
Optional<ExampleServerTrackedEntityData> entityTrackedData = TrackedDataRegistries.ENTITY.getContainer(entity).get(EXAMPLE_SERVER_TRACKED_ENTITY_DATA);
entityTrackedData.ifPresent(entityTrackedData1 -> entityTrackedData1.setTrackedInt(1));
...
or if we want to get our tracked int we can do:
...
Optional<ExampleServerTrackedEntityData> entityTrackedData = TrackedDataRegistries.ENTITY.getContainer(entity).get(EXAMPLE_SERVER_TRACKED_ENTITY_DATA);
entityTrackedData.ifPresent(entityTrackedData1 -> {
int trackedInt = entityTrackedData1.getTrackedInt();
// Do something with our trackedInt
});
...