use of net.silentchaos512.gear.api.traits.ITrait in project Silent-Gear by SilentChaos512.
the class TraitHelper method getTraits.
/**
* Gets a Map of Traits and levels from the parts, used to calculate trait levels and should not
* be used in most cases. Consider using {@link #getTraitLevel(ItemStack, ResourceLocation)} or
* {@link #hasTrait(ItemStack, ResourceLocation)} when appropriate.
*
* @param gear The item
* @param gearType The gear type
* @param parts The list of all parts used in constructing the gear.
* @return A Map of Traits to their levels
*/
public static Map<ITrait, Integer> getTraits(ItemStack gear, GearType gearType, PartDataList parts) {
if (parts.isEmpty() || (!gear.isEmpty() && GearHelper.isBroken(gear)))
return ImmutableMap.of();
Map<ITrait, Integer> result = new LinkedHashMap<>();
for (PartData part : parts) {
PartGearKey key = PartGearKey.of(gearType, part);
for (TraitInstance inst : part.getTraits(key, gear)) {
if (inst.conditionsMatch(key, gear, parts)) {
ITrait trait = inst.getTrait();
// Get the highest value in any part
result.merge(trait, inst.getLevel(), Integer::max);
}
}
}
ITrait[] keys = result.keySet().toArray(new ITrait[0]);
cancelTraits(result, keys);
MinecraftForge.EVENT_BUS.post(new GetTraitsEvent(gear, parts, result));
return result;
}
use of net.silentchaos512.gear.api.traits.ITrait in project Silent-Gear by SilentChaos512.
the class TraitHelper method cancelTraits.
private static void cancelTraits(Map<ITrait, Integer> mapToModify, ITrait[] keys) {
for (int i = 0; i < keys.length; ++i) {
ITrait t1 = keys[i];
if (mapToModify.containsKey(t1)) {
for (int j = i + 1; j < keys.length; ++j) {
ITrait t2 = keys[j];
if (mapToModify.containsKey(t2) && t1.willCancelWith(t2)) {
final int level = mapToModify.get(t1);
final int otherLevel = mapToModify.get(t2);
final int cancelLevel = t1.getCanceledLevel(level, t2, otherLevel);
if (cancelLevel > 0) {
mapToModify.put(t1, cancelLevel);
mapToModify.remove(t2);
} else if (cancelLevel < 0) {
mapToModify.put(t2, -cancelLevel);
mapToModify.remove(t1);
break;
} else {
mapToModify.remove(t1);
mapToModify.remove(t2);
break;
}
}
}
}
}
}
use of net.silentchaos512.gear.api.traits.ITrait 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);
}
}
use of net.silentchaos512.gear.api.traits.ITrait in project Silent-Gear by SilentChaos512.
the class TraitsCommand method runDescribe.
private static int runDescribe(CommandContext<CommandSourceStack> context, ResourceLocation traitId) {
ITrait trait = TraitManager.get(traitId);
if (trait == null) {
context.getSource().sendFailure(new TranslatableComponent("command.silentgear.traits.traitNotFound", traitId));
return 0;
}
context.getSource().sendSuccess(trait.getDisplayName(0), true);
context.getSource().sendSuccess(trait.getDescription(1), true);
context.getSource().sendSuccess(new TranslatableComponent("command.silentgear.traits.maxLevel", trait.getMaxLevel()), true);
context.getSource().sendSuccess(new TextComponent("Object: " + trait), true);
context.getSource().sendSuccess(new TextComponent("Serializer: " + trait.getSerializer()), true);
return 1;
}
use of net.silentchaos512.gear.api.traits.ITrait in project Silent-Gear by SilentChaos512.
the class TraitsCommand method getDataSources.
private static String getDataSources() {
Set<String> sourceSet = new LinkedHashSet<>();
for (ITrait trait : TraitManager.getValues()) {
sourceSet.add(trait.getId().getNamespace());
}
StringBuilder ret = new StringBuilder();
for (String id : sourceSet) {
ret.append("- ");
Optional<? extends ModContainer> container = ModList.get().getModContainerById(id);
if (container.isPresent()) {
IModInfo modInfo = container.get().getModInfo();
ret.append(modInfo.getDisplayName()).append(" (").append(id).append(") ").append(modInfo.getVersion()).append("\n");
} else {
ret.append(id);
}
}
return ret.toString();
}
Aggregations