Search in sources :

Example 1 with IModifiable

use of slimeknights.tconstruct.library.tools.item.IModifiable in project TinkersConstruct by SlimeKnights.

the class ToolBuildingRecipeSerializer method fromNetworkSafe.

@Nullable
@Override
protected ToolBuildingRecipe fromNetworkSafe(ResourceLocation recipeId, FriendlyByteBuf buffer) {
    String group = buffer.readUtf(Short.MAX_VALUE);
    IModifiable result = RecipeHelper.readItem(buffer, IModifiable.class);
    return new ToolBuildingRecipe(recipeId, group, result);
}
Also used : IModifiable(slimeknights.tconstruct.library.tools.item.IModifiable) Nullable(javax.annotation.Nullable)

Example 2 with IModifiable

use of slimeknights.tconstruct.library.tools.item.IModifiable in project TinkersConstruct by SlimeKnights.

the class ToolHarvestLogicTest method calcSpeed_effective_withMiningModifier.

@Test
void calcSpeed_effective_withMiningModifier() {
    float modifier = 2f;
    ToolDefinition definition = ToolDefinition.builder(new ResourceLocation("test", "mining_tool")).meleeHarvest().skipRegister().build();
    definition.setData(ToolDefinitionDataBuilder.builder().harvestLogic(new BlockHarvestLogic(Blocks.COBBLESTONE)).part(MaterialItemFixture.MATERIAL_ITEM_HEAD).part(MaterialItemFixture.MATERIAL_ITEM_HANDLE).part(MaterialItemFixture.MATERIAL_ITEM_EXTRA).multiplier(ToolStats.MINING_SPEED, modifier).build());
    IModifiable toolWithMiningModifier = new ModifiableItem(new Item.Properties(), definition);
    ItemStack tool = buildTestTool(toolWithMiningModifier);
    // boosted by correct block
    float speed = ToolHarvestLogic.getDestroySpeed(tool, Blocks.COBBLESTONE.defaultBlockState());
    assertThat(speed).isEqualTo(MaterialStatsFixture.MATERIAL_STATS_HEAD.getMiningSpeed() * modifier);
    // default speed
    speed = ToolHarvestLogic.getDestroySpeed(tool, Blocks.STONE.defaultBlockState());
    assertThat(speed).isEqualTo(1.0f);
}
Also used : Item(net.minecraft.world.item.Item) ModifiableItem(slimeknights.tconstruct.library.tools.item.ModifiableItem) BlockHarvestLogic(slimeknights.tconstruct.test.BlockHarvestLogic) ResourceLocation(net.minecraft.resources.ResourceLocation) ToolDefinition(slimeknights.tconstruct.library.tools.definition.ToolDefinition) ModifiableItem(slimeknights.tconstruct.library.tools.item.ModifiableItem) IModifiable(slimeknights.tconstruct.library.tools.item.IModifiable) ItemStack(net.minecraft.world.item.ItemStack) ToolItemTest(slimeknights.tconstruct.library.tools.item.ToolItemTest) Test(org.junit.jupiter.api.Test)

Example 3 with IModifiable

use of slimeknights.tconstruct.library.tools.item.IModifiable in project TinkersConstruct by SlimeKnights.

the class ToolStack method from.

/* Creating */
/**
 * Creates a tool stack from an item stack
 * @param stack    Base stack
 * @param copyNbt  If true, NBT is copied from the stack
 * @return  Tool stack
 */
private static ToolStack from(ItemStack stack, boolean copyNbt) {
    Item item = stack.getItem();
    ToolDefinition definition = item instanceof IModifiable ? ((IModifiable) item).getToolDefinition() : ToolDefinition.EMPTY;
    CompoundTag nbt = stack.getTag();
    if (nbt == null) {
        nbt = new CompoundTag();
        if (!copyNbt) {
            stack.setTag(nbt);
        }
    } else if (copyNbt) {
        nbt = nbt.copy();
    }
    return from(item, definition, nbt);
}
Also used : Item(net.minecraft.world.item.Item) ToolDefinition(slimeknights.tconstruct.library.tools.definition.ToolDefinition) IModifiable(slimeknights.tconstruct.library.tools.item.IModifiable) RestrictedCompoundTag(slimeknights.tconstruct.library.utils.RestrictedCompoundTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 4 with IModifiable

use of slimeknights.tconstruct.library.tools.item.IModifiable in project TinkersConstruct by SlimeKnights.

the class ModifierUsageCommand method runForType.

private static int runForType(CommandContext<CommandSourceStack> context, ModifierUsages filter, @Nullable OptionalSlotType slotFilter) {
    // recipe modifiers are used in a displayable modifier recipe
    HashMultimap<SlotType, Modifier> recipeModifiers = context.getSource().getLevel().getRecipeManager().byType(RecipeTypes.TINKER_STATION).values().stream().filter(r -> r instanceof IModifierRecipe).map(r -> (IModifierRecipe) r).collect(Collector.of(HashMultimap::create, (map, r) -> map.put(r.getSlotType(), r.getModifier()), (m1, m2) -> {
        m1.putAll(m2);
        return m1;
    }));
    // material traits are used in material traits (kinda obvious)
    IMaterialRegistry matReg = MaterialRegistry.getInstance();
    Set<Modifier> materialTraits = matReg.getAllMaterials().stream().flatMap(mat -> {
        MaterialId matId = mat.getIdentifier();
        return Stream.concat(matReg.getDefaultTraits(matId).stream(), matReg.getAllStats(matId).stream().filter(stat -> matReg.hasUniqueTraits(matId, stat.getIdentifier())).flatMap(stat -> matReg.getTraits(matId, stat.getIdentifier()).stream()));
    }).map(ModifierEntry::getModifier).collect(Collectors.toSet());
    // finally, tool traits we limit to anything in the modifiable tag
    Set<Modifier> toolTraits = TinkerTags.Items.MODIFIABLE.getValues().stream().filter(item -> item instanceof IModifiable).flatMap(item -> ((IModifiable) item).getToolDefinition().getData().getTraits().stream()).map(ModifierEntry::getModifier).collect(Collectors.toSet());
    // next, get our list of modifiers
    Stream<Modifier> modifierStream;
    switch(filter) {
        case RECIPE:
            // filter to just one type of modifier if requested
            if (slotFilter != null) {
                modifierStream = recipeModifiers.get(slotFilter.slotType()).stream();
            } else {
                modifierStream = recipeModifiers.values().stream();
            }
            break;
        case MATERIAL_TRAIT:
            modifierStream = materialTraits.stream();
            break;
        case TOOL_TRAIT:
            modifierStream = toolTraits.stream();
            break;
        default:
            modifierStream = TinkerRegistries.MODIFIERS.getValues().stream();
            break;
    }
    // if requested, filter out all
    if (filter == ModifierUsages.UNUSED) {
        modifierStream = modifierStream.filter(modifier -> !recipeModifiers.containsValue(modifier) && !materialTraits.contains(modifier) && !toolTraits.contains(modifier));
    }
    // start building the table for output
    TablePrinter<ModifierUsageRow> table = new TablePrinter<>();
    table.header("ID", r -> r.modifierId().toString());
    if (filter != ModifierUsages.UNUSED) {
        if (filter != ModifierUsages.RECIPE || slotFilter == null) {
            table.header("Recipe", ModifierUsageRow::recipe);
        }
        if (filter != ModifierUsages.MATERIAL_TRAIT) {
            table.header("material Trait", r -> r.materialTrait() ? "Material trait" : "");
        }
        if (filter != ModifierUsages.TOOL_TRAIT) {
            table.header("tool Trait", r -> r.toolTrait() ? "Tool trait" : "");
        }
    }
    StringBuilder logOutput = new StringBuilder();
    logOutput.append(filter.logPrefix);
    if (slotFilter != null) {
        if (slotFilter.slotType() == null) {
            logOutput.append(" (slotless)");
        } else {
            logOutput.append(" (").append(slotFilter.slotType().getName()).append(")");
        }
    }
    logOutput.append(System.lineSeparator());
    // for all the modifiers (sorted), add table rows
    Collection<Modifier> finalList = modifierStream.sorted(Comparator.comparing(Modifier::getId)).toList();
    finalList.forEach(modifier -> {
        // determine which recipes use this by slot type
        List<String> recipeUsages = SlotType.getAllSlotTypes().stream().filter(type -> recipeModifiers.containsEntry(type, modifier)).map(SlotType::getName).collect(Collectors.toList());
        String recipes;
        if (recipeUsages.isEmpty()) {
            recipes = recipeModifiers.containsEntry(null, modifier) ? "slotless" : "";
        } else {
            recipes = String.join(", ", recipeUsages);
        }
        table.add(new ModifierUsageRow(modifier.getId(), recipes, toolTraits.contains(modifier), materialTraits.contains(modifier)));
    });
    // finally, output the table
    table.build(logOutput);
    TConstruct.LOG.info(logOutput.toString());
    context.getSource().sendSuccess(SUCCESS, true);
    return finalList.size();
}
Also used : CommandSourceStack(net.minecraft.commands.CommandSourceStack) RequiredArgsConstructor(lombok.RequiredArgsConstructor) SlotType(slimeknights.tconstruct.library.tools.SlotType) TablePrinter(net.minecraftforge.common.util.TablePrinter) HashMultimap(com.google.common.collect.HashMultimap) MaterialRegistry(slimeknights.tconstruct.library.materials.MaterialRegistry) SlotTypeArgument(slimeknights.tconstruct.shared.command.argument.SlotTypeArgument) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) IModifierRecipe(slimeknights.tconstruct.library.recipe.modifiers.adding.IModifierRecipe) ModifierId(slimeknights.tconstruct.library.modifiers.ModifierId) IModifiable(slimeknights.tconstruct.library.tools.item.IModifiable) Collector(java.util.stream.Collector) TranslatableComponent(net.minecraft.network.chat.TranslatableComponent) Nullable(javax.annotation.Nullable) RecipeTypes(slimeknights.tconstruct.library.recipe.RecipeTypes) Component(net.minecraft.network.chat.Component) CommandContext(com.mojang.brigadier.context.CommandContext) Commands(net.minecraft.commands.Commands) MantleCommand(slimeknights.mantle.command.MantleCommand) TinkerTags(slimeknights.tconstruct.common.TinkerTags) Collection(java.util.Collection) TinkerRegistries(slimeknights.tconstruct.library.TinkerRegistries) Set(java.util.Set) OptionalSlotType(slimeknights.tconstruct.shared.command.argument.SlotTypeArgument.OptionalSlotType) Collectors(java.util.stream.Collectors) LiteralArgumentBuilder(com.mojang.brigadier.builder.LiteralArgumentBuilder) TConstruct(slimeknights.tconstruct.TConstruct) List(java.util.List) Stream(java.util.stream.Stream) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) IMaterialRegistry(slimeknights.tconstruct.library.materials.IMaterialRegistry) Modifier(slimeknights.tconstruct.library.modifiers.Modifier) Comparator(java.util.Comparator) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) IModifierRecipe(slimeknights.tconstruct.library.recipe.modifiers.adding.IModifierRecipe) IModifiable(slimeknights.tconstruct.library.tools.item.IModifiable) SlotType(slimeknights.tconstruct.library.tools.SlotType) OptionalSlotType(slimeknights.tconstruct.shared.command.argument.SlotTypeArgument.OptionalSlotType) TablePrinter(net.minecraftforge.common.util.TablePrinter) IMaterialRegistry(slimeknights.tconstruct.library.materials.IMaterialRegistry) Modifier(slimeknights.tconstruct.library.modifiers.Modifier)

Example 5 with IModifiable

use of slimeknights.tconstruct.library.tools.item.IModifiable 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)

Aggregations

IModifiable (slimeknights.tconstruct.library.tools.item.IModifiable)6 Nullable (javax.annotation.Nullable)3 Collection (java.util.Collection)2 List (java.util.List)2 Set (java.util.Set)2 CompoundTag (net.minecraft.nbt.CompoundTag)2 Component (net.minecraft.network.chat.Component)2 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)2 ResourceLocation (net.minecraft.resources.ResourceLocation)2 Item (net.minecraft.world.item.Item)2 TConstruct (slimeknights.tconstruct.TConstruct)2 TinkerTags (slimeknights.tconstruct.common.TinkerTags)2 IMaterialRegistry (slimeknights.tconstruct.library.materials.IMaterialRegistry)2 MaterialRegistry (slimeknights.tconstruct.library.materials.MaterialRegistry)2 ModifierEntry (slimeknights.tconstruct.library.modifiers.ModifierEntry)2 ToolDefinition (slimeknights.tconstruct.library.tools.definition.ToolDefinition)2 HashMultimap (com.google.common.collect.HashMultimap)1 Multimap (com.google.common.collect.Multimap)1 Sets (com.google.common.collect.Sets)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1