Create/src/main/java/com/simibubi/create/compat/Mods.java

42 lines
1007 B
Java
Raw Normal View History

2021-05-20 08:31:35 +02:00
package com.simibubi.create.compat;
import java.util.Optional;
import java.util.function.Supplier;
import com.simibubi.create.foundation.utility.Lang;
import net.minecraftforge.fml.ModList;
2021-05-20 08:31:35 +02:00
/**
* For compatibility with and without another mod present, we have to define load conditions of the specific code
*/
public enum Mods {
DYNAMICTREES,
TCONSTRUCT;
2021-05-20 08:31:35 +02:00
/**
2021-05-20 08:39:22 +02:00
* @return a boolean of whether the mod is loaded or not based on mod id
2021-05-20 08:31:35 +02:00
*/
public boolean isLoaded() {
return ModList.get().isLoaded(asId());
}
2021-05-20 08:39:22 +02:00
/**
* @return the mod id
*/
2021-05-20 08:31:35 +02:00
public String asId() {
return Lang.asId(name());
2021-05-20 08:31:35 +02:00
}
/**
* Simple hook to run code if a mod is installed
2021-05-20 08:39:22 +02:00
* @param toRun will be run only if the mod is loaded
* @return Optional.empty() if the mod is not loaded, otherwise an Optional of the return value of the given supplier
2021-05-20 08:31:35 +02:00
*/
public <T> Optional<T> runIfInstalled(Supplier<Supplier<T>> toRun) {
if (isLoaded())
return Optional.of(toRun.get().get());
return Optional.empty();
}
}