R/extractItemNames.R

#' Extract item names from item stat ID
#' 
#' Extract common item names from a list of item stats. Uses \code{strings.items} for 
#' substitutions of item names if specified.
#' @keywords items
#' @param stat The item stat(s) from which item names should be extracted
#' @param itemData Dataset of descriptive strings, defaults to \link[wurstmineR]{strings.items}
#' @param type Either \code{id} or \code{name}
#' @return Vector of item names
#' @export
#' @note Required to produce \code{itemStats}, see \link[wurstmineR]{getItemStats}
#' @examples
#' extractItemNames(stat = "craftItem.minecraft.coal_block", itemData = wurstmineR::strings.items)
extractItemNames <- function(stat, itemData = NULL, type = "name"){
  if (is.null(itemData)){
    itemData <- wurstmineR::strings.items
  }
  item <- sub("mineBlock.", "", stat)
  item <- sub("craftItem.", "", item)
  item <- sub("useItem.",   "", item)
  item <- sub("breakItem.", "", item)

  if (type == "id"){
    return(item)
  } else if (type == "name"){
  item <- lapply(item, function(item){
            if (item %in% itemData$id){
              sub(item, itemData$name[itemData$id == item][1], item)
            } else {item}
          })
  item <- unlist(item)
  return(item)
  } else {
    stop("Something went very wrong.")
  }
}
jemus42/wurstmineR documentation built on May 19, 2019, 4:03 a.m.