Search in sources :

Example 6 with MaterialId

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

the class MaterialTraitsManager method finishLoad.

@Override
protected void finishLoad(Map<ResourceLocation, MaterialTraits.Builder> map, ResourceManager manager) {
    ImmutableMap.Builder<MaterialId, MaterialTraits> builder = ImmutableMap.builder();
    for (Entry<ResourceLocation, MaterialTraits.Builder> entry : map.entrySet()) {
        MaterialTraits traits = entry.getValue().build();
        builder.put(new MaterialId(entry.getKey()), traits);
        log.debug("Loaded traits for material '{}': \n\tDefault - {}{}", entry.getKey(), Arrays.toString(traits.getDefaultTraits().toArray()), Util.toIndentedStringList(traits.getTraitsPerStats().entrySet().stream().map(entry2 -> String.format("%s - %s", entry2.getKey(), Arrays.toString(entry2.getValue().toArray()))).collect(Collectors.toList())));
    }
    materialTraits = builder.build();
    onLoaded.run();
}
Also used : ResourceLocation(net.minecraft.resources.ResourceLocation) MaterialStatsId(slimeknights.tconstruct.library.materials.stats.MaterialStatsId) Arrays(java.util.Arrays) ImmutableMap(com.google.common.collect.ImmutableMap) JsonSyntaxException(com.google.gson.JsonSyntaxException) ResourceManager(net.minecraft.server.packs.resources.ResourceManager) Collectors(java.util.stream.Collectors) GsonBuilder(com.google.gson.GsonBuilder) Util(slimeknights.tconstruct.library.utils.Util) JsonElement(com.google.gson.JsonElement) List(java.util.List) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) Gson(com.google.gson.Gson) Map(java.util.Map) Log4j2(lombok.extern.log4j.Log4j2) Entry(java.util.Map.Entry) ModifierEntry(slimeknights.tconstruct.library.modifiers.ModifierEntry) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) MaterialTraitsJson(slimeknights.tconstruct.library.materials.json.MaterialTraitsJson) MergingJsonDataLoader(slimeknights.mantle.data.MergingJsonDataLoader) MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) ResourceLocation(net.minecraft.resources.ResourceLocation) GsonBuilder(com.google.gson.GsonBuilder) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 7 with MaterialId

use of slimeknights.tconstruct.library.materials.definition.MaterialId 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)

Example 8 with MaterialId

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

the class MaterialStatsManagerTest method loadMissingFile_ignored.

@Test
void loadMissingFile_ignored() {
    MaterialId material = new MaterialId(TConstruct.getResource("nonexistant"));
    fileLoader.loadAndParseFiles(null);
    // ensure that we get this far and that querying the missing material causes no errors
    Optional<ComplexTestStats> optionalStats = materialStatsManager.getStats(material, STATS_ID_DONT_CARE);
    assertThat(optionalStats).isEmpty();
}
Also used : MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) Test(org.junit.jupiter.api.Test) BaseMcTest(slimeknights.tconstruct.test.BaseMcTest)

Example 9 with MaterialId

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

the class MaterialStatsManagerTest method testLoadFileWithoutStats_ok.

@Test
void testLoadFileWithoutStats_ok() {
    MaterialId material = new MaterialId(TConstruct.getResource("missing_stats"));
    fileLoader.loadAndParseFiles(null, material);
    // ensure that we get this far and that querying the missing material causes no errors
    Optional<ComplexTestStats> optionalStats = materialStatsManager.getStats(material, STATS_ID_DONT_CARE);
    assertThat(optionalStats).isEmpty();
}
Also used : MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) Test(org.junit.jupiter.api.Test) BaseMcTest(slimeknights.tconstruct.test.BaseMcTest)

Example 10 with MaterialId

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

the class MaterialStatsManagerTest method testLoadMultipleFiles_addSameStatsFromDifferentSources_useFirst.

// Tests the behaviour when multiple mods try to add the same material
// the top data pack should override lower ones, meaning the duplicate stats are kept
@Test
void testLoadMultipleFiles_addSameStatsFromDifferentSources_useFirst() {
    materialStatsManager.registerMaterialStat(DEFAULT, ComplexTestStats.class, ComplexTestStats::new);
    MaterialId material = new MaterialId(TConstruct.getResource("teststat"));
    fileLoader.loadAndParseFiles("duplicate", material);
    Optional<ComplexTestStats> stats = materialStatsManager.getStats(material, STATS_ID_SIMPLE);
    assertThat(stats).isNotEmpty();
    // top pack value replaces the bottom one
    assertThat(stats.get().getNum()).isEqualTo(321);
    // top pack unspecified value defaults to the bottom one
    assertThat(stats.get().getFloating()).isCloseTo(12.34f, Offset.strictOffset(0.01f));
}
Also used : MaterialId(slimeknights.tconstruct.library.materials.definition.MaterialId) Test(org.junit.jupiter.api.Test) BaseMcTest(slimeknights.tconstruct.test.BaseMcTest)

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