Add service loader example. Close #7
This commit is contained in:
parent
805b9d23d1
commit
36367b2382
|
|
@ -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;
|
||||||
|
|
@ -11,27 +12,27 @@ import net.minecraft.world.item.TooltipFlag;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class CommonClass {
|
public class CommonClass {
|
||||||
|
|
||||||
// This method serves as an initialization hook for the mod. The vanilla
|
// This method serves as an initialization hook for the mod. The vanilla
|
||||||
// game has no mechanism to load tooltip listeners so this must be
|
// game has no mechanism to load tooltip listeners so this must be
|
||||||
// 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));
|
||||||
}
|
}
|
||||||
|
|
||||||
// This method serves as a hook to modify item tooltips. The vanilla game
|
// 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
|
// has no mechanism to load tooltip listeners so this must be registered
|
||||||
// by a mod loader like Forge or Fabric.
|
// by a mod loader like Forge or Fabric.
|
||||||
public static void onItemTooltip(ItemStack stack, TooltipFlag context, List<Component> tooltip) {
|
public static void onItemTooltip(ItemStack stack, TooltipFlag context, List<Component> tooltip) {
|
||||||
|
|
||||||
if (!stack.isEmpty()) {
|
if (!stack.isEmpty()) {
|
||||||
|
|
||||||
final FoodProperties food = stack.getItem().getFoodProperties();
|
final FoodProperties food = stack.getItem().getFoodProperties();
|
||||||
|
|
||||||
if (food != null) {
|
if (food != null) {
|
||||||
|
|
||||||
tooltip.add(new TextComponent("Nutrition: " + food.getNutrition()));
|
tooltip.add(new TextComponent("Nutrition: " + food.getNutrition()));
|
||||||
tooltip.add(new TextComponent("Saturation: " + food.getSaturationModifier()));
|
tooltip.add(new TextComponent("Saturation: " + food.getSaturationModifier()));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
com.example.examplemod.platform.FabricPlatformHelper
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
com.example.examplemod.platform.ForgePlatformHelper
|
||||||
|
|
@ -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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user