| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import net.minecraft.stats.Stats;
|
| 4 | import net.minecraft.world.InteractionHand;
|
| 5 | import net.minecraft.world.InteractionResult;
|
| 6 | import net.minecraft.world.entity.Entity;
|
| 7 | import net.minecraft.world.entity.EntityType;
|
| 8 | import net.minecraft.world.entity.EquipmentSlot;
|
| 9 | import net.minecraft.world.entity.ItemSteerable;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.level.Level;
|
| 12 |
|
| 13 | public class FoodOnAStickItem<T extends Entity & ItemSteerable> extends Item {
|
| 14 | private final EntityType<T> canInteractWith;
|
| 15 | private final int consumeItemDamage;
|
| 16 |
|
| 17 | public FoodOnAStickItem(final EntityType<T> canInteractWith, final int consumeItemDamage, final Item.Properties properties) {
|
| 18 | super(properties);
|
| 19 | this.canInteractWith = canInteractWith;
|
| 20 | this.consumeItemDamage = consumeItemDamage;
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 25 | ItemStack itemStack = player.getItemInHand(hand);
|
| 26 | if (level.isClientSide()) {
|
| 27 | return InteractionResult.PASS;
|
| 28 | } else {
|
| 29 | Entity vehicle = player.getControlledVehicle();
|
| 30 | if (player.isPassenger() && vehicle instanceof ItemSteerable steerable && vehicle.is(this.canInteractWith) && steerable.boost()) {
|
| 31 | EquipmentSlot slot = hand.asEquipmentSlot();
|
| 32 | ItemStack result = itemStack.hurtAndConvertOnBreak(this.consumeItemDamage, Items.FISHING_ROD, player, slot);
|
| 33 | return InteractionResult.SUCCESS_SERVER.heldItemTransformedTo(result);
|
| 34 | } else {
|
| 35 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 36 | return InteractionResult.PASS;
|
| 37 | }
|
| 38 | }
|
| 39 | }
|
| 40 | }
|
| 41 |
|