Search in sources :

Example 1 with MaterialStatsId

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

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

the class PartRequirement method read.

/**
 * Reads a tool definition stat object from a packet buffer
 */
public static PartRequirement read(FriendlyByteBuf buffer) {
    if (buffer.readBoolean()) {
        IToolPart part = RecipeHelper.readItem(buffer, IToolPart.class);
        int weight = buffer.readVarInt();
        return ofPart(part, weight);
    } else {
        MaterialStatsId statsId = new MaterialStatsId(buffer.readResourceLocation());
        int weight = buffer.readVarInt();
        return ofStat(statsId, weight);
    }
}
Also used : MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) IToolPart(slimeknights.tconstruct.library.tools.part.IToolPart)

Example 3 with MaterialStatsId

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

the class ClientGeneratePartTexturesCommand method loadPartSprites.

/**
 * Loads all part sprites file
 */
private static List<PartSpriteInfo> loadPartSprites(ResourceManager manager) {
    ImmutableList.Builder<PartSpriteInfo> builder = ImmutableList.builder();
    // each namespace loads separately
    for (String namespace : manager.getNamespaces()) {
        ResourceLocation location = new ResourceLocation(namespace, GENERATOR_PART_TEXTURES);
        if (manager.hasResource(location)) {
            // if the namespace has the file, we will start building
            try {
                // start from the top most pack and work down, lets us break the loop as soon as we find a "replace"
                List<Resource> resources = manager.getResources(location);
                for (int r = resources.size() - 1; r >= 0; r--) {
                    Resource resource = resources.get(r);
                    try (BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8))) {
                        JsonObject object = GsonHelper.parse(reader);
                        List<PartSpriteInfo> parts = JsonHelper.parseList(object, "parts", (element, name) -> {
                            JsonObject part = GsonHelper.convertToJsonObject(element, name);
                            ResourceLocation path = JsonHelper.getResourceLocation(part, "path");
                            MaterialStatsId statId = new MaterialStatsId(JsonHelper.getResourceLocation(part, "statType"));
                            return new PartSpriteInfo(path, statId);
                        });
                        builder.addAll(parts);
                        // if we find replace, don't process lower files from this namespace
                        if (GsonHelper.getAsBoolean(object, "replace", false)) {
                            break;
                        }
                    } catch (IOException ex) {
                        log.error("Failed to load modifier models from {} for pack {}", location, resource.getSourceName(), ex);
                    }
                }
            } catch (IOException ex) {
                log.error("Failed to load modifier models from {}", location, ex);
            }
        }
    }
    return builder.build();
}
Also used : InputStreamReader(java.io.InputStreamReader) ImmutableList(com.google.common.collect.ImmutableList) Resource(net.minecraft.server.packs.resources.Resource) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) PartSpriteInfo(slimeknights.tconstruct.library.client.data.material.AbstractPartSpriteProvider.PartSpriteInfo) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) ResourceLocation(net.minecraft.resources.ResourceLocation) BufferedReader(java.io.BufferedReader)

Example 4 with MaterialStatsId

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

the class MaterialTraits method read.

/**
 * Reads this object from the packet buffer
 * @param buffer  Buffer
 * @return Read MaterialTraits
 */
public static MaterialTraits read(FriendlyByteBuf buffer) {
    List<ModifierEntry> defaultTraits = readTraitList(buffer);
    int statTypeCount = buffer.readVarInt();
    Map<MaterialStatsId, List<ModifierEntry>> statsTraits = new HashMap<>(statTypeCount);
    for (int i = 0; i < statTypeCount; i++) {
        MaterialStatsId statsId = new MaterialStatsId(buffer.readResourceLocation());
        List<ModifierEntry> traitsList = readTraitList(buffer);
        statsTraits.put(statsId, traitsList);
    }
    return new MaterialTraits(defaultTraits, statsTraits);
}
Also used : MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) HashMap(java.util.HashMap) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 5 with MaterialStatsId

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

the class UpdateMaterialTraitsPacketTest method testGenericEncodeDecode.

@Test
void testGenericEncodeDecode() {
    List<ModifierEntry> defaultTraits1 = Arrays.asList(new ModifierEntry(ModifierFixture.TEST_MODIFIER_1, 1), new ModifierEntry(ModifierFixture.TEST_MODIFIER_2, 2));
    MaterialTraits materialTraits1 = new MaterialTraits(defaultTraits1, Collections.emptyMap());
    List<ModifierEntry> defaultTraits2 = Collections.singletonList(new ModifierEntry(ModifierFixture.TEST_MODIFIER_1, 3));
    Map<MaterialStatsId, List<ModifierEntry>> statsTraits2 = new HashMap<>();
    statsTraits2.put(MaterialStatsFixture.STATS_TYPE, Arrays.asList(new ModifierEntry(ModifierFixture.TEST_MODIFIER_1, 4), new ModifierEntry(ModifierFixture.TEST_MODIFIER_2, 5)));
    statsTraits2.put(MaterialStatsFixture.STATS_TYPE_2, Collections.singletonList(new ModifierEntry(ModifierFixture.TEST_MODIFIER_2, 6)));
    MaterialTraits materialTraits2 = new MaterialTraits(defaultTraits2, statsTraits2);
    Map<MaterialId, MaterialTraits> map = new HashMap<>();
    map.put(MATERIAL_ID_1, materialTraits1);
    map.put(MATERIAL_ID_2, materialTraits2);
    // send a packet over the buffer
    FriendlyByteBuf buffer = new FriendlyByteBuf(Unpooled.buffer());
    UpdateMaterialTraitsPacket packetToEncode = new UpdateMaterialTraitsPacket(map);
    packetToEncode.encode(buffer);
    UpdateMaterialTraitsPacket decoded = new UpdateMaterialTraitsPacket(buffer);
    // parse results
    Map<MaterialId, MaterialTraits> parsed = decoded.getMaterialToTraits();
    assertThat(parsed).hasSize(2);
    // material traits 1
    MaterialTraits parsedTraits1 = parsed.get(MATERIAL_ID_1);
    assertThat(parsedTraits1).isNotNull();
    // default
    assertThat(parsedTraits1.getDefaultTraits()).hasSize(2);
    ModifierEntry trait1 = parsedTraits1.getDefaultTraits().get(0);
    assertThat(trait1.getModifier()).isEqualTo(ModifierFixture.TEST_MODIFIER_1);
    assertThat(trait1.getLevel()).isEqualTo(1);
    ModifierEntry trait2 = parsedTraits1.getDefaultTraits().get(1);
    assertThat(trait2.getModifier()).isEqualTo(ModifierFixture.TEST_MODIFIER_2);
    assertThat(trait2.getLevel()).isEqualTo(2);
    // per stat
    assertThat(parsedTraits1.getTraitsPerStats()).isEmpty();
    // material traits 2
    MaterialTraits parsedTraits2 = parsed.get(MATERIAL_ID_2);
    assertThat(parsedTraits2).isNotNull();
    // default
    assertThat(parsedTraits2.getDefaultTraits()).hasSize(1);
    trait1 = parsedTraits2.getDefaultTraits().get(0);
    assertThat(trait1.getModifier()).isEqualTo(ModifierFixture.TEST_MODIFIER_1);
    assertThat(trait1.getLevel()).isEqualTo(3);
    // per stat type
    assertThat(parsedTraits2.getTraitsPerStats()).hasSize(2);
    // stat type 1
    List<ModifierEntry> traitsForStats1 = parsedTraits2.getTraits(MaterialStatsFixture.STATS_TYPE);
    assertThat(traitsForStats1).isNotNull();
    assertThat(traitsForStats1).hasSize(2);
    trait1 = traitsForStats1.get(0);
    assertThat(trait1.getModifier()).isEqualTo(ModifierFixture.TEST_MODIFIER_1);
    assertThat(trait1.getLevel()).isEqualTo(4);
    trait2 = traitsForStats1.get(1);
    assertThat(trait2.getModifier()).isEqualTo(ModifierFixture.TEST_MODIFIER_2);
    assertThat(trait2.getLevel()).isEqualTo(5);
    // stat type 2
    List<ModifierEntry> traitsForStats2 = parsedTraits2.getTraits(MaterialStatsFixture.STATS_TYPE_2);
    assertThat(traitsForStats2).isNotNull();
    assertThat(traitsForStats2).hasSize(1);
    trait1 = traitsForStats2.get(0);
    assertThat(trait1.getModifier()).isEqualTo(ModifierFixture.TEST_MODIFIER_2);
    assertThat(trait1.getLevel()).isEqualTo(6);
}
Also used : FriendlyByteBuf(net.minecraft.network.FriendlyByteBuf) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) HashMap(java.util.HashMap) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) List(java.util.List) BaseMcTest(slimeknights.tconstruct.test.BaseMcTest) Test(org.junit.jupiter.api.Test)

Aggregations

MaterialStatsId (slimeknights.tconstruct.library.materials.stats.MaterialStatsId)8 List (java.util.List)5 MaterialId (slimeknights.tconstruct.library.materials.definition.MaterialId)3 ImmutableList (com.google.common.collect.ImmutableList)2 HashMap (java.util.HashMap)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 IMaterialStats (slimeknights.tconstruct.library.materials.stats.IMaterialStats)2 ModifierEntry (slimeknights.tconstruct.library.modifiers.ModifierEntry)2 JsonObject (com.google.gson.JsonObject)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 FriendlyByteBuf (net.minecraft.network.FriendlyByteBuf)1 ResourceLocation (net.minecraft.resources.ResourceLocation)1