Search in sources :

Example 1 with ToolDefinitionData

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();
}
Also used : MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) Arrays(java.util.Arrays) Item(net.minecraft.world.item.Item) Config(slimeknights.tconstruct.common.config.Config) Function(java.util.function.Function) ArrayList(java.util.ArrayList) MaterialRegistry(slimeknights.tconstruct.library.materials.MaterialRegistry) ToolDefinitionData(slimeknights.tconstruct.library.tools.definition.ToolDefinitionData) ToolDefinition(slimeknights.tconstruct.library.tools.definition.ToolDefinition) Map(java.util.Map) IModifiable(slimeknights.tconstruct.library.tools.item.IModifiable) IMaterial(slimeknights.tconstruct.library.materials.definition.IMaterial) PartRequirement(slimeknights.tconstruct.library.tools.definition.PartRequirement) MaterialVariant(slimeknights.tconstruct.library.materials.definition.MaterialVariant) MaterialVariantId(slimeknights.tconstruct.library.materials.definition.MaterialVariantId) Collectors(java.util.stream.Collectors) TConstruct(slimeknights.tconstruct.TConstruct) List(java.util.List) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) IMaterialRegistry(slimeknights.tconstruct.library.materials.IMaterialRegistry) MaterialNBT(slimeknights.tconstruct.library.tools.nbt.MaterialNBT) IMaterialStats(slimeknights.tconstruct.library.materials.stats.IMaterialStats) ItemStack(net.minecraft.world.item.ItemStack) MaterialIdNBT(slimeknights.tconstruct.library.tools.nbt.MaterialIdNBT) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) IMaterial(slimeknights.tconstruct.library.materials.definition.IMaterial) ArrayList(java.util.ArrayList) MaterialNBT(slimeknights.tconstruct.library.tools.nbt.MaterialNBT) PartRequirement(slimeknights.tconstruct.library.tools.definition.PartRequirement) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) IMaterialStats(slimeknights.tconstruct.library.materials.stats.IMaterialStats) ArrayList(java.util.ArrayList) List(java.util.List) IMaterialRegistry(slimeknights.tconstruct.library.materials.IMaterialRegistry)

Example 2 with ToolDefinitionData

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));
}
Also used : ToolDefinitionData(slimeknights.tconstruct.library.tools.definition.ToolDefinitionData) PartRequirement(slimeknights.tconstruct.library.tools.definition.PartRequirement)

Example 3 with ToolDefinitionData

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));
}
Also used : ToolDefinitionData(slimeknights.tconstruct.library.tools.definition.ToolDefinitionData) PartRequirement(slimeknights.tconstruct.library.tools.definition.PartRequirement)

Example 4 with ToolDefinitionData

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;
}
Also used : ToolDefinitionData(slimeknights.tconstruct.library.tools.definition.ToolDefinitionData) ItemStack(net.minecraft.world.item.ItemStack) MaterialRecipe(slimeknights.tconstruct.library.recipe.material.MaterialRecipe) IToolPart(slimeknights.tconstruct.library.tools.part.IToolPart)

Example 5 with ToolDefinitionData

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);
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) ToolDefinitionLoader(slimeknights.tconstruct.library.tools.definition.ToolDefinitionLoader) IToolPart(slimeknights.tconstruct.library.tools.part.IToolPart) ToolStats(slimeknights.tconstruct.library.tools.stat.ToolStats) HashMap(java.util.HashMap) PackType(net.minecraft.server.packs.PackType) Function(java.util.function.Function) Supplier(java.util.function.Supplier) SlotType(slimeknights.tconstruct.library.tools.SlotType) ImmutableList(com.google.common.collect.ImmutableList) ArmorSlotType(slimeknights.tconstruct.tools.item.ArmorSlotType) ItemLike(net.minecraft.world.level.ItemLike) ToolDefinitionData(slimeknights.tconstruct.library.tools.definition.ToolDefinitionData) ToolDefinition(slimeknights.tconstruct.library.tools.definition.ToolDefinition) Map(java.util.Map) GenericDataProvider(slimeknights.mantle.data.GenericDataProvider) FloatToolStat(slimeknights.tconstruct.library.tools.stat.FloatToolStat) DataGenerator(net.minecraft.data.DataGenerator) ToolDefinitionDataBuilder(slimeknights.tconstruct.library.tools.definition.ToolDefinitionDataBuilder) HashCache(net.minecraft.data.HashCache) ModifiableArmorMaterial(slimeknights.tconstruct.library.tools.definition.ModifiableArmorMaterial) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) List(java.util.List) Modifier(slimeknights.tconstruct.library.modifiers.Modifier) IToolStat(slimeknights.tconstruct.library.tools.stat.IToolStat) Entry(java.util.Map.Entry) ToolDefinitionData(slimeknights.tconstruct.library.tools.definition.ToolDefinitionData) ToolDefinitionDataBuilder(slimeknights.tconstruct.library.tools.definition.ToolDefinitionDataBuilder) ResourceLocation(net.minecraft.resources.ResourceLocation) ToolDefinition(slimeknights.tconstruct.library.tools.definition.ToolDefinition)

Aggregations

ToolDefinitionData (slimeknights.tconstruct.library.tools.definition.ToolDefinitionData)5 PartRequirement (slimeknights.tconstruct.library.tools.definition.PartRequirement)3 List (java.util.List)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 ItemStack (net.minecraft.world.item.ItemStack)2 MaterialStatsId (slimeknights.tconstruct.library.materials.stats.MaterialStatsId)2 ToolDefinition (slimeknights.tconstruct.library.tools.definition.ToolDefinition)2 IToolPart (slimeknights.tconstruct.library.tools.part.IToolPart)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1 Supplier (java.util.function.Supplier)1 DataGenerator (net.minecraft.data.DataGenerator)1 HashCache (net.minecraft.data.HashCache)1