Skip to content

Commit a59a15d

Browse files
authored
Use conventional mixin structure for submixin pattern (#195)
1 parent d4b747f commit a59a15d

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

tags/faq/mixinoverride.ytag

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For mod compatibility reasons, you should not do this. Instead, you should use o
1818

1919
// Mixin the parent, and use instanceof to check what `this` is
2020
@Mixin(Entity.class)
21-
class EntityMixin {
21+
abstract class EntityMixin {
2222
@ModifyReturnValue(
2323
method = "getPose",
2424
at = @At("RETURN")
@@ -33,26 +33,25 @@ class EntityMixin {
3333

3434
// use submixin to conditionally modify the parent
3535
@Mixin(Entity.class)
36-
class EntityMixin {
36+
abstract class EntityMixin {
3737
// 1. define a dummy handler in a mixin to the superclass, that does nothing, but is protected
3838
@WrapMethod(
3939
method = "getPose"
4040
)
4141
protected EntityPose overrideForPlayer(Operation<EntityPose> original) {
4242
return original.call();
4343
}
44-
45-
@Mixin(PlayerEntity.class)
46-
private static class PlayerEntityMixin extends EntityMixin /* !important! */ {
47-
// 2. Override the handler in the child mixin, and make the actual changes
48-
@Override
49-
protected EntityPose overrideForPlayer(Operation<EntityPose> original) {
50-
return EntityPose.SWIMMING;
51-
}
44+
}
45+
@Mixin(PlayerEntity.class)
46+
abstract class PlayerEntityMixin extends EntityMixin /* !important! */ {
47+
// 2. Override the handler in the child mixin, and make the actual changes
48+
@Override
49+
protected EntityPose overrideForPlayer(Operation<EntityPose> original) {
50+
return EntityPose.SWIMMING;
5251
}
5352
}
5453
```
5554

5655
Both of the above patterns can work for any mixin injector, and even when you just need to make small changes to the superclass.
5756

58-
The `instanceof` pattern is more direct, and can be simpler to understand, while the submixin pattern can lead to a code structure more similar to just overriding the target method and keeps all code relevant to a given class in a mixin to that class. the general agreement is that it doesn't matter which you pick; either is better than `@Override`.
57+
The `instanceof` pattern is more direct, and can be simpler to understand, while the submixin pattern can lead to a code structure more similar to just overriding the target method and keeps all code relevant to a given class in a mixin to that class. the general agreement is that it doesn't matter which you pick; either is better than `@Override`.

0 commit comments

Comments
 (0)