|
| 1 | +--- |
| 2 | +title: Access Widening |
| 3 | +description: Learn how to use access wideners from class tweaker files. |
| 4 | +authors-nogithub: |
| 5 | + - lightningtow |
| 6 | + - siglong |
| 7 | +authors: |
| 8 | + - Ayutac |
| 9 | + - cassiancc |
| 10 | + - cootshk |
| 11 | + - Earthcomputer |
| 12 | + - florensie |
| 13 | + - froyo4u |
| 14 | + - haykam821 |
| 15 | + - hYdos |
| 16 | + - its-miroma |
| 17 | + - kb-1000 |
| 18 | + - kcrca |
| 19 | + - liach |
| 20 | + - lmvdz |
| 21 | + - matjojo |
| 22 | + - MildestToucan |
| 23 | + - modmuss50 |
| 24 | + - octylFractal |
| 25 | + - OroArmor |
| 26 | + - T3sT3ro |
| 27 | + - Technici4n |
| 28 | + - TheGlitch76 |
| 29 | + - UpcraftLP |
| 30 | + - YTG1234 |
| 31 | +--- |
| 32 | + |
| 33 | +Access widening is a type of [class tweaking](../class-tweakers) used to loosen the access limits of classes, methods and fields and reflect that change in the decompiled source. |
| 34 | +This includes making them public, extendable and/or mutable. |
| 35 | + |
| 36 | +To access fields or methods, it can be safer and simpler to use [accessor mixins](https://wiki.fabricmc.net/tutorial:mixin_accessors), |
| 37 | +but there are two situations where accessors are insufficient and access widening is necessary: |
| 38 | + |
| 39 | +- If you need to access a `private`, `protected` or package-private class |
| 40 | +- If you need to override a `final` method, or subclass a `final` class |
| 41 | + |
| 42 | +However, unlike [accessor mixins](https://wiki.fabricmc.net/tutorial:mixin_accessors), [class tweaking](../class-tweakers) only works on Vanilla Minecraft classes, and not on other mods. |
| 43 | + |
| 44 | +## Access Directives {#access-directives} |
| 45 | + |
| 46 | +Access widener entries start with one of three directive keywords to specify the type of modification to apply. |
| 47 | + |
| 48 | +### Accessible {#accessible} |
| 49 | + |
| 50 | +`accessible` can target classes, methods and fields: |
| 51 | + |
| 52 | +- Fields and Classes are made public. |
| 53 | +- Methods are made public, and final if originally private. |
| 54 | + |
| 55 | +Making a method or field accessible also makes its class accessible. |
| 56 | + |
| 57 | +### Extendable {#extendable} |
| 58 | + |
| 59 | +`extendable` can target classes and methods: |
| 60 | + |
| 61 | +- Classes are made public and non-final |
| 62 | +- Methods are made protected and non-final |
| 63 | + |
| 64 | +Making a method extendable also makes its class extendable. |
| 65 | + |
| 66 | +### Mutable {#mutable} |
| 67 | + |
| 68 | +`mutable` can make a field non-final. |
| 69 | + |
| 70 | +To make a private final field both accessible and mutable, you must make two separate entries in the file. |
| 71 | + |
| 72 | +### Transitive Directives {#transitive-directives} |
| 73 | + |
| 74 | +In order to expose certain access widener changes to mods depending on yours, you prefix the relevant directives with `transitive-*`: |
| 75 | + |
| 76 | +```txt:no-line-numbers |
| 77 | +transitive-accessible |
| 78 | +transitive-extendable |
| 79 | +transitive-mutable |
| 80 | +``` |
| 81 | + |
| 82 | +## Specifying Targets {#specifying-targets} |
| 83 | + |
| 84 | +For class tweaking, classes use their [internal names](../mixins/bytecode#class-names). For fields and methods you must specify their class name, their name, and their [bytecode descriptor](../mixins/bytecode#field-and-method-descriptors). |
| 85 | + |
| 86 | +::: tip |
| 87 | + |
| 88 | +The names of targets need to correspond to your current mappings. |
| 89 | + |
| 90 | +::: |
| 91 | + |
| 92 | +::: tabs |
| 93 | + |
| 94 | +== Classes |
| 95 | + |
| 96 | +Format: |
| 97 | + |
| 98 | +```txt:no-line-numbers |
| 99 | +<accessible / extendable> class <className> |
| 100 | +``` |
| 101 | + |
| 102 | +Example: |
| 103 | + |
| 104 | +@[code lang=txt:no-line-numbers transcludeWith=:::accesswidening-examples:classes:::](@/reference/latest/src/main/resources/example-mod.classtweaker) |
| 105 | + |
| 106 | +== Methods |
| 107 | + |
| 108 | +Format: |
| 109 | + |
| 110 | +```txt:no-line-numbers |
| 111 | +<accessible / extendable> method <className> <methodName> <methodDescriptor> |
| 112 | +``` |
| 113 | + |
| 114 | +Example: |
| 115 | + |
| 116 | +@[code lang=txt:no-line-numbers transcludeWith=:::accesswidening-examples:methods:::](@/reference/latest/src/main/resources/example-mod.classtweaker) |
| 117 | + |
| 118 | +== Fields |
| 119 | + |
| 120 | +Format: |
| 121 | + |
| 122 | +```txt:no-line-numbers |
| 123 | +<accessible / mutable> field <className> <fieldName> <fieldDescriptor> |
| 124 | +``` |
| 125 | + |
| 126 | +Example: |
| 127 | + |
| 128 | +@[code lang=txt:no-line-numbers transcludeWith=:::accesswidening-examples:fields:::](@/reference/latest/src/main/resources/example-mod.classtweaker) |
| 129 | + |
| 130 | +::: |
| 131 | + |
| 132 | +## Generating Entries {#generating-entries} |
| 133 | + |
| 134 | +Manually writing access widener entries is time-consuming and prone to human error. Let's look at tools that simplify a part of the process by allowing you to generate and copy entries. |
| 135 | + |
| 136 | +### mcsrc.dev {#mcsrc-dev} |
| 137 | + |
| 138 | +Available for all versions with an [unobfuscated JAR](../migrating-mappings/index#whats-going-on-with-mappings) namely 1.21.11 and above, |
| 139 | +[mcsrc](https://mcsrc.dev) allows you to decompile and navigate Minecraft source in the browser and copy Mixin, access widener or access transformer targets to clipboard. |
| 140 | +The names of classes, methods and fields on [mcsrc](https://mcsrc.dev) will align with [Mojang Mappings](../migrating-mappings/index#mappings). |
| 141 | + |
| 142 | +To copy an access widener entry, first navigate to the class which you want to modify, and right-click on your target to open the popup menu. |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +Then, click on `Copy Class Tweaker / Access Widener`, and a confirmation should appear at the top of the page. |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | +You can then paste the entry in your class tweaker file. |
| 151 | + |
| 152 | +### Minecraft Development Plugin (IntelliJ IDEA) {#mcdev-plugin} |
| 153 | + |
| 154 | +The [Minecraft Development Plugin](../getting-started/intellij-idea/setting-up#installing-idea-plugins), also known as MCDev, is an IntelliJ IDEA plugin to assist in various aspects of Minecraft mod development. |
| 155 | +For example, it lets you copy access widener entries from the decompiled source target to the clipboard. |
| 156 | + |
| 157 | +To copy an access widener entry, first navigate to the class which you want to modify, and right-click on your target to open the popup menu. |
| 158 | + |
| 159 | + |
| 160 | + |
| 161 | +Then, click on `Copy / Paste Special` and `AW Entry`. |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +A confirmation should now pop up on the element you right-clicked. |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | +You can then paste the entry in your class tweaker file. |
| 170 | + |
| 171 | +### Linkie {#linkie} |
| 172 | + |
| 173 | +[Linkie](https://linkie.shedaniel.dev) is a website that allows you to browse and translate across mappings. It also provides access widener entries for the class, method or field you're viewing. |
| 174 | + |
| 175 | +First, make sure you have the correct version and mappings selected on the menu on the left: |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | +Then, search for the element you want to modify, and the access widener entry will be listed as `AW` under the result: |
| 180 | + |
| 181 | + |
| 182 | + |
| 183 | +You can copy it and then paste the entry in your class tweaker file. |
| 184 | + |
| 185 | +## Applying Changes {#applying-changes} |
| 186 | + |
| 187 | +To see your changes applied, you must refresh your Gradle project by [regenerating sources](../getting-started/generating-sources). The elements you targeted should |
| 188 | +have their access limits modified accordingly. If modifications do not appear, you can try [validating the file](../class-tweakers/index#validating-the-file) |
| 189 | +and checking if any errors appear. |
0 commit comments