From 36367b2382b6e889a386b725590070f20859aee8 Mon Sep 17 00:00:00 2001 From: Jared Date: Tue, 1 Feb 2022 23:24:59 +0200 Subject: [PATCH] Add service loader example. Close #7 --- .../com/example/examplemod/CommonClass.java | 17 ++++++------ .../com/example/examplemod/Constants.java | 4 +-- .../example/examplemod/platform/Services.java | 20 ++++++++++++++ .../platform/services/IPlatformHelper.java | 26 +++++++++++++++++++ .../platform/FabricPlatformHelper.java | 24 +++++++++++++++++ ...amplemod.platform.services.IPlatformHelper | 1 + .../platform/ForgePlatformHelper.java | 26 +++++++++++++++++++ ...amplemod.platform.services.IPlatformHelper | 1 + gradle.properties | 2 +- 9 files changed, 110 insertions(+), 11 deletions(-) create mode 100644 Common/src/main/java/com/example/examplemod/platform/Services.java create mode 100644 Common/src/main/java/com/example/examplemod/platform/services/IPlatformHelper.java create mode 100644 Fabric/src/main/java/com/example/examplemod/platform/FabricPlatformHelper.java create mode 100644 Fabric/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper create mode 100644 Forge/src/main/java/com/example/examplemod/platform/ForgePlatformHelper.java create mode 100644 Forge/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper diff --git a/Common/src/main/java/com/example/examplemod/CommonClass.java b/Common/src/main/java/com/example/examplemod/CommonClass.java index f5659ad..7d74614 100644 --- a/Common/src/main/java/com/example/examplemod/CommonClass.java +++ b/Common/src/main/java/com/example/examplemod/CommonClass.java @@ -1,5 +1,6 @@ package com.example.examplemod; +import com.example.examplemod.platform.Services; import net.minecraft.core.Registry; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.TextComponent; @@ -11,27 +12,27 @@ import net.minecraft.world.item.TooltipFlag; import java.util.List; public class CommonClass { - + // This method serves as an initialization hook for the mod. The vanilla // game has no mechanism to load tooltip listeners so this must be // invoked from a mod loader specific project like Forge or Fabric. public static void init() { - - Constants.LOG.info("Hello from Common init!"); + + Constants.LOG.info("Hello from Common init on {}! we are currently in a {} environment!", Services.PLATFORM.getPlatformName(), Services.PLATFORM.isDevelopmentEnvironment() ? "development" : "production"); Constants.LOG.info("Diamond Item >> {}", Registry.ITEM.getKey(Items.DIAMOND)); } - + // This method serves as a hook to modify item tooltips. The vanilla game // has no mechanism to load tooltip listeners so this must be registered // by a mod loader like Forge or Fabric. public static void onItemTooltip(ItemStack stack, TooltipFlag context, List tooltip) { - + if (!stack.isEmpty()) { - + final FoodProperties food = stack.getItem().getFoodProperties(); - + if (food != null) { - + tooltip.add(new TextComponent("Nutrition: " + food.getNutrition())); tooltip.add(new TextComponent("Saturation: " + food.getSaturationModifier())); } diff --git a/Common/src/main/java/com/example/examplemod/Constants.java b/Common/src/main/java/com/example/examplemod/Constants.java index 8cd59c5..f68ceda 100644 --- a/Common/src/main/java/com/example/examplemod/Constants.java +++ b/Common/src/main/java/com/example/examplemod/Constants.java @@ -5,7 +5,7 @@ import org.apache.logging.log4j.Logger; public class Constants { - public static final String MOD_ID = "multiloader"; - public static final String MOD_NAME = "MultiLoader"; + public static final String MOD_ID = "examplemod"; + public static final String MOD_NAME = "Example Mod"; public static final Logger LOG = LogManager.getLogger(MOD_NAME); } \ No newline at end of file diff --git a/Common/src/main/java/com/example/examplemod/platform/Services.java b/Common/src/main/java/com/example/examplemod/platform/Services.java new file mode 100644 index 0000000..738a418 --- /dev/null +++ b/Common/src/main/java/com/example/examplemod/platform/Services.java @@ -0,0 +1,20 @@ +package com.example.examplemod.platform; + +import com.example.examplemod.Constants; +import com.example.examplemod.platform.services.IPlatformHelper; + +import java.util.ServiceLoader; + +public class Services { + + public static final IPlatformHelper PLATFORM = load(IPlatformHelper.class); + + public static T load(Class clazz) { + + final T loadedService = ServiceLoader.load(clazz) + .findFirst() + .orElseThrow(() -> new NullPointerException("Failed to load service for " + clazz.getName())); + Constants.LOG.debug("Loaded {} for service {}", loadedService, clazz); + return loadedService; + } +} diff --git a/Common/src/main/java/com/example/examplemod/platform/services/IPlatformHelper.java b/Common/src/main/java/com/example/examplemod/platform/services/IPlatformHelper.java new file mode 100644 index 0000000..21e3452 --- /dev/null +++ b/Common/src/main/java/com/example/examplemod/platform/services/IPlatformHelper.java @@ -0,0 +1,26 @@ +package com.example.examplemod.platform.services; + +public interface IPlatformHelper { + + /** + * Gets the name of the current platform + * + * @return The name of the current platform. + */ + String getPlatformName(); + + /** + * Checks if a mod with the given id is loaded. + * + * @param modId The mod to check if it is loaded. + * @return True if the mod is loaded, false otherwise. + */ + boolean isModLoaded(String modId); + + /** + * Check if the game is currently in a development environment. + * + * @return True if in a development environment, false otherwise. + */ + boolean isDevelopmentEnvironment(); +} diff --git a/Fabric/src/main/java/com/example/examplemod/platform/FabricPlatformHelper.java b/Fabric/src/main/java/com/example/examplemod/platform/FabricPlatformHelper.java new file mode 100644 index 0000000..1a3371f --- /dev/null +++ b/Fabric/src/main/java/com/example/examplemod/platform/FabricPlatformHelper.java @@ -0,0 +1,24 @@ +package com.example.examplemod.platform; + +import com.example.examplemod.platform.services.IPlatformHelper; +import net.fabricmc.loader.api.FabricLoader; + +public class FabricPlatformHelper implements IPlatformHelper { + + @Override + public String getPlatformName() { + return "Fabric"; + } + + @Override + public boolean isModLoaded(String modId) { + + return FabricLoader.getInstance().isModLoaded(modId); + } + + @Override + public boolean isDevelopmentEnvironment() { + + return FabricLoader.getInstance().isDevelopmentEnvironment(); + } +} diff --git a/Fabric/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper b/Fabric/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper new file mode 100644 index 0000000..30c54ee --- /dev/null +++ b/Fabric/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper @@ -0,0 +1 @@ +com.example.examplemod.platform.FabricPlatformHelper \ No newline at end of file diff --git a/Forge/src/main/java/com/example/examplemod/platform/ForgePlatformHelper.java b/Forge/src/main/java/com/example/examplemod/platform/ForgePlatformHelper.java new file mode 100644 index 0000000..43edb0d --- /dev/null +++ b/Forge/src/main/java/com/example/examplemod/platform/ForgePlatformHelper.java @@ -0,0 +1,26 @@ +package com.example.examplemod.platform; + +import com.example.examplemod.platform.services.IPlatformHelper; +import net.minecraftforge.fml.ModList; +import net.minecraftforge.fml.loading.FMLLoader; + +public class ForgePlatformHelper implements IPlatformHelper { + + @Override + public String getPlatformName() { + + return "Forge"; + } + + @Override + public boolean isModLoaded(String modId) { + + return ModList.get().isLoaded(modId); + } + + @Override + public boolean isDevelopmentEnvironment() { + + return !FMLLoader.isProduction(); + } +} diff --git a/Forge/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper b/Forge/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper new file mode 100644 index 0000000..ff2910d --- /dev/null +++ b/Forge/src/main/resources/META-INF/services/com.example.examplemod.platform.services.IPlatformHelper @@ -0,0 +1 @@ +com.example.examplemod.platform.ForgePlatformHelper \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 1303f26..ee4aa35 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ # Project version=1.0.0 -group=com.blamejared.multiloader +group=com.example.examplemod # Common minecraft_version=1.18.1