|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2026 Wurst-Imperium and contributors. |
| 3 | + * |
| 4 | + * This source code is subject to the terms of the GNU General Public |
| 5 | + * License, version 3. If a copy of the GPL was not distributed with this |
| 6 | + * file, You can obtain one at: https://www.gnu.org/licenses/gpl-3.0.txt |
| 7 | + */ |
| 8 | +package net.wurstclient.addons; |
| 9 | + |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.Collections; |
| 12 | +import java.util.List; |
| 13 | +import java.util.stream.Collectors; |
| 14 | +import net.fabricmc.loader.api.FabricLoader; |
| 15 | +import net.fabricmc.loader.api.entrypoint.EntrypointContainer; |
| 16 | +import net.fabricmc.loader.api.metadata.ModMetadata; |
| 17 | +import net.fabricmc.loader.api.metadata.Person; |
| 18 | + |
| 19 | +public final class AddonManager |
| 20 | +{ |
| 21 | + private static final ArrayList<WurstAddon> addons = new ArrayList<>(); |
| 22 | + private static boolean initialized; |
| 23 | + |
| 24 | + private AddonManager() |
| 25 | + {} |
| 26 | + |
| 27 | + public static void init() |
| 28 | + { |
| 29 | + if(initialized) |
| 30 | + return; |
| 31 | + |
| 32 | + initialized = true; |
| 33 | + |
| 34 | + loadAddons(); |
| 35 | + initializeAddons(); |
| 36 | + |
| 37 | + if(!addons.isEmpty()) |
| 38 | + System.out.println("Loaded Wurst addons: " + addons.stream() |
| 39 | + .map(a -> a.name).collect(Collectors.joining(", "))); |
| 40 | + } |
| 41 | + |
| 42 | + private static void loadAddons() |
| 43 | + { |
| 44 | + for(EntrypointContainer<WurstAddon> entrypoint : FabricLoader |
| 45 | + .getInstance().getEntrypointContainers("wurst", WurstAddon.class)) |
| 46 | + { |
| 47 | + ModMetadata metadata = entrypoint.getProvider().getMetadata(); |
| 48 | + WurstAddon addon; |
| 49 | + try |
| 50 | + { |
| 51 | + addon = entrypoint.getEntrypoint(); |
| 52 | + |
| 53 | + }catch(Throwable t) |
| 54 | + { |
| 55 | + throw new RuntimeException( |
| 56 | + "Exception while preparing Wurst addon \"" |
| 57 | + + metadata.getName() + "\".", |
| 58 | + t); |
| 59 | + } |
| 60 | + |
| 61 | + addon.id = metadata.getId(); |
| 62 | + addon.name = metadata.getName(); |
| 63 | + addon.version = metadata.getVersion().getFriendlyString(); |
| 64 | + |
| 65 | + if(metadata.getAuthors().isEmpty()) |
| 66 | + throw new RuntimeException("Wurst addon \"" + addon.name |
| 67 | + + "\" requires at least one author in fabric.mod.json."); |
| 68 | + |
| 69 | + addon.authors = new String[metadata.getAuthors().size()]; |
| 70 | + int i = 0; |
| 71 | + for(Person author : metadata.getAuthors()) |
| 72 | + addon.authors[i++] = author.getName(); |
| 73 | + |
| 74 | + addons.add(addon); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static void initializeAddons() |
| 79 | + { |
| 80 | + for(WurstAddon addon : addons) |
| 81 | + try |
| 82 | + { |
| 83 | + addon.onInitialize(); |
| 84 | + |
| 85 | + }catch(Throwable t) |
| 86 | + { |
| 87 | + throw new RuntimeException( |
| 88 | + "Exception during Wurst addon init \"" + addon.name + "\".", |
| 89 | + t); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + public static List<WurstAddon> getAddons() |
| 94 | + { |
| 95 | + return Collections.unmodifiableList(addons); |
| 96 | + } |
| 97 | +} |
0 commit comments