Skip to content

Commit fa79016

Browse files
committed
Merge DataFixers update for API 8. Merges #2622.
Signed-off-by: Gabriel Harris-Rouquette <[email protected]>
2 parents 0ee1ec8 + 3b8a981 commit fa79016

File tree

7 files changed

+173
-147
lines changed

7 files changed

+173
-147
lines changed

invalid/main/java/org/spongepowered/common/data/fixer/entity/EntityTrackedUser.java

-67
This file was deleted.

invalid/main/java/org/spongepowered/common/data/fixer/entity/player/PlayerRespawnData.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
package org.spongepowered.common.data.fixer.entity.player;
2626

2727
import com.google.common.collect.Maps;
28+
import com.mojang.datafixers.DataFix;
29+
import com.mojang.datafixers.TypeRewriteRule;
30+
import com.mojang.datafixers.schemas.Schema;
2831
import net.minecraft.nbt.CompoundNBT;
2932
import net.minecraft.nbt.ListNBT;
3033
import net.minecraft.util.datafix.IFixableData;
@@ -35,12 +38,13 @@
3538
import java.util.Map;
3639
import java.util.UUID;
3740

38-
public class PlayerRespawnData implements IFixableData {
41+
// TODO this is actually doing nothing?
42+
public class PlayerRespawnData extends DataFix {
3943

40-
@Override
41-
public int getFixVersion() {
42-
return Constants.Sponge.PlayerData.RESPAWN_DATA_1_9_VERSION;
44+
public PlayerRespawnData(Schema outputSchema, boolean changesType) {
45+
super(outputSchema, changesType);
4346
}
47+
FixTypes.PLAYER,
4448

4549
@Override
4650
public CompoundNBT fixTagCompound(CompoundNBT compound) {
@@ -71,4 +75,9 @@ public CompoundNBT fixTagCompound(CompoundNBT compound) {
7175
}
7276
return compound;
7377
}
78+
79+
@Override
80+
protected TypeRewriteRule makeRule() {
81+
return null;
82+
}
7483
}

invalid/main/java/org/spongepowered/common/data/fixer/world/SpongeLevelFixer.java

-67
This file was deleted.

invalid/main/java/org/spongepowered/common/data/util/DataUtil.java

-8
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,8 @@
4343

4444
public class DataUtil {
4545

46-
47-
public static final DataFixer spongeDataFixer = new DataFixer(Constants.Sponge.SPONGE_DATA_VERSION);
4846
private static final Supplier<InvalidDataException> INVALID_DATA_EXCEPTION_SUPPLIER = InvalidDataException::new;
4947

50-
static {
51-
spongeDataFixer.registerFix(FixTypes.LEVEL, new SpongeLevelFixer());
52-
spongeDataFixer.registerFix(FixTypes.ENTITY, new EntityTrackedUser());
53-
spongeDataFixer.registerFix(FixTypes.PLAYER, new PlayerRespawnData());
54-
}
55-
5648
@SuppressWarnings("rawtypes")
5749
public static <T> T getData(final DataView dataView, final Key<? extends Value<T>> key) throws InvalidDataException {
5850
checkDataExists(dataView, checkNotNull(key).getQuery());

src/main/java/org/spongepowered/common/data/SpongeDataManager.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,16 @@
2727
import com.google.common.base.Preconditions;
2828
import com.google.common.collect.ImmutableList;
2929
import com.google.common.collect.MapMaker;
30-
import com.google.common.collect.Maps;
3130
import com.google.common.reflect.TypeToken;
3231
import com.google.inject.Inject;
3332
import com.google.inject.Singleton;
33+
import com.mojang.datafixers.DataFixer;
34+
import com.mojang.datafixers.DataFixerBuilder;
35+
import com.mojang.datafixers.schemas.Schema;
3436
import net.minecraft.nbt.CompoundNBT;
3537
import net.minecraft.nbt.INBT;
3638
import net.minecraft.nbt.ListNBT;
39+
import net.minecraft.util.Util;
3740
import net.minecraft.util.registry.Registry;
3841
import ninja.leaping.configurate.objectmapping.serialize.TypeSerializers;
3942
import org.spongepowered.api.CatalogKey;
@@ -62,6 +65,9 @@
6265
import org.spongepowered.common.SpongeImpl;
6366
import org.spongepowered.common.bridge.data.CustomDataHolderBridge;
6467
import org.spongepowered.common.config.DataSerializableTypeSerializer;
68+
import org.spongepowered.common.data.fixer.entity.EntityTrackedUser;
69+
import org.spongepowered.common.data.fixer.world.SpongeLevelFixer;
70+
import org.spongepowered.common.data.key.KeyBasedDataListener;
6571
import org.spongepowered.common.data.persistence.NbtTranslator;
6672
import org.spongepowered.common.registry.MappedRegistry;
6773
import org.spongepowered.common.registry.SpongeCatalogRegistry;
@@ -91,6 +97,16 @@ public final class SpongeDataManager implements DataManager {
9197
);
9298
}
9399

100+
101+
public static final DataFixer spongeDataFixer = addFixers(new DataFixerBuilder(Constants.Sponge.SPONGE_DATA_VERSION)).build(Util.getServerExecutor());
102+
103+
static DataFixerBuilder addFixers(DataFixerBuilder builder) {
104+
builder.addFixer(new SpongeLevelFixer(builder.addSchema(Constants.Legacy.World.WORLD_UUID_1_9_VERSION, Schema::new), true));
105+
builder.addFixer(new EntityTrackedUser(builder.addSchema(Constants.Legacy.Entity.TRACKER_ID_VERSION, Schema::new), true));
106+
// TODO this fixer did nothing builder.addFixer(new PlayerRespawnData(builder.addSchema( Constants.Sponge.PlayerData.RESPAWN_DATA_1_9_VERSION, Schema::new), true));
107+
return builder;
108+
}
109+
94110
// Builders
95111
private final Map<Class<?>, DataBuilder<?>> builders = new IdentityHashMap<>();
96112

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.data.fixer.entity;
26+
27+
import com.mojang.datafixers.DSL;
28+
import com.mojang.datafixers.DataFix;
29+
import com.mojang.datafixers.OpticFinder;
30+
import com.mojang.datafixers.TypeRewriteRule;
31+
import com.mojang.datafixers.Typed;
32+
import com.mojang.datafixers.schemas.Schema;
33+
import com.mojang.datafixers.types.Type;
34+
import net.minecraft.util.datafix.TypeReferences;
35+
import org.spongepowered.common.data.fixer.world.SpongeLevelFixer;
36+
import org.spongepowered.common.util.Constants;
37+
38+
public class EntityTrackedUser extends DataFix {
39+
40+
public EntityTrackedUser(Schema outputSchema, boolean changesType) {
41+
super(outputSchema, changesType);
42+
}
43+
44+
@Override
45+
protected TypeRewriteRule makeRule() {
46+
final Type<?> entityType = this.getInputSchema().getType(TypeReferences.ENTITY);
47+
final Type<?> forgeDataType = entityType.findFieldType(Constants.Forge.FORGE_DATA);
48+
final Type<?> spongeDataType = forgeDataType.findFieldType(Constants.Sponge.SPONGE_DATA);
49+
50+
final OpticFinder<?> forgeDataFinder = DSL.fieldFinder(Constants.Forge.FORGE_DATA, forgeDataType);
51+
final OpticFinder<?> spongeDataFinder = DSL.fieldFinder(Constants.Sponge.SPONGE_DATA, spongeDataType);
52+
53+
return TypeRewriteRule.seq(this.fixTracked(forgeDataFinder, spongeDataFinder, spongeDataType, Constants.Sponge.SPONGE_ENTITY_CREATOR),
54+
this.fixTracked(forgeDataFinder, spongeDataFinder, spongeDataType, Constants.Sponge.SPONGE_ENTITY_NOTIFIER));
55+
}
56+
57+
private TypeRewriteRule fixTracked(OpticFinder<?> forgeDataFinder, OpticFinder<?> spongeDataFinder, Type<?> spongeDataType, String name) {
58+
final Type<?> trackedType = spongeDataType.findFieldType(name);
59+
final OpticFinder<?> trackedFinder = DSL.fieldFinder(name, trackedType);
60+
61+
return this.fixTypeEverywhereTyped("Entity" + name + "UserFix", this.getInputSchema().getType(TypeReferences.ENTITY),
62+
type -> {
63+
final Typed<?> forge = type.getTyped(forgeDataFinder);
64+
final Typed<?> sponge = forge.getTyped(spongeDataFinder);
65+
return SpongeLevelFixer.updateUUIDIn(sponge.getTyped(trackedFinder));
66+
});
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.data.fixer.world;
26+
27+
import com.mojang.datafixers.DSL;
28+
import com.mojang.datafixers.DataFix;
29+
import com.mojang.datafixers.OpticFinder;
30+
import com.mojang.datafixers.TypeRewriteRule;
31+
import com.mojang.datafixers.Typed;
32+
import com.mojang.datafixers.schemas.Schema;
33+
import com.mojang.datafixers.types.Type;
34+
import com.mojang.datafixers.types.templates.List;
35+
import net.minecraft.util.datafix.TypeReferences;
36+
import org.spongepowered.common.util.Constants;
37+
38+
public class SpongeLevelFixer extends DataFix {
39+
40+
public SpongeLevelFixer(Schema outputSchema, boolean changesType) {
41+
super(outputSchema, changesType);
42+
}
43+
44+
public TypeRewriteRule makeRule() {
45+
final Type<?> levelType = this.getOutputSchema().getType(TypeReferences.LEVEL);
46+
return TypeRewriteRule.seq(
47+
this.fixTypeEverywhereTyped("FixWorldUniqueId", levelType, SpongeLevelFixer::updateUUIDIn),
48+
this.fixTypeEverywhereTyped("FixPlayerIdTable", levelType, type -> this.fixPlayerIdTable(type, levelType))
49+
);
50+
}
51+
52+
private Typed<?> fixPlayerIdTable(Typed<?> typed, Type<?> levelType) {
53+
Type<?> fieldType = levelType.findFieldType(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE);
54+
OpticFinder<List.ListType<?>> listFinder = DSL.fieldFinder(Constants.Sponge.SPONGE_PLAYER_UUID_TABLE, (List.ListType)fieldType);
55+
56+
Typed<List.ListType<?>> listTyped = typed.getTyped(listFinder);
57+
// TODO is this correct?
58+
return listTyped.updateRecursiveTyped(DSL.remainderFinder(), SpongeLevelFixer::updateUUIDIn);
59+
}
60+
61+
public static Typed<?> updateUUIDIn(Typed<?> typed) {
62+
return typed.update(DSL.remainderFinder(), data -> {
63+
final long least = data.get(Constants.Legacy.Entity.UUID_LEAST_1_8).asLong(0L);
64+
final long most = data.get(Constants.Legacy.Entity.UUID_MOST_1_8).asLong(0L);
65+
if (least != 0 && most != 0) {
66+
return data.remove(Constants.Legacy.Entity.UUID_LEAST_1_8)
67+
.remove(Constants.Legacy.Entity.UUID_MOST_1_8)
68+
.set(Constants.UUID_MOST, data.createLong(most))
69+
.set(Constants.UUID_LEAST, data.createLong(least));
70+
}
71+
return data;
72+
});
73+
}
74+
}

0 commit comments

Comments
 (0)