1212import org .bukkit .Location ;
1313import org .bukkit .Material ;
1414import org .bukkit .block .Block ;
15+ import org .bukkit .block .BlockFace ;
1516import org .bukkit .entity .ArmorStand ;
1617import org .bukkit .entity .Entity ;
1718import org .bukkit .entity .FallingBlock ;
2021import org .bukkit .util .Vector ;
2122
2223import java .util .ArrayList ;
24+ import java .util .HashSet ;
2325import java .util .List ;
26+ import java .util .Set ;
2427
2528/**
2629 * Represents an instance of a rocket gadget summoned by a player.
3033 */
3134public class GadgetRocket extends Gadget {
3235
33- public static final List <Block > BLOCKS = new ArrayList <>();
36+ private static final BlockFace [] CARDINAL = new BlockFace [] {BlockFace .NORTH , BlockFace .SOUTH , BlockFace .EAST , BlockFace .WEST };
37+ private static final Material FENCE = BlockUtils .getOldMaterial ("FENCE" );
38+ public static final Set <GadgetRocket > ROCKETS_WITH_BLOCKS = new HashSet <>();
3439
35- boolean launching ;
36- ArmorStand armorStand ;
37- List <FallingBlock > fallingBlocks = new ArrayList <>();
40+ private boolean launching ;
41+ private ArmorStand armorStand ;
42+ private List <Block > blocks = new ArrayList <>();
43+ private List <FallingBlock > fallingBlocks = new ArrayList <>();
3844
3945 public GadgetRocket (UltraPlayer owner , UltraCosmetics ultraCosmetics ) {
4046 super (owner , GadgetType .valueOf ("rocket" ), ultraCosmetics );
@@ -48,45 +54,30 @@ void onRightClick() {
4854 loc .setY (loc .getBlockY ());
4955 loc .setZ (loc .getBlockZ () + 0.5 );
5056 Bukkit .getScheduler ().runTaskLater (getUltraCosmetics (), () -> {
57+ ROCKETS_WITH_BLOCKS .add (this );
5158 for (int i = 0 ; i < 2 ; i ++) {
52- Block b1 = loc .clone ().add (1 , i , 0 ).getBlock ();
53- Block b2 = loc .clone ().add (-1 , i , 0 ).getBlock ();
54- Block b3 = loc .clone ().add (0 , i , 1 ).getBlock ();
55- Block b4 = loc .clone ().add (0 , i , -1 ).getBlock ();
56- Block b5 = loc .clone ().add (0 , i + 1 , 0 ).getBlock ();
57- b1 .setType (BlockUtils .getOldMaterial ("FENCE" ));
58- b2 .setType (BlockUtils .getOldMaterial ("FENCE" ));
59- b3 .setType (BlockUtils .getOldMaterial ("FENCE" ));
60- b4 .setType (BlockUtils .getOldMaterial ("FENCE" ));
61- b5 .setType (Material .QUARTZ_BLOCK );
62- BLOCKS .add (b1 );
63- BLOCKS .add (b2 );
64- BLOCKS .add (b3 );
65- BLOCKS .add (b4 );
66- BLOCKS .add (b5 );
59+ Block center = loc .clone ().add (0 , i , 0 ).getBlock ();
60+ for (BlockFace face : CARDINAL ) {
61+ Block side = center .getRelative (face );
62+ side .setType (FENCE );
63+ blocks .add (side );
64+ }
65+ Block quartz = center .getRelative (BlockFace .UP );
66+ quartz .setType (Material .QUARTZ_BLOCK );
67+ blocks .add (quartz );
6768 }
6869 armorStand = loc .getWorld ().spawn (loc .add (0 , 1.5 , 0 ), ArmorStand .class );
6970 armorStand .setVisible (false );
7071 armorStand .setGravity (false );
7172 }, 10 );
7273 Bukkit .getScheduler ().runTaskLater (UltraCosmeticsData .get ().getPlugin (), () -> {
7374 armorStand .setPassenger (getPlayer ());
74- BukkitRunnable runnable = new BukkitRunnable () {
75+ new BukkitRunnable () {
7576 int i = 5 ;
7677
7778 @ Override
7879 public void run () {
79- if (getOwner () == null ) {
80- cancel ();
81- return ;
82- }
83-
84- if (getPlayer () == null ) {
85- cancel ();
86- return ;
87- }
88-
89- if (!getPlayer ().isOnline ()) {
80+ if (getOwner () == null || getPlayer () == null || !getPlayer ().isOnline ()) {
9081 cancel ();
9182 return ;
9283 }
@@ -99,61 +90,57 @@ public void run() {
9990 getPlayer ().sendTitle (ChatColor .RED + "" + ChatColor .BOLD + i , "" );
10091 SoundUtil .playSound (getPlayer (), Sounds .NOTE_BASS_DRUM , 1.0f , 1.0f );
10192 i --;
102- } else {
103- if (!isStillCurrentGadget ()) {
104- cancel ();
105- return ;
106- }
93+ return ;
94+ }
95+ if (!isStillCurrentGadget ()) {
96+ cancel ();
97+ return ;
98+ }
10799
108- getPlayer ().sendTitle (MessageManager .getMessage ("Gadgets.Rocket.Takeoff" ), "" );
109- SoundUtil .playSound (getPlayer ().getLocation (), Sounds .EXPLODE , 1.0f , 1.0f );
110- armorStand .remove ();
111- armorStand = null ;
100+ getPlayer ().sendTitle (MessageManager .getMessage ("Gadgets.Rocket.Takeoff" ), "" );
101+ SoundUtil .playSound (getPlayer ().getLocation (), Sounds .EXPLODE , 1.0f , 1.0f );
102+ armorStand .remove ();
103+ armorStand = null ;
112104
113105
114- for (Block block : BLOCKS ) {
115- block .setType (Material .AIR );
116- }
106+ for (Block block : blocks ) {
107+ block .setType (Material .AIR );
108+ }
117109
118- BLOCKS .clear ();
119-
120- final FallingBlock top = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 3 , 0 ), Material .QUARTZ_BLOCK , (byte ) 0 );
121- FallingBlock base = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 2 , 0 ), Material .QUARTZ_BLOCK , (byte ) 0 );
122- for (int i = 0 ; i < 2 ; i ++) {
123- FallingBlock fence1 = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 1 + i , 1 ), BlockUtils .getOldMaterial ("FENCE" ), (byte ) 0 );
124- FallingBlock fence2 = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 1 + i , -1 ), BlockUtils .getOldMaterial ("FENCE" ), (byte ) 0 );
125- FallingBlock fence3 = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (1 , 1 + i , 0 ), BlockUtils .getOldMaterial ("FENCE" ), (byte ) 0 );
126- FallingBlock fence4 = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (-1 , 1 + i , 0 ), BlockUtils .getOldMaterial ("FENCE" ), (byte ) 0 );
127- fallingBlocks .add (fence1 );
128- fallingBlocks .add (fence2 );
129- fallingBlocks .add (fence3 );
130- fallingBlocks .add (fence4 );
131- }
110+ blocks .clear ();
111+ ROCKETS_WITH_BLOCKS .remove (GadgetRocket .this );
132112
133- fallingBlocks .add (top );
134- fallingBlocks .add (base );
135- if (fallingBlocks .get (8 ).getPassenger () == null ) {
136- fallingBlocks .get (8 ).setPassenger (getPlayer ());
137- }
138- top .setPassenger (getPlayer ());
139- launching = true ;
140- Bukkit .getScheduler ().runTaskLater (getUltraCosmetics (), () -> {
141- if (!isStillCurrentGadget ()) {
142- cancel ();
143- return ;
144- }
145- fallingBlocks .forEach (Entity ::remove );
146- fallingBlocks .clear ();
147- FallDamageManager .addNoFall (getPlayer ());
148- SoundUtil .playSound (getPlayer ().getLocation (), Sounds .EXPLODE , 1.0f , 1.0f );
149- UtilParticles .display (Particles .EXPLOSION_HUGE , getPlayer ().getLocation ());
150- launching = false ;
151- }, 80 );
152- cancel ();
113+ final FallingBlock top = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 3 , 0 ), Material .QUARTZ_BLOCK , (byte ) 0 );
114+ FallingBlock base = getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 2 , 0 ), Material .QUARTZ_BLOCK , (byte ) 0 );
115+ for (int i = 0 ; i < 2 ; i ++) {
116+ fallingBlocks .add (getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 1 + i , 1 ), FENCE , (byte ) 0 ));
117+ fallingBlocks .add (getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (0 , 1 + i , -1 ), FENCE , (byte ) 0 ));
118+ fallingBlocks .add (getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (1 , 1 + i , 0 ), FENCE , (byte ) 0 ));
119+ fallingBlocks .add (getPlayer ().getWorld ().spawnFallingBlock (getPlayer ().getLocation ().add (-1 , 1 + i , 0 ), FENCE , (byte ) 0 ));
153120 }
121+
122+ fallingBlocks .add (top );
123+ fallingBlocks .add (base );
124+ if (fallingBlocks .get (8 ).getPassenger () == null ) {
125+ fallingBlocks .get (8 ).setPassenger (getPlayer ());
126+ }
127+ top .setPassenger (getPlayer ());
128+ launching = true ;
129+ Bukkit .getScheduler ().runTaskLater (getUltraCosmetics (), () -> {
130+ if (!isStillCurrentGadget ()) {
131+ cancel ();
132+ return ;
133+ }
134+ fallingBlocks .forEach (Entity ::remove );
135+ fallingBlocks .clear ();
136+ FallDamageManager .addNoFall (getPlayer ());
137+ SoundUtil .playSound (getPlayer ().getLocation (), Sounds .EXPLODE , 1.0f , 1.0f );
138+ UtilParticles .display (Particles .EXPLOSION_HUGE , getPlayer ().getLocation ());
139+ launching = false ;
140+ }, 80 );
141+ cancel ();
154142 }
155- };
156- runnable .runTaskTimer (getUltraCosmetics (), 0 , 20 );
143+ }.runTaskTimer (getUltraCosmetics (), 0 , 20 );
157144 }, 12 );
158145 }
159146
@@ -206,13 +193,13 @@ public void run() {
206193
207194 @ Override
208195 public void onClear () {
209- for (Block block : BLOCKS ) {
196+ for (Block block : blocks ) {
210197 block .setType (Material .AIR );
211198 }
212199 for (FallingBlock fallingBlock : fallingBlocks ) {
213200 fallingBlock .remove ();
214201 }
215- BLOCKS .clear ();
202+ blocks .clear ();
216203 fallingBlocks .clear ();
217204 if (armorStand != null ) {
218205 armorStand .remove ();
@@ -225,6 +212,9 @@ public void onClear() {
225212 }
226213
227214 @ Override
228- void onLeftClick () {
215+ void onLeftClick () {}
216+
217+ public List <Block > getBlocks () {
218+ return blocks ;
229219 }
230220}
0 commit comments