-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
7 changed files
with
87 additions
and
18 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
projects/common/src/main/java/dan200/computercraft/mixin/DataFixersMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...n/src/main/java/dan200/computercraft/shared/datafix/TurtleUpgradeComponentizationFix.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters