use of net.silentchaos512.gear.api.part.PartDataList in project Silent-Gear by SilentChaos512.
the class StatsCommand method runInfo.
private static int runInfo(CommandContext<CommandSourceStack> context, ServerPlayer player) {
ItemStack stack = player.getMainHandItem();
if (!GearHelper.isGear(stack)) {
context.getSource().sendFailure(TextUtil.translate("command", "invalidItemType", stack.getHoverName()));
return 0;
}
context.getSource().sendSuccess(TextUtil.translate("command", "stats.info.header", player.getName(), stack.getHoverName()).withStyle(ChatFormatting.BOLD), true);
ICoreItem item = (ICoreItem) stack.getItem();
PartDataList parts = GearData.getConstructionParts(stack);
StatModifierMap stats = GearData.getStatModifiers(stack, item, parts);
for (ItemStat stat : ItemStats.allStatsOrderedExcluding((item).getExcludedStats(stack))) {
StatGearKey key = StatGearKey.of(stat, item.getGearType());
Collection<StatInstance> mods = stats.get(key);
if (!mods.isEmpty()) {
Component name = TextUtil.withColor(stat.getDisplayName(), stat.getNameColor());
Component modsText = StatModifierMap.formatText(mods, stat, 5, true);
float statValue = stat.compute(0f, true, item.getGearType(), mods);
Component valueText = TextUtil.withColor(StatInstance.of(statValue, StatInstance.Operation.AVG, key).getFormattedText(stat, 5, false), ChatFormatting.YELLOW);
context.getSource().sendSuccess(TextUtil.translate("command", "stats.info.format", name, modsText, valueText), true);
for (PartData part : parts) {
Collection<StatInstance> partMods = part.getStatModifiers(key, stack);
if (!partMods.isEmpty()) {
Component partName = part.getDisplayName(stack);
Component partModsText = StatModifierMap.formatText(partMods, stat, 5, true);
context.getSource().sendSuccess(TextUtil.translate("command", "stats.info.formatPart", partName, partModsText), true);
}
}
}
}
return 1;
}
use of net.silentchaos512.gear.api.part.PartDataList in project Silent-Gear by SilentChaos512.
the class GearData method addOrReplacePart.
public static Optional<PartData> addOrReplacePart(ItemStack gear, PartData part) {
PartType partType = part.getType();
PartDataList parts = getConstructionParts(gear);
List<PartData> partsOfType = parts.getPartsOfType(partType);
PartData removedPart = null;
if (!partsOfType.isEmpty() && partsOfType.size() >= partType.getMaxPerItem(GearHelper.getType(gear))) {
removedPart = partsOfType.get(0);
parts.remove(removedPart);
removedPart.onRemoveFromGear(gear);
}
parts.add(part);
writeConstructionParts(gear, parts);
return Optional.ofNullable(removedPart);
}
use of net.silentchaos512.gear.api.part.PartDataList in project Silent-Gear by SilentChaos512.
the class GearData method removeFirstPartOfType.
public static boolean removeFirstPartOfType(ItemStack gear, PartType type) {
PartDataList parts = getConstructionParts(gear);
List<PartData> partsOfType = new ArrayList<>(parts.getPartsOfType(type));
if (!partsOfType.isEmpty()) {
PartData removed = partsOfType.remove(0);
parts.remove(removed);
writeConstructionParts(gear, parts);
removed.onRemoveFromGear(gear);
return true;
}
return false;
}
use of net.silentchaos512.gear.api.part.PartDataList in project Silent-Gear by SilentChaos512.
the class GearData method tryRecalculateStats.
@SuppressWarnings({ "OverlyLongMethod", "OverlyComplexMethod" })
private static void tryRecalculateStats(ItemStack gear, @Nullable Player player) {
if (checkNonGearItem(gear, "recalculateStats"))
return;
getUUID(gear);
TraitHelper.activateTraits(gear, 0f, (trait, level, value) -> {
trait.onRecalculatePre(new TraitActionContext(player, level, gear));
return 0f;
});
ICoreItem item = (ICoreItem) gear.getItem();
PartDataList parts = getConstructionParts(gear);
CompoundTag propertiesCompound = getData(gear, NBT_ROOT_PROPERTIES);
if (!propertiesCompound.contains(NBT_LOCK_STATS))
propertiesCompound.putBoolean(NBT_LOCK_STATS, false);
String playerName = player != null ? player.getScoreboardName() : "somebody";
String playersItemText = String.format("%s's %s", playerName, gear.getHoverName().getString());
final boolean statsUnlocked = !propertiesCompound.getBoolean(NBT_LOCK_STATS);
final boolean partsListValid = !parts.isEmpty() && !parts.getMains().isEmpty();
if (statsUnlocked && partsListValid) {
// We should recalculate the item's stats!
if (player != null) {
SilentGear.LOGGER.debug("Recalculating for {}", playersItemText);
}
clearCachedData(gear);
propertiesCompound.putString("ModVersion", SilentGear.getVersion());
Map<ITrait, Integer> traits = TraitHelper.getTraits(gear, item.getGearType(), parts);
// Get all stat modifiers from all parts and item class modifiers
StatModifierMap stats = getStatModifiers(gear, item, parts);
// For debugging
Map<ItemStat, Float> oldStatValues = getCurrentStatsForDebugging(gear);
// Cache traits in properties compound as well
ListTag traitList = new ListTag();
traits.forEach((trait, level) -> traitList.add(trait.write(level)));
propertiesCompound.put("Traits", traitList);
propertiesCompound.remove(NBT_SYNERGY);
// Calculate and write stats
int maxDamage = gear.getMaxDamage() > 0 ? gear.getMaxDamage() : 1;
final float damageRatio = Mth.clamp((float) gear.getDamageValue() / maxDamage, 0f, 1f);
CompoundTag statsCompound = new CompoundTag();
for (ItemStat stat : ItemStats.allStatsOrderedExcluding(item.getExcludedStats(gear))) {
StatGearKey key = StatGearKey.of(stat, item.getGearType());
Collection<StatInstance> modifiers = stats.get(key);
GearType statGearType = stats.getMostSpecificKey(key).getGearType();
final float initialValue = stat.compute(stat.getBaseValue(), true, item.getGearType(), statGearType, modifiers);
// Allow traits to modify stat
final float withTraits = TraitHelper.activateTraits(gear, initialValue, (trait, level, val) -> {
TraitActionContext context = new TraitActionContext(player, level, gear);
return trait.onGetStat(context, stat, val, damageRatio);
});
final float value = Config.Common.getStatWithMultiplier(stat, withTraits);
if (!Mth.equal(value, 0f) || stats.containsKey(key)) {
ResourceLocation statId = Objects.requireNonNull(stat.getRegistryName());
// Remove old keys
propertiesCompound.remove(statId.getPath());
statsCompound.putFloat(statId.toString(), stat.clampValue(value));
}
}
// Put missing relevant stats in the map to avoid recalculate stats packet spam
for (ItemStat stat : item.getRelevantStats(gear)) {
String statKey = stat.getStatId().toString();
if (!statsCompound.contains(statKey)) {
statsCompound.putFloat(statKey, stat.getDefaultValue());
}
}
propertiesCompound.put(NBT_STATS, statsCompound);
if (player != null) {
printStatsForDebugging(gear, stats, oldStatValues);
}
// Remove enchantments if mod is configured to. Must be done before traits add enchantments!
if (gear.getOrCreateTag().contains("Enchantments") && Config.Common.forceRemoveEnchantments.get()) {
SilentGear.LOGGER.debug("Forcibly removing all enchantments from {} as per config settings", playersItemText);
gear.removeTagKey("Enchantments");
}
// Remove trait-added enchantments then let traits re-add them
EnchantmentTrait.removeTraitEnchantments(gear);
TraitHelper.activateTraits(gear, 0f, (trait, level, value) -> {
trait.onRecalculatePost(new TraitActionContext(player, level, gear));
return 0f;
});
} else {
SilentGear.LOGGER.debug("Not recalculating stats for {}", playersItemText);
}
// Update rendering info even if we didn't update stats
updateRenderingInfo(gear, parts);
}
use of net.silentchaos512.gear.api.part.PartDataList in project Silent-Gear by SilentChaos512.
the class GearData method getConstructionParts.
public static PartDataList getConstructionParts(ItemStack stack) {
if (!GearHelper.isGear(stack))
return PartDataList.empty();
CompoundTag tags = getData(stack, NBT_ROOT_CONSTRUCTION);
ListTag tagList = tags.getList(NBT_CONSTRUCTION_PARTS, Tag.TAG_COMPOUND);
PartDataList list = PartDataList.of();
for (Tag nbt : tagList) {
if (nbt instanceof CompoundTag) {
CompoundTag partCompound = (CompoundTag) nbt;
PartData part = PartData.read(partCompound);
if (part != null) {
list.add(part);
}
}
}
return list;
}
Aggregations