1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-10 09:20:28 +00:00

Move block entity component fixes to separate fixer

Some mods run their own datafixer chain, rather than piggybacking on top
of vanilla's. This is A BAD IDEA, but what can you do. If such a mod
tries to use ItemStackComponentizationFix in their own schema, then
CC:T's mixins will try to look up the turtle block entitie, and fail (as
they're not registered under the modded schema).

We now inject the block entity fix as a separate fixer, rather than
abusing ItemStackComponentizationFix.

See #2012
This commit is contained in:
Jonathan Coates 2024-11-23 08:53:54 +00:00
parent 0056709999
commit 7af2c14327
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
7 changed files with 87 additions and 18 deletions

View File

@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.mixin;
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.datafixers.DataFixUtils;
import com.mojang.datafixers.DataFixerBuilder;
import com.mojang.datafixers.schemas.Schema;
import dan200.computercraft.shared.datafix.TurtleUpgradeComponentizationFix;
import net.minecraft.util.datafix.DataFixers;
import net.minecraft.util.datafix.fixes.ItemStackComponentizationFix;
import net.minecraft.util.datafix.schemas.V3818_5;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
@Mixin(DataFixers.class)
abstract class DataFixersMixin {
/**
* Register {@link TurtleUpgradeComponentizationFix} alongside {@link ItemStackComponentizationFix}.
* <p>
* We use a {@link ModifyArg} to capture the schema passed to {@link ItemStackComponentizationFix}. This is a
* little gross, but is the easiest way to obtain the schema without hard-coding local ordinals.
*
* @param schema The {@link V3818_5} schema.
* @param builder The current datafixer builder
* @return The input schema.
*/
@ModifyArg(
method = "addFixers",
at = @At(value = "INVOKE", target = "Lnet/minecraft/util/datafix/fixes/ItemStackComponentizationFix;<init>(Lcom/mojang/datafixers/schemas/Schema;)V"),
index = 0,
allow = 1
)
@SuppressWarnings("UnusedMethod")
private static Schema addComponentizationFixes(Schema schema, @Local DataFixerBuilder builder) {
assertSchemaVersion(schema, DataFixUtils.makeKey(3818, 5));
builder.addFixer(new TurtleUpgradeComponentizationFix(schema));
return schema;
}
@Unique
private static void assertSchemaVersion(Schema schema, int version) {
if (schema.getVersionKey() != version) {
throw new IllegalStateException("Unexpected schema version. Expected " + version + ", got " + schema.getVersionKey());
}
}
}

View File

@ -4,14 +4,11 @@
package dan200.computercraft.mixin; package dan200.computercraft.mixin;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.datafixers.DataFix; import com.mojang.datafixers.DataFix;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema; import com.mojang.datafixers.schemas.Schema;
import com.mojang.serialization.Dynamic; import com.mojang.serialization.Dynamic;
import dan200.computercraft.shared.util.ComponentizationFixers; import dan200.computercraft.shared.datafix.ComponentizationFixers;
import net.minecraft.util.datafix.fixes.ItemStackComponentizationFix; import net.minecraft.util.datafix.fixes.ItemStackComponentizationFix;
import net.minecraft.util.datafix.fixes.References;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
@ -35,15 +32,4 @@ abstract class ItemStackComponentizationFixMixin extends DataFix {
private static void fixItemStack(ItemStackComponentizationFix.ItemStackData data, Dynamic<?> ops, CallbackInfo ci) { private static void fixItemStack(ItemStackComponentizationFix.ItemStackData data, Dynamic<?> ops, CallbackInfo ci) {
ComponentizationFixers.fixItemComponents(data, ops); ComponentizationFixers.fixItemComponents(data, ops);
} }
@ModifyReturnValue(method = "makeRule", at = @At("RETURN"), remap = false)
@SuppressWarnings("UnusedMethod")
private TypeRewriteRule wrapMakeRule(TypeRewriteRule existing) {
return TypeRewriteRule.seq(existing, fixTypeEverywhereTyped(
"Turtle upgrade componentization",
getInputSchema().getType(References.BLOCK_ENTITY),
getOutputSchema().getType(References.BLOCK_ENTITY),
ComponentizationFixers.makeBlockEntityRewrites(getInputSchema(), getOutputSchema())
));
}
} }

View File

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: MPL-2.0 // SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.util; package dan200.computercraft.shared.datafix;
import com.mojang.datafixers.DSL; import com.mojang.datafixers.DSL;
import com.mojang.datafixers.Typed; import com.mojang.datafixers.Typed;

View File

@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: 2024 The CC: Tweaked Developers
//
// SPDX-License-Identifier: MPL-2.0
package dan200.computercraft.shared.datafix;
import com.mojang.datafixers.DataFix;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
import net.minecraft.util.datafix.fixes.References;
/**
* Rewrites turtle block entities to store upgrades as components.
*
* @see ComponentizationFixers#makeBlockEntityRewrites(Schema, Schema)
*/
public class TurtleUpgradeComponentizationFix extends DataFix {
public TurtleUpgradeComponentizationFix(Schema outputSchema) {
super(outputSchema, true);
}
@Override
protected TypeRewriteRule makeRule() {
return fixTypeEverywhereTyped(
"Turtle upgrade componentization",
getInputSchema().getType(References.BLOCK_ENTITY),
getOutputSchema().getType(References.BLOCK_ENTITY),
ComponentizationFixers.makeBlockEntityRewrites(getInputSchema(), getOutputSchema())
);
}
}

View File

@ -7,6 +7,7 @@
"defaultRequire": 1 "defaultRequire": 1
}, },
"mixins": [ "mixins": [
"DataFixersMixin",
"ItemStackComponentizationFixMixin", "ItemStackComponentizationFixMixin",
"V1460Mixin" "V1460Mixin"
] ]

View File

@ -7,7 +7,7 @@ package dan200.computercraft.mixin;
import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.datafixers.schemas.Schema; import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.templates.TypeTemplate; import com.mojang.datafixers.types.templates.TypeTemplate;
import dan200.computercraft.shared.util.ComponentizationFixers; import dan200.computercraft.shared.datafix.ComponentizationFixers;
import net.minecraft.util.datafix.schemas.V3818_3; import net.minecraft.util.datafix.schemas.V3818_3;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;

View File

@ -7,7 +7,7 @@ package dan200.computercraft.mixin;
import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.datafixers.schemas.Schema; import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.templates.TypeTemplate; import com.mojang.datafixers.types.templates.TypeTemplate;
import dan200.computercraft.shared.util.ComponentizationFixers; import dan200.computercraft.shared.datafix.ComponentizationFixers;
import net.minecraft.util.datafix.schemas.V3818_3; import net.minecraft.util.datafix.schemas.V3818_3;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;