aboutsummaryrefslogtreecommitdiff
path: root/docs/development/mixins
diff options
context:
space:
mode:
Diffstat (limited to 'docs/development/mixins')
-rw-r--r--docs/development/mixins/accessors.md45
-rw-r--r--docs/development/mixins/adding-fields.md50
-rw-r--r--docs/development/mixins/simple-injects.md133
3 files changed, 228 insertions, 0 deletions
diff --git a/docs/development/mixins/accessors.md b/docs/development/mixins/accessors.md
new file mode 100644
index 0000000..f9ad7b8
--- /dev/null
+++ b/docs/development/mixins/accessors.md
@@ -0,0 +1,45 @@
+# Accessor Mixins
+
+> a/k/a Reverse Patch
+
+Let's start with the easiest form of Mixins — Accessors. Accessors allow you to access functions that are otherwise private in a Minecraft class. You can also do that using reflection, but you might notice that your reflection call will not easily work in both devenv and a live env. This is because the method names are different in a devenv compared to a normal Forge installation. You can still specify both names and just look through all the names using reflection, but Accessor Mixins are a lot easier to use, with less accidental pitfalls, and better performance.
+
+```java
+// This Mixin targets the Minecraft class
+@Mixin(Minecraft.class)
+public interface AccessorMinecraft {
+
+ // Getter for the field theIntegratedServer
+ // Notice the _mymodid at the end.
+ @Accessor("theIntegratedServer")
+ IntegratedServer getIntegratedServe_mymodid();
+
+ // Setter for serverPort
+ @Accessor("serverPort")
+ void setServerPort_mymodid(int port);
+
+ // Invoker for rightClickMouse.
+ @Invoker("rightClickMouse")
+ void rightClickMouse_mymodid();
+}
+```
+
+First, notice that we need to use an `interface`. Most mixins are `class`es. Accessors are the exception, since we don't want to actually put any code into the `Minecraft` class. Accessor mixins can also not be mixed with other mixin styles, but since you should have multiple mixins even for the same class for different things anyway, this shouldn't be an issue.
+
+Next we put the `@Mixin` annotation on our Accessor to let it known which class we want to inject into.
+
+Then for a field we use the `@Accessor` annotation, with the name of the field we want to access. Please give all your mixin methods a `_mymodid` indicator, to avoid name collissions with other mods.
+
+For a setter, the method returns void and takes one argument of the type of the field you are targetting. For a getter, the method returns the type of the field you are targeting and takes no arguments.
+
+For an method invoker, you copy the method signature you want to call, but rename it to something unique with a `_mymodid` postfix.
+
+Now if you want to use those methods, you can simply cast any instance of your targeted class to your accessor mixin class:
+
+```java
+Minecraft mc = Minecraft.getMinecraft();
+AccessorMinecraft accessorMc = (AccessorMinecraft) mc;
+accessorMc.rightClickMouse_mymodid();
+```
+
+If you get a class cast exception here, it means your mixin was not applied. This can happen if you forgot to register your mixin, or if your mixin contains errors.
diff --git a/docs/development/mixins/adding-fields.md b/docs/development/mixins/adding-fields.md
new file mode 100644
index 0000000..c3533e9
--- /dev/null
+++ b/docs/development/mixins/adding-fields.md
@@ -0,0 +1,50 @@
+# Adding new fields and methods
+
+The next step up is injecting fields and methods into a class. This allows you to store additional state in objects, or can serve as an alternative to accessors for more complex operations that need to access private state of a class.
+
+```java
+@Mixin(EntityArmorStand.class)
+public class InjectCustomField {
+ Color colorOverride_mymodid;
+
+ public void setColorOverride_mymodid(Color color) {
+ colorOverride_mymodid = color;
+ }
+
+ public Color getColorOverride_mymodid() {
+ return colorOverride_mymodid;
+ }
+}
+```
+
+This mixin is a `class`, like all mixin (except for accessors) are. You can make the class abstract if you want.
+
+First we add a new field (of course with modid postfix) into every armor stand.
+
+Then we also add a getter and a setter method for that field.
+
+Right now we run into a problem. We can't access mixin classes directly, so we cannot simply cast the `EntityArmorStand` into a `InjectCustomField`. Instead we create an interface (inside of our regular code, not inside of the mixin package) and implement that interface in our mixin class. You can also implement other interfaces this way, not just your own.
+
+```java
+// Inside our regular code. Not in the mixin package
+public interface ColorFieldAccessor {
+ void setColorOverride_mymodid(Color color);
+ Color getColorOverride_mymodid();
+}
+
+// And the updated mixin
+@Mixin(EntityArmorStand.class)
+public class InjectCustomField implement ColorFieldAccessor {
+ Color colorOverride_mymodid;
+
+ @Override
+ public void setColorOverride_mymodid(Color color) {
+ colorOverride_mymodid = color;
+ }
+
+ @Override
+ public Color getColorOverride_mymodid() {
+ return colorOverride_mymodid;
+ }
+}
+```
diff --git a/docs/development/mixins/simple-injects.md b/docs/development/mixins/simple-injects.md
new file mode 100644
index 0000000..eeea65e
--- /dev/null
+++ b/docs/development/mixins/simple-injects.md
@@ -0,0 +1,133 @@
+# Simple Injects
+
+Let's get into method modifications. The real interesting part of mixins. Hopefully you know the basics from the first two mixin tutorials by now, because now we get into a whole another layer of complexity.
+
+Now we will modify an existing method in Minecrafts code. This will allow us to react to changes in Minecrafts state. This is how almost all custom [events](../events.md) are done, but we are of course not limited to just events that observe state changes. Using method modifying mixins we can change almost any behaviour in Minecrafts code.
+
+!!! note
+ This is the simple tutorial, I will tell you *how* to use `@Inject`s and co, but I won't tell you the *why*. Check out the [advanced tutorial](./advanced-injects.md) for that.
+
+## The easiest of the easiest
+
+Let's start with probably the easiest `@Inject` out there. The HEAD inject. This mixin will inject whatever code you have inside your method at the start of the method you target.
+
+```java
+@Mixin(PlayerControllerMP.class)
+public class RightClickWithItemEvent {
+
+ @Inject(method = "sendUseItem", at = @At("HEAD"))
+ private void onSendUseItem_mymod(EntityPlayer playerIn, World worldIn, ItemStack itemStackIn, CallbackInfoReturnable<Boolean> cir) {
+ MinecraftForge.EVENT_BUS.post(new SendUseItemEvent(playerIn, worldIn, itemStackIn));
+ }
+}
+```
+
+First we want to inject into the `PlayerControllerMP` class.
+
+We create an `@Inject`. This tells us in which method we want to inject (`sendUseItem`) and where in that method (`HEAD`, meaning the very top of the method).
+
+The actual method signature for an inject is always to return a `void`. You can make them `private` or `public`. The arguments are the same arguments as the method you want to inject into, as well as a `CallbackInfo`.
+
+For a method returning void, you just use a `CallbackInfo`, and if the method returns something, you use `CallbackInfoReturnable<ReturnTypeOfTheInjectedIntoMethod>`.
+
+Your method will now be called every time the `sendUseItem` is called with the arguments to that method and the `CallbackInfo`.
+
+!!! important
+ Your method will be *called* at the beginning of the injected into method like this:
+
+ ```java
+ public boolean sendUseItem(EntityPlayer playerIn, World worldIn, ItemStack itemStackIn) {
+ onSendUseItem_mymod(playerIn, worldIn, itemStackIn, new CallbackInfo(/* ... */));
+ // All the other code that is normally in the method
+ }
+ ```
+
+ This means returning from your method will just continue as normal. See [cancelling](#cancelling) for info on how to return from the outer method.
+
+## At a method call
+
+Let's take this example method:
+
+```java
+public void methodA() {
+ // ...
+}
+
+public void methodB() {
+ System.out.println("Here 1");
+ methodA();
+ // We want to inject our method call right here.
+ System.out.println("Here 2");
+}
+```
+
+We can inject ourselves into `methodB` as well. It is *just* a bit more complicated than the `HEAD` inject.
+
+```java
+@Inject(method = "methodB", at = @At(target = "Lnet/some/Class;methodA()V", value = "INVOKE"))
+private void onMethodBJustCalledMethodA(CallbackInfo ci) {
+}
+```
+
+> **HUUUUH, where does that come from???**
+
+Don't worry! I won't explain you how to understand these `target`s in this tutorial, but you also don't need to understand that `target`. Instead you can simply use the Minecraft Development IntelliJ Plugin to help you. Simply type `@At(value = "INVOKE", target = "")`, place your cursor inside of the target and use auto completion (<kbd>Ctrl + Space</kbd>) and the plugin will recommend you a bunch of method calls. Find whichever seems right to you and press enter. You can now (also thanks to the plugin) <kbd>Ctrl</kbd> click on the `target` string, which will take you to the decompiled code exactly to where that target will inject.
+
+## Ordinals
+
+Let's take the method injection example from before and change it a bit:
+
+```java
+public void methodA() {
+ // ...
+}
+
+public void methodB() {
+ System.out.println("Here 1");
+ if (Math.random() < 0.4)
+ methodA();
+ System.out.println("Here 2");
+ methodA();
+ // We want to inject our method call right here.
+ System.out.println("Here 3");
+}
+```
+
+We can't simply use the same `@Inject` from before, since by default a `INVOKE` inject will inject just after *every* method call. Here, we can use the `ordinal` classifier to specify which method call we want to use. Keep in mind this is about where to place our injection, so many method calls in a loop will not increment the ordinal, only unique code locations that call the function will increase the ordinal. Keep in mind: we are programmers, we start counting with `0`.
+
+```java
+@Inject(method = "methodB", at = @At(target = "Lnet/some/Class;methodA()V", value = "INVOKE", ordinal = 1))
+private void onMethodBJustCalledMethodA(CallbackInfo ci) {
+}
+```
+
+## Cancelling
+
+Cancelling a method means you return from the method you are injected to as soon as your injector method is done. In order to be able to use the cancelling methods, you need to mark your injection as cancellable.
+
+```java
+@Inject(method = "syncCurrentPlayItem", at = @At("HEAD"), cancellable = true)
+private void onSyncCurrentPlayItem_mymod(CallbackInfo ci) {
+ System.out.println("This code will be executed");
+ if (Math.random() < 0.5)
+ ci.cancel();
+ System.out.println("This code will *also* be executed");
+ // As soon as this method returns, the outer method will see that it was cancelled and *also* return
+}
+
+@Inject(method = "isHittingPosition", at = @At("HEAD"), cancellable = true)
+private void onIsHittingPosition_mymod(BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
+ cir.setReturnValue(true);
+}
+```
+
+For `void` methods you need to use `callbackInfo.cancel()`. For all other methods you need to use `callbackInfoReturnable.setReturnValue(returnValue)`.
+
+
+!!! important
+ Cancelling a `CallbackInfo` will only have an effect as soon as you return from your injector method.
+ The rest of your method will run as normal.
+
+
+
+