| 1 | package net.minecraft.world.item;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.Holder;
|
| 6 | import net.minecraft.core.Position;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.sounds.SoundEvent;
|
| 9 | import net.minecraft.sounds.SoundEvents;
|
| 10 | import net.minecraft.sounds.SoundSource;
|
| 11 | import net.minecraft.stats.Stats;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.world.InteractionHand;
|
| 14 | import net.minecraft.world.InteractionResult;
|
| 15 | import net.minecraft.world.entity.EquipmentSlotGroup;
|
| 16 | import net.minecraft.world.entity.LivingEntity;
|
| 17 | import net.minecraft.world.entity.MoverType;
|
| 18 | import net.minecraft.world.entity.ai.attributes.AttributeModifier;
|
| 19 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 20 | import net.minecraft.world.entity.player.Player;
|
| 21 | import net.minecraft.world.entity.projectile.Projectile;
|
| 22 | import net.minecraft.world.entity.projectile.arrow.AbstractArrow;
|
| 23 | import net.minecraft.world.entity.projectile.arrow.ThrownTrident;
|
| 24 | import net.minecraft.world.item.component.ItemAttributeModifiers;
|
| 25 | import net.minecraft.world.item.component.Tool;
|
| 26 | import net.minecraft.world.item.enchantment.EnchantmentEffectComponents;
|
| 27 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 28 | import net.minecraft.world.level.Level;
|
| 29 | import net.minecraft.world.phys.Vec3;
|
| 30 |
|
| 31 | public class TridentItem extends Item implements ProjectileItem {
|
| 32 | public static final int THROW_THRESHOLD_TIME = 10;
|
| 33 | public static final float BASE_DAMAGE = 8.0F;
|
| 34 | public static final float PROJECTILE_SHOOT_POWER = 2.5F;
|
| 35 |
|
| 36 | public TridentItem(final Item.Properties properties) {
|
| 37 | super(properties);
|
| 38 | }
|
| 39 |
|
| 40 | public static ItemAttributeModifiers createAttributes() {
|
| 41 | return ItemAttributeModifiers.builder()
|
| 42 | .add(Attributes.ATTACK_DAMAGE, new AttributeModifier(BASE_ATTACK_DAMAGE_ID, 8.0, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND)
|
| 43 | .add(Attributes.ATTACK_SPEED, new AttributeModifier(BASE_ATTACK_SPEED_ID, -2.9F, AttributeModifier.Operation.ADD_VALUE), EquipmentSlotGroup.MAINHAND)
|
| 44 | .build();
|
| 45 | }
|
| 46 |
|
| 47 | public static Tool createToolProperties() {
|
| 48 | return new Tool(List.of(), 1.0F, 2, false);
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public ItemUseAnimation getUseAnimation(final ItemStack itemStack) {
|
| 53 | return ItemUseAnimation.TRIDENT;
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public int getUseDuration(final ItemStack itemStack, final LivingEntity user) {
|
| 58 | return 72000;
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public boolean releaseUsing(final ItemStack itemStack, final Level level, final LivingEntity entity, final int remainingTime) {
|
| 63 | if (entity instanceof Player player) {
|
| 64 | int timeHeld = this.getUseDuration(itemStack, entity) - remainingTime;
|
| 65 | if (timeHeld < 10) {
|
| 66 | return false;
|
| 67 | }
|
| 68 |
|
| 69 | float riptideStrength = EnchantmentHelper.getTridentSpinAttackStrength(itemStack, player);
|
| 70 | if (!(riptideStrength > 0.0F) || player.isInWaterOrRain() && !player.isPassenger()) {
|
| 71 | if (itemStack.nextDamageWillBreak()) {
|
| 72 | return false;
|
| 73 | }
|
| 74 |
|
| 75 | Holder<SoundEvent> sound = EnchantmentHelper.pickHighestLevel(itemStack, EnchantmentEffectComponents.TRIDENT_SOUND).orElse(SoundEvents.TRIDENT_THROW);
|
| 76 | player.awardStat(Stats.ITEM_USED.get(this));
|
| 77 | if (level instanceof ServerLevel serverLevel) {
|
| 78 | itemStack.hurtWithoutBreaking(1, player);
|
| 79 | if (riptideStrength == 0.0F) {
|
| 80 | ItemStack thrownItemStack = itemStack.consumeAndReturn(1, player);
|
| 81 | ThrownTrident trident = Projectile.spawnProjectileFromRotation(ThrownTrident::new, serverLevel, thrownItemStack, player, 0.0F, 2.5F, 1.0F);
|
| 82 | if (player.hasInfiniteMaterials()) {
|
| 83 | trident.pickup = AbstractArrow.Pickup.CREATIVE_ONLY;
|
| 84 | }
|
| 85 |
|
| 86 | level.playSound(null, trident, sound.value(), SoundSource.PLAYERS, 1.0F, 1.0F);
|
| 87 | return true;
|
| 88 | }
|
| 89 | }
|
| 90 |
|
| 91 | if (riptideStrength > 0.0F) {
|
| 92 | float yRot = player.getYRot();
|
| 93 | float xRot = player.getXRot();
|
| 94 | float xd = -Mth.sin(yRot * (float) (Math.PI / 180.0)) * Mth.cos(xRot * (float) (Math.PI / 180.0));
|
| 95 | float yd = -Mth.sin(xRot * (float) (Math.PI / 180.0));
|
| 96 | float zd = Mth.cos(yRot * (float) (Math.PI / 180.0)) * Mth.cos(xRot * (float) (Math.PI / 180.0));
|
| 97 | float dist = Mth.sqrt(xd * xd + yd * yd + zd * zd);
|
| 98 | xd *= riptideStrength / dist;
|
| 99 | yd *= riptideStrength / dist;
|
| 100 | zd *= riptideStrength / dist;
|
| 101 | player.push(xd, yd, zd);
|
| 102 | player.startAutoSpinAttack(20, 8.0F, itemStack);
|
| 103 | if (player.onGround()) {
|
| 104 | float heightDifference = 1.1999999F;
|
| 105 | player.move(MoverType.SELF, new Vec3(0.0, 1.1999999F, 0.0));
|
| 106 | }
|
| 107 |
|
| 108 | level.playSound(null, player, sound.value(), SoundSource.PLAYERS, 1.0F, 1.0F);
|
| 109 | return true;
|
| 110 | } else {
|
| 111 | return false;
|
| 112 | }
|
| 113 | } else {
|
| 114 | return false;
|
| 115 | }
|
| 116 | } else {
|
| 117 | return false;
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | @Override
|
| 122 | public InteractionResult use(final Level level, final Player player, final InteractionHand hand) {
|
| 123 | ItemStack itemInHand = player.getItemInHand(hand);
|
| 124 | if (itemInHand.nextDamageWillBreak()) {
|
| 125 | return InteractionResult.FAIL;
|
| 126 | }
|
| 127 |
|
| 128 | if (EnchantmentHelper.getTridentSpinAttackStrength(itemInHand, player) > 0.0F && !player.isInWaterOrRain()) {
|
| 129 | return InteractionResult.FAIL;
|
| 130 | }
|
| 131 |
|
| 132 | player.startUsingItem(hand);
|
| 133 | return InteractionResult.CONSUME;
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public Projectile asProjectile(final Level level, final Position position, final ItemStack itemStack, final Direction direction) {
|
| 138 | ThrownTrident trident = new ThrownTrident(level, position.x(), position.y(), position.z(), itemStack.copyWithCount(1));
|
| 139 | trident.pickup = AbstractArrow.Pickup.ALLOWED;
|
| 140 | return trident;
|
| 141 | }
|
| 142 | }
|
| 143 |
|