1 | @EventHandler
|
2 | public void onBowShoot(EntityShootBowEvent event) {
|
3 | if (!plugin.getConfig().getBoolean("ender_bow.enable")) {
|
4 | return;
|
5 | }
|
6 | if (!(event.getEntity() instanceof Player)) {
|
7 | return;
|
8 | }
|
9 |
|
10 | Player player = (Player) event.getEntity();
|
11 | if (player.isSneaking() || isOnCooldown(player) || playersWithActiveArrows.contains(player.getUniqueId())) {
|
12 | return;
|
13 | }
|
14 |
|
15 | ItemStack bow = event.getBow();
|
16 | if (bow == null || !bow.hasItemMeta()) {
|
17 | return;
|
18 | }
|
19 |
|
20 | PersistentDataContainer bowContainer = bow.getItemMeta().getPersistentDataContainer();
|
21 | if (bowContainer.has(CustomEnchantsMain.ENDER_BOW_KEY, PersistentDataType.INTEGER)) {
|
22 | int enchantmentLevel = bowContainer.get(CustomEnchantsMain.ENDER_BOW_KEY, PersistentDataType.INTEGER);
|
23 |
|
24 | if (enchantmentLevel > 0 && isFlagAllowed(player.getLocation(), enderBowFlag)) {
|
25 | Entity entity = event.getProjectile();
|
26 | if (entity instanceof Projectile) {
|
27 | Projectile projectile = (Projectile) entity;
|
28 | if (projectile instanceof Arrow || projectile instanceof SpectralArrow) {
|
29 | handleArrow(player, projectile, bow, enchantmentLevel);
|
30 | }
|
31 | }
|
32 | }
|
33 | }
|
34 | }
|
35 |
|
36 | private void handleArrow(Player player, Projectile arrow, ItemStack bow, int enchantmentLevel) {
|
37 | PersistentDataContainer arrowContainer = arrow.getPersistentDataContainer();
|
38 | arrowContainer.set(CustomEnchantsMain.ENDER_BOW_KEY, PersistentDataType.INTEGER, enchantmentLevel);
|
39 |
|
40 | playersWithActiveArrows.add(player.getUniqueId());
|
41 | arrow.addPassenger(player);
|
42 | enderBowCooldowns.put(player.getUniqueId(), System.currentTimeMillis() + cooldownTime * 50); // convert ticks to milliseconds
|
43 | damageItem(player, bow);
|
44 |
|
45 | Bukkit.getScheduler().runTaskLater(plugin, () -> {
|
46 | playersWithActiveArrows.remove(player.getUniqueId());
|
47 | arrow.remove();
|
48 | }, 20L * 20); // 10 seconds
|
49 | }
|
50 |
|
51 | @EventHandler
|
52 | public void onEntityDismount(EntityDismountEvent event) {
|
53 | if (!(event.getEntity() instanceof Player)) {
|
54 | return;
|
55 | }
|
56 | if (!(event.getDismounted() instanceof Arrow)) {
|
57 | return;
|
58 | }
|
59 | Player player = (Player) event.getEntity();
|
60 | Arrow arrow = (Arrow) event.getDismounted();
|
61 |
|
62 | // Remove the player's UUID from the active arrows set and remove the arrow
|
63 | if (playersWithActiveArrows.contains(player.getUniqueId())) {
|
64 | playersWithActiveArrows.remove(player.getUniqueId());
|
65 | arrow.remove();
|
66 | }
|
67 |
|
68 | // If the player is on cooldown, remove the cooldown
|
69 | if (isOnCooldown(player)) {
|
70 | enderBowCooldowns.remove(player.getUniqueId());
|
71 | }
|
72 | }
|
73 |
|
74 | @EventHandler
|
75 | public void onProjectileHit(ProjectileHitEvent event) {
|
76 | if (event.getEntity() instanceof Arrow) {
|
77 | Arrow arrow = (Arrow) event.getEntity();
|
78 | if (arrow.getPassengers().size() > 0) {
|
79 | Entity entity = arrow.getPassengers().get(0);
|
80 | if (entity instanceof Player) {
|
81 | Player player = (Player) entity;
|
82 |
|
83 | // Check if arrow is in water
|
84 | if(arrow.getLocation().getBlock().getType() == Material.WATER) {
|
85 | // Remove arrow as it's no longer needed
|
86 | arrow.remove();
|
87 | }
|
88 |
|
89 | playersWithActiveArrows.remove(player.getUniqueId());
|
90 | arrow.eject();
|
91 | arrow.remove();
|
92 | }
|
93 | }
|
94 | }
|
95 | }
|