Search in sources :

Example 1 with MaterialId

use of slimeknights.tconstruct.library.materials.definition.MaterialId in project TinkersConstruct by SlimeKnights.

the class ToolBuildHandler method addDefaultSubItems.

/* Item groups */
/**
 * Adds all sub items to a tool
 * @param item             item being created
 * @param itemList         List to fill with items
 * @param fixedMaterials   Materials that should be forced
 */
public static void addDefaultSubItems(IModifiable item, List<ItemStack> itemList, MaterialVariantId... fixedMaterials) {
    ToolDefinition definition = item.getToolDefinition();
    boolean isMultipart = definition.isMultipart();
    if (!definition.isDataLoaded() || (isMultipart && !MaterialRegistry.isFullyLoaded())) {
        // not loaded? cannot properly build it
        itemList.add(new ItemStack(item));
    } else if (!isMultipart) {
        // no parts? just add this item
        itemList.add(buildItemFromMaterials(item, MaterialNBT.EMPTY));
    } else {
        // if a specific material is set, show just that
        String showOnlyId = Config.COMMON.showOnlyToolMaterial.get();
        boolean added = false;
        if (!showOnlyId.isEmpty()) {
            MaterialId materialId = MaterialId.tryParse(showOnlyId);
            if (materialId != null) {
                IMaterial material = MaterialRegistry.getMaterial(materialId);
                if (material != IMaterial.UNKNOWN) {
                    if (addSubItem(item, itemList, material, fixedMaterials)) {
                        added = true;
                    }
                }
            }
        }
        // if the material was not applicable or we do not have a filter set, search the rest
        if (!added) {
            for (IMaterial material : MaterialRegistry.getInstance().getVisibleMaterials()) {
                // if we added it and we want a single material, we are done
                if (addSubItem(item, itemList, material, fixedMaterials) && !showOnlyId.isEmpty()) {
                    break;
                }
            }
        }
    }
}
Also used : IMaterial(slimeknights.tconstruct.library.materials.definition.IMaterial) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) ToolDefinition(slimeknights.tconstruct.library.tools.definition.ToolDefinition) ItemStack(net.minecraft.world.item.ItemStack)

Example 2 with MaterialId

use of slimeknights.tconstruct.library.materials.definition.MaterialId in project TinkersConstruct by SlimeKnights.

the class SpecializedRepairRecipeSerializer method fromJson.

@Override
public T fromJson(ResourceLocation id, JsonObject json) {
    Ingredient tool = Ingredient.fromJson(JsonHelper.getElement(json, "tool"));
    MaterialId repairMaterial = MaterialId.fromJson(json, "repair_material");
    return factory.create(id, tool, repairMaterial);
}
Also used : MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) Ingredient(net.minecraft.world.item.crafting.Ingredient)

Example 3 with MaterialId

use of slimeknights.tconstruct.library.materials.definition.MaterialId in project TinkersConstruct by SlimeKnights.

the class ToolPredicate method deserialize.

/**
 * Deserializes the tool predicate from JSON
 */
public static ToolPredicate deserialize(JsonObject json) {
    // item
    Item item = null;
    if (json.has("item")) {
        item = RecipeHelper.deserializeItem(GsonHelper.getAsString(json, "item"), "item", Item.class);
    }
    // tag
    Tag<Item> tag = null;
    if (json.has("tag")) {
        ResourceLocation tagName = new ResourceLocation(GsonHelper.getAsString(json, "tag"));
        tag = SerializationTags.getInstance().getTagOrThrow(Registry.ITEM_REGISTRY, tagName, name -> new JsonSyntaxException("Unknown item tag '" + name + '\''));
    }
    // materials
    List<MaterialId> materials = Collections.emptyList();
    if (json.has("materials")) {
        materials = JsonHelper.parseList(json, "materials", (element, key) -> new MaterialId(GsonHelper.convertToString(element, key)));
    }
    // upgrades
    boolean hasUpgrades = GsonHelper.getAsBoolean(json, "has_upgrades", false);
    ModifierMatch upgrades = ModifierMatch.ALWAYS;
    if (json.has("upgrades")) {
        upgrades = ModifierMatch.deserialize(GsonHelper.getAsJsonObject(json, "upgrades"));
    }
    // modifiers
    ModifierMatch modifiers = ModifierMatch.ALWAYS;
    if (json.has("modifiers")) {
        modifiers = ModifierMatch.deserialize(GsonHelper.getAsJsonObject(json, "modifiers"));
    }
    // stats
    List<StatPredicate> stats = Collections.emptyList();
    if (json.has("stats")) {
        stats = JsonHelper.parseList(json, "stats", StatPredicate::deserialize);
    }
    return new ToolPredicate(item, tag, materials, hasUpgrades, upgrades, modifiers, stats);
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) JsonObject(com.google.gson.JsonObject) Setter(lombok.Setter) Accessors(lombok.experimental.Accessors) Item(net.minecraft.world.item.Item) RequiredArgsConstructor(lombok.RequiredArgsConstructor) RecipeHelper(slimeknights.mantle.recipe.helper.RecipeHelper) Function(java.util.function.Function) ArrayList(java.util.ArrayList) JsonElement(com.google.gson.JsonElement) Registry(net.minecraft.core.Registry) AccessLevel(lombok.AccessLevel) GsonHelper(net.minecraft.util.GsonHelper) JsonPrimitive(com.google.gson.JsonPrimitive) StatsNBT(slimeknights.tconstruct.library.tools.nbt.StatsNBT) ItemPredicate(net.minecraft.advancements.critereon.ItemPredicate) StatPredicate(slimeknights.tconstruct.library.tools.stat.StatPredicate) SerializationTags(net.minecraft.tags.SerializationTags) Nullable(javax.annotation.Nullable) JsonSyntaxException(com.google.gson.JsonSyntaxException) ModifierMatch(slimeknights.tconstruct.library.recipe.modifiers.ModifierMatch) MaterialVariant(slimeknights.tconstruct.library.materials.definition.MaterialVariant) Tag(net.minecraft.tags.Tag) TConstruct(slimeknights.tconstruct.TConstruct) Objects(java.util.Objects) JsonArray(com.google.gson.JsonArray) JsonHelper(slimeknights.mantle.util.JsonHelper) Items(slimeknights.tconstruct.common.TinkerTags.Items) List(java.util.List) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) ItemStack(net.minecraft.world.item.ItemStack) Collections(java.util.Collections) ToolStack(slimeknights.tconstruct.library.tools.nbt.ToolStack) Item(net.minecraft.world.item.Item) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) StatPredicate(slimeknights.tconstruct.library.tools.stat.StatPredicate) JsonSyntaxException(com.google.gson.JsonSyntaxException) ModifierMatch(slimeknights.tconstruct.library.recipe.modifiers.ModifierMatch) ResourceLocation(net.minecraft.resources.ResourceLocation)

Example 4 with MaterialId

use of slimeknights.tconstruct.library.materials.definition.MaterialId in project TinkersConstruct by SlimeKnights.

the class ClientGeneratePartTexturesCommand method loadMaterialRenderInfoGenerators.

/**
 * Loads all material render info that contain palette generator info into the given consumer
 * @param manager          Resource manager instance
 * @param validMaterialId  Predicate to check if a material ID should be considered
 * @return List of material sprites loaded
 */
private static List<MaterialSpriteInfo> loadMaterialRenderInfoGenerators(ResourceManager manager, Predicate<ResourceLocation> validMaterialId) {
    ImmutableList.Builder<MaterialSpriteInfo> builder = ImmutableList.builder();
    int trim = MaterialRenderInfoLoader.FOLDER.length() + 1;
    for (ResourceLocation location : manager.listResources(MaterialRenderInfoLoader.FOLDER, loc -> loc.endsWith(".json"))) {
        // clean up ID by trimming off the extension
        String path = location.getPath();
        MaterialId id = new MaterialId(location.getNamespace(), path.substring(trim, path.length() - 5));
        // ensure its a material we care about
        if (validMaterialId.test(id)) {
            try (Resource iresource = manager.getResource(location);
                InputStream inputstream = iresource.getInputStream();
                Reader reader = new BufferedReader(new InputStreamReader(inputstream, StandardCharsets.UTF_8))) {
                // if the JSON has generator info, add it to the consumer
                MaterialRenderInfoJson json = MaterialRenderInfoLoader.GSON.fromJson(reader, MaterialRenderInfoJson.class);
                MaterialGeneratorJson generator = json.getGenerator();
                if (generator != null) {
                    builder.add(new MaterialSpriteInfo(Objects.requireNonNullElse(json.getTexture(), id), Objects.requireNonNullElse(json.getFallbacks(), new String[0]), generator));
                }
            } catch (JsonSyntaxException e) {
                log.error("Failed to read tool part texture generator info for {}", id, e);
            } catch (Exception e) {
            // NO-OP, that is a resource pack bug, not our job
            }
        }
    }
    return builder.build();
}
Also used : MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) InputStreamReader(java.io.InputStreamReader) ImmutableList(com.google.common.collect.ImmutableList) InputStream(java.io.InputStream) Resource(net.minecraft.server.packs.resources.Resource) ResourceManagerSpriteReader(slimeknights.tconstruct.library.client.data.util.ResourceManagerSpriteReader) AbstractSpriteReader(slimeknights.tconstruct.library.client.data.util.AbstractSpriteReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) MaterialRenderInfoJson(slimeknights.tconstruct.library.client.materials.MaterialRenderInfoJson) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) JsonSyntaxException(com.google.gson.JsonSyntaxException) MaterialGeneratorJson(slimeknights.tconstruct.library.client.materials.MaterialRenderInfoJson.MaterialGeneratorJson) MaterialSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractMaterialSpriteProvider.MaterialSpriteInfo) ResourceLocation(net.minecraft.resources.ResourceLocation) BufferedReader(java.io.BufferedReader)

Example 5 with MaterialId

use of slimeknights.tconstruct.library.materials.definition.MaterialId in project TinkersConstruct by SlimeKnights.

the class AbstractMaterialTraitDataProvider method run.

@Override
public void run(HashCache cache) {
    addMaterialTraits();
    // ensure we have traits for all materials
    // if you want no traits for your material, use an empty list
    Set<MaterialId> materialsGenerated = materials.getAllMaterials();
    for (MaterialId material : materialsGenerated) {
        if (!allMaterialTraits.containsKey(material)) {
            throw new IllegalStateException(String.format("Missing material traits for '%s'", material));
        }
    }
    // generate
    allMaterialTraits.forEach((materialId, traits) -> saveThing(cache, materialId, traits.serialize()));
}
Also used : MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId)

Aggregations

MaterialId (slimeknights.tconstruct.library.materials.definition.MaterialId)41 Test (org.junit.jupiter.api.Test)11 BaseMcTest (slimeknights.tconstruct.test.BaseMcTest)11 Nullable (javax.annotation.Nullable)9 ModifierEntry (slimeknights.tconstruct.library.modifiers.ModifierEntry)9 ResourceLocation (net.minecraft.resources.ResourceLocation)8 ItemStack (net.minecraft.world.item.ItemStack)7 Collections (java.util.Collections)6 List (java.util.List)6 IMaterial (slimeknights.tconstruct.library.materials.definition.IMaterial)6 ToolStack (slimeknights.tconstruct.library.tools.nbt.ToolStack)6 Collectors (java.util.stream.Collectors)5 MaterialVariantId (slimeknights.tconstruct.library.materials.definition.MaterialVariantId)5 JsonObject (com.google.gson.JsonObject)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)4 TConstruct (slimeknights.tconstruct.TConstruct)4 MaterialStatsId (slimeknights.tconstruct.library.materials.stats.MaterialStatsId)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 JsonElement (com.google.gson.JsonElement)3 Function (java.util.function.Function)3