Search in sources :

Example 21 with ICoreItem

use of net.silentchaos512.gear.api.item.ICoreItem in project Silent-Gear by SilentChaos512.

the class CuriosCompat method getEquippedCurios.

public static Collection<ItemStack> getEquippedCurios(LivingEntity entity) {
    LazyOptional<IItemHandlerModifiable> lazy = CuriosApi.getCuriosHelper().getEquippedCurios(entity);
    Collection<ItemStack> ret = new ArrayList<>();
    if (lazy.isPresent()) {
        IItemHandlerModifiable handler = lazy.orElseThrow(IllegalStateException::new);
        for (int i = 0; i < handler.getSlots(); ++i) {
            ItemStack stack = handler.getStackInSlot(i);
            if (stack.getItem() instanceof ICoreItem) {
                ret.add(stack);
            }
        }
    }
    return ret;
}
Also used : IItemHandlerModifiable(net.minecraftforge.items.IItemHandlerModifiable) ArrayList(java.util.ArrayList) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) ItemStack(net.minecraft.world.item.ItemStack)

Example 22 with ICoreItem

use of net.silentchaos512.gear.api.item.ICoreItem in project Silent-Gear by SilentChaos512.

the class ModKitRemovePartRecipe method getRemainingItems.

@Override
public NonNullList<ItemStack> getRemainingItems(CraftingContainer inv) {
    NonNullList<ItemStack> list = NonNullList.withSize(inv.getContainerSize(), ItemStack.EMPTY);
    ItemStack gear = StackList.from(inv).uniqueOfType(ICoreItem.class);
    ItemStack modKit = StackList.from(inv).uniqueOfType(ModKitItem.class);
    PartType type = ModKitItem.getSelectedType(modKit);
    PartData part = GearData.getPartOfType(gear, type);
    for (int i = 0; i < list.size(); ++i) {
        ItemStack stack = inv.getItem(i);
        if (stack.getItem() instanceof ICoreItem) {
            list.set(i, part != null ? part.getItem() : ItemStack.EMPTY);
        } else if (stack.hasContainerItem()) {
            list.set(i, stack.getContainerItem());
        }
    }
    return list;
}
Also used : PartType(net.silentchaos512.gear.api.part.PartType) PartData(net.silentchaos512.gear.gear.part.PartData) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) ItemStack(net.minecraft.world.item.ItemStack)

Example 23 with ICoreItem

use of net.silentchaos512.gear.api.item.ICoreItem in project Silent-Gear by SilentChaos512.

the class ModKitRemovePartRecipe method matches.

@Override
public boolean matches(CraftingContainer inv, Level worldIn) {
    ItemStack gear = ItemStack.EMPTY;
    boolean foundModKit = false;
    PartType type = PartType.NONE;
    for (int i = 0; i < inv.getContainerSize(); ++i) {
        ItemStack stack = inv.getItem(i);
        if (!stack.isEmpty()) {
            // noinspection ChainOfInstanceofChecks
            if (gear.isEmpty() && stack.getItem() instanceof ICoreItem) {
                gear = stack;
            } else if (!foundModKit && stack.getItem() instanceof ModKitItem) {
                type = ModKitItem.getSelectedType(stack);
                if (type == PartType.NONE) {
                    return false;
                }
                foundModKit = true;
            } else {
                return false;
            }
        }
    }
    return !gear.isEmpty() && foundModKit && GearData.hasPartOfType(gear, type);
}
Also used : PartType(net.silentchaos512.gear.api.part.PartType) ModKitItem(net.silentchaos512.gear.item.ModKitItem) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) ItemStack(net.minecraft.world.item.ItemStack)

Example 24 with ICoreItem

use of net.silentchaos512.gear.api.item.ICoreItem 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);
}
Also used : PartDataList(net.silentchaos512.gear.api.part.PartDataList) ITrait(net.silentchaos512.gear.api.traits.ITrait) ListTag(net.minecraft.nbt.ListTag) TraitActionContext(net.silentchaos512.gear.api.traits.TraitActionContext) GearType(net.silentchaos512.gear.api.item.GearType) ResourceLocation(net.minecraft.resources.ResourceLocation) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) CompoundTag(net.minecraft.nbt.CompoundTag) StatGearKey(net.silentchaos512.gear.api.util.StatGearKey)

Example 25 with ICoreItem

use of net.silentchaos512.gear.api.item.ICoreItem in project Silent-Gear by SilentChaos512.

the class SetPartsFunction method run.

@Override
protected ItemStack run(ItemStack stack, LootContext context) {
    if (!(stack.getItem() instanceof ICoreItem))
        return stack;
    ItemStack result = stack.copy();
    List<PartData> parts = LazyPartData.createPartList(this.parts);
    parts.forEach(p -> p.onAddToGear(result));
    GearData.writeConstructionParts(result, parts);
    GearData.recalculateStats(result, null);
    return result;
}
Also used : LazyPartData(net.silentchaos512.gear.gear.part.LazyPartData) PartData(net.silentchaos512.gear.gear.part.PartData) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) ItemStack(net.minecraft.world.item.ItemStack)

Aggregations

ICoreItem (net.silentchaos512.gear.api.item.ICoreItem)25 ItemStack (net.minecraft.world.item.ItemStack)16 PartData (net.silentchaos512.gear.gear.part.PartData)12 PartType (net.silentchaos512.gear.api.part.PartType)10 PartDataList (net.silentchaos512.gear.api.part.PartDataList)7 Component (net.minecraft.network.chat.Component)5 TextComponent (net.minecraft.network.chat.TextComponent)5 TranslatableComponent (net.minecraft.network.chat.TranslatableComponent)5 ArrayList (java.util.ArrayList)4 GearType (net.silentchaos512.gear.api.item.GearType)4 ResourceLocation (net.minecraft.resources.ResourceLocation)3 Item (net.minecraft.world.item.Item)3 IPartData (net.silentchaos512.gear.api.part.IPartData)3 StatGearKey (net.silentchaos512.gear.api.util.StatGearKey)3 MaterialInstance (net.silentchaos512.gear.gear.material.MaterialInstance)3 StackList (net.silentchaos512.lib.collection.StackList)3 CompoundTag (net.minecraft.nbt.CompoundTag)2 ServerPlayer (net.minecraft.server.level.ServerPlayer)2 Level (net.minecraft.world.level.Level)2 IItemHandlerModifiable (net.minecraftforge.items.IItemHandlerModifiable)2