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;
}
}
}
}
}
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);
}
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);
}
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();
}
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()));
}
Aggregations