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