Skip to content

Instantly share code, notes, and snippets.

@TheGlitch76
Last active February 17, 2020 17:01
Show Gist options
  • Save TheGlitch76/256b45f336784ddcc5879a63ed9b0051 to your computer and use it in GitHub Desktop.
Save TheGlitch76/256b45f336784ddcc5879a63ed9b0051 to your computer and use it in GitHub Desktop.
:tiny_potato:
@Mixin(StatusEffectInstance.class)
public abstract class MixinStatusEffectInstance implements UpdateDurationSuperShim {
@Unique
private static final Object BLANK = new Object();
@Shadow
protected abstract int updateDuration();
@Unique
private ThreadLocal<Object> updateDurationLocal = new ThreadLocal<>();
@Inject(method = "updateDuration", at = @At("HEAD"), cancellable = true)
private void hookUpdateDuration(CallbackInfoReturnable<Integer> cir) {
// If this is *not* a super call
if(updateDurationLocal.get() == null) {
if(this instanceof OverridesUpdateDuration) {
// cancel + return whatever it spits out
cir.setReturnValue(((OverridesUpdateDuration) this).patchwork$override$updateDuration());
}
} else {
updateDurationLocal.remove();
// continue with execution
}
}
@Override
public int patchwork$superShim$updateDuration() {
updateDurationLocal.set(BLANK);
return updateDuration();
}
public interface OverridesUpdateDuration {
public int patchwork$override$updateDuration();
}
public class PatchworkStatusEffectInstance extends StatusEffectInstance implements OverridesUpdateDuration {
public PatchworkStatusEffectInstance(StatusEffectInstance statusEffectInstance) {
super(statusEffectInstance);
}
@Override
public int patchwork$override$updateDuration() {
throw new RuntimeException("duration: " + ((UpdateDurationSuperShim) this).patchwork$superShim$updateDuration());
}
@Override
public int compareTo(StatusEffectInstance statusEffectInstance) {
throw new AssertionError("unimplemented");
}
}
public interface UpdateDurationSuperShim {
public int patchwork$superShim$updateDuration();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment