Add service loader example. Close #7

This commit is contained in:
Jared 2022-02-01 23:24:59 +02:00
parent 805b9d23d1
commit 36367b2382
No known key found for this signature in database
GPG Key ID: DB3BA3B9088A1BDF
9 changed files with 110 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package com.example.examplemod; package com.example.examplemod;
import com.example.examplemod.platform.Services;
import net.minecraft.core.Registry; import net.minecraft.core.Registry;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent; import net.minecraft.network.chat.TextComponent;
@ -17,7 +18,7 @@ public class CommonClass {
// invoked from a mod loader specific project like Forge or Fabric. // invoked from a mod loader specific project like Forge or Fabric.
public static void init() { 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)); Constants.LOG.info("Diamond Item >> {}", Registry.ITEM.getKey(Items.DIAMOND));
} }

View File

@ -5,7 +5,7 @@ import org.apache.logging.log4j.Logger;
public class Constants { public class Constants {
public static final String MOD_ID = "multiloader"; public static final String MOD_ID = "examplemod";
public static final String MOD_NAME = "MultiLoader"; public static final String MOD_NAME = "Example Mod";
public static final Logger LOG = LogManager.getLogger(MOD_NAME); public static final Logger LOG = LogManager.getLogger(MOD_NAME);
} }

View File

@ -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> T load(Class<T> 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;
}
}

View File

@ -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();
}

View File

@ -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();
}
}

View File

@ -0,0 +1 @@
com.example.examplemod.platform.FabricPlatformHelper

View File

@ -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();
}
}

View File

@ -0,0 +1 @@
com.example.examplemod.platform.ForgePlatformHelper

View File

@ -1,6 +1,6 @@
# Project # Project
version=1.0.0 version=1.0.0
group=com.blamejared.multiloader group=com.example.examplemod
# Common # Common
minecraft_version=1.18.1 minecraft_version=1.18.1