use of net.silentchaos512.gear.api.material in project Silent-Gear by SilentChaos512.
the class TraitHelper method activateTraits.
/**
* An easy way to activate an item's traits from anywhere. <strong>Use with care!</strong>
* Calling this frequently (like every render tick) causes FPS to tank.
* <p>
* This implementation pulls the item's traits straight from NBT to minimize object creation.
* The {@link TraitFunction} is applied to every trait.
*
* @param gear The {@link net.silentchaos512.gear.api.item.ICoreItem} affected
* @param inputValue The base value to have the traits act on.
* @param action The specific action to apply to each trait. This is {@code (trait, level,
* value) -> modifiedInputValue}, where 'value' is the currently calculated
* result.
* @return The {@code inputValue} modified by traits.
*/
public static float activateTraits(ItemStack gear, final float inputValue, TraitFunction action) {
if (!GearHelper.isGear(gear)) {
SilentGear.LOGGER.error("Called activateTraits on non-gear item, {}", gear);
SilentGear.LOGGER.catching(new IllegalArgumentException());
return inputValue;
}
ListTag tagList = GearData.getPropertiesData(gear).getList("Traits", Tag.TAG_COMPOUND);
float value = inputValue;
for (Tag nbt : tagList) {
if (nbt instanceof CompoundTag) {
CompoundTag tagCompound = (CompoundTag) nbt;
String regName = tagCompound.getString("Name");
ITrait trait = TraitManager.get(regName);
if (trait != null) {
int level = tagCompound.getByte("Level");
value = action.apply(trait, level, value);
}
}
}
return value;
}
use of net.silentchaos512.gear.api.material in project Silent-Gear by SilentChaos512.
the class TraitsCommand method runDumpMdClient.
public static void runDumpMdClient() {
Player player = SilentGear.PROXY.getClientPlayer();
if (player == null) {
SilentGear.LOGGER.error("TraitsCommand#runDumpMcClient: player is null?");
return;
}
String fileName = "traits_list.md";
String dirPath = "output/silentgear";
File output = new File(dirPath, fileName);
File directory = output.getParentFile();
if (!directory.exists() && !directory.mkdirs()) {
player.sendMessage(new TextComponent("Could not create directory: " + output.getParent()), Util.NIL_UUID);
return;
}
try (Writer writer = new OutputStreamWriter(new FileOutputStream(output), StandardCharsets.UTF_8)) {
writer.write("# Traits\n\n");
writer.write("Generated in-game by `sgear_traits dump_md` command on " + getCurrentDateTime() + "\n\n");
writer.write("This data may or may not be accurate depending on the mod pack you are playing and the mods or data packs installed.\n\n");
writer.write("## Data Sources\n\n");
writer.write("The following mods and data packs have added traits to the output. Running the dump command yourself may produce different results.\n\n");
writer.write(getDataSources() + "\n");
writer.write("## Trait Types\n\n");
writer.write("These are trait serializers. You can define custom instances of these types using data packs.\n");
writer.write("Code for traits and their serializers can be found in `net.silentchaos512.gear.gear.trait`.\n\n");
writer.write("Note that \"simple\" traits are often used where custom code is required.\n");
writer.write("They are not especially useful when just defined by a data pack.\n\n");
for (ITraitSerializer<?> serializer : TraitSerializers.getSerializers()) {
String typeName = serializer instanceof SimpleTrait.Serializer ? ((SimpleTrait.Serializer) serializer).getTypeName() : "";
writer.write("- `" + serializer.getName() + "`");
if (!typeName.isEmpty()) {
writer.write(" _(" + typeName + ")_");
}
writer.write("\n");
}
writer.write("\n## List of Traits");
List<ResourceLocation> ids = new ArrayList<>(TraitManager.getKeys());
ids.sort(Comparator.comparing(id -> Objects.requireNonNull(TraitManager.get(id)).getDisplayName(0).getString()));
for (ResourceLocation id : ids) {
ITrait trait = TraitManager.get(id);
assert trait != null;
writer.write("\n");
writer.write("### " + getLinkToBuiltinTraitJson(id, trait.getDisplayName(0).getString()) + "\n");
writer.write("- " + trait.getDescription(0).getString() + "\n");
String materialsWithTrait = getMaterialsWithTrait(trait);
writer.write("- Found On:\n - Materials: " + (materialsWithTrait.isEmpty() ? "Nothing" : materialsWithTrait) + "\n");
String partsWithTrait = getPartsWithTrait(trait);
if (!partsWithTrait.isEmpty()) {
writer.write(" - Parts: " + partsWithTrait + "\n");
}
if (!trait.getConditions().isEmpty()) {
// Just wrap all of them inside an AND condition, since that's how the logic works anyway
AndTraitCondition condition = new AndTraitCondition(trait.getConditions().toArray(new ITraitCondition[0]));
writer.write("- Conditions: " + condition.getDisplayText().getString() + "\n");
}
writer.write("- ID: `" + id + "`\n");
writer.write("- Type: `" + trait.getSerializer().getName() + "`\n");
writer.write("- Max Level: " + trait.getMaxLevel() + "\n");
Collection<String> cancelsWithSet = trait.getCancelsWithSet().stream().map(s -> "`" + s + "`").collect(Collectors.toList());
if (!cancelsWithSet.isEmpty()) {
writer.write("- Cancels With: " + String.join(", ", cancelsWithSet) + "\n");
}
Collection<String> wikiLines = trait.getExtraWikiLines();
if (!wikiLines.isEmpty()) {
writer.write("- Extra Info:\n");
for (String line : wikiLines) {
writer.write(line + "\n");
}
}
}
writer.write("\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
Component fileNameText = (new TextComponent(output.getAbsolutePath())).withStyle(ChatFormatting.UNDERLINE).withStyle(style -> style.withClickEvent(new ClickEvent(ClickEvent.Action.OPEN_FILE, output.getAbsolutePath())));
player.sendMessage(new TextComponent("Wrote to ").append(fileNameText), Util.NIL_UUID);
}
}
Aggregations