├── .github └── workflows │ └── build.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main ├── java └── io │ └── github │ └── apple502j │ └── updateblockentity │ ├── UpdateBlockEntityMod.java │ ├── UpdateBlockEntityUtils.java │ └── mixin │ └── VersionedChunkStorageMixin.java └── resources ├── assets └── updateblockentity │ └── icon.png ├── fabric.mod.json └── updateblockentity.mixins.json /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # Automatically build the project and run any configured tests for every push 2 | # and submitted pull request. This can help catch issues that only occur on 3 | # certain platforms or Java versions, and provides a first line of defence 4 | # against bad commits. 5 | 6 | name: build 7 | on: [pull_request, push] 8 | 9 | jobs: 10 | build: 11 | strategy: 12 | matrix: 13 | # Use these Java versions 14 | java: [ 15 | 17, # Current Java LTS & minimum supported by Minecraft 16 | ] 17 | # and run on both Linux and Windows 18 | os: [ubuntu-20.04, windows-2022] 19 | runs-on: ${{ matrix.os }} 20 | steps: 21 | - name: checkout repository 22 | uses: actions/checkout@v2 23 | - name: validate gradle wrapper 24 | uses: gradle/wrapper-validation-action@v1 25 | - name: setup jdk ${{ matrix.java }} 26 | uses: actions/setup-java@v1 27 | with: 28 | java-version: ${{ matrix.java }} 29 | - name: make gradle wrapper executable 30 | if: ${{ runner.os != 'Windows' }} 31 | run: chmod +x ./gradlew 32 | - name: build 33 | run: ./gradlew build 34 | - name: capture build artifacts 35 | if: ${{ runner.os == 'Linux' && matrix.java == '17' }} # Only upload artifacts built from latest java on one OS 36 | uses: actions/upload-artifact@v2 37 | with: 38 | name: Artifacts 39 | path: build/libs/ 40 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # gradle 2 | 3 | .gradle/ 4 | build/ 5 | out/ 6 | classes/ 7 | 8 | # eclipse 9 | 10 | *.launch 11 | 12 | # idea 13 | 14 | .idea/ 15 | *.iml 16 | *.ipr 17 | *.iws 18 | 19 | # vscode 20 | 21 | .settings/ 22 | .vscode/ 23 | bin/ 24 | .classpath 25 | .project 26 | 27 | # macos 28 | 29 | *.DS_Store 30 | 31 | # fabric 32 | 33 | run/ 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # UpdateBlockEntity (Experimental) 2 | Fixes [the bug](https://bugs.mojang.com/browse/MC-241670) that blocks chunks with modded block entities from being updated properly. **This is an experimental mod. Remember to back up worlds before opening, it can corrupt the world!** 3 | 4 | Supports 1.18 release. Licensed under Apache 2.0. 5 | 6 | ## How does this work? 7 | This mod works by: 8 | 9 | 1. updating worlds to data version 2841 (before the NBT changes). 10 | 2. copying the block entity NBTs to temporary tag. 11 | 3. updating worlds to the latest version from version 2841. This will cause ALL block entity NBTs to vanish if the chunk contained any modded block entities - including vanilla ones. 12 | 4. running datafiers manually to each block entity. 13 | 5. copying the block entity NBTs back to the intended location. 14 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'fabric-loom' version '0.10-SNAPSHOT' 3 | id 'maven-publish' 4 | } 5 | 6 | sourceCompatibility = JavaVersion.VERSION_17 7 | targetCompatibility = JavaVersion.VERSION_17 8 | 9 | archivesBaseName = project.archives_base_name 10 | version = project.mod_version 11 | group = project.maven_group 12 | 13 | repositories { 14 | // Add repositories to retrieve artifacts from in here. 15 | // You should only use this when depending on other mods because 16 | // Loom adds the essential maven repositories to download Minecraft and libraries from automatically. 17 | // See https://docs.gradle.org/current/userguide/declaring_repositories.html 18 | // for more information about repositories. 19 | } 20 | 21 | dependencies { 22 | // To change the versions see the gradle.properties file 23 | minecraft "com.mojang:minecraft:${project.minecraft_version}" 24 | mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2" 25 | modImplementation "net.fabricmc:fabric-loader:${project.loader_version}" 26 | 27 | // Fabric API. This is technically optional, but you probably want it anyway. 28 | modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}" 29 | } 30 | 31 | processResources { 32 | inputs.property "version", project.version 33 | 34 | filesMatching("fabric.mod.json") { 35 | expand "version": project.version 36 | } 37 | } 38 | 39 | tasks.withType(JavaCompile).configureEach { 40 | // Minecraft 1.18 (1.18-pre2) upwards uses Java 17. 41 | it.options.release = 17 42 | } 43 | 44 | java { 45 | // Loom will automatically attach sourcesJar to a RemapSourcesJar task and to the "build" task 46 | // if it is present. 47 | // If you remove this line, sources will not be generated. 48 | withSourcesJar() 49 | } 50 | 51 | jar { 52 | from("LICENSE") { 53 | rename { "${it}_${project.archivesBaseName}"} 54 | } 55 | } 56 | 57 | // configure the maven publication 58 | publishing { 59 | publications { 60 | mavenJava(MavenPublication) { 61 | from components.java 62 | } 63 | } 64 | 65 | // See https://docs.gradle.org/current/userguide/publishing_maven.html for information on how to set up publishing. 66 | repositories { 67 | // Add repositories to publish to here. 68 | // Notice: This block does NOT have the same function as the block in the top level. 69 | // The repositories here will be used for publishing your artifact, not for 70 | // retrieving dependencies. 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Done to increase the memory available to gradle. 2 | org.gradle.jvmargs=-Xmx1G 3 | 4 | # Fabric Properties 5 | # check these on https://fabricmc.net/versions.html 6 | minecraft_version=1.18 7 | yarn_mappings=1.18+build.1 8 | loader_version=0.12.8 9 | 10 | # Mod Properties 11 | mod_version = 1.1.0 12 | maven_group = io.github.apple502j 13 | archives_base_name = updateblockentity 14 | 15 | # Dependencies 16 | fabric_version=0.43.1+1.18 17 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple502j/UpdateBlockEntity/06c6f4313fe7c22249392a70c0b0079a8a840050/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.1-bin.zip 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple502j/UpdateBlockEntity/06c6f4313fe7c22249392a70c0b0079a8a840050/gradlew -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%" == "" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%" == "" set DIRNAME=. 29 | set APP_BASE_NAME=%~n0 30 | set APP_HOME=%DIRNAME% 31 | 32 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 33 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 34 | 35 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 36 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 37 | 38 | @rem Find java.exe 39 | if defined JAVA_HOME goto findJavaFromJavaHome 40 | 41 | set JAVA_EXE=java.exe 42 | %JAVA_EXE% -version >NUL 2>&1 43 | if "%ERRORLEVEL%" == "0" goto execute 44 | 45 | echo. 46 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 47 | echo. 48 | echo Please set the JAVA_HOME variable in your environment to match the 49 | echo location of your Java installation. 50 | 51 | goto fail 52 | 53 | :findJavaFromJavaHome 54 | set JAVA_HOME=%JAVA_HOME:"=% 55 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 56 | 57 | if exist "%JAVA_EXE%" goto execute 58 | 59 | echo. 60 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 61 | echo. 62 | echo Please set the JAVA_HOME variable in your environment to match the 63 | echo location of your Java installation. 64 | 65 | goto fail 66 | 67 | :execute 68 | @rem Setup the command line 69 | 70 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 71 | 72 | 73 | @rem Execute Gradle 74 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 75 | 76 | :end 77 | @rem End local scope for the variables with windows NT shell 78 | if "%ERRORLEVEL%"=="0" goto mainEnd 79 | 80 | :fail 81 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 82 | rem the _cmd.exe /c_ return code! 83 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 84 | exit /b 1 85 | 86 | :mainEnd 87 | if "%OS%"=="Windows_NT" endlocal 88 | 89 | :omega 90 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | pluginManagement { 2 | repositories { 3 | maven { 4 | name = 'Fabric' 5 | url = 'https://maven.fabricmc.net/' 6 | } 7 | mavenCentral() 8 | gradlePluginPortal() 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/main/java/io/github/apple502j/updateblockentity/UpdateBlockEntityMod.java: -------------------------------------------------------------------------------- 1 | package io.github.apple502j.updateblockentity; 2 | 3 | import net.fabricmc.api.ModInitializer; 4 | import org.apache.logging.log4j.LogManager; 5 | import org.apache.logging.log4j.Logger; 6 | 7 | public class UpdateBlockEntityMod implements ModInitializer { 8 | public static final Logger LOGGER = LogManager.getLogger("updateblockentity"); 9 | 10 | @Override 11 | public void onInitialize() { 12 | LOGGER.info("Loaded UpdateBlockEntity"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/io/github/apple502j/updateblockentity/UpdateBlockEntityUtils.java: -------------------------------------------------------------------------------- 1 | package io.github.apple502j.updateblockentity; 2 | 3 | import com.mojang.datafixers.DataFixer; 4 | import com.mojang.serialization.Dynamic; 5 | import net.minecraft.datafixer.TypeReferences; 6 | import net.minecraft.nbt.NbtCompound; 7 | import net.minecraft.nbt.NbtOps; 8 | 9 | public final class UpdateBlockEntityUtils { 10 | private UpdateBlockEntityUtils() {} 11 | 12 | public static final int SAFE_UPGRADE_DATA_VERSION = 2841; 13 | public static final int CHUNK_LEVEL_FIX_DATA_VERSION = 2842; 14 | public static final String KEY = "UpdateBlockEntity"; 15 | 16 | public static NbtCompound update(DataFixer dataFixer, NbtCompound nbt, int target) { 17 | return (NbtCompound)dataFixer.update(TypeReferences.BLOCK_ENTITY, new Dynamic(NbtOps.INSTANCE, nbt), SAFE_UPGRADE_DATA_VERSION, target).getValue(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/io/github/apple502j/updateblockentity/mixin/VersionedChunkStorageMixin.java: -------------------------------------------------------------------------------- 1 | package io.github.apple502j.updateblockentity.mixin; 2 | 3 | import com.mojang.datafixers.DataFixer; 4 | import com.mojang.serialization.Codec; 5 | import io.github.apple502j.updateblockentity.UpdateBlockEntityMod; 6 | import io.github.apple502j.updateblockentity.UpdateBlockEntityUtils; 7 | import net.minecraft.SharedConstants; 8 | import net.minecraft.datafixer.DataFixTypes; 9 | import net.minecraft.nbt.NbtCompound; 10 | import net.minecraft.nbt.NbtElement; 11 | import net.minecraft.nbt.NbtHelper; 12 | import net.minecraft.nbt.NbtList; 13 | import net.minecraft.util.registry.RegistryKey; 14 | import net.minecraft.world.PersistentStateManager; 15 | import net.minecraft.world.World; 16 | import net.minecraft.world.gen.chunk.ChunkGenerator; 17 | import net.minecraft.world.storage.VersionedChunkStorage; 18 | import org.spongepowered.asm.mixin.Final; 19 | import org.spongepowered.asm.mixin.Mixin; 20 | import org.spongepowered.asm.mixin.Shadow; 21 | import org.spongepowered.asm.mixin.injection.At; 22 | import org.spongepowered.asm.mixin.injection.Inject; 23 | import org.spongepowered.asm.mixin.injection.ModifyArgs; 24 | import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; 25 | import org.spongepowered.asm.mixin.injection.invoke.arg.Args; 26 | 27 | import java.util.Optional; 28 | import java.util.function.Supplier; 29 | 30 | @Mixin(VersionedChunkStorage.class) 31 | public class VersionedChunkStorageMixin { 32 | @Shadow @Final protected DataFixer dataFixer; 33 | 34 | @ModifyArgs(method = "updateChunkNbt", at = @At(value = "INVOKE", target = "Lnet/minecraft/nbt/NbtHelper;update(Lcom/mojang/datafixers/DataFixer;Lnet/minecraft/datafixer/DataFixTypes;Lnet/minecraft/nbt/NbtCompound;I)Lnet/minecraft/nbt/NbtCompound;")) 35 | private void beforeUpdate(Args args) { 36 | NbtCompound nbt = args.get(2); 37 | int oldVersion = VersionedChunkStorage.getDataVersion(nbt); 38 | if (oldVersion >= UpdateBlockEntityUtils.CHUNK_LEVEL_FIX_DATA_VERSION) { 39 | // This fix only applies to pre-2842 to post-2842 for now 40 | return; 41 | } 42 | // Update to data version 2841 first to apply any changes in previous versions 43 | nbt = NbtHelper.update(this.dataFixer, DataFixTypes.CHUNK, nbt, oldVersion, UpdateBlockEntityUtils.SAFE_UPGRADE_DATA_VERSION); 44 | NbtCompound context = nbt.getCompound("__context"); 45 | NbtList blockEntities = nbt.getCompound("Level").getList("TileEntities", NbtElement.COMPOUND_TYPE); 46 | context.put(UpdateBlockEntityUtils.KEY, blockEntities); 47 | args.set(2, nbt); 48 | args.set(3, UpdateBlockEntityUtils.SAFE_UPGRADE_DATA_VERSION); 49 | } 50 | 51 | @Inject(method = "updateChunkNbt", at = @At(value = "INVOKE", target = "Lnet/minecraft/nbt/NbtCompound;remove(Ljava/lang/String;)V")) 52 | private void afterUpdate(RegistryKey worldKey, Supplier persistentStateManagerFactory, NbtCompound nbt, Optional>> generatorCodecKey, CallbackInfoReturnable cir) { 53 | NbtCompound context = nbt.getCompound("__context"); 54 | if (context == null) { 55 | return; 56 | } 57 | NbtList blockEntities = context.getList(UpdateBlockEntityUtils.KEY, NbtElement.COMPOUND_TYPE); 58 | if (blockEntities == null) { 59 | // Updating between safe versions; no action necessary 60 | return; 61 | } 62 | if (blockEntities.size() > 0) { 63 | UpdateBlockEntityMod.LOGGER.info("Updating " + blockEntities.size() + " block entities"); 64 | for (int i = 0; i < blockEntities.size(); i++) { 65 | NbtCompound blockEntityNbt = blockEntities.getCompound(i); 66 | blockEntities.set(i, UpdateBlockEntityUtils.update(this.dataFixer, blockEntityNbt, SharedConstants.getGameVersion().getWorldVersion())); 67 | } 68 | nbt.put("block_entities", blockEntities); 69 | } 70 | context.remove(UpdateBlockEntityUtils.KEY); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/resources/assets/updateblockentity/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/apple502j/UpdateBlockEntity/06c6f4313fe7c22249392a70c0b0079a8a840050/src/main/resources/assets/updateblockentity/icon.png -------------------------------------------------------------------------------- /src/main/resources/fabric.mod.json: -------------------------------------------------------------------------------- 1 | { 2 | "schemaVersion": 1, 3 | "id": "updateblockentity", 4 | "version": "${version}", 5 | 6 | "name": "UpdateBlockEntity", 7 | "description": "Fixes block entity data corruption in 1.18 world upgrade", 8 | "authors": [ 9 | "apple502j" 10 | ], 11 | "contact": { 12 | "sources": "https://github.com/apple502j/UpdateBlockEntity" 13 | }, 14 | 15 | "license": "Apache-2.0", 16 | "icon": "assets/updateblockentity/icon.png", 17 | 18 | "environment": "*", 19 | "entrypoints": { 20 | "main": [ 21 | "io.github.apple502j.updateblockentity.UpdateBlockEntityMod" 22 | ] 23 | }, 24 | "mixins": [ 25 | "updateblockentity.mixins.json" 26 | ], 27 | 28 | "depends": { 29 | "fabricloader": ">=0.12.5", 30 | "minecraft": "1.18.x", 31 | "java": ">=17" 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/resources/updateblockentity.mixins.json: -------------------------------------------------------------------------------- 1 | { 2 | "required": true, 3 | "minVersion": "0.8", 4 | "package": "io.github.apple502j.updateblockentity.mixin", 5 | "compatibilityLevel": "JAVA_16", 6 | "mixins": [ 7 | "VersionedChunkStorageMixin" 8 | ], 9 | "injectors": { 10 | "defaultRequire": 1 11 | } 12 | } 13 | --------------------------------------------------------------------------------