Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix API8 datafixers #2622

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd probably want to call this PlayerRespawnDataFix

@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) {
Expand Down Expand Up @@ -71,4 +75,9 @@ public CompoundNBT fixTagCompound(CompoundNBT compound) {
}
return compound;
}

@Override
protected TypeRewriteRule makeRule() {
return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,8 @@

public class DataUtil {


public static final DataFixer spongeDataFixer = new DataFixer(Constants.Sponge.SPONGE_DATA_VERSION);
private static final Supplier<InvalidDataException> 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> T getData(final DataView dataView, final Key<? extends Value<T>> key) throws InvalidDataException {
checkDataExists(dataView, checkNotNull(key).getQuery());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<Class<?>, DataBuilder<?>> builders = new IdentityHashMap<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is where things go down hill...

So TypeReferences.ENTITY does not exist on any schemas of Sponge's datafixer.

This means this will fail to fix, hard fail. The solution to this is the include minecraft's own schema into your datafixer, at a lower version than sponge's current schema version so the type reference exists.

Even then, there is a whole extra can of beans with the entity type reference, but that is closer to how modded entities are handled during registration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However that itself can be a mess, and I need to sleep. So I can clarify tomorrow.

type -> {
final Typed<?> forge = type.getTyped(forgeDataFinder);
final Typed<?> sponge = forge.getTyped(spongeDataFinder);
return SpongeLevelFixer.updateUUIDIn(sponge.getTyped(trackedFinder));
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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<List.ListType<?>> listFinder = DSL.fieldFinder(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE, (List.ListType)fieldType);

Typed<List.ListType<?>> 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;
});
}
}