Skip to content

Commit

Permalink
Move block entity component fixes to separate fixer
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SquidDev committed Nov 23, 2024
1 parent 0056709 commit 7af2c14
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,11 @@

package dan200.computercraft.mixin;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.datafixers.DataFix;
import com.mojang.datafixers.TypeRewriteRule;
import com.mojang.datafixers.schemas.Schema;
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.References;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -35,15 +32,4 @@ private ItemStackComponentizationFixMixin(Schema outputSchema, boolean changesTy
private static void fixItemStack(ItemStackComponentizationFix.ItemStackData data, Dynamic<?> ops, CallbackInfo ci) {
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())
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// 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.Typed;
Expand Down
Original file line number Diff line number Diff line change
@@ -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())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"defaultRequire": 1
},
"mixins": [
"DataFixersMixin",
"ItemStackComponentizationFixMixin",
"V1460Mixin"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.datafixers.schemas.Schema;
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 org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.datafixers.schemas.Schema;
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 org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
Expand Down

0 comments on commit 7af2c14

Please sign in to comment.