Search in sources :

Example 1 with IMaterialStats

use of slimeknights.tconstruct.library.materials.stats.IMaterialStats 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 IMaterialStats

use of slimeknights.tconstruct.library.materials.stats.IMaterialStats in project TinkersConstruct by SlimeKnights.

the class PartBuilderScreen method setDisplayForMaterial.

/**
 * Updates the data in the material display
 * @param materialRecipe  New material recipe
 */
private void setDisplayForMaterial(MaterialRecipe materialRecipe) {
    MaterialVariant materialVariant = materialRecipe.getMaterial();
    this.infoPanelScreen.setCaption(MaterialTooltipCache.getColoredDisplayName(materialVariant.getVariant()));
    // determine how much material we have
    // get exact number of material, rather than rounded
    float value = materialRecipe.getMaterialValue(this.tile.getInventoryWrapper());
    MutableComponent formatted = new TextComponent(Util.COMMA_FORMAT.format(value));
    // if we have a part recipe, mark material red when not enough
    IPartBuilderRecipe partRecipe = this.tile.getPartRecipe();
    if (partRecipe != null && value < partRecipe.getCost()) {
        formatted = formatted.withStyle(ChatFormatting.DARK_RED);
    }
    this.infoPanelScreen.setMaterialValue(formatted);
    // update stats and traits
    List<Component> stats = Lists.newLinkedList();
    List<Component> tips = Lists.newArrayList();
    // add warning that the material is uncraftable
    if (!materialVariant.get().isCraftable()) {
        stats.add(UNCRAFTABLE_MATERIAL);
        stats.add(TextComponent.EMPTY);
        tips.add(UNCRAFTABLE_MATERIAL_TOOLTIP);
        tips.add(TextComponent.EMPTY);
    }
    MaterialId id = materialVariant.getId();
    for (IMaterialStats stat : MaterialRegistry.getInstance().getAllStats(id)) {
        List<Component> info = stat.getLocalizedInfo();
        if (!info.isEmpty()) {
            stats.add(stat.getLocalizedName().withStyle(ChatFormatting.UNDERLINE));
            tips.add(TextComponent.EMPTY);
            stats.addAll(info);
            tips.addAll(stat.getLocalizedDescriptions());
            List<ModifierEntry> traits = MaterialRegistry.getInstance().getTraits(id, stat.getIdentifier());
            if (!traits.isEmpty()) {
                for (ModifierEntry trait : traits) {
                    Modifier mod = trait.getModifier();
                    stats.add(mod.getDisplayName(trait.getLevel()));
                    tips.add(mod.getDescription(trait.getLevel()));
                }
            }
            stats.add(TextComponent.EMPTY);
            tips.add(TextComponent.EMPTY);
        }
    }
    // remove last line if empty
    if (!stats.isEmpty() && stats.get(stats.size() - 1).getString().isEmpty()) {
        stats.remove(stats.size() - 1);
        tips.remove(tips.size() - 1);
    }
    this.infoPanelScreen.setText(stats, tips);
}
Also used : TextComponent(net.minecraft.network.chat.TextComponent) IPartBuilderRecipe(slimeknights.tconstruct.library.recipe.partbuilder.IPartBuilderRecipe) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) MutableComponent(net.minecraft.network.chat.MutableComponent) MaterialVariant(slimeknights.tconstruct.library.materials.definition.MaterialVariant) IMaterialStats(slimeknights.tconstruct.library.materials.stats.IMaterialStats) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) MutableComponent(net.minecraft.network.chat.MutableComponent) Component(net.minecraft.network.chat.Component) TextComponent(net.minecraft.network.chat.TextComponent) Modifier(slimeknights.tconstruct.library.modifiers.Modifier)

Example 3 with IMaterialStats

use of slimeknights.tconstruct.library.materials.stats.IMaterialStats in project TinkersConstruct by SlimeKnights.

the class MaterialRegistryExtension method beforeEach.

@Override
public void beforeEach(ExtensionContext context) {
    Map<IMaterial, List<IMaterialStats>> materialSetup = MaterialFixture.ALL_MATERIAL_FIXTURES;
    Map<MaterialId, IMaterial> materials = materialSetup.keySet().stream().collect(Collectors.toMap(IMaterial::getIdentifier, Function.identity()));
    Map<MaterialId, Map<MaterialStatsId, IMaterialStats>> stats = materialSetup.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().getIdentifier(), entry -> entry.getValue().stream().collect(Collectors.toMap(IMaterialStats::getIdentifier, Function.identity()))));
    Map<MaterialStatsId, IMaterialStats> defaultStats = MaterialStatsFixture.TIC_DEFAULT_STATS.stream().collect(Collectors.toMap(IMaterialStats::getIdentifier, Function.identity()));
    // empty map as nothing using the extension uses traits
    materialRegistry = new MaterialRegistryFixture(materials, stats, defaultStats, Collections.emptyMap());
    MaterialRegistry.INSTANCE = new MaterialRegistry(materialRegistry);
}
Also used : MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) MaterialFixture(slimeknights.tconstruct.fixture.MaterialFixture) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) MaterialRegistryFixture(slimeknights.tconstruct.fixture.MaterialRegistryFixture) List(java.util.List) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) AfterAllCallback(org.junit.jupiter.api.extension.AfterAllCallback) Map(java.util.Map) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) IMaterialStats(slimeknights.tconstruct.library.materials.stats.IMaterialStats) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) MaterialStatsFixture(slimeknights.tconstruct.fixture.MaterialStatsFixture) Collections(java.util.Collections) IMaterial(slimeknights.tconstruct.library.materials.definition.IMaterial) Mockito.mock(org.mockito.Mockito.mock) IMaterial(slimeknights.tconstruct.library.materials.definition.IMaterial) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) MaterialRegistryFixture(slimeknights.tconstruct.fixture.MaterialRegistryFixture) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) IMaterialStats(slimeknights.tconstruct.library.materials.stats.IMaterialStats) List(java.util.List) Map(java.util.Map)

Aggregations

MaterialId (slimeknights.tconstruct.library.materials.definition.MaterialId)3 IMaterialStats (slimeknights.tconstruct.library.materials.stats.IMaterialStats)3 List (java.util.List)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 IMaterial (slimeknights.tconstruct.library.materials.definition.IMaterial)2 MaterialVariant (slimeknights.tconstruct.library.materials.definition.MaterialVariant)2 MaterialStatsId (slimeknights.tconstruct.library.materials.stats.MaterialStatsId)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Component (net.minecraft.network.chat.Component)1 MutableComponent (net.minecraft.network.chat.MutableComponent)1 TextComponent (net.minecraft.network.chat.TextComponent)1 Item (net.minecraft.world.item.Item)1 ItemStack (net.minecraft.world.item.ItemStack)1 AfterAllCallback (org.junit.jupiter.api.extension.AfterAllCallback)1 BeforeEachCallback (org.junit.jupiter.api.extension.BeforeEachCallback)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1