Volver al índice
src/main/java/ar/com/companeros/identity/ProfileUuidRemapper.java
package ar.com.companeros.identity;
import java.util.*;
import java.util.regex.Pattern;
import net.minecraft.nbt.*;
/** Migración exacta de referencias conocidas; no cambia recursos ni IDs ajenos. */
public final class ProfileUuidRemapper {
private ProfileUuidRemapper() { }
public static boolean remap(CompoundTag root, Map<UUID, UUID> mapping) {
CompoundTag original = root.copy(); visit(root, mapping); return !root.equals(original);
}
public static String text(String value, Map<UUID, UUID> mapping) {
String result = value;
for (var entry : mapping.entrySet()) result = result.replaceAll(
"(?i)(?<![a-z0-9])" + Pattern.quote(entry.getKey().toString()) + "(?![a-z0-9])", entry.getValue().toString());
return result;
}
// NBT moderno, UUID dividido en dos longs y claves UUID de capacidades de otros mods.
private static Tag visit(Tag tag, Map<UUID, UUID> mapping) {
if (tag instanceof IntArrayTag array && array.getAsIntArray().length == 4) {
UUID next = mapping.get(NbtUtils.loadUUID(array));
if (next != null) return NbtUtils.createUUID(next);
} else if (tag instanceof StringTag string) return StringTag.valueOf(text(string.getAsString(), mapping));
else if (tag instanceof ListTag list) {
for (int i = 0; i < list.size(); i++) list.set(i, visit(list.get(i), mapping));
} else if (tag instanceof CompoundTag compound) {
for (String key : Set.copyOf(compound.getAllKeys())) {
if (key.endsWith("Most") && compound.contains(key, Tag.TAG_LONG)) {
String least = key.substring(0, key.length() - 4) + "Least";
if (compound.contains(least, Tag.TAG_LONG)) {
UUID next = mapping.get(new UUID(compound.getLong(key), compound.getLong(least)));
if (next != null) { compound.putLong(key, next.getMostSignificantBits()); compound.putLong(least, next.getLeastSignificantBits()); }
}
}
String renamed = text(key, mapping);
if (!renamed.equals(key) && compound.contains(renamed)) throw new IllegalStateException("Colisión de referencias UUID");
Tag child = visit(compound.get(key), mapping);
if (!renamed.equals(key)) compound.remove(key);
compound.put(renamed, child);
}
}
return tag;
}
}