From 3b8a981500df9034d631de60ba17e5c88ab44fe5 Mon Sep 17 00:00:00 2001 From: Anselm Brehme Date: Sun, 24 May 2020 09:19:25 +0200 Subject: [PATCH] datafixers --- .../data/fixer/entity/EntityTrackedUser.java | 67 ----------------- .../entity/player/PlayerRespawnData.java | 17 ++++- .../data/fixer/world/SpongeLevelFixer.java | 67 ----------------- .../common/data/util/DataUtil.java | 8 -- .../common/data/SpongeDataManager.java | 18 ++++- .../data/fixer/entity/EntityTrackedUser.java | 69 +++++++++++++++++ .../data/fixer/world/SpongeLevelFixer.java | 74 +++++++++++++++++++ 7 files changed, 173 insertions(+), 147 deletions(-) delete mode 100644 invalid/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java delete mode 100644 invalid/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java create mode 100644 src/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java create mode 100644 src/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java diff --git a/invalid/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java b/invalid/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java deleted file mode 100644 index 844c8380825..00000000000 --- a/invalid/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * This file is part of Sponge, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.spongepowered.common.data.fixer.entity; - -import net.minecraft.nbt.CompoundNBT; -import net.minecraft.nbt.NBTBase; -import net.minecraft.util.datafix.IFixableData; -import org.spongepowered.common.util.Constants; - -import java.util.UUID; - -public class EntityTrackedUser implements IFixableData { - - @Override - public int getFixVersion() { - return Constants.Legacy.Entity.TRACKER_ID_VERSION; - } - - @Override - public CompoundNBT fixTagCompound(CompoundNBT compound) { - final NBTBase forgeCompound = compound.get(Constants.Forge.FORGE_DATA); - if (forgeCompound != null) { - final CompoundNBT forgeData = (CompoundNBT) forgeCompound; - final NBTBase spongeCompound = forgeData.get(Constants.Sponge.SPONGE_DATA); - if (spongeCompound != null) { - final CompoundNBT spongeData = (CompoundNBT) spongeCompound; - process(spongeData, Constants.Sponge.SPONGE_ENTITY_CREATOR); - process(spongeData, Constants.Sponge.SPONGE_ENTITY_NOTIFIER); - } - } - return compound; - } - - private static void process(CompoundNBT spongeData, String type) { - if (spongeData.contains(type, Constants.NBT.TAG_COMPOUND)) { - final CompoundNBT creatorTag = spongeData.getCompound(type); - final long least = creatorTag.getLong(Constants.Legacy.Entity.UUID_LEAST_1_8); - final long most = creatorTag.getLong(Constants.Legacy.Entity.UUID_MOST_1_8); - final UUID creator = new UUID(most, least); - creatorTag.remove(Constants.Legacy.Entity.UUID_LEAST_1_8); - creatorTag.remove(Constants.Legacy.Entity.UUID_MOST_1_8); - creatorTag.putUniqueId(Constants.UUID, creator); - } - } -} diff --git a/invalid/main/java/org/spongepowered/common/data/fixer/entity/player/PlayerRespawnData.java b/invalid/main/java/org/spongepowered/common/data/fixer/entity/player/PlayerRespawnData.java index 5523d5cc68b..ba1c98575f6 100644 --- a/invalid/main/java/org/spongepowered/common/data/fixer/entity/player/PlayerRespawnData.java +++ b/invalid/main/java/org/spongepowered/common/data/fixer/entity/player/PlayerRespawnData.java @@ -25,6 +25,9 @@ package org.spongepowered.common.data.fixer.entity.player; import com.google.common.collect.Maps; +import com.mojang.datafixers.DataFix; +import com.mojang.datafixers.TypeRewriteRule; +import com.mojang.datafixers.schemas.Schema; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.ListNBT; import net.minecraft.util.datafix.IFixableData; @@ -35,12 +38,13 @@ import java.util.Map; import java.util.UUID; -public class PlayerRespawnData implements IFixableData { +// TODO this is actually doing nothing? +public class PlayerRespawnData extends DataFix { - @Override - public int getFixVersion() { - return Constants.Sponge.PlayerData.RESPAWN_DATA_1_9_VERSION; + public PlayerRespawnData(Schema outputSchema, boolean changesType) { + super(outputSchema, changesType); } + FixTypes.PLAYER, @Override public CompoundNBT fixTagCompound(CompoundNBT compound) { @@ -71,4 +75,9 @@ public CompoundNBT fixTagCompound(CompoundNBT compound) { } return compound; } + + @Override + protected TypeRewriteRule makeRule() { + return null; + } } diff --git a/invalid/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java b/invalid/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java deleted file mode 100644 index d362757174d..00000000000 --- a/invalid/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * This file is part of Sponge, licensed under the MIT License (MIT). - * - * Copyright (c) SpongePowered - * Copyright (c) contributors - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -package org.spongepowered.common.data.fixer.world; - -import net.minecraft.nbt.CompoundNBT; -import net.minecraft.nbt.ListNBT; -import net.minecraft.util.datafix.IFixableData; -import org.spongepowered.common.util.Constants; - -import java.util.UUID; - -public class SpongeLevelFixer implements IFixableData { - - @Override - public int getFixVersion() { - return Constants.Legacy.World.WORLD_UUID_1_9_VERSION; - } - - @Override - public CompoundNBT fixTagCompound(CompoundNBT compound) { - { // Fixes the world unique id - final long least = compound.getLong(Constants.Legacy.World.WORLD_UUID_LEAST_1_8); - final long most = compound.getLong(Constants.Legacy.World.WORLD_UUID_MOST_1_8); - final UUID worldId = new UUID(most, least); - compound.remove(Constants.Legacy.World.WORLD_UUID_LEAST_1_8); - compound.remove(Constants.Legacy.World.WORLD_UUID_MOST_1_8); - compound.putUniqueId(Constants.UUID, worldId); - - } - // Fixes the Player Id Table - if (compound.contains(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE, Constants.NBT.TAG_LIST)) { - final ListNBT playerIdList = compound.getList(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE, Constants.NBT.TAG_COMPOUND); - for (int i = 0; i < playerIdList.tagCount(); i++) { - final CompoundNBT playerIdCompound = playerIdList.getCompound(i); - final long least = playerIdCompound.getLong(Constants.Legacy.World.WORLD_UUID_LEAST_1_8); - final long most = playerIdCompound.getLong(Constants.Legacy.World.WORLD_UUID_MOST_1_8); - playerIdCompound.remove(Constants.Legacy.World.WORLD_UUID_LEAST_1_8); - playerIdCompound.remove(Constants.Legacy.World.WORLD_UUID_MOST_1_8); - final UUID playerId = new UUID(most, least); - playerIdCompound.putUniqueId(Constants.UUID, playerId); - } - } - return compound; - } -} diff --git a/invalid/main/java/org/spongepowered/common/data/util/DataUtil.java b/invalid/main/java/org/spongepowered/common/data/util/DataUtil.java index 807bcd6dfbe..bc4f9e1735e 100644 --- a/invalid/main/java/org/spongepowered/common/data/util/DataUtil.java +++ b/invalid/main/java/org/spongepowered/common/data/util/DataUtil.java @@ -43,16 +43,8 @@ public class DataUtil { - - public static final DataFixer spongeDataFixer = new DataFixer(Constants.Sponge.SPONGE_DATA_VERSION); private static final Supplier INVALID_DATA_EXCEPTION_SUPPLIER = InvalidDataException::new; - static { - spongeDataFixer.registerFix(FixTypes.LEVEL, new SpongeLevelFixer()); - spongeDataFixer.registerFix(FixTypes.ENTITY, new EntityTrackedUser()); - spongeDataFixer.registerFix(FixTypes.PLAYER, new PlayerRespawnData()); - } - @SuppressWarnings("rawtypes") public static T getData(final DataView dataView, final Key> key) throws InvalidDataException { checkDataExists(dataView, checkNotNull(key).getQuery()); diff --git a/src/main/java/org/spongepowered/common/data/SpongeDataManager.java b/src/main/java/org/spongepowered/common/data/SpongeDataManager.java index 3bcc07d44bf..02040e2171a 100644 --- a/src/main/java/org/spongepowered/common/data/SpongeDataManager.java +++ b/src/main/java/org/spongepowered/common/data/SpongeDataManager.java @@ -27,13 +27,16 @@ import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableList; import com.google.common.collect.MapMaker; -import com.google.common.collect.Maps; import com.google.common.reflect.TypeToken; import com.google.inject.Inject; import com.google.inject.Singleton; +import com.mojang.datafixers.DataFixer; +import com.mojang.datafixers.DataFixerBuilder; +import com.mojang.datafixers.schemas.Schema; import net.minecraft.nbt.CompoundNBT; import net.minecraft.nbt.INBT; import net.minecraft.nbt.ListNBT; +import net.minecraft.util.Util; import net.minecraft.util.registry.Registry; import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers; import org.spongepowered.api.CatalogKey; @@ -62,6 +65,9 @@ import org.spongepowered.common.SpongeImpl; import org.spongepowered.common.bridge.data.CustomDataHolderBridge; import org.spongepowered.common.config.DataSerializableTypeSerializer; +import org.spongepowered.common.data.fixer.entity.EntityTrackedUser; +import org.spongepowered.common.data.fixer.world.SpongeLevelFixer; +import org.spongepowered.common.data.key.KeyBasedDataListener; import org.spongepowered.common.data.persistence.NbtTranslator; import org.spongepowered.common.registry.MappedRegistry; import org.spongepowered.common.registry.SpongeCatalogRegistry; @@ -91,6 +97,16 @@ public final class SpongeDataManager implements DataManager { ); } + + public static final DataFixer spongeDataFixer = addFixers(new DataFixerBuilder(Constants.Sponge.SPONGE_DATA_VERSION)).build(Util.getServerExecutor()); + + static DataFixerBuilder addFixers(DataFixerBuilder builder) { + builder.addFixer(new SpongeLevelFixer(builder.addSchema(Constants.Legacy.World.WORLD_UUID_1_9_VERSION, Schema::new), true)); + builder.addFixer(new EntityTrackedUser(builder.addSchema(Constants.Legacy.Entity.TRACKER_ID_VERSION, Schema::new), true)); +// TODO this fixer did nothing builder.addFixer(new PlayerRespawnData(builder.addSchema( Constants.Sponge.PlayerData.RESPAWN_DATA_1_9_VERSION, Schema::new), true)); + return builder; + } + // Builders private final Map, DataBuilder> builders = new IdentityHashMap<>(); diff --git a/src/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java b/src/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java new file mode 100644 index 00000000000..8af84419dcb --- /dev/null +++ b/src/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java @@ -0,0 +1,69 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.common.data.fixer.entity; + +import com.mojang.datafixers.DSL; +import com.mojang.datafixers.DataFix; +import com.mojang.datafixers.OpticFinder; +import com.mojang.datafixers.TypeRewriteRule; +import com.mojang.datafixers.Typed; +import com.mojang.datafixers.schemas.Schema; +import com.mojang.datafixers.types.Type; +import net.minecraft.util.datafix.TypeReferences; +import org.spongepowered.common.data.fixer.world.SpongeLevelFixer; +import org.spongepowered.common.util.Constants; + +public class EntityTrackedUser extends DataFix { + + public EntityTrackedUser(Schema outputSchema, boolean changesType) { + super(outputSchema, changesType); + } + + @Override + protected TypeRewriteRule makeRule() { + final Type entityType = this.getInputSchema().getType(TypeReferences.ENTITY); + final Type forgeDataType = entityType.findFieldType(Constants.Forge.FORGE_DATA); + final Type spongeDataType = forgeDataType.findFieldType(Constants.Sponge.SPONGE_DATA); + + final OpticFinder forgeDataFinder = DSL.fieldFinder(Constants.Forge.FORGE_DATA, forgeDataType); + final OpticFinder spongeDataFinder = DSL.fieldFinder(Constants.Sponge.SPONGE_DATA, spongeDataType); + + return TypeRewriteRule.seq(this.fixTracked(forgeDataFinder, spongeDataFinder, spongeDataType, Constants.Sponge.SPONGE_ENTITY_CREATOR), + this.fixTracked(forgeDataFinder, spongeDataFinder, spongeDataType, Constants.Sponge.SPONGE_ENTITY_NOTIFIER)); + } + + private TypeRewriteRule fixTracked(OpticFinder forgeDataFinder, OpticFinder spongeDataFinder, Type spongeDataType, String name) { + final Type trackedType = spongeDataType.findFieldType(name); + final OpticFinder trackedFinder = DSL.fieldFinder(name, trackedType); + + return this.fixTypeEverywhereTyped("Entity" + name + "UserFix", this.getInputSchema().getType(TypeReferences.ENTITY), + type -> { + final Typed forge = type.getTyped(forgeDataFinder); + final Typed sponge = forge.getTyped(spongeDataFinder); + return SpongeLevelFixer.updateUUIDIn(sponge.getTyped(trackedFinder)); + }); + } + +} diff --git a/src/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java b/src/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java new file mode 100644 index 00000000000..76437f5f6f8 --- /dev/null +++ b/src/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java @@ -0,0 +1,74 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.common.data.fixer.world; + +import com.mojang.datafixers.DSL; +import com.mojang.datafixers.DataFix; +import com.mojang.datafixers.OpticFinder; +import com.mojang.datafixers.TypeRewriteRule; +import com.mojang.datafixers.Typed; +import com.mojang.datafixers.schemas.Schema; +import com.mojang.datafixers.types.Type; +import com.mojang.datafixers.types.templates.List; +import net.minecraft.util.datafix.TypeReferences; +import org.spongepowered.common.util.Constants; + +public class SpongeLevelFixer extends DataFix { + + public SpongeLevelFixer(Schema outputSchema, boolean changesType) { + super(outputSchema, changesType); + } + + public TypeRewriteRule makeRule() { + final Type levelType = this.getOutputSchema().getType(TypeReferences.LEVEL); + return TypeRewriteRule.seq( + this.fixTypeEverywhereTyped("FixWorldUniqueId", levelType, SpongeLevelFixer::updateUUIDIn), + this.fixTypeEverywhereTyped("FixPlayerIdTable", levelType, type -> this.fixPlayerIdTable(type, levelType)) + ); + } + + private Typed fixPlayerIdTable(Typed typed, Type levelType) { + Type fieldType = levelType.findFieldType(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE); + OpticFinder> listFinder = DSL.fieldFinder(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE, (List.ListType)fieldType); + + Typed> listTyped = typed.getTyped(listFinder); + // TODO is this correct? + return listTyped.updateRecursiveTyped(DSL.remainderFinder(), SpongeLevelFixer::updateUUIDIn); + } + + public static Typed updateUUIDIn(Typed typed) { + return typed.update(DSL.remainderFinder(), data -> { + final long least = data.get(Constants.Legacy.Entity.UUID_LEAST_1_8).asLong(0L); + final long most = data.get(Constants.Legacy.Entity.UUID_MOST_1_8).asLong(0L); + if (least != 0 && most != 0) { + return data.remove(Constants.Legacy.Entity.UUID_LEAST_1_8) + .remove(Constants.Legacy.Entity.UUID_MOST_1_8) + .set(Constants.UUID_MOST, data.createLong(most)) + .set(Constants.UUID_LEAST, data.createLong(least)); + } + return data; + }); + } +}