Skip to content

Instantly share code, notes, and snippets.

@KnightMiner
Created December 3, 2017 19:03
Show Gist options
  • Save KnightMiner/18953ce7baa0d11e8cfae9a40c9bb176 to your computer and use it in GitHub Desktop.
Save KnightMiner/18953ce7baa0d11e8cfae9a40c9bb176 to your computer and use it in GitHub Desktop.
Example of how the ore preference getter would look
/*
* Ore preferences
*/
private static Map<String,ItemStack> preferenceCache = new HashMap<>();
public static ItemStack getPreference(String oreName, List<ItemStack> items) {
// just stop now if we have nothing
if(items.isEmpty()) {
return null;
}
// if we found one before, use that
if(preferenceCache.containsKey(oreName)) {
return preferenceCache.get(oreName).copy();
}
// if not, try the config to choose our best option
// start by seeing if we have a stack which matches the config
List<ItemStack> itemList = items
.stream()
.filter(stack -> Config.hasPreference.contains(stack.getItem().getRegistryName().getResourceDomain()))
.collect(ArrayList::new, ArrayList::add, ArrayList::addAll);
if(!itemList.isEmpty()) {
for(String mod : Config.modPreference) {
// find the first stack matching this mod ID
Optional<ItemStack> optional = itemList
.stream()
.filter(stack -> !stack.isEmpty())
.filter(stack -> stack.getItem().getRegistryName().getResourceDomain().equals(mod))
.findFirst();
// if we found something, return it
if(optional.isPresent()) {
preferenceCache.put(oreName, optional.get());
return optional.get().copy();
}
}
}
// nothing? just return the first item
return items.get(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment