|
8 | 8 | import java.util.List; |
9 | 9 | import java.util.Objects; |
10 | 10 | import java.util.concurrent.CompletableFuture; |
11 | | -import java.util.concurrent.Executor; |
12 | 11 | import java.util.concurrent.TimeoutException; |
13 | 12 | import java.util.concurrent.atomic.AtomicBoolean; |
14 | 13 | import java.util.function.Function; |
|
24 | 23 | import io.github.axolotlclient.AxolotlClientConfig.impl.options.StringOption; |
25 | 24 | import lombok.Getter; |
26 | 25 | import net.fabricmc.api.ClientModInitializer; |
27 | | -import net.fabricmc.fabric.api.resource.ResourceManagerHelper; |
28 | | -import net.fabricmc.fabric.api.resource.SimpleResourceReloadListener; |
| 26 | +import net.fabricmc.fabric.api.resource.v1.ResourceLoader; |
29 | 27 | import net.fabricmc.loader.api.FabricLoader; |
30 | 28 | import net.minecraft.client.Minecraft; |
31 | 29 | import net.minecraft.resources.ResourceLocation; |
32 | 30 | import net.minecraft.server.packs.PackType; |
33 | | -import net.minecraft.server.packs.resources.ResourceManager; |
34 | 31 | import net.minecraft.util.RandomSource; |
35 | 32 | import org.apache.commons.lang3.ArrayUtils; |
36 | 33 | import org.apache.commons.lang3.StringUtils; |
@@ -69,53 +66,42 @@ public void onInitializeClient() { |
69 | 66 | var configManager = new JsonConfigManager(FabricLoader.getInstance().getConfigDir().resolve(MODID + ".json"), category); |
70 | 67 | AxolotlClientConfig.getInstance().register(configManager); |
71 | 68 | configManager.load(); |
72 | | - ResourceManagerHelper.get(PackType.CLIENT_RESOURCES) |
73 | | - .registerReloadListener(new SimpleResourceReloadListener<List<String>>() { |
74 | | - @Override |
75 | | - public ResourceLocation getFabricId() { |
76 | | - return rl("name-reloader"); |
77 | | - } |
78 | | - |
79 | | - @Override |
80 | | - public CompletableFuture<List<String>> load(ResourceManager resourceManager, Executor executor) { |
81 | | - return CompletableFuture.supplyAsync(() -> { |
82 | | - var blacklist = resourceManager.getResourceStack(BLACKLIST_LOCATION) |
83 | | - .stream().map(resource -> { |
84 | | - try { |
85 | | - return GSON.fromJson(resource.openAsReader(), String[].class); |
86 | | - } catch (IOException e) { |
87 | | - log.warn("Failed to load world names from {}: ", resource.sourcePackId(), e); |
88 | | - return null; |
89 | | - } |
90 | | - }).filter(Objects::nonNull) |
91 | | - .flatMap(Arrays::stream) |
92 | | - .toList(); |
93 | | - return resourceManager.getResourceStack(NAME_LOCATION) |
94 | | - .stream().map(resource -> { |
95 | | - if (blacklist.contains(resource.sourcePackId())) { |
96 | | - log.info("Skipping names from blacklisted pack: {}", resource.sourcePackId()); |
97 | | - return null; |
98 | | - } |
99 | | - try { |
100 | | - return GSON.fromJson(resource.openAsReader(), String[].class); |
101 | | - } catch (IOException e) { |
102 | | - log.warn("Failed to load world names from {}: ", resource.sourcePackId(), e); |
103 | | - return null; |
104 | | - } |
105 | | - }).filter(Objects::nonNull) |
106 | | - .flatMap(Arrays::stream) |
107 | | - .toList(); |
108 | | - }, executor); |
109 | | - } |
110 | | - |
111 | | - @Override |
112 | | - public CompletableFuture<Void> apply(List<String> o, ResourceManager resourceManager, Executor executor) { |
113 | | - return CompletableFuture.runAsync(() -> { |
114 | | - worldNames.clear(); |
115 | | - worldNames.addAll(o); |
116 | | - log.info("Loaded {} names for random world names!", o.size()); |
117 | | - }, executor); |
118 | | - } |
| 69 | + ResourceLoader.get(PackType.CLIENT_RESOURCES).registerReloader(rl("name-reloader"), |
| 70 | + (sharedState, executor, preparationBarrier, executor2) -> { |
| 71 | + var resourceManager = sharedState.resourceManager(); |
| 72 | + return CompletableFuture.supplyAsync(() -> { |
| 73 | + var blacklist = resourceManager.getResourceStack(BLACKLIST_LOCATION) |
| 74 | + .stream().map(resource -> { |
| 75 | + try { |
| 76 | + return GSON.fromJson(resource.openAsReader(), String[].class); |
| 77 | + } catch (IOException e) { |
| 78 | + log.warn("Failed to load world names from {}: ", resource.sourcePackId(), e); |
| 79 | + return null; |
| 80 | + } |
| 81 | + }).filter(Objects::nonNull) |
| 82 | + .flatMap(Arrays::stream) |
| 83 | + .toList(); |
| 84 | + return resourceManager.getResourceStack(NAME_LOCATION) |
| 85 | + .stream().map(resource -> { |
| 86 | + if (blacklist.contains(resource.sourcePackId())) { |
| 87 | + log.info("Skipping names from blacklisted pack: {}", resource.sourcePackId()); |
| 88 | + return null; |
| 89 | + } |
| 90 | + try { |
| 91 | + return GSON.fromJson(resource.openAsReader(), String[].class); |
| 92 | + } catch (IOException e) { |
| 93 | + log.warn("Failed to load world names from {}: ", resource.sourcePackId(), e); |
| 94 | + return null; |
| 95 | + } |
| 96 | + }).filter(Objects::nonNull) |
| 97 | + .flatMap(Arrays::stream) |
| 98 | + .toList(); |
| 99 | + }, executor).thenCompose(preparationBarrier::wait) |
| 100 | + .thenAcceptAsync(o -> { |
| 101 | + worldNames.clear(); |
| 102 | + worldNames.addAll(o); |
| 103 | + log.info("Loaded {} names for random world names!", o.size()); |
| 104 | + }, executor2); |
119 | 105 | }); |
120 | 106 | } |
121 | 107 |
|
@@ -146,12 +132,12 @@ private String generateRandomName() throws TimeoutException, LimitExceededExcept |
146 | 132 | } |
147 | 133 | CompletableFuture.runAsync(() -> { |
148 | 134 | try { |
149 | | - Thread.sleep(timeout.get()*1000); |
| 135 | + Thread.sleep(timeout.get() * 1000); |
150 | 136 | } catch (InterruptedException ignored) { |
151 | 137 | } |
152 | 138 | timeoutReached.set(true); |
153 | 139 | }); |
154 | | - double maxCombinations = Math.pow(nameLength.get(), limit+1); |
| 140 | + double maxCombinations = Math.pow(nameLength.get(), limit + 1); |
155 | 141 | for (int total = 0; total < maxCombinations; total++) { |
156 | 142 | if (timeoutReached.get()) { |
157 | 143 | throw new TimeoutException("Generation timeout reached."); |
|
0 commit comments