77import net .countercraft .movecraft .worldguard .MovecraftWorldGuard ;
88import net .countercraft .movecraft .worldguard .utils .WorldGuardUtils .State ;
99
10- import org .bukkit .*;
10+ import org .bukkit .Bukkit ;
11+ import org .bukkit .Effect ;
12+ import org .bukkit .Location ;
13+ import org .bukkit .Material ;
14+ import org .bukkit .Sound ;
15+ import org .bukkit .Tag ;
16+ import org .bukkit .World ;
1117import org .bukkit .block .Block ;
12- import org .bukkit .block .BlockFace ;
1318import org .bukkit .block .Sign ;
1419import org .bukkit .block .data .BlockData ;
1520import org .bukkit .block .data .Directional ;
2025import org .bukkit .inventory .InventoryHolder ;
2126import org .bukkit .inventory .ItemStack ;
2227import org .bukkit .inventory .PlayerInventory ;
28+ import org .bukkit .inventory .meta .Damageable ;
29+ import org .bukkit .inventory .meta .ItemMeta ;
2330import org .bukkit .metadata .FixedMetadataValue ;
2431import org .bukkit .potion .PotionEffect ;
2532import org .bukkit .potion .PotionEffectType ;
@@ -36,8 +43,8 @@ public class TurretManager {
3643 private final HashSet <Player > reloading = new HashSet <>();
3744
3845 public void disable () {
39- for (Player p : onTurrets ) {
40- if (p == null )
46+ for (Player p : onTurrets ) {
47+ if (p == null )
4148 continue ;
4249
4350 demount (p , p .getLocation ());
@@ -47,7 +54,7 @@ public void disable() {
4754 }
4855
4956 public void mount (Player player , @ NotNull Location signPos ) {
50- if (!signPos .getBlock ().getType (). name (). contains ( "SIGN" ))
57+ if (!Tag . SIGNS . isTagged ( signPos .getBlock ().getType ()))
5158 return ;
5259
5360 if (onTurrets .contains (player )) {
@@ -74,12 +81,10 @@ public void demount(Player player, @Nullable Location signPos) {
7481 onTurrets .remove (player );
7582 reloading .remove (player );
7683
77- if (signPos != null ) {
78- if (signPos .getBlock ().getType ().name ().contains ("SIGN" )) {
79- Sign sign = (Sign ) signPos .getBlock ().getState ();
80- sign .setLine (2 , "" );
81- sign .update ();
82- }
84+ if (signPos != null && Tag .SIGNS .isTagged (signPos .getBlock ().getType ())) {
85+ Sign sign = (Sign ) signPos .getBlock ().getState ();
86+ sign .setLine (2 , "" );
87+ sign .update ();
8388 }
8489
8590 // Remove potion effects and set their walking speed back to normal
@@ -88,49 +93,54 @@ public void demount(Player player, @Nullable Location signPos) {
8893 }
8994
9095 public void fire (Player player ) {
91- if (!onTurrets .contains (player ))
96+ if (!onTurrets .contains (player ))
9297 return ;
9398
94- if (player .isGliding () || player .isFlying ())
99+ if (player .isGliding () || player .isFlying ()) {
95100 demount (player , player .getLocation ());
101+ return ;
102+ }
96103
97104 startReloading (player );
98105
99- // do ammo taking
100- if (Config .RequireAmmo ) {
101- if (!takeAmmo (player ))
102- // If they run out of ammo, don't let them fire and play the empty sound
103- player .getWorld ().playSound (player .getLocation (), Sound .BLOCK_DISPENSER_FAIL , 1.0F , 2.0F );
106+ if (Config .RequireAmmo && !takeAmmo (player )) {
107+ // If they run out of ammo, don't let them fire and play the empty sound
108+ player .getWorld ().playSound (player .getLocation (), Sound .BLOCK_DISPENSER_FAIL , 1.0F , 2.0F );
109+ return ;
104110 }
105111
106- if (runRaycast (player ))
112+ // Run a raycast, if that fails fire normally
113+ if (runRaycast (player ))
107114 return ;
108115
109116 Arrow arrow = launchArrow (player );
110117
111- if (Config .UseParticleTracers )
118+ if (Config .UseParticleTracers ) {
112119 TurretsMain .getInstance ().getTracerManager ().startTracing (arrow );
113- else
120+ } else {
114121 arrow .setCritical (true );
122+ }
115123
116124 World world = player .getWorld ();
117125 world .playSound (player .getLocation (), Sound .ENTITY_FIREWORK_ROCKET_BLAST , 1.0F , 2.0F );
118126 world .playEffect (player .getLocation (), Effect .MOBSPAWNER_FLAMES , 0 );
119127 }
120128
121-
122129 @ NotNull
123130 private Arrow launchArrow (Player player ) {
124131 Arrow arrow ;
125132 try {
126133 Object nmsPlayer = player .getClass ().getMethod ("getHandle" ).invoke (player );
127134 Object nmsWorld = nmsPlayer .getClass ().getMethod ("getWorld" ).invoke (nmsPlayer );
128- Object nmsArrow = TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("EntityTippedArrow" ).getConstructor (TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("World" ), TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("EntityLiving" )).newInstance (nmsWorld , nmsPlayer );
135+ Object nmsArrow = TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("EntityTippedArrow" )
136+ .getConstructor (TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("World" ),
137+ TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("EntityLiving" ))
138+ .newInstance (nmsWorld , nmsPlayer );
129139 nmsArrow .getClass ().getMethod ("setNoGravity" , boolean .class ).invoke (nmsArrow , true );
130- nmsWorld .getClass ().getMethod ("addEntity" , TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("Entity" )).invoke (nmsWorld , nmsArrow );
140+ nmsWorld .getClass ().getMethod ("addEntity" , TurretsMain .getInstance ().getNMSUtils ().getNMSClass ("Entity" ))
141+ .invoke (nmsWorld , nmsArrow );
131142 arrow = (Arrow ) nmsArrow .getClass ().getMethod ("getBukkitEntity" ).invoke (nmsArrow );
132- }
133- catch (Throwable e ) {
143+ } catch (Exception e ) {
134144 throw new ArrowLaunchException ("Something went wrong when trying to launch an arrow" , e );
135145 }
136146
@@ -148,77 +158,77 @@ private Arrow launchArrow(Player player) {
148158 }
149159
150160 private boolean takeAmmo (Player player ) {
151- if (Config .TakeFromChest && CraftManager .getInstance () != null ) {
161+ if (Config .TakeFromChest && CraftManager .getInstance () != null ) {
152162 Block signBlock = player .getLocation ().getBlock ();
153- if (signBlock . getType (). name (). contains ( "SIGN" )) {
163+ if (Tag . SIGNS . isTagged ( signBlock . getType () )) {
154164 Block adjacentBlock = getBlockSignAttachedTo (signBlock );
155- if (adjacentBlock instanceof InventoryHolder ) {
165+ if (adjacentBlock instanceof InventoryHolder ) {
156166 Inventory i = ((InventoryHolder ) adjacentBlock .getState ()).getInventory ();
157- if (i .containsAtLeast (new ItemStack (Config .TurretAmmo ), 1 )) {
167+ if (i .containsAtLeast (new ItemStack (Config .TurretAmmo ), 1 )) {
158168 i .remove (Config .TurretAmmo );
159169 return true ;
160170 }
161171 }
162172 }
163173 }
164- if (Config .TakeFromInventory ) {
165- if (player .getInventory ().containsAtLeast (Config .TurretAmmo , 1 )) {
166- player .getInventory ().removeItem (Config .TurretAmmo );
167- player .updateInventory ();
168- return true ;
169- }
174+ if (Config .TakeFromInventory && player .getInventory ().containsAtLeast (Config .TurretAmmo , 1 )) {
175+ player .getInventory ().removeItem (Config .TurretAmmo );
176+ player .updateInventory ();
177+ return true ;
170178 }
171179 return false ;
172180 }
173181
174182 private void startReloading (final Player player ) {
175183 reloading .add (player );
176- Bukkit .getScheduler ().scheduleSyncDelayedTask (TurretsMain .getInstance (), new Runnable () {
177- @ Override
178- public void run () {
179- reloading .remove (player );
180- }
181- }, ((int ) (Config .DelayBetweenShots * 10.0 )));
184+ Bukkit .getScheduler ().scheduleSyncDelayedTask (TurretsMain .getInstance (), () -> reloading .remove (player ),
185+ ((int ) (Config .DelayBetweenShots * 10.0 )));
182186 }
183187
184188 private boolean runRaycast (@ NotNull Player shooter ) {
185189 Location shooterLoc = shooter .getLocation ();
186190 Vector shooterVector = shooterLoc .getDirection ();
187191
188- for (Player p : Bukkit .getOnlinePlayers ()) {
189- if (p == null || !p .isOnline () || p == shooter || p .getWorld () != shooter .getWorld ())
192+ for (Player p : Bukkit .getOnlinePlayers ()) {
193+ if (p == null || !p .isOnline () || p == shooter || p .getWorld () != shooter .getWorld ())
190194 continue ;
191195
192196 // Check for elytra
193197 PlayerInventory inv = p .getInventory ();
194- if (inv == null )
198+ if (inv == null )
195199 continue ;
196- ItemStack chestplate = inv .getChestplate ();
197- if (chestplate == null || chestplate .getType () != Material .ELYTRA )
200+ ItemStack chestplate = inv .getChestplate ();
201+ if (chestplate == null || chestplate .getType () != Material .ELYTRA )
198202 continue ;
199203
200204 // Check for angle
201205 Vector v = p .getLocation ().subtract (shooterLoc ).toVector ();
202- if (v .angle (shooterVector ) > Config .RaycastRadians )
206+ if (v .angle (shooterVector ) > Config .RaycastRadians )
203207 continue ;
204208
205209 // Check for distance
206210 double distSquared = p .getLocation ().distanceSquared (shooterLoc );
207- if (distSquared > Config .RaycastRange * Config .RaycastRange )
211+ if (distSquared > Config .RaycastRange * Config .RaycastRange )
208212 continue ;
209213
210214 // Check for WG PVP flag
211- if (MovecraftWorldGuard .getInstance ().getWGUtils ().getState (null , p .getLocation (), Flags .PVP ) == State .DENY )
215+ if (MovecraftWorldGuard .getInstance ().getWGUtils ().getState (null , p .getLocation (), Flags .PVP ) == State .DENY )
212216 continue ;
213217
214218 // Check for block directly between
215219 Block targetBlock = shooter .getTargetBlock (null , Config .RaycastRange );
216- if (targetBlock .getLocation ().distanceSquared (shooterLoc ) < distSquared )
220+ if (targetBlock .getLocation ().distanceSquared (shooterLoc ) < distSquared )
217221 continue ;
218222
219223 // Time to hit them!
220- if (Config .RaycastBreakElytra )
221- chestplate .setDurability ((short ) 431 );
224+ if (Config .RaycastBreakElytra ) {
225+ ItemMeta meta = chestplate .getItemMeta ();
226+ if (meta instanceof Damageable ) {
227+ Damageable damageable = (Damageable ) meta ;
228+ damageable .setDamage (431 );
229+ chestplate .setItemMeta ((ItemMeta ) damageable );
230+ }
231+ }
222232
223233 p .setGliding (false );
224234 p .setSprinting (false );
@@ -241,14 +251,13 @@ public boolean isReloading(Player p) {
241251 return reloading .contains (p );
242252 }
243253
244-
245254 @ Nullable
246255 public static Block getBlockSignAttachedTo (@ NotNull Block block ) {
247256 BlockData data = block .getState ().getBlockData ();
248- if (data instanceof Rotatable ) {
257+ if (data instanceof Rotatable ) {
249258 return block .getRelative (((Rotatable ) data ).getRotation ());
250259 }
251- if (data instanceof Directional ) {
260+ if (data instanceof Directional ) {
252261 return block .getRelative (((Directional ) data ).getFacing ());
253262 }
254263 return null ;
0 commit comments