Search in sources :

Example 1 with PartRequirement

use of slimeknights.tconstruct.library.tools.definition.PartRequirement 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 PartRequirement

use of slimeknights.tconstruct.library.tools.definition.PartRequirement 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 PartRequirement

use of slimeknights.tconstruct.library.tools.definition.PartRequirement 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 PartRequirement

use of slimeknights.tconstruct.library.tools.definition.PartRequirement 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);
        }
    }
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) Item(net.minecraft.world.item.Item) ToolStats(slimeknights.tconstruct.library.tools.stat.ToolStats) Registry(net.minecraft.core.Registry) MaterialRegistry(slimeknights.tconstruct.library.materials.MaterialRegistry) ChatFormatting(net.minecraft.ChatFormatting) ToolDefinition(slimeknights.tconstruct.library.tools.definition.ToolDefinition) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) IModifiable(slimeknights.tconstruct.library.tools.item.IModifiable) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) IMaterial(slimeknights.tconstruct.library.materials.definition.IMaterial) TinkerTags(slimeknights.tconstruct.common.TinkerTags) SafeClientAccess(slimeknights.tconstruct.library.utils.SafeClientAccess) Collection(java.util.Collection) Set(java.util.Set) Sets(com.google.common.collect.Sets) MaterialTooltipCache(slimeknights.tconstruct.library.client.materials.MaterialTooltipCache) Player(net.minecraft.world.entity.player.Player) TextComponent(net.minecraft.network.chat.TextComponent) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) Entry(java.util.Map.Entry) ItemStack(net.minecraft.world.item.ItemStack) Level(net.minecraft.world.level.Level) Tag(net.minecraft.nbt.Tag) TinkerModifiers(slimeknights.tconstruct.tools.TinkerModifiers) Multimap(com.google.common.collect.Multimap) ITinkerStationDisplay(slimeknights.tconstruct.library.tools.item.ITinkerStationDisplay) BiPredicate(java.util.function.BiPredicate) AttributeModifier(net.minecraft.world.entity.ai.attributes.AttributeModifier) IModifiableDisplay(slimeknights.tconstruct.library.tools.item.IModifiableDisplay) Nullable(javax.annotation.Nullable) PartRequirement(slimeknights.tconstruct.library.tools.definition.PartRequirement) Component(net.minecraft.network.chat.Component) Iterator(java.util.Iterator) IToolStackView(slimeknights.tconstruct.library.tools.nbt.IToolStackView) MaterialVariantId(slimeknights.tconstruct.library.materials.definition.MaterialVariantId) Util(slimeknights.tconstruct.library.utils.Util) Attribute(net.minecraft.world.entity.ai.attributes.Attribute) TConstruct(slimeknights.tconstruct.TConstruct) TooltipKey(slimeknights.tconstruct.library.utils.TooltipKey) Operation(net.minecraft.world.entity.ai.attributes.AttributeModifier.Operation) IMaterialRegistry(slimeknights.tconstruct.library.materials.IMaterialRegistry) EquipmentSlot(net.minecraft.world.entity.EquipmentSlot) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) MaterialNBT(slimeknights.tconstruct.library.tools.nbt.MaterialNBT) TooltipFlag(net.minecraft.world.item.TooltipFlag) ListTag(net.minecraft.nbt.ListTag) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) PartRequirement(slimeknights.tconstruct.library.tools.definition.PartRequirement) MaterialVariantId(slimeknights.tconstruct.library.materials.definition.MaterialVariantId) MaterialNBT(slimeknights.tconstruct.library.tools.nbt.MaterialNBT)

Example 5 with PartRequirement

use of slimeknights.tconstruct.library.tools.definition.PartRequirement 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);
}
Also used : PartRequirement(slimeknights.tconstruct.library.tools.definition.PartRequirement) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) MaterialVariantId(slimeknights.tconstruct.library.materials.definition.MaterialVariantId) MaterialNBT(slimeknights.tconstruct.library.tools.nbt.MaterialNBT) IMaterialRegistry(slimeknights.tconstruct.library.materials.IMaterialRegistry) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) TextComponent(net.minecraft.network.chat.TextComponent) Component(net.minecraft.network.chat.Component)

Aggregations

PartRequirement (slimeknights.tconstruct.library.tools.definition.PartRequirement)7 MaterialNBT (slimeknights.tconstruct.library.tools.nbt.MaterialNBT)4 IMaterialRegistry (slimeknights.tconstruct.library.materials.IMaterialRegistry)3 MaterialVariantId (slimeknights.tconstruct.library.materials.definition.MaterialVariantId)3 List (java.util.List)2 Component (net.minecraft.network.chat.Component)2 TextComponent (net.minecraft.network.chat.TextComponent)2 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)2 Item (net.minecraft.world.item.Item)2 ItemStack (net.minecraft.world.item.ItemStack)2 TConstruct (slimeknights.tconstruct.TConstruct)2 MaterialRegistry (slimeknights.tconstruct.library.materials.MaterialRegistry)2 IMaterial (slimeknights.tconstruct.library.materials.definition.IMaterial)2 ModifierEntry (slimeknights.tconstruct.library.modifiers.ModifierEntry)2 ToolDefinitionData (slimeknights.tconstruct.library.tools.definition.ToolDefinitionData)2 ImmutableList (com.google.common.collect.ImmutableList)1 Multimap (com.google.common.collect.Multimap)1 Sets (com.google.common.collect.Sets)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1