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;
}
Aggregations