|
1 | 1 | package me.wurgo.antiresourcereload.mixin; |
2 | 2 |
|
| 3 | +import com.llamalad7.mixinextras.injector.ModifyExpressionValue; |
| 4 | +import com.llamalad7.mixinextras.injector.WrapWithCondition; |
| 5 | +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; |
| 6 | +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; |
| 7 | +import com.llamalad7.mixinextras.sugar.Local; |
3 | 8 | import me.wurgo.antiresourcereload.AntiResourceReload; |
4 | 9 | import net.minecraft.client.MinecraftClient; |
5 | | -import net.minecraft.resource.ResourcePack; |
6 | | -import net.minecraft.resource.ServerResourceManager; |
| 10 | +import net.minecraft.resource.*; |
7 | 11 | import net.minecraft.server.command.CommandManager; |
8 | | -import org.spongepowered.asm.mixin.*; |
| 12 | +import net.minecraft.tag.BlockTags; |
| 13 | +import net.minecraft.tag.EntityTypeTags; |
| 14 | +import net.minecraft.tag.FluidTags; |
| 15 | +import net.minecraft.tag.ItemTags; |
| 16 | +import org.spongepowered.asm.mixin.Mixin; |
9 | 17 | import org.spongepowered.asm.mixin.injection.At; |
10 | | -import org.spongepowered.asm.mixin.injection.Redirect; |
11 | 18 |
|
12 | 19 | import java.util.List; |
13 | 20 | import java.util.concurrent.CompletableFuture; |
14 | 21 | import java.util.concurrent.ExecutionException; |
15 | 22 | import java.util.concurrent.Executor; |
| 23 | +import java.util.stream.Collectors; |
16 | 24 |
|
17 | 25 | @Mixin(MinecraftClient.class) |
18 | | -public class MinecraftClientMixin { |
19 | | - @Unique |
20 | | - private boolean hasLoadedTags; |
| 26 | +public abstract class MinecraftClientMixin { |
21 | 27 |
|
22 | | - @Redirect( |
| 28 | + @WrapOperation( |
23 | 29 | method = "createIntegratedResourceManager", |
24 | 30 | at = @At( |
25 | 31 | value = "INVOKE", |
26 | 32 | target = "Lnet/minecraft/resource/ServerResourceManager;reload(Ljava/util/List;Lnet/minecraft/server/command/CommandManager$RegistrationEnvironment;ILjava/util/concurrent/Executor;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;" |
27 | 33 | ) |
28 | 34 | ) |
29 | | - private CompletableFuture<ServerResourceManager> antiresourcereload_cachedReload(List<ResourcePack> dataPacks, CommandManager.RegistrationEnvironment registrationEnvironment, int i, Executor executor, Executor executor2) throws ExecutionException, InterruptedException { |
30 | | - if (dataPacks.size() != 1) { AntiResourceReload.log("Using data-packs, reloading."); } |
31 | | - else if (AntiResourceReload.cache == null) { AntiResourceReload.log("Cached resources unavailable, reloading & caching."); } |
32 | | - else { |
| 35 | + private CompletableFuture<ServerResourceManager> cachedReload(List<ResourcePack> dataPacks, CommandManager.RegistrationEnvironment registrationEnvironment, int i, Executor executor, Executor executor2, Operation<CompletableFuture<ServerResourceManager>> original, @Local ResourcePackManager<ResourcePackProfile> resourcePackManager) { |
| 36 | + boolean usingDataPacks = !resourcePackManager.getEnabledProfiles().stream().map(ResourcePackProfile::getName).collect(Collectors.toList()).equals(DataPackSettings.SAFE_MODE.getEnabled()); |
| 37 | + if (usingDataPacks) { |
| 38 | + AntiResourceReload.log("Using data-packs, reloading."); |
| 39 | + } else if (AntiResourceReload.cache == null) { |
| 40 | + AntiResourceReload.log("Cached resources unavailable, reloading & caching."); |
| 41 | + } else { |
33 | 42 | AntiResourceReload.log("Using cached server resources."); |
34 | | - if (AntiResourceReload.hasSeenRecipes) { |
35 | | - ((RecipeManagerAccess) AntiResourceReload.cache.get().getRecipeManager()).invokeApply(AntiResourceReload.recipes, null, null); |
36 | | - } |
37 | | - AntiResourceReload.hasSeenRecipes = false; |
38 | 43 | return AntiResourceReload.cache; |
39 | 44 | } |
40 | 45 |
|
41 | | - CompletableFuture<ServerResourceManager> reloaded = ServerResourceManager.reload(dataPacks, registrationEnvironment, i, executor, executor2); |
| 46 | + CompletableFuture<ServerResourceManager> reloaded = original.call(dataPacks, registrationEnvironment, i, executor, executor2); |
42 | 47 |
|
43 | | - if (dataPacks.size() == 1) { AntiResourceReload.cache = reloaded; } |
44 | | - |
| 48 | + if (!usingDataPacks) { |
| 49 | + AntiResourceReload.cache = reloaded; |
| 50 | + } |
45 | 51 | return reloaded; |
46 | 52 | } |
47 | 53 |
|
48 | | - @Redirect( |
| 54 | + @ModifyExpressionValue( |
49 | 55 | method = "startIntegratedServer(Ljava/lang/String;Lnet/minecraft/util/registry/RegistryTracker$Modifiable;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;ZLnet/minecraft/client/MinecraftClient$WorldLoadAction;)V", |
50 | 56 | at = @At( |
51 | 57 | value = "INVOKE", |
52 | | - target = "Lnet/minecraft/resource/ServerResourceManager;loadRegistryTags()V" |
| 58 | + target = "Lnet/minecraft/client/MinecraftClient;createIntegratedResourceManager(Lnet/minecraft/util/registry/RegistryTracker$Modifiable;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;ZLnet/minecraft/world/level/storage/LevelStorage$Session;)Lnet/minecraft/client/MinecraftClient$IntegratedResourceManager;" |
53 | 59 | ) |
54 | 60 | ) |
55 | | - private void antiresourcereload_skipLoad(ServerResourceManager manager) throws ExecutionException, InterruptedException { |
56 | | - if (AntiResourceReload.cache != null && manager == AntiResourceReload.cache.get()) { |
57 | | - if (hasLoadedTags) return; |
58 | | - hasLoadedTags = true; |
| 61 | + private MinecraftClient.IntegratedResourceManager reloadRecipes(MinecraftClient.IntegratedResourceManager integratedResourceManager) throws ExecutionException, InterruptedException { |
| 62 | + ServerResourceManager manager; |
| 63 | + // only reload recipes on the main thread |
| 64 | + // this is for compat with seedqueue creating servers in the background |
| 65 | + if (MinecraftClient.getInstance().isOnThread() && AntiResourceReload.hasSeenRecipes && AntiResourceReload.cache != null && (manager = AntiResourceReload.cache.get()) == integratedResourceManager.getServerResourceManager()) { |
| 66 | + ((RecipeManagerAccess) manager.getRecipeManager()).invokeApply(AntiResourceReload.recipes, manager.getResourceManager(), MinecraftClient.getInstance().getProfiler()); |
| 67 | + AntiResourceReload.hasSeenRecipes = false; |
59 | 68 | } |
60 | | - manager.loadRegistryTags(); |
| 69 | + return integratedResourceManager; |
| 70 | + } |
| 71 | + |
| 72 | + @WrapWithCondition( |
| 73 | + method = "startIntegratedServer(Ljava/lang/String;Lnet/minecraft/util/registry/RegistryTracker$Modifiable;Ljava/util/function/Function;Lcom/mojang/datafixers/util/Function4;ZLnet/minecraft/client/MinecraftClient$WorldLoadAction;)V", |
| 74 | + at = @At( |
| 75 | + value = "INVOKE", |
| 76 | + target = "Lnet/minecraft/resource/ServerResourceManager;loadRegistryTags()V" |
| 77 | + ) |
| 78 | + ) |
| 79 | + private boolean skipReloadingRegistryTags(ServerResourceManager manager) { |
| 80 | + return manager.getRegistryTagManager().blocks() != BlockTags.getContainer() || |
| 81 | + manager.getRegistryTagManager().items() != ItemTags.getContainer() || |
| 82 | + manager.getRegistryTagManager().fluids() != FluidTags.getContainer() || |
| 83 | + manager.getRegistryTagManager().entityTypes() != EntityTypeTags.getContainer(); |
61 | 84 | } |
62 | 85 | } |
0 commit comments