use of slimeknights.tconstruct.library.tools.nbt.MaterialNBT in project TinkersConstruct by SlimeKnights.
the class ToolBuildHandler method randomMaterials.
/**
* Gets a list of random materials consistent with the given tool definition data
* @param data Data, primarily used for part requirements
* @param maxTier Max tier of material allowed
* @param allowHidden If true, hidden materials may be used
* @return List of random materials
*/
public static MaterialNBT randomMaterials(ToolDefinitionData data, int maxTier, boolean allowHidden) {
// start by getting a list of materials for each stat type we need
List<PartRequirement> requirements = data.getParts();
// figure out which stat types we need
Map<MaterialStatsId, List<IMaterial>> materialChoices = requirements.stream().map(PartRequirement::getStatType).distinct().collect(Collectors.toMap(Function.identity(), t -> new ArrayList<>()));
IMaterialRegistry registry = MaterialRegistry.getInstance();
registry.getAllMaterials().stream().filter(mat -> (allowHidden || !mat.isHidden()) && mat.getTier() <= maxTier).forEach(mat -> {
for (IMaterialStats stats : registry.getAllStats(mat.getIdentifier())) {
List<IMaterial> list = materialChoices.get(stats.getIdentifier());
if (list != null) {
list.add(mat);
}
}
});
// then randomly choose a material from the lists for each part
MaterialNBT.Builder builder = MaterialNBT.builder();
for (PartRequirement requirement : requirements) {
// if the list has no materials for some reason, skip, null should be impossible but might as well be safe
List<IMaterial> choices = materialChoices.get(requirement.getStatType());
if (choices == null || choices.isEmpty()) {
builder.add(MaterialVariant.UNKNOWN);
TConstruct.LOG.error("Failed to find a {} material of type {} below tier {}", allowHidden ? "non-hidden " : "", requirement.getStatType(), maxTier);
} else {
builder.add(choices.get(TConstruct.RANDOM.nextInt(choices.size())));
}
}
return builder.build();
}
use of slimeknights.tconstruct.library.tools.nbt.MaterialNBT in project TinkersConstruct by SlimeKnights.
the class TooltipUtil method getComponents.
/**
* Gets the tooltip of the components list of a tool
* @param item Modifiable item instance
* @param stack Item stack being displayed
* @param tooltips List of tooltips
*/
public static void getComponents(IModifiable item, ItemStack stack, List<Component> tooltips) {
// no components, nothing to do
List<PartRequirement> components = item.getToolDefinition().getData().getParts();
if (components.isEmpty()) {
return;
}
// no materials is bad
MaterialNBT materials = ToolStack.from(stack).getMaterials();
if (materials.size() == 0) {
tooltips.add(NO_DATA);
return;
}
// wrong number is bad
if (materials.size() < components.size()) {
return;
}
// finally, display them all
int max = components.size() - 1;
for (int i = 0; i <= max; i++) {
PartRequirement requirement = components.get(i);
MaterialVariantId material = materials.get(i).getVariant();
tooltips.add(requirement.nameForMaterial(material).copy().withStyle(ChatFormatting.UNDERLINE).withStyle(style -> style.withColor(MaterialTooltipCache.getColor(material))));
MaterialRegistry.getInstance().getMaterialStats(material.getId(), requirement.getStatType()).ifPresent(stat -> tooltips.addAll(stat.getLocalizedInfo()));
if (i != max) {
tooltips.add(TextComponent.EMPTY);
}
}
}
use of slimeknights.tconstruct.library.tools.nbt.MaterialNBT in project TinkersConstruct by SlimeKnights.
the class TooltipUtil method getDisplayName.
/**
* Gets the display name for a tool including the head material in the name
* @param stack Stack instance
* @param tool Tool instance
* @return Display name including the head material
*/
public static Component getDisplayName(ItemStack stack, @Nullable IToolStackView tool, ToolDefinition toolDefinition) {
List<PartRequirement> components = toolDefinition.getData().getParts();
Component baseName = new TranslatableComponent(stack.getDescriptionId());
if (components.isEmpty()) {
return baseName;
}
// if there is a mismatch in material size, just stop here
if (tool == null)
tool = ToolStack.from(stack);
MaterialNBT materials = tool.getMaterials();
if (materials.size() != components.size()) {
return baseName;
}
// if the tool is not named we use the repair materials for a prefix like thing
// set ensures we don't use the same name twice, specifically a set of components ensures if two variants have the same name we don't use both
Set<Component> nameMaterials = Sets.newLinkedHashSet();
MaterialVariantId firstMaterial = null;
IMaterialRegistry registry = MaterialRegistry.getInstance();
for (int i = 0; i < components.size(); i++) {
if (i < materials.size() && registry.canRepair(components.get(i).getStatType())) {
MaterialVariantId material = materials.get(i).getVariant();
if (!IMaterial.UNKNOWN_ID.equals(material)) {
if (firstMaterial == null) {
firstMaterial = material;
}
nameMaterials.add(MaterialTooltipCache.getDisplayName(material));
}
}
}
// if a single material, use the single material logic
if (nameMaterials.size() == 1) {
return getMaterialItemName(stack, baseName, firstMaterial);
}
// multiple means we mix them together
return getCombinedItemName(baseName, nameMaterials);
}
use of slimeknights.tconstruct.library.tools.nbt.MaterialNBT in project TinkersConstruct by SlimeKnights.
the class ToolStatsBuilder method listOfCompatibleWith.
/**
* Gets a list of all stats for the given part type
* @param statsId Stat type
* @param materials Materials list
* @param parts List of required components, filters stat types
* @param <T> Type of stats
* @return List of stats
*/
public static <T extends IMaterialStats> List<T> listOfCompatibleWith(MaterialStatsId statsId, MaterialNBT materials, List<PartRequirement> parts) {
ImmutableList.Builder<T> builder = ImmutableList.builder();
// iterating both lists at once, precondition that they have the same size
int size = parts.size();
for (int i = 0; i < size; i++) {
// ensure stat type is valid
PartRequirement part = parts.get(i);
if (part.getStatType().equals(statsId)) {
T stats = fetchStatsOrDefault(materials.get(i).getId(), part.getStatType());
if (stats != null) {
// add a copy of the stat once per weight, lazy way to do weighting
for (int w = 0; w < part.getWeight(); w++) {
builder.add(stats);
}
}
}
}
return builder.build();
}
Aggregations