Skip to content

Commit

Permalink
Add datafixers that allow 1.17->1.18 upgrades
Browse files Browse the repository at this point in the history
  • Loading branch information
jsorrell committed May 2, 2022
1 parent 27abad0 commit 345029f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 1 deletion.
43 changes: 43 additions & 0 deletions src/main/java/com/jsorrell/skyblock/datafixer/Schema2551.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.jsorrell.skyblock.datafixer;

import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.templates.TypeTemplate;
import net.minecraft.datafixer.TypeReferences;
import net.minecraft.datafixer.schema.IdentifierNormalizingSchema;

import java.util.Map;
import java.util.function.Supplier;

public class Schema2551 extends IdentifierNormalizingSchema {
public Schema2551(int versionKey, Schema parent) {
super(versionKey, parent);
}

@Override
public void registerTypes(Schema schema, Map<String, Supplier<TypeTemplate>> entityTypes, Map<String, Supplier<TypeTemplate>> blockEntityTypes) {
super.registerTypes(schema, entityTypes, blockEntityTypes);
schema.registerType(false, TypeReferences.WORLD_GEN_SETTINGS, () -> {
return DSL.fields("dimensions", DSL.compoundList(DSL.constType(getIdentifierType()), DSL.fields("generator", DSL.taggedChoiceLazy("type", DSL.string(), ImmutableMap.of("minecraft:debug", DSL::remainder, "minecraft:flat", () -> {
return DSL.optionalFields("settings", DSL.optionalFields("biome", TypeReferences.BIOME.in(schema), "layers", DSL.list(DSL.optionalFields("block", TypeReferences.BLOCK_NAME.in(schema)))));
}, "minecraft:noise", () -> {
return DSL.optionalFields("biome_source", DSL.taggedChoiceLazy("type", DSL.string(), ImmutableMap.of("minecraft:fixed", () -> {
return DSL.fields("biome", TypeReferences.BIOME.in(schema));
}, "minecraft:multi_noise", () -> {
return DSL.list(DSL.fields("biome", TypeReferences.BIOME.in(schema)));
}, "minecraft:checkerboard", () -> {
return DSL.fields("biomes", DSL.list(TypeReferences.BIOME.in(schema)));
}, "minecraft:vanilla_layered", DSL::remainder, "minecraft:the_end", DSL::remainder)), "settings", DSL.or(DSL.constType(DSL.string()), DSL.optionalFields("default_block", TypeReferences.BLOCK_NAME.in(schema), "default_fluid", TypeReferences.BLOCK_NAME.in(schema))));
}, "minecraft:skyblock", () -> { // Add minecraft:skyblock as valid option
return DSL.optionalFields("biome_source", DSL.taggedChoiceLazy("type", DSL.string(), ImmutableMap.of("minecraft:fixed", () -> {
return DSL.fields("biome", TypeReferences.BIOME.in(schema));
}, "minecraft:multi_noise", () -> {
return DSL.list(DSL.fields("biome", TypeReferences.BIOME.in(schema)));
}, "minecraft:checkerboard", () -> {
return DSL.fields("biomes", DSL.list(TypeReferences.BIOME.in(schema)));
}, "minecraft:vanilla_layered", DSL::remainder, "minecraft:the_end", DSL::remainder)), "settings", DSL.or(DSL.constType(DSL.string()), DSL.optionalFields("default_block", TypeReferences.BLOCK_NAME.in(schema), "default_fluid", TypeReferences.BLOCK_NAME.in(schema))));
})))));
});
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/jsorrell/skyblock/datafixer/Schema2832.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.jsorrell.skyblock.datafixer;

import com.google.common.collect.ImmutableMap;
import com.mojang.datafixers.DSL;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.templates.TypeTemplate;
import net.minecraft.datafixer.TypeReferences;
import net.minecraft.datafixer.schema.IdentifierNormalizingSchema;

import java.util.Map;
import java.util.function.Supplier;

public class Schema2832 extends IdentifierNormalizingSchema {
public Schema2832(int versionKey, Schema parent) {
super(versionKey, parent);
}

@Override
public void registerTypes(Schema schema, Map<String, Supplier<TypeTemplate>> entityTypes, Map<String, Supplier<TypeTemplate>> blockEntityTypes) {
super.registerTypes(schema, entityTypes, blockEntityTypes);
schema.registerType(false, TypeReferences.CHUNK, () -> {
return DSL.fields("Level", DSL.optionalFields("Entities", DSL.list(TypeReferences.ENTITY_TREE.in(schema)), "TileEntities", DSL.list(DSL.or(TypeReferences.BLOCK_ENTITY.in(schema), DSL.remainder())), "TileTicks", DSL.list(DSL.fields("i", TypeReferences.BLOCK_NAME.in(schema))), "Sections", DSL.list(DSL.optionalFields("biomes", DSL.optionalFields("palette", DSL.list(TypeReferences.BIOME.in(schema))), "block_states", DSL.optionalFields("palette", DSL.list(TypeReferences.BLOCK_STATE.in(schema))))), "Structures", DSL.optionalFields("Starts", DSL.compoundList(TypeReferences.STRUCTURE_FEATURE.in(schema)))));
});
schema.registerType(false, TypeReferences.WORLD_GEN_SETTINGS, () -> {
return DSL.fields("dimensions", DSL.compoundList(DSL.constType(getIdentifierType()), DSL.fields("generator", DSL.taggedChoiceLazy("type", DSL.string(), ImmutableMap.of("minecraft:debug", DSL::remainder, "minecraft:flat", () -> {
return DSL.optionalFields("settings", DSL.optionalFields("biome", TypeReferences.BIOME.in(schema), "layers", DSL.list(DSL.optionalFields("block", TypeReferences.BLOCK_NAME.in(schema)))));
}, "minecraft:noise", () -> {
return DSL.optionalFields("biome_source", DSL.taggedChoiceLazy("type", DSL.string(), ImmutableMap.of("minecraft:fixed", () -> {
return DSL.fields("biome", TypeReferences.BIOME.in(schema));
}, "minecraft:multi_noise", () -> {
return DSL.or(DSL.fields("preset", getIdentifierType().template()), DSL.list(DSL.fields("biome", TypeReferences.BIOME.in(schema))));
}, "minecraft:checkerboard", () -> {
return DSL.fields("biomes", DSL.list(TypeReferences.BIOME.in(schema)));
}, "minecraft:the_end", DSL::remainder)), "settings", DSL.or(DSL.constType(DSL.string()), DSL.optionalFields("default_block", TypeReferences.BLOCK_NAME.in(schema), "default_fluid", TypeReferences.BLOCK_NAME.in(schema))));
}, "minecraft:skyblock", () -> { // Add minecraft:skyblock as valid option
return DSL.optionalFields("biome_source", DSL.taggedChoiceLazy("type", DSL.string(), ImmutableMap.of("minecraft:fixed", () -> {
return DSL.fields("biome", TypeReferences.BIOME.in(schema));
}, "minecraft:multi_noise", () -> {
return DSL.or(DSL.fields("preset", getIdentifierType().template()), DSL.list(DSL.fields("biome", TypeReferences.BIOME.in(schema))));
}, "minecraft:checkerboard", () -> {
return DSL.fields("biomes", DSL.list(TypeReferences.BIOME.in(schema)));
}, "minecraft:the_end", DSL::remainder)), "settings", DSL.or(DSL.constType(DSL.string()), DSL.optionalFields("default_block", TypeReferences.BLOCK_NAME.in(schema), "default_fluid", TypeReferences.BLOCK_NAME.in(schema))));
})))));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jsorrell.skyblock.mixin;

import com.mojang.datafixers.DataFix;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.Type;
import net.minecraft.datafixer.fix.MissingDimensionFix;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.HashMap;
import java.util.Map;

@Mixin(MissingDimensionFix.class)
public abstract class MissingDimensionFixMixin extends DataFix {
public MissingDimensionFixMixin(Schema outputSchema, boolean changesType) {
super(outputSchema, changesType);
}

@ModifyArg(method = "makeRule",
at = @At(value = "INVOKE",
target = "Lcom/mojang/datafixers/types/templates/TaggedChoice$TaggedChoiceType;<init>(Ljava/lang/String;Lcom/mojang/datafixers/types/Type;Ljava/util/Map;)V",
ordinal = 0),
index = 2)
private Map<String, Type<?>> modifyTypes(Map<String, Type<?>> types) {
Map<String, Type<?>> newTypes = new HashMap<>(types);
newTypes.put("minecraft:skyblock", types.get("minecraft:noise"));
return newTypes;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/jsorrell/skyblock/mixin/SchemasMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.jsorrell.skyblock.mixin;

import com.jsorrell.skyblock.datafixer.Schema2551;
import com.jsorrell.skyblock.datafixer.Schema2832;
import com.mojang.datafixers.schemas.Schema;
import net.minecraft.datafixer.Schemas;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;

import java.util.function.BiFunction;

@Mixin(Schemas.class)
public abstract class SchemasMixin {
@ModifyArg(
method = "build",
at = @At(value = "INVOKE",
target = "Lcom/mojang/datafixers/DataFixerBuilder;addSchema(ILjava/util/function/BiFunction;)Lcom/mojang/datafixers/schemas/Schema;",
ordinal = 112),
index = 1
)
private static BiFunction<Integer, Schema, Schema> replaceSchema2551(BiFunction<Integer, Schema, Schema> factory) {
return Schema2551::new;
}

@ModifyArg(
method = "build",
at = @At(value = "INVOKE",
target = "Lcom/mojang/datafixers/DataFixerBuilder;addSchema(ILjava/util/function/BiFunction;)Lcom/mojang/datafixers/schemas/Schema;",
ordinal = 136),
index = 1
)
private static BiFunction<Integer, Schema, Schema> replaceSchema2832(BiFunction<Integer, Schema, Schema> factory) {
return Schema2832::new;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.jsorrell.skyblock.mixin;

import net.minecraft.datafixer.fix.WorldGenSettingsHeightAndBiomeFix;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(WorldGenSettingsHeightAndBiomeFix.class)
public class WorldGenSettingsHeightAndBiomeFixMixin {
@Redirect(method = "method_38834(ZZLcom/mojang/serialization/Dynamic;)Lcom/mojang/serialization/Dynamic;",
at = @At(value = "INVOKE", target = "Ljava/lang/String;equals(Ljava/lang/Object;)Z"))
private static boolean datafixSkyBlock(String s1, Object s2) {
return s1.equals(s2) || "minecraft:skyblock".equals(s2);
}
}
5 changes: 4 additions & 1 deletion src/main/resources/skyblock.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
"LightningEntityMixin",
"LivingEntityMixin",
"MinecraftServerMixin",
"MissingDimensionFixMixin",
"RamImpactTaskMixin",
"SaplingBlockMixin",
"SchemasMixin",
"SinglePoolElementAccessor",
"SpawnRestrictionAccessor",
"SpiderEntityMixin",
"WanderingTraderEntityMixin"
"WanderingTraderEntityMixin",
"WorldGenSettingsHeightAndBiomeFixMixin"
],
"client": [
"GeneratorTypeAccessor"
Expand Down

0 comments on commit 345029f

Please sign in to comment.