From 192b26e8ab5615bc27d97af1f97ccdf08d7be619 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 14 Aug 2024 20:08:58 +0700 Subject: [PATCH] Update energy sword --- .../mc/otm/item/weapon/EnergySwordItem.kt | 71 ++++++++----------- 1 file changed, 29 insertions(+), 42 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt index cc39cae4b..6e7a3cf54 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt @@ -1,27 +1,24 @@ package ru.dbotthepony.mc.otm.item.weapon -import com.google.common.collect.ImmutableMultimap -import com.google.common.collect.Multimap import net.minecraft.core.BlockPos -import net.minecraft.core.Holder -import net.minecraft.nbt.CompoundTag import net.minecraft.tags.BlockTags -import net.minecraft.world.entity.EquipmentSlot +import net.minecraft.world.entity.EquipmentSlotGroup import net.minecraft.world.entity.LivingEntity -import net.minecraft.world.entity.ai.attributes.Attribute import net.minecraft.world.entity.ai.attributes.AttributeModifier import net.minecraft.world.entity.ai.attributes.Attributes import net.minecraft.world.entity.player.Player import net.minecraft.world.item.ItemStack import net.minecraft.world.item.Rarity import net.minecraft.world.item.component.ItemAttributeModifiers -import net.minecraft.world.item.enchantment.Enchantment -import net.minecraft.world.item.enchantment.EnchantmentCategory -import net.minecraft.world.item.enchantment.Enchantments import net.minecraft.world.level.Level import net.minecraft.world.level.block.Blocks import net.minecraft.world.level.block.state.BlockState +import net.neoforged.neoforge.capabilities.Capabilities +import net.neoforged.neoforge.capabilities.RegisterCapabilitiesEvent +import net.neoforged.neoforge.common.ItemAbilities +import net.neoforged.neoforge.common.ItemAbility import net.neoforged.neoforge.common.ModConfigSpec +import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.capability.MatteryCapability import ru.dbotthepony.mc.otm.capability.energy.EnergyConsumerItem import ru.dbotthepony.mc.otm.capability.energy.extractEnergyExact @@ -29,6 +26,7 @@ import ru.dbotthepony.mc.otm.capability.energy.getBarColor import ru.dbotthepony.mc.otm.capability.energy.getBarWidth import ru.dbotthepony.mc.otm.capability.matteryEnergy import ru.dbotthepony.mc.otm.capability.matteryPlayer +import ru.dbotthepony.mc.otm.core.ResourceLocation import ru.dbotthepony.mc.otm.core.damageType import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.math.DecimalConfigValue @@ -37,26 +35,25 @@ import ru.dbotthepony.mc.otm.core.math.nextVariance import ru.dbotthepony.mc.otm.core.util.WriteOnce import ru.dbotthepony.mc.otm.item.MatteryItem import ru.dbotthepony.mc.otm.item.addSimpleDescription +import ru.dbotthepony.mc.otm.registry.CapabilitiesRegisterListener import ru.dbotthepony.mc.otm.registry.MDamageTypes import ru.dbotthepony.mc.otm.registry.MatteryDamageSource -class EnergySwordItem : MatteryItem(Properties().stacksTo(1).rarity(Rarity.RARE)) { - val chargedAttributes: ItemAttributeModifiers - val dischargedAttributes: ItemAttributeModifiers +class EnergySwordItem : MatteryItem(Properties().stacksTo(1).rarity(Rarity.RARE)), CapabilitiesRegisterListener { + private val chargedAttributes: ItemAttributeModifiers + private val dischargedAttributes: ItemAttributeModifiers init { - var builder = ImmutableMultimap.builder, AttributeModifier>() + var builder = ItemAttributeModifiers.builder() - builder.put(Attributes.ATTACK_DAMAGE, AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "Weapon modifier", 11.0, AttributeModifier.Operation.ADDITION)) - builder.put(Attributes.ATTACK_SPEED, AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", -2.4, AttributeModifier.Operation.ADDITION)) + builder.add(Attributes.ATTACK_DAMAGE, AttributeModifier(MODIFIER_ID, 11.0, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) + builder.add(Attributes.ATTACK_SPEED, AttributeModifier(MODIFIER_ID, -2.4, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) chargedAttributes = builder.build() + builder = ItemAttributeModifiers.builder() - builder = ImmutableMultimap.builder() - - builder.put(Attributes.ATTACK_DAMAGE, AttributeModifier(BASE_ATTACK_DAMAGE_UUID, "Weapon modifier", 3.5, AttributeModifier.Operation.ADDITION)) - builder.put(Attributes.ATTACK_SPEED, AttributeModifier(BASE_ATTACK_SPEED_UUID, "Weapon modifier", -2.4, AttributeModifier.Operation.ADDITION)) - + builder.add(Attributes.ATTACK_DAMAGE, AttributeModifier(MODIFIER_ID, 3.5, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) + builder.add(Attributes.ATTACK_SPEED, AttributeModifier(MODIFIER_ID, -2.4, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND) dischargedAttributes = builder.build() } @@ -64,10 +61,6 @@ class EnergySwordItem : MatteryItem(Properties().stacksTo(1).rarity(Rarity.RARE) return p_41456_.count == 1 } - override fun canApplyAtEnchantingTable(stack: ItemStack, enchantment: Enchantment): Boolean { - return enchantment.category == EnchantmentCategory.WEAPON && enchantment != Enchantments.SWEEPING_EDGE - } - override fun getEnchantmentValue(stack: ItemStack): Int { return 12 } @@ -160,34 +153,26 @@ class EnergySwordItem : MatteryItem(Properties().stacksTo(1).rarity(Rarity.RARE) return true } - override fun initCapabilities(stack: ItemStack, nbt: CompoundTag?): ICapabilityProvider { - return EnergyConsumerItem(stack, MAX_ENERGY) + private fun cap(stack: ItemStack) = EnergyConsumerItem(stack, MAX_ENERGY) + + override fun registerCapabilities(event: RegisterCapabilitiesEvent) { + event.registerItem(MatteryCapability.ITEM_ENERGY, { o, _ -> cap(o) }, this) + event.registerItem(Capabilities.EnergyStorage.ITEM, { o, _ -> cap(o) }, this) } override fun getDefaultAttributeModifiers(stack: ItemStack): ItemAttributeModifiers { - return super.getDefaultAttributeModifiers(stack) - } + val energy = cap(stack) - override fun getAttributeModifiers( - slot: EquipmentSlot, - itemStack: ItemStack - ): Multimap { - if (slot != EquipmentSlot.MAINHAND) { - return ImmutableMultimap.of() - } - - val energy = itemStack.matteryEnergy - - if (energy != null && energy.batteryLevel >= ENERGY_PER_SWING) { + if (energy.batteryLevel >= ENERGY_PER_SWING) { return chargedAttributes } return dischargedAttributes } - override fun canPerformAction(stack: ItemStack, toolAction: ToolAction): Boolean { - if (stack.matteryEnergy?.extractEnergyExact(ENERGY_PER_SWING, true) == true) { - return ToolActions.DEFAULT_SWORD_ACTIONS.contains(toolAction) + override fun canPerformAction(stack: ItemStack, toolAction: ItemAbility): Boolean { + if (cap(stack).extractEnergyExact(ENERGY_PER_SWING, true)) { + return ItemAbilities.DEFAULT_SWORD_ACTIONS.contains(toolAction) } return false @@ -214,6 +199,8 @@ class EnergySwordItem : MatteryItem(Properties().stacksTo(1).rarity(Rarity.RARE) private var _PLANT_POWER_COST: DecimalConfigValue by WriteOnce() private var _PLANT_POWER_COST_VARIANCE: DecimalConfigValue by WriteOnce() + private val MODIFIER_ID = ResourceLocation(OverdriveThatMatters.MOD_ID, "energy_sword") + fun registerConfig(builder: ModConfigSpec.Builder) { builder.comment("Energy sword values").push("EnergySword")