Search in sources :

Example 1 with RecalculateStatsPacket

use of net.silentchaos512.gear.network.RecalculateStatsPacket in project Silent-Gear by SilentChaos512.

the class GearData method getStat.

public static float getStat(ItemStack stack, IItemStat stat, boolean calculateIfMissing) {
    CompoundTag tags = getData(stack, NBT_ROOT_PROPERTIES).getCompound(NBT_STATS);
    String key = stat.getStatId().toString();
    if (tags.contains(key)) {
        return tags.getFloat(key);
    }
    if (calculateIfMissing) {
        // Stat is missing, notify server to recalculate
        Level level = SilentGear.PROXY.getClientLevel();
        if (level != null && GearHelper.isValidGear(stack) && ((ICoreItem) stack.getItem()).getRelevantStats(stack).contains(stat)) {
            SilentGear.LOGGER.debug("Sending recalculate stats packet for item with missing {} stat: {}", stat.getStatId(), stack.getHoverName().getString());
            Network.channel.sendToServer(new RecalculateStatsPacket(level, stack, stat));
            // Prevent the packet from being spammed...
            putStatInNbtIfMissing(stack, stat);
        }
    }
    return stat.getDefaultValue();
}
Also used : RecalculateStatsPacket(net.silentchaos512.gear.network.RecalculateStatsPacket) Level(net.minecraft.world.level.Level) ICoreItem(net.silentchaos512.gear.api.item.ICoreItem) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with RecalculateStatsPacket

use of net.silentchaos512.gear.network.RecalculateStatsPacket in project Silent-Gear by SilentChaos512.

the class RecalculateStatsPacket method decode.

public static RecalculateStatsPacket decode(FriendlyByteBuf buffer) {
    int slot = buffer.readVarInt();
    ItemStat stat = ItemStats.byName(buffer.readResourceLocation());
    return new RecalculateStatsPacket(slot, Objects.requireNonNull(stat));
}
Also used : ItemStat(net.silentchaos512.gear.api.stats.ItemStat) IItemStat(net.silentchaos512.gear.api.stats.IItemStat)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)1 Level (net.minecraft.world.level.Level)1 ICoreItem (net.silentchaos512.gear.api.item.ICoreItem)1 IItemStat (net.silentchaos512.gear.api.stats.IItemStat)1 ItemStat (net.silentchaos512.gear.api.stats.ItemStat)1 RecalculateStatsPacket (net.silentchaos512.gear.network.RecalculateStatsPacket)1