Search in sources :

Example 1 with FilterType

use of mekanism.common.content.filter.FilterType in project Mekanism by mekanism.

the class CCArgumentWrapper method convertMapToFilter.

@Nullable
private static Object convertMapToFilter(Class<?> expectedType, Map<?, ?> map) {
    // We may want to try improving this at some point, or somehow making it slightly less hardcoded
    // but for now this will have to do
    Object type = map.get("type");
    if (type instanceof String) {
        // Handle filters as arguments, this may not be the best implementation, but it will do for now
        FilterType filterType = sanitizeStringToEnum(FilterType.class, (String) type);
        if (filterType != null) {
            IFilter<?> filter = BaseFilter.fromType(filterType);
            if (expectedType.isInstance(filter)) {
                // Validate the filter is of the type we expect
                if (filter instanceof IItemStackFilter) {
                    ItemStack stack = tryCreateFilterItem(map.get("item"), map.get("itemNBT"));
                    if (stack.isEmpty()) {
                        return null;
                    }
                    ((IItemStackFilter<?>) filter).setItemStack(stack);
                } else if (filter instanceof IMaterialFilter) {
                    ItemStack stack = tryCreateFilterItem(map.get("materialItem"), null);
                    if (stack.isEmpty()) {
                        return null;
                    }
                    ((IMaterialFilter<?>) filter).setMaterialItem(stack);
                } else if (filter instanceof IModIDFilter) {
                    String modId = tryGetFilterModId(map.get("modId"));
                    if (modId == null) {
                        return null;
                    }
                    ((IModIDFilter<?>) filter).setModID(modId);
                } else if (filter instanceof ITagFilter) {
                    String tag = tryGetFilterTag(map.get("tag"));
                    if (tag == null) {
                        return null;
                    }
                    ((ITagFilter<?>) filter).setTagName(tag);
                }
                if (filter instanceof MinerFilter) {
                    MinerFilter<?> minerFilter = (MinerFilter<?>) filter;
                    minerFilter.requiresReplacement = getBooleanFromRaw(map.get("requiresReplacement"));
                    minerFilter.replaceTarget = tryCreateItem(map.get("replaceTarget"));
                } else if (filter instanceof SorterFilter) {
                    SorterFilter<?> sorterFilter = (SorterFilter<?>) filter;
                    sorterFilter.allowDefault = getBooleanFromRaw(map.get("allowDefault"));
                    Object rawColor = map.get("color");
                    if (rawColor instanceof String) {
                        sorterFilter.color = sanitizeStringToEnum(EnumColor.class, (String) rawColor);
                    }
                    sorterFilter.sizeMode = getBooleanFromRaw(map.get("size"));
                    sorterFilter.min = getIntFromRaw(map.get("min"));
                    sorterFilter.max = getIntFromRaw(map.get("max"));
                    if (sorterFilter.min < 0 || sorterFilter.max < 0 || sorterFilter.min > sorterFilter.max || sorterFilter.max > 64) {
                        return null;
                    }
                    if (sorterFilter instanceof SorterItemStackFilter) {
                        SorterItemStackFilter sorterItemFilter = (SorterItemStackFilter) sorterFilter;
                        sorterItemFilter.fuzzyMode = getBooleanFromRaw(map.get("fuzzy"));
                    }
                } else if (filter instanceof QIOFilter) {
                    QIOFilter<?> qioFilter = (QIOFilter<?>) filter;
                    if (qioFilter instanceof QIOItemStackFilter) {
                        QIOItemStackFilter qioItemFilter = (QIOItemStackFilter) qioFilter;
                        qioItemFilter.fuzzyMode = getBooleanFromRaw(map.get("fuzzy"));
                    }
                } else if (filter instanceof OredictionificatorFilter) {
                    OredictionificatorFilter<?, ?, ?> oredictionificatorFilter = (OredictionificatorFilter<?, ?, ?>) filter;
                    Object rawTag = map.get("target");
                    if (!(rawTag instanceof String)) {
                        return null;
                    }
                    String tag = (String) rawTag;
                    if (tag.isEmpty()) {
                        return null;
                    }
                    ResourceLocation rl = ResourceLocation.tryParse(tag);
                    if (rl == null || !TileEntityOredictionificator.isValidTarget(rl)) {
                        return null;
                    }
                    oredictionificatorFilter.setFilter(rl);
                    if (oredictionificatorFilter instanceof OredictionificatorItemFilter) {
                        Item item = tryCreateItem(map.get("selected"));
                        if (item != Items.AIR) {
                            ((OredictionificatorItemFilter) oredictionificatorFilter).setSelectedOutput(item);
                        }
                    }
                }
                return filter;
            }
        }
    }
    return null;
}
Also used : SorterFilter(mekanism.common.content.transporter.SorterFilter) IMaterialFilter(mekanism.common.content.filter.IMaterialFilter) MinerFilter(mekanism.common.content.miner.MinerFilter) QIOItemStackFilter(mekanism.common.content.qio.filter.QIOItemStackFilter) ITagFilter(mekanism.common.content.filter.ITagFilter) Item(net.minecraft.item.Item) FilterType(mekanism.common.content.filter.FilterType) IItemStackFilter(mekanism.common.content.filter.IItemStackFilter) SorterItemStackFilter(mekanism.common.content.transporter.SorterItemStackFilter) ResourceLocation(net.minecraft.util.ResourceLocation) QIOFilter(mekanism.common.content.qio.filter.QIOFilter) OredictionificatorFilter(mekanism.common.content.oredictionificator.OredictionificatorFilter) ItemStack(net.minecraft.item.ItemStack) IModIDFilter(mekanism.common.content.filter.IModIDFilter) OredictionificatorItemFilter(mekanism.common.content.oredictionificator.OredictionificatorItemFilter) Nullable(javax.annotation.Nullable)

Aggregations

Nullable (javax.annotation.Nullable)1 FilterType (mekanism.common.content.filter.FilterType)1 IItemStackFilter (mekanism.common.content.filter.IItemStackFilter)1 IMaterialFilter (mekanism.common.content.filter.IMaterialFilter)1 IModIDFilter (mekanism.common.content.filter.IModIDFilter)1 ITagFilter (mekanism.common.content.filter.ITagFilter)1 MinerFilter (mekanism.common.content.miner.MinerFilter)1 OredictionificatorFilter (mekanism.common.content.oredictionificator.OredictionificatorFilter)1 OredictionificatorItemFilter (mekanism.common.content.oredictionificator.OredictionificatorItemFilter)1 QIOFilter (mekanism.common.content.qio.filter.QIOFilter)1 QIOItemStackFilter (mekanism.common.content.qio.filter.QIOItemStackFilter)1 SorterFilter (mekanism.common.content.transporter.SorterFilter)1 SorterItemStackFilter (mekanism.common.content.transporter.SorterItemStackFilter)1 Item (net.minecraft.item.Item)1 ItemStack (net.minecraft.item.ItemStack)1 ResourceLocation (net.minecraft.util.ResourceLocation)1