use of mekanism.common.content.qio.filter.QIOItemStackFilter 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;
}
use of mekanism.common.content.qio.filter.QIOItemStackFilter in project Mekanism by mekanism.
the class CCArgumentWrapper method wrapReturnType.
private static Object wrapReturnType(Object result) {
if (result == null || result instanceof Number || result instanceof Boolean || result instanceof String) {
// Short circuit if it doesn't need wrapping
return result;
} else if (result instanceof ResourceLocation || result instanceof UUID) {
return result.toString();
} else if (result instanceof ForgeRegistryEntry<?>) {
return getName((ForgeRegistryEntry<?>) result);
} else if (result instanceof ChemicalStack<?>) {
ChemicalStack<?> stack = (ChemicalStack<?>) result;
Map<String, Object> wrapped = new HashMap<>(2);
wrapped.put("name", getName(stack.getType()));
wrapped.put("amount", stack.getAmount());
return wrapped;
} else if (result instanceof FluidStack) {
FluidStack stack = (FluidStack) result;
return wrapStack(stack.getFluid(), "amount", stack.getAmount(), stack.getTag());
} else if (result instanceof ItemStack) {
ItemStack stack = (ItemStack) result;
return wrapStack(stack.getItem(), "count", stack.getCount(), stack.getTag());
} else if (result instanceof INBT) {
Object wrapped = wrapNBT((INBT) result);
if (wrapped != null) {
return wrapped;
}
} else if (result instanceof Vector3i) {
// BlockPos is covered by this case
Vector3i pos = (Vector3i) result;
Map<String, Object> wrapped = new HashMap<>(3);
wrapped.put("x", pos.getX());
wrapped.put("y", pos.getY());
wrapped.put("z", pos.getZ());
return wrapped;
} else if (result instanceof Coord4D) {
// BlockPos is covered by this case
Coord4D coord = (Coord4D) result;
Map<String, Object> wrapped = new HashMap<>(4);
wrapped.put("x", coord.getX());
wrapped.put("y", coord.getY());
wrapped.put("z", coord.getZ());
wrapped.put("dimension", wrapReturnType(coord.dimension.location()));
return wrapped;
} else if (result instanceof Frequency) {
FrequencyIdentity identity = ((Frequency) result).getIdentity();
Map<String, Object> wrapped = new HashMap<>(2);
wrapped.put("key", wrapReturnType(identity.getKey()));
wrapped.put("public", identity.isPublic());
return wrapped;
} else if (result instanceof Enum) {
return ((Enum<?>) result).name();
} else if (result instanceof IFilter) {
Map<String, Object> wrapped = new HashMap<>();
wrapped.put("type", wrapReturnType(((IFilter<?>) result).getFilterType()));
if (result instanceof IItemStackFilter) {
ItemStack stack = ((IItemStackFilter<?>) result).getItemStack();
wrapped.put("item", wrapReturnType(stack.getItem()));
if (!stack.isEmpty()) {
CompoundNBT tag = stack.getTag();
if (tag != null && !tag.isEmpty()) {
wrapped.put("itemNBT", wrapNBT(tag));
}
}
} else if (result instanceof IMaterialFilter) {
wrapped.put("materialItem", wrapReturnType(((IMaterialFilter<?>) result).getMaterialItem().getItem()));
} else if (result instanceof IModIDFilter) {
wrapped.put("modId", ((IModIDFilter<?>) result).getModID());
} else if (result instanceof ITagFilter) {
wrapped.put("tag", ((ITagFilter<?>) result).getTagName());
}
if (result instanceof MinerFilter) {
MinerFilter<?> minerFilter = (MinerFilter<?>) result;
wrapped.put("requiresReplacement", minerFilter.requiresReplacement);
wrapped.put("replaceTarget", wrapReturnType(minerFilter.replaceTarget));
} else if (result instanceof SorterFilter) {
SorterFilter<?> sorterFilter = (SorterFilter<?>) result;
wrapped.put("allowDefault", sorterFilter.allowDefault);
wrapped.put("color", wrapReturnType(sorterFilter.color));
wrapped.put("size", sorterFilter.sizeMode);
wrapped.put("min", sorterFilter.min);
wrapped.put("max", sorterFilter.max);
if (sorterFilter instanceof SorterItemStackFilter) {
SorterItemStackFilter filter = (SorterItemStackFilter) sorterFilter;
wrapped.put("fuzzy", filter.fuzzyMode);
}
} else if (result instanceof QIOFilter) {
QIOFilter<?> qioFilter = (QIOFilter<?>) result;
if (qioFilter instanceof QIOItemStackFilter) {
QIOItemStackFilter filter = (QIOItemStackFilter) qioFilter;
wrapped.put("fuzzy", filter.fuzzyMode);
}
} else if (result instanceof OredictionificatorFilter) {
OredictionificatorFilter<?, ?, ?> filter = (OredictionificatorFilter<?, ?, ?>) result;
wrapped.put("target", filter.getFilterText());
wrapped.put("selected", wrapReturnType(filter.getResultElement()));
}
return wrapped;
} else if (result instanceof Map) {
return ((Map<?, ?>) result).entrySet().stream().collect(Collectors.toMap(entry -> wrapReturnType(entry.getKey()), entry -> wrapReturnType(entry.getValue()), (a, b) -> b));
} else if (result instanceof Collection) {
// so there is no real difference at that point about the type it is
return ((Collection<?>) result).stream().map(CCArgumentWrapper::wrapReturnType).collect(Collectors.toList());
} else if (result instanceof Object[]) {
// Note: This doesn't handle/deal with primitive arrays
return Arrays.stream((Object[]) result).map(CCArgumentWrapper::wrapReturnType).toArray();
}
return result;
}
use of mekanism.common.content.qio.filter.QIOItemStackFilter in project Mekanism by mekanism.
the class TileEntityQIOExporter method getFilterEjectMap.
private Object2LongMap<HashedItem> getFilterEjectMap(TileEntity back, QIOFrequency freq) {
Object2LongMap<HashedItem> map = new Object2LongOpenHashMap<>();
for (QIOFilter<?> filter : getFilters()) {
if (filter instanceof QIOItemStackFilter) {
QIOItemStackFilter itemFilter = (QIOItemStackFilter) filter;
if (itemFilter.fuzzyMode) {
map.putAll(freq.getStacksByItem(itemFilter.getItemStack().getItem()));
} else {
HashedItem type = HashedItem.create(itemFilter.getItemStack());
map.put(type, freq.getStored(type));
}
} else if (filter instanceof QIOTagFilter) {
String tagName = ((QIOTagFilter) filter).getTagName();
map.putAll(freq.getStacksByTagWildcard(tagName));
} else if (filter instanceof QIOModIDFilter) {
String modID = ((QIOModIDFilter) filter).getModID();
map.putAll(freq.getStacksByModIDWildcard(modID));
}
}
return map;
}
Aggregations