|
| 1 | +package net.devtech.stacc; |
| 2 | + |
| 3 | +import com.google.gson.JsonElement; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import com.google.gson.JsonPrimitive; |
| 6 | +import com.mojang.logging.LogUtils; |
| 7 | +import net.devtech.stacc.mixin.ItemMaxCountAccess; |
| 8 | +import net.fabricmc.api.ModInitializer; |
| 9 | +import net.fabricmc.fabric.api.resource.IdentifiableResourceReloadListener; |
| 10 | +import net.fabricmc.fabric.api.resource.ResourceManagerHelper; |
| 11 | +import net.minecraft.item.Item; |
| 12 | +import net.minecraft.item.Items; |
| 13 | +import net.minecraft.registry.Registries; |
| 14 | +import net.minecraft.resource.Resource; |
| 15 | +import net.minecraft.resource.ResourceManager; |
| 16 | +import net.minecraft.resource.ResourceType; |
| 17 | +import net.minecraft.util.Identifier; |
| 18 | +import net.minecraft.util.JsonHelper; |
| 19 | +import net.minecraft.util.profiler.Profiler; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import java.io.BufferedReader; |
| 22 | +import java.io.IOException; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.IdentityHashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.concurrent.CompletableFuture; |
| 28 | +import java.util.concurrent.Executor; |
| 29 | + |
| 30 | +public class StaccLoader implements ModInitializer { |
| 31 | + private static final Logger LOGGER = LogUtils.getLogger(); |
| 32 | + public static final String MOD_ID = "stacc"; |
| 33 | + |
| 34 | + @Override |
| 35 | + public void onInitialize() { |
| 36 | + Map<Item, Integer> originalSizes = new IdentityHashMap<>(); |
| 37 | + ResourceManagerHelper.get(ResourceType.SERVER_DATA).registerReloadListener(new IdentifiableResourceReloadListener() { |
| 38 | + final Identifier id = id("cfg"); |
| 39 | + final Identifier resourceId = id("config.json"); |
| 40 | + |
| 41 | + @Override |
| 42 | + public Identifier getFabricId() { |
| 43 | + return this.id; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public CompletableFuture<Void> reload( |
| 48 | + Synchronizer synchronizer, |
| 49 | + ResourceManager manager, |
| 50 | + Profiler prepareProfiler, |
| 51 | + Profiler applyProfiler, |
| 52 | + Executor prepareExecutor, |
| 53 | + Executor applyExecutor |
| 54 | + ) { |
| 55 | + return CompletableFuture.supplyAsync(() -> { |
| 56 | + Map<Identifier, Integer> stackSizes = new HashMap<>(); |
| 57 | + List<Resource> resources = manager.getAllResources(this.resourceId); |
| 58 | + for (Resource resource : resources) { |
| 59 | + try(BufferedReader reader = resource.getReader()) { |
| 60 | + JsonObject keys = JsonHelper.deserialize(reader); |
| 61 | + for (var entry : keys.entrySet()) { |
| 62 | + Identifier itemId = new Identifier(entry.getKey()); |
| 63 | + JsonElement value = entry.getValue(); |
| 64 | + if(value.isJsonNull()) { |
| 65 | + stackSizes.remove(itemId); |
| 66 | + } else if(value.isJsonArray() || value.isJsonObject()) { |
| 67 | + throw new IOException("Invalid stack size for %s: %s! Must be: {[0-1,000,000,000], \"default\", or null}".formatted(itemId, value)); |
| 68 | + } else if(value instanceof JsonPrimitive p) { |
| 69 | + if(p.isNumber()) { |
| 70 | + int size = p.getAsNumber().intValue(); |
| 71 | + if(size < 0 || size > 1_000_000_000) { |
| 72 | + throw new IOException("Invalid stack size for %s: %s! Must be: {[0-1,000,000,000], \"default\", or null}".formatted(itemId, size)); |
| 73 | + } else { |
| 74 | + stackSizes.put(itemId, size); |
| 75 | + } |
| 76 | + } else if(p.isString() && p.getAsString().equals("default")) { |
| 77 | + stackSizes.remove(itemId); |
| 78 | + } else { |
| 79 | + throw new IOException("Invalid stack size for %s: %s! Must be: {[0-1,000,000,000], \"default\", or null}".formatted(itemId, value)); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } catch (IOException e) { |
| 84 | + throw rethrow(new IOException("Invalid stack:config.json in pack \"%s\", format is {\"item:id\": 43, \"item:id2\": 5785, ...}!".formatted(resource.getPack().getName()), e)); |
| 85 | + } |
| 86 | + } |
| 87 | + return stackSizes; |
| 88 | + }, prepareExecutor).thenCompose(synchronizer::whenPrepared).thenAcceptAsync(u -> { |
| 89 | + Map<Item, Integer> restore = new HashMap<>(originalSizes); |
| 90 | + for (var entry : u.entrySet()) { |
| 91 | + Identifier key = entry.getKey(); |
| 92 | + Integer value = entry.getValue(); |
| 93 | + Item item = Registries.ITEM.get(key); |
| 94 | + originalSizes.putIfAbsent(item, item.getMaxCount()); |
| 95 | + restore.remove(item); |
| 96 | + if(item == Items.AIR) { |
| 97 | + LOGGER.warn("No item found for: %s".formatted(key)); |
| 98 | + } else { |
| 99 | + int old = item.getMaxCount(); |
| 100 | + ((ItemMaxCountAccess)item).setMaxCount(value); |
| 101 | + LOGGER.info("Changed max count of %s from %s to %s!".formatted(key, old, value)); |
| 102 | + } |
| 103 | + } |
| 104 | + restore.forEach((item, originalCount) -> { |
| 105 | + ((ItemMaxCountAccess) item).setMaxCount(originalCount); |
| 106 | + originalSizes.remove(item); |
| 107 | + }); |
| 108 | + StaccGlobals.lastSize = -1; |
| 109 | + }, applyExecutor); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + public static Identifier id(String path) { |
| 115 | + return new Identifier(MOD_ID, path); |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * @return nothing, because it throws |
| 120 | + * @throws T rethrows {@code throwable} |
| 121 | + */ |
| 122 | + @SuppressWarnings("unchecked") |
| 123 | + public static <T extends Throwable> RuntimeException rethrow(Throwable throwable) throws T { |
| 124 | + throw (T) throwable; |
| 125 | + } |
| 126 | +} |
0 commit comments