aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/shcm/shsupercm/fabric/citresewn/OptionalCompat.java
blob: 580aef508e85852de64ca3d8f7d60950bdbe5a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package shcm.shsupercm.fabric.citresewn;

import io.github.apace100.cosmetic_armor.CosmeticArmor;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.NoticeScreen;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import shcm.shsupercm.fabric.citresewn.config.CITResewnConfigScreenFactory;

import java.util.function.Function;
import java.util.function.Predicate;

public final class OptionalCompat {
    private static final OptionalCompat INSTANCE = new OptionalCompat(s -> FabricLoader.getInstance().isModLoaded(s));

    public final CompatClothConfig compatClothConfig;

    public final CompatCosmeticArmor compatCosmeticArmor;

    private OptionalCompat(Predicate<String> isLoaded) {
        compatClothConfig = isLoaded.test("cloth-config2") ? CompatClothConfig.impl() : null;
        compatCosmeticArmor = isLoaded.test("cosmetic-armor") ? CompatCosmeticArmor.impl() : null;
    }

    public static Function<Screen, Screen> getModConfigScreenFactory() {
        if (INSTANCE.compatClothConfig != null) {
            return INSTANCE.compatClothConfig.getModConfigScreenFactory();
        }

        return parent -> new NoticeScreen(() -> MinecraftClient.getInstance().setScreen(parent), Text.of("CIT Resewn"), Text.of("CIT Resewn requires Cloth Config 2 to be able to show the config."));
    }

    public static ItemStack getCosmeticArmor(ItemStack original, LivingEntity entity, EquipmentSlot slot, boolean elytra) {
        if (INSTANCE.compatCosmeticArmor != null) {
            ItemStack stackInCosmeticSlot = INSTANCE.compatCosmeticArmor.getStackInCosmeticSlot(entity, slot);
            if (!stackInCosmeticSlot.isEmpty() && (!elytra || stackInCosmeticSlot.isOf(Items.ELYTRA)))
                return stackInCosmeticSlot;
        }

        return original;
    }

    /**
     * Compatibility with 'cloth-config2': Custom gui for CITResewn's config
     */
    public interface CompatClothConfig {
        private static CompatClothConfig impl() {
            return () -> CITResewnConfigScreenFactory::create;
        }

        Function<Screen, Screen> getModConfigScreenFactory();
    }

    /**
     * Compatibility with 'cosmetic-armor': Display cits for cosmetic armors instead of equipped armors
     */
    public interface CompatCosmeticArmor {
        private static CompatCosmeticArmor impl() {
            return CosmeticArmor::getCosmeticArmor;
        }

        ItemStack getStackInCosmeticSlot(LivingEntity entity, EquipmentSlot slot);
    }
}