11package com .nisovin .magicspells .spells .targeted ;
22
3- import java .util .ArrayList ;
3+ import java .util .Set ;
4+ import java .util .List ;
45import java .util .HashSet ;
6+ import java .util .ArrayList ;
57
6- import com .nisovin .magicspells .util .TimeUtil ;
78import org .bukkit .Location ;
89import org .bukkit .Material ;
910import org .bukkit .block .Block ;
10- import org .bukkit .entity .LivingEntity ;
1111import org .bukkit .entity .Player ;
1212import org .bukkit .event .EventHandler ;
13+ import org .bukkit .entity .LivingEntity ;
1314import org .bukkit .event .block .BlockBreakEvent ;
1415
1516import com .nisovin .magicspells .MagicSpells ;
17+ import com .nisovin .magicspells .util .TimeUtil ;
18+ import com .nisovin .magicspells .util .TargetInfo ;
19+ import com .nisovin .magicspells .util .MagicConfig ;
20+ import com .nisovin .magicspells .spells .TargetedSpell ;
1621import com .nisovin .magicspells .materials .MagicMaterial ;
17- import com .nisovin .magicspells .spelleffects .EffectPosition ;
1822import com .nisovin .magicspells .spells .TargetedEntitySpell ;
19- import com .nisovin .magicspells .spells .TargetedSpell ;
20- import com .nisovin .magicspells .util .MagicConfig ;
21- import com .nisovin .magicspells .util .TargetInfo ;
23+ import com .nisovin .magicspells .spelleffects .EffectPosition ;
2224
23- // Special effect position is played in the blocks that are spawned to create the tomb
24- // Makes use of the BLOCK_DESTRUCTION position
2525public class EntombSpell extends TargetedSpell implements TargetedEntitySpell {
26-
26+
27+ Set <Block > blocks ;
2728 MagicMaterial tombBlockType ;
29+
2830 private int tombDuration ;
29- private boolean closeTopAndBottom ;
3031 private boolean allowBreaking ;
31-
32- HashSet < Block > blocks ;
32+ private boolean closeTopAndBottom ;
33+ private String blockDestroyMessage ;
3334
3435 public EntombSpell (MagicConfig config , String spellName ) {
3536 super (config , spellName );
36-
37+
3738 tombBlockType = MagicSpells .getItemNameResolver ().resolveBlock (getConfigString ("tomb-block-type" , "glass" ));
3839 tombDuration = getConfigInt ("tomb-duration" , 20 );
39- closeTopAndBottom = getConfigBoolean ("close-top-and-bottom" , true );
4040 allowBreaking = getConfigBoolean ("allow-breaking" , true );
41+ closeTopAndBottom = getConfigBoolean ("close-top-and-bottom" , true );
42+ blockDestroyMessage = getConfigString ("block-destroy-message" , "" );
4143
4244 blocks = new HashSet <>();
4345
44- if (tombBlockType == null ) MagicSpells .error ("Entomb spell '" + spellName + "' has an invalid tomb-block-type!" );
46+ if (tombBlockType == null ) MagicSpells .error ("EntombSpell '" + internalName + "' has an invalid tomb-block-type defined !" );
4547 }
46-
48+
49+ @ Override
50+ public void turnOff () {
51+ super .turnOff ();
52+
53+ for (Block block : blocks ) {
54+ if (block .getType () != tombBlockType .getMaterial ()) continue ;
55+ block .setType (Material .AIR );
56+ playSpellEffects (EffectPosition .BLOCK_DESTRUCTION , block .getLocation ());
57+ }
58+
59+ }
60+
4761 @ Override
4862 public PostCastAction castSpell (Player player , SpellCastState state , float power , String [] args ) {
4963 if (state == SpellCastState .NORMAL ) {
5064 TargetInfo <LivingEntity > targetInfo = getTargetedEntity (player , power );
51- if (targetInfo != null ) {
52- LivingEntity target = targetInfo .getTarget ();
53- Location locationTarget = target .getLocation ();
54- power = targetInfo .getPower ();
55- int x = locationTarget .getBlockX ();
56- int y = locationTarget .getBlockY ();
57- int z = locationTarget .getBlockZ ();
58-
59- Location loc = new Location (locationTarget .getWorld (), x + .5 , y + .5 , z + .5 , locationTarget .getYaw (), locationTarget .getPitch ());
60- target .teleport (loc );
61-
62- createTomb (target , power );
63- playSpellEffects (player , target );
64- sendMessages (player , target );
65- return PostCastAction .NO_MESSAGES ;
66- }
67- return noTarget (player );
68- }
69- return PostCastAction .HANDLE_NORMALLY ;
70- }
71-
72- private void createTomb (LivingEntity target , float power ) {
73- ArrayList <Block > tombBlocks = new ArrayList <>();
74- Block feet = target .getLocation ().getBlock ();
75-
76- Block temp = feet .getRelative (1 , 0 , 0 );
77- if (temp .getType () == Material .AIR ) {
78- tombBlockType .setBlock (temp );
79- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
80- tombBlocks .add (temp );
81- }
82- temp = feet .getRelative (1 , 1 , 0 );
83- if (temp .getType () == Material .AIR ) {
84- tombBlockType .setBlock (temp );
85- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
86- tombBlocks .add (temp );
87- }
88- temp = feet .getRelative (-1 , 0 , 0 );
89- if (temp .getType () == Material .AIR ) {
90- tombBlockType .setBlock (temp );
91- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
92- tombBlocks .add (temp );
93- }
94- temp = feet .getRelative (-1 , 1 , 0 );
95- if (temp .getType () == Material .AIR ) {
96- tombBlockType .setBlock (temp );
97- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
98- tombBlocks .add (temp );
99- }
100- temp = feet .getRelative (0 , 0 , 1 );
101- if (temp .getType () == Material .AIR ) {
102- tombBlockType .setBlock (temp );
103- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
104- tombBlocks .add (temp );
65+ if (targetInfo == null ) return noTarget (player );
66+
67+ LivingEntity target = targetInfo .getTarget ();
68+ power = targetInfo .getPower ();
69+
70+ createTomb (target , power );
71+ sendMessages (player , target );
72+ playSpellEffects (player , target );
73+
74+ return PostCastAction .NO_MESSAGES ;
10575 }
106- temp = feet .getRelative (0 , 1 , 1 );
107- if (temp .getType () == Material .AIR ) {
108- tombBlockType .setBlock (temp );
109- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
110- tombBlocks .add (temp );
111- }
112- temp = feet .getRelative (0 , 0 , -1 );
113- if (temp .getType () == Material .AIR ) {
114- tombBlockType .setBlock (temp );
115- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
116- tombBlocks .add (temp );
117- }
118- temp = feet .getRelative (0 , 1 , -1 );
119- if (temp .getType () == Material .AIR ) {
120- tombBlockType .setBlock (temp );
121- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
122- tombBlocks .add (temp );
123- }
124- if (closeTopAndBottom ) {
125- temp = feet .getRelative (0 , -1 , 0 );
126- if (temp .getType () == Material .AIR ) {
127- tombBlockType .setBlock (temp );
128- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
129- tombBlocks .add (temp );
130- }
131- temp = feet .getRelative (0 , 2 , 0 );
132- if (temp .getType () == Material .AIR ) {
133- tombBlockType .setBlock (temp );
134- playSpellEffects (EffectPosition .SPECIAL , temp .getLocation ());
135- tombBlocks .add (temp );
136- }
137- }
13876
139- if (tombDuration > 0 && !tombBlocks .isEmpty ()) {
140- blocks .addAll (tombBlocks );
141- MagicSpells .plugin .getServer ().getScheduler ().scheduleSyncDelayedTask (MagicSpells .plugin , new TombRemover (tombBlocks ), Math .round (tombDuration * TimeUtil .TICKS_PER_SECOND * power ));
142- }
77+ return PostCastAction .HANDLE_NORMALLY ;
14378 }
144-
79+
14580 @ Override
14681 public boolean castAtEntity (Player caster , LivingEntity target , float power ) {
14782 if (!validTargetList .canTarget (caster , target )) return false ;
14883 createTomb (target , power );
14984 playSpellEffects (caster , target );
15085 return true ;
15186 }
152-
87+
15388 @ Override
15489 public boolean castAtEntity (LivingEntity target , float power ) {
15590 if (!validTargetList .canTarget (target )) return false ;
@@ -158,32 +93,64 @@ public boolean castAtEntity(LivingEntity target, float power) {
15893 return true ;
15994 }
16095
161- @ EventHandler
162- public void onBlockBreak (BlockBreakEvent event ) {
163- if (!blocks .contains (event .getBlock ())) return ;
164- event .setCancelled (true );
165- if (allowBreaking ) event .getBlock ().setType (Material .AIR );
166- }
167-
168- private class TombRemover implements Runnable {
169-
170- ArrayList <Block > tomb ;
96+ private void createTomb (LivingEntity target , float power ) {
97+ List <Block > tempBlocks = new ArrayList <>();
98+ List <Block > tombBlocks = new ArrayList <>();
99+
100+ Block feet = target .getLocation ().getBlock ();
101+ float pitch = target .getLocation ().getPitch ();
102+ float yaw = target .getLocation ().getYaw ();
103+
104+ Location tpLoc = feet .getLocation ().add (0.5 , 0 , 0.5 );
105+ tpLoc .setYaw (yaw );
106+ tpLoc .setPitch (pitch );
107+ target .teleport (tpLoc );
108+
109+ tempBlocks .add (feet .getRelative (1 , 0 , 0 ));
110+ tempBlocks .add (feet .getRelative (1 , 1 , 0 ));
111+ tempBlocks .add (feet .getRelative (-1 , 0 , 0 ));
112+ tempBlocks .add (feet .getRelative (-1 , 1 , 0 ));
113+ tempBlocks .add (feet .getRelative (0 , 0 , 1 ));
114+ tempBlocks .add (feet .getRelative (0 , 1 , 1 ));
115+ tempBlocks .add (feet .getRelative (0 , 0 , -1 ));
116+ tempBlocks .add (feet .getRelative (0 , 1 , -1 ));
171117
172- public TombRemover (ArrayList <Block > tomb ) {
173- this .tomb = tomb ;
118+ if (closeTopAndBottom ) {
119+ tempBlocks .add (feet .getRelative (0 , -1 , 0 ));
120+ tempBlocks .add (feet .getRelative (0 , 2 , 0 ));
174121 }
175122
176- @ Override
177- public void run () {
178- Material tombBlockMaterial = tombBlockType .getMaterial ();
179- for (Block block : tomb ) {
180- if (tombBlockMaterial != block .getType ()) continue ;
181- block .setType (Material .AIR );
182- playSpellEffects (EffectPosition .BLOCK_DESTRUCTION , block .getLocation ());
183- }
184- blocks .removeAll (tomb );
123+ for (Block b : tempBlocks ) {
124+ if (b .getType () != Material .AIR ) continue ;
125+
126+ tombBlockType .setBlock (b );
127+ playSpellEffects (EffectPosition .SPECIAL , b .getLocation ().add (0.5 , 0.5 , 0.5 ));
128+ tombBlocks .add (b );
185129 }
186130
131+ blocks .addAll (tombBlocks );
132+
133+ if (tombDuration > 0 && !tombBlocks .isEmpty ()) {
134+ MagicSpells .scheduleDelayedTask (() -> removeTomb (tombBlocks ), Math .round (tombDuration * TimeUtil .TICKS_PER_SECOND * power ));
135+ }
187136 }
188-
137+
138+ private void removeTomb (List <Block > entomb ) {
139+ for (Block block : entomb ) {
140+ if (block .getType () != tombBlockType .getMaterial ()) continue ;
141+ block .setType (Material .AIR );
142+ playSpellEffects (EffectPosition .BLOCK_DESTRUCTION , block .getLocation ());
143+ }
144+
145+ blocks .removeAll (entomb );
146+ }
147+
148+ @ EventHandler
149+ public void onBlockBreak (BlockBreakEvent event ) {
150+ if (!blocks .contains (event .getBlock ())) return ;
151+ event .setCancelled (true );
152+ if (allowBreaking ) event .getBlock ().setType (Material .AIR );
153+ if (!blockDestroyMessage .isEmpty ()) MagicSpells .sendMessage (event .getPlayer (), blockDestroyMessage );
154+ }
155+
189156}
0 commit comments