forked from neoforged/NeoForge
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add back Level data attachments (neoforged#435)
The typical use case is to store non-persistent client-side data, or have similar handling for data on both the client and server side. The implementation for persistence is basically the same as for the pre-1.20.2 capabilities.
- Loading branch information
1 parent
30ac00e
commit 9ae1def
Showing
4 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/net/neoforged/neoforge/attachment/LevelAttachmentsSavedData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) NeoForged and contributors | ||
* SPDX-License-Identifier: LGPL-2.1-only | ||
*/ | ||
|
||
package net.neoforged.neoforge.attachment; | ||
|
||
import java.util.Objects; | ||
import net.minecraft.nbt.CompoundTag; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.level.saveddata.SavedData; | ||
import org.jetbrains.annotations.ApiStatus; | ||
|
||
@ApiStatus.Internal | ||
public class LevelAttachmentsSavedData extends SavedData { | ||
private static final String NAME = "neoforge_data_attachments"; | ||
|
||
public static void init(ServerLevel level) { | ||
var factory = new SavedData.Factory<>( | ||
() -> new LevelAttachmentsSavedData(level), | ||
tag -> new LevelAttachmentsSavedData(level, tag)); | ||
// Querying the attachment a single time is enough to initialize it, | ||
// and make sure it gets saved when the level is saved. | ||
level.getDataStorage().computeIfAbsent(factory, NAME); | ||
} | ||
|
||
private final ServerLevel level; | ||
|
||
public LevelAttachmentsSavedData(ServerLevel level) { | ||
this.level = level; | ||
} | ||
|
||
public LevelAttachmentsSavedData(ServerLevel level, CompoundTag tag) { | ||
this.level = level; | ||
level.deserializeAttachments(tag); | ||
} | ||
|
||
@Override | ||
public CompoundTag save(CompoundTag tag) { | ||
// Make sure we don't return null | ||
return Objects.requireNonNullElseGet(level.serializeAttachments(), CompoundTag::new); | ||
} | ||
|
||
@Override | ||
public boolean isDirty() { | ||
// Always re-save | ||
return true; | ||
} | ||
} |