use of slimeknights.tconstruct.library.tools.definition.ToolDefinitionData 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.definition.ToolDefinitionData in project TinkersConstruct by SlimeKnights.
the class MeleeHarvestToolStatsBuilder method from.
/**
* Creates a builder from the definition and materials
*/
public static ToolStatsBuilder from(ToolDefinition toolDefinition, MaterialNBT materials) {
ToolDefinitionData data = toolDefinition.getData();
List<PartRequirement> requiredComponents = data.getParts();
// if the NBT is invalid, at least we can return the default stats builder, as an exception here could kill itemstacks
if (materials.size() != requiredComponents.size()) {
return ToolStatsBuilder.noParts(toolDefinition);
}
return new MeleeHarvestToolStatsBuilder(data, listOfCompatibleWith(HeadMaterialStats.ID, materials, requiredComponents), listOfCompatibleWith(HandleMaterialStats.ID, materials, requiredComponents), listOfCompatibleWith(ExtraMaterialStats.ID, materials, requiredComponents));
}
use of slimeknights.tconstruct.library.tools.definition.ToolDefinitionData in project TinkersConstruct by SlimeKnights.
the class SkullToolStatsBuilder method from.
/**
* Creates a builder from the definition and materials
*/
public static ToolStatsBuilder from(ToolDefinition toolDefinition, MaterialNBT materials) {
ToolDefinitionData data = toolDefinition.getData();
List<PartRequirement> requiredComponents = data.getParts();
// if the NBT is invalid, at least we can return the default stats builder, as an exception here could kill itemstacks
if (materials.size() != requiredComponents.size()) {
return ToolStatsBuilder.noParts(toolDefinition);
}
return new SkullToolStatsBuilder(data, listOfCompatibleWith(SkullStats.ID, materials, requiredComponents));
}
use of slimeknights.tconstruct.library.tools.definition.ToolDefinitionData in project TinkersConstruct by SlimeKnights.
the class TinkerStationRepairRecipe method getRepairPerItem.
/**
* Gets the amount to repair per item
*/
protected float getRepairPerItem(ToolStack tool, ITinkerStationContainer inv, int slot, MaterialId repairMaterial) {
ItemStack stack = inv.getInput(slot);
// repair kit first
ToolDefinitionData toolData = tool.getDefinition().getData();
if (stack.getItem() == TinkerToolParts.repairKit.get()) {
// multiply by 2 (part cost), divide again by the repair factor to get the final percent
return MaterialRecipe.getRepairDurability(toolData, repairMaterial, getDefaultStatsId(tool, repairMaterial)) * 2 / MaterialRecipe.INGOTS_PER_REPAIR;
} else {
// material recipe fallback
MaterialRecipe recipe = inv.getInputMaterial(slot);
if (recipe != null) {
if (stack.getItem() instanceof IToolPart) {
return recipe.getRepairPerItem(toolData, ((IToolPart) stack.getItem()).getStatType());
}
return recipe.getRepairPerItem(toolData, getDefaultStatsId(tool, repairMaterial));
}
}
return 0;
}
use of slimeknights.tconstruct.library.tools.definition.ToolDefinitionData in project TinkersConstruct by SlimeKnights.
the class AbstractToolDefinitionDataProvider method run.
@Override
public void run(HashCache cache) throws IOException {
addToolDefinitions();
Map<ResourceLocation, ToolDefinition> relevantDefinitions = ToolDefinitionLoader.getInstance().getRegisteredToolDefinitions().stream().filter(def -> def.getId().getNamespace().equals(modId)).collect(Collectors.toMap(ToolDefinition::getId, Function.identity()));
// ensure all required definitions are included
for (ToolDefinition definition : relevantDefinitions.values()) {
ResourceLocation name = definition.getId();
if (!allTools.containsKey(name)) {
throw new IllegalStateException(String.format("Missing tool definition for '%s'", name));
}
}
// ensure all included ones are required, and the built ones are valid
for (Entry<ResourceLocation, ToolDefinitionDataBuilder> entry : allTools.entrySet()) {
ResourceLocation id = entry.getKey();
ToolDefinition definition = relevantDefinitions.get(id);
if (definition == null) {
throw new IllegalStateException("Unknown tool definition with ID " + id);
}
ToolDefinitionData data = entry.getValue().build();
definition.validate(data);
saveThing(cache, id, data);
}
}
Aggregations