|
| 1 | +package hiiragi283.ragium.common.item |
| 2 | + |
| 3 | +import hiiragi283.ragium.api.extension.dropStackAt |
| 4 | +import hiiragi283.ragium.common.init.RagiumItems |
| 5 | +import net.minecraft.advancements.CriteriaTriggers |
| 6 | +import net.minecraft.core.component.DataComponents |
| 7 | +import net.minecraft.network.chat.Component |
| 8 | +import net.minecraft.server.level.ServerPlayer |
| 9 | +import net.minecraft.stats.Stats |
| 10 | +import net.minecraft.world.InteractionHand |
| 11 | +import net.minecraft.world.InteractionResultHolder |
| 12 | +import net.minecraft.world.effect.MobEffectInstance |
| 13 | +import net.minecraft.world.entity.LivingEntity |
| 14 | +import net.minecraft.world.entity.player.Player |
| 15 | +import net.minecraft.world.item.* |
| 16 | +import net.minecraft.world.item.alchemy.PotionContents |
| 17 | +import net.minecraft.world.item.alchemy.Potions |
| 18 | +import net.minecraft.world.level.Level |
| 19 | +import net.minecraft.world.level.gameevent.GameEvent |
| 20 | + |
| 21 | +class HTPotionCanItem(properties: Properties) : Item(properties.durability(3)) { |
| 22 | + override fun getDefaultInstance(): ItemStack = |
| 23 | + super.getDefaultInstance().apply { set(DataComponents.POTION_CONTENTS, PotionContents(Potions.WATER)) } |
| 24 | + |
| 25 | + override fun finishUsingItem(stack: ItemStack, level: Level, livingEntity: LivingEntity): ItemStack { |
| 26 | + val player: Player? = livingEntity as? Player |
| 27 | + if (player is ServerPlayer) { |
| 28 | + CriteriaTriggers.CONSUME_ITEM.trigger(player, stack) |
| 29 | + } |
| 30 | + if (!level.isClientSide) { |
| 31 | + val potion: PotionContents = stack.getOrDefault(DataComponents.POTION_CONTENTS, PotionContents.EMPTY) |
| 32 | + potion.forEachEffect { instance: MobEffectInstance -> |
| 33 | + if (instance.effect.value().isInstantenous) { |
| 34 | + instance.effect |
| 35 | + .value() |
| 36 | + .applyInstantenousEffect(player, player, livingEntity, instance.amplifier, 1.0) |
| 37 | + } else { |
| 38 | + livingEntity.addEffect(instance) |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + player?.let { playerIn: Player -> |
| 44 | + playerIn.awardStat(Stats.ITEM_USED.get(this)) |
| 45 | + val drankStack = stack.hurtAndConvertOnBreak( |
| 46 | + 1, |
| 47 | + RagiumItems.ALUMINUM_CAN, |
| 48 | + playerIn, |
| 49 | + LivingEntity.getSlotForHand(playerIn.usedItemHand), |
| 50 | + ) |
| 51 | + if (drankStack != stack) { |
| 52 | + dropStackAt(playerIn, drankStack) |
| 53 | + } |
| 54 | + } |
| 55 | + livingEntity.gameEvent(GameEvent.DRINK) |
| 56 | + return stack |
| 57 | + } |
| 58 | + |
| 59 | + override fun getUseDuration(stack: ItemStack, entity: LivingEntity): Int = 32 |
| 60 | + |
| 61 | + override fun getUseAnimation(stack: ItemStack): UseAnim = UseAnim.DRINK |
| 62 | + |
| 63 | + override fun use(level: Level, player: Player, usedHand: InteractionHand): InteractionResultHolder<ItemStack?> = |
| 64 | + ItemUtils.startUsingInstantly(level, player, usedHand) |
| 65 | + |
| 66 | + override fun appendHoverText( |
| 67 | + stack: ItemStack, |
| 68 | + context: TooltipContext, |
| 69 | + tooltipComponents: MutableList<Component>, |
| 70 | + tooltipFlag: TooltipFlag, |
| 71 | + ) { |
| 72 | + stack.get(DataComponents.POTION_CONTENTS)?.addPotionTooltip(tooltipComponents::add, 1f, context.tickRate()) |
| 73 | + } |
| 74 | +} |
0 commit comments