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

View File

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

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
version=1.0.0
group=com.blamejared.multiloader
group=com.example.examplemod
# Common
minecraft_version=1.18.1