Fix RS2 restore: remove() before set() + reflection fallback
repo.set(uuid, storage) throws IllegalArgumentException if the UUID already exists in the StorageRepository. This happens when a player revisits a server where the disk was previously used. Items appeared briefly (data was decoded correctly) but then the exception prevented the set() and the storage fell back to empty. Fix: - Call repo.remove(uuid) before repo.set(uuid, storage) - If set() still fails, inject directly into the entries map via reflection + mark SavedData dirty - setDirty() ensures the injected data persists to disk Vyrriox
This commit is contained in:
parent
2baa8e4c39
commit
12645a1d3d
|
|
@ -582,8 +582,31 @@ public class ModsSupport {
|
|||
@SuppressWarnings("unchecked")
|
||||
java.util.Map<UUID, ?> decoded = (java.util.Map<UUID, ?>) pair.getFirst();
|
||||
for (java.util.Map.Entry<UUID, ?> entry : decoded.entrySet()) {
|
||||
repo.set(entry.getKey(),
|
||||
(com.refinedmods.refinedstorage.common.api.storage.SerializableStorage) entry.getValue());
|
||||
// FIX: repo.set() throws IllegalArgumentException if UUID already exists.
|
||||
// Remove first, then set. Also inject directly into the entries map
|
||||
// via reflection as a fallback if the public API fails.
|
||||
try {
|
||||
repo.remove(entry.getKey());
|
||||
} catch (Exception ignored) {}
|
||||
try {
|
||||
repo.set(entry.getKey(),
|
||||
(com.refinedmods.refinedstorage.common.api.storage.SerializableStorage) entry.getValue());
|
||||
} catch (Exception setEx) {
|
||||
// Fallback: inject directly into the entries map
|
||||
PlayerSync.LOGGER.debug("repo.set() failed, using reflection fallback", setEx);
|
||||
try {
|
||||
java.lang.reflect.Field entriesField = repo.getClass().getDeclaredField("entries");
|
||||
entriesField.setAccessible(true);
|
||||
@SuppressWarnings("unchecked")
|
||||
java.util.Map<UUID, Object> entries = (java.util.Map<UUID, Object>) entriesField.get(repo);
|
||||
entries.put(entry.getKey(), entry.getValue());
|
||||
if (repo instanceof net.minecraft.world.level.saveddata.SavedData sdRef) {
|
||||
sdRef.setDirty();
|
||||
}
|
||||
} catch (Exception reflectEx) {
|
||||
PlayerSync.LOGGER.error("RS2 reflection fallback also failed for UUID {}", entry.getKey(), reflectEx);
|
||||
}
|
||||
}
|
||||
PlayerSync.LOGGER.info("Restored RS2 disk data for UUID {}", entry.getKey());
|
||||
}
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user