Skip to content

Commit

Permalink
🌟 修改 integration 实现
Browse files Browse the repository at this point in the history
  • Loading branch information
Gu-ZT committed Dec 19, 2024
1 parent 9167a0e commit 1f75782
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/main/java/dev/anvilcraft/lib/AnvilLib.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

import com.electronwill.nightconfig.core.UnmodifiableConfig;
import dev.anvilcraft.lib.integration.AnvilLibIntegrations;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.ModList;
import net.neoforged.fml.common.Mod;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforgespi.language.IModInfo;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand All @@ -22,7 +24,7 @@ public class AnvilLib {
public static final String MOD_NAME = "AnvilLib";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_NAME);

public AnvilLib() {
public AnvilLib(IEventBus modEventBus) {
for (IModInfo mod : ModList.get().getMods()) {
Map<String, Object> modProperties = mod.getModProperties();
for (Map.Entry<String, Object> entry : modProperties.entrySet()) {
Expand All @@ -36,13 +38,18 @@ public AnvilLib() {
}
}
AnvilLibIntegrations.apply();
modEventBus.addListener(AnvilLib::clientSetup);
}

public static void clientSetup(FMLClientSetupEvent event) {
AnvilLibIntegrations.applyClient();
}

private static void loadIntegrations(@NotNull UnmodifiableConfig integrations) {
for (UnmodifiableConfig.Entry entry2 : integrations.entrySet()) {
String modid = entry2.getKey();
Object value = entry2.getValue();
List<String> classes = Collections.synchronizedList(new ArrayList<>());
List<String> classes = AnvilLibIntegrations.INTEGRATIONS.getOrDefault(modid, Collections.synchronizedList(new ArrayList<>()));
if (value instanceof String string) {
classes.add(string);
} else if (value instanceof List<?> list) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ public static void apply() {
}
}

public static void applyClient() {
for (Map.Entry<String, List<String>> entry : AnvilLibIntegrations.INTEGRATIONS.entrySet()) {
String modid = entry.getKey();
if (AnvilLib.isLoaded(modid)) {
AnvilLib.LOGGER.info("{}'s client integrations is loading...", modid);
List<String> classes = entry.getValue();
classes.forEach(AnvilLibIntegrations::applyClient);
AnvilLib.LOGGER.info("{}'s client integrations is loaded!", modid);
}
}
}

private static void apply(String name) {
try {
Class<?> clazz = Class.forName(name);
Expand All @@ -36,4 +48,16 @@ private static void apply(String name) {
AnvilLib.LOGGER.error(ex.getMessage(), ex);
}
}

private static void applyClient(String name) {
try {
Class<?> clazz = Class.forName(name);
if (!Integration.class.isAssignableFrom(clazz)) return;
Class<? extends Integration> integrationClass = clazz.asSubclass(Integration.class);
Integration integration = integrationClass.getDeclaredConstructor().newInstance();
integration.applyClient();
} catch (Exception ex) {
AnvilLib.LOGGER.error(ex.getMessage(), ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
* 集成
*/
public interface Integration {
void apply();
default void apply() {
}

default void applyClient() {
}
}

0 comments on commit 1f75782

Please sign in to comment.