-
Notifications
You must be signed in to change notification settings - Fork 17
/
ImpaledTridentItem.java
117 lines (103 loc) · 5.37 KB
/
ImpaledTridentItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package ladysnake.impaled.common.item;
import ladysnake.impaled.common.entity.ImpaledTridentEntity;
import ladysnake.sincereloyalty.LoyalTrident;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.MovementType;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.projectile.PersistentProjectileEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.TridentItem;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.sound.SoundEvents;
import net.minecraft.stat.Stats;
import net.minecraft.util.Hand;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
public class ImpaledTridentItem extends TridentItem {
EntityType<? extends ImpaledTridentEntity> type;
public ImpaledTridentItem(Settings settings, EntityType<? extends ImpaledTridentEntity> entityType) {
super(settings);
this.type = entityType;
}
public EntityType<? extends ImpaledTridentEntity> getEntityType() {
return type;
}
@Override
public void onStoppedUsing(ItemStack stack, World world, LivingEntity user, int remainingUseTicks) {
if (user instanceof PlayerEntity) {
PlayerEntity playerEntity = (PlayerEntity) user;
int i = this.getMaxUseTime(stack) - remainingUseTicks;
if (i >= 10) {
int j = EnchantmentHelper.getRiptide(stack);
if (j <= 0 || canRiptide(playerEntity)) {
if (!world.isClient) {
stack.damage(1, (LivingEntity) playerEntity, livingEntity -> livingEntity.sendToolBreakStatus(user.getActiveHand()));
if (j == 0) {
ImpaledTridentEntity trident = createTrident(world, playerEntity, stack);
LoyalTrident.of(trident).loyaltrident_setReturnSlot(playerEntity.getActiveHand() == Hand.OFF_HAND ? -1 : playerEntity.getInventory().selectedSlot);
if (playerEntity.getAbilities().creativeMode) {
trident.pickupType = PersistentProjectileEntity.PickupPermission.CREATIVE_ONLY;
}
world.spawnEntity(trident);
world.playSoundFromEntity(null, trident, SoundEvents.ITEM_TRIDENT_THROW, SoundCategory.PLAYERS, 1.0F, 1.0F);
if (!playerEntity.getAbilities().creativeMode) {
playerEntity.getInventory().removeOne(stack);
}
}
}
playerEntity.incrementStat(Stats.USED.getOrCreateStat(this));
if (j > 0) {
float f = playerEntity.getYaw();
float g = playerEntity.getPitch();
float h = -MathHelper.sin(f * 0.017453292F) * MathHelper.cos(g * 0.017453292F);
float k = -MathHelper.sin(g * 0.017453292F);
float l = MathHelper.cos(f * 0.017453292F) * MathHelper.cos(g * 0.017453292F);
float m = MathHelper.sqrt(h * h + k * k + l * l);
float n = 3.0F * ((1.0F + (float) j) / 4.0F);
h *= n / m;
k *= n / m;
l *= n / m;
playerEntity.addVelocity((double) h, (double) k, (double) l);
playerEntity.setRiptideTicks(20);
if (playerEntity.isOnGround()) {
float o = 1.1999999F;
playerEntity.move(MovementType.SELF, new Vec3d(0.0D, 1.1999999284744263D, 0.0D));
}
SoundEvent soundEvent3;
if (j >= 3) {
soundEvent3 = SoundEvents.ITEM_TRIDENT_RIPTIDE_3;
} else if (j == 2) {
soundEvent3 = SoundEvents.ITEM_TRIDENT_RIPTIDE_2;
} else {
soundEvent3 = SoundEvents.ITEM_TRIDENT_RIPTIDE_1;
}
world.playSoundFromEntity(null, playerEntity, soundEvent3, SoundCategory.PLAYERS, 1.0F, 1.0F);
}
}
}
}
}
protected boolean canRiptide(PlayerEntity playerEntity) {
return playerEntity.isTouchingWaterOrRain();
}
public @NotNull ImpaledTridentEntity createTrident(World world, LivingEntity user, ItemStack stack) {
ImpaledTridentEntity impaledTridentEntity = Objects.requireNonNull(this.type.create(world));
impaledTridentEntity.setTridentAttributes(world, user, stack);
impaledTridentEntity.setOwner(user);
impaledTridentEntity.setTridentStack(stack);
impaledTridentEntity.setVelocity(user, user.getPitch(), user.getYaw(), 0.0F, 2.5F, 1.0F);
impaledTridentEntity.updatePosition(user.getX(), user.getEyeY() - 0.1, user.getZ());
return impaledTridentEntity;
}
@Override
public boolean damage(DamageSource source) {
return super.damage(source);
}
}