-
Notifications
You must be signed in to change notification settings - Fork 2
Soft Dependency
petrolpark edited this page Jan 25, 2025
·
5 revisions
If you have a mod, you may want to be able to make use of the Quality of your mod's Items in a unique way if the player has pquality installed, without having pquality as a hard dependency. This is possible.
Make a class called something like PqualityUtil of a format like this:
public class PqualityUtil {
protected static BiFunction<ItemStack, Double, Double> qualityMultiplier = (stack, d) -> d, qualityBigMultiplier = (stack, d) -> d, qualityReducer = (stack, d) -> d;
public static final double multiply(ItemStack stack, double value) {
return qualityMultiplier.apply(stack, value);
};
public static final double bigMultiply(ItemStack stack, double value) {
return qualityBigMultiplier.apply(stack, value);
};
public static final double reduce(ItemStack stack, double value) {
return qualityReducer.apply(stack, value);
};
}As this class does not have any dependency on any pquality classes, it can be used throughout your mod even if pquality is not installed. If the static methods are called without pquality installed it will just return the base number, same as if the Item Stack had no Quality.
Then, somewhere in mod initiation, add some code that replaces the behaviour of the Quality functions if pquality is installed:
import com.petrolpark.pquality.core.QualityUtil;
@Mod(MyMod.MODID)
public class MyMod {
public MyMod() {
// Other initiation stuff
if (ModList.get().isLoaded("pquality")) pqualityCompat().get().run()
};
private Supplier<Runnable> pqualityCompat() {
return () -> () -> {
PqualityUtil.qualityMultiplier = (stack, d) -> QualityUtil.getQuality(stack).multiply(d);
PqualityUtil.qualityBigMultiplier = (stack, d) -> QualityUtil.getQuality(stack).bigMultiply(d);
PqualityUtil.qualityReducer = (stack, d) -> QualityUtil.getQuality(stack).reduce(d);
};
};
}