diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java index 5d13cda3..62878768 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/InjectionPoint.java @@ -289,6 +289,19 @@ public abstract class InjectionPoint { * @return restriction level */ public RestrictTargetLevel getTargetRestriction(IInjectionPointContext context) { + return RestrictTargetLevel.CONSTRUCTORS_AFTER_DELEGATE; // Fabric change: allow inject in constructors + } + + /** + * Returns the target restriction level for this injection point's cancellation for + * {@literal @Inject} annotations. This level defines whether an injection point + * can declare {@code cancellable = true}. + * + * @param context injection-specific context + * @return restriction level + */ + // Fabric addition: prevent cancellation of inject in constructors + public RestrictTargetLevel getCancellationRestriction(IInjectionPointContext context) { return RestrictTargetLevel.METHODS_ONLY; } diff --git a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java index d36ed952..98be09bc 100644 --- a/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java +++ b/src/main/java/org/spongepowered/asm/mixin/injection/callback/CallbackInjector.java @@ -397,6 +397,16 @@ public class CallbackInjector extends Injector { throw new InvalidInjectionException(this.info, String.format("%s selector %s", ip, ex.getMessage())); } + // Fabric start: prevent cancellation in constructor injections + if (this.cancellable) { + try { + this.checkTargetForNode(target, injectionNode, ip.getCancellationRestriction(this.info)); + } catch (InvalidInjectionException ex) { + throw new InvalidInjectionException(this.info, String.format("%s selector (cancellable = true) %s", ip, ex.getMessage())); + } + } + // Fabric end + String id = ip.getId(); if (Strings.isNullOrEmpty(id)) { continue;