1
1
package isaac .bastion .listeners ;
2
2
3
- import java .util .ArrayList ;
3
+ import java .util .HashSet ;
4
4
import java .util .Iterator ;
5
+ import java .util .logging .Level ;
5
6
6
7
import org .bukkit .Location ;
8
+ import org .bukkit .Material ;
7
9
import org .bukkit .block .Block ;
8
10
import org .bukkit .block .PistonMoveReaction ;
9
11
import org .bukkit .event .EventHandler ;
20
22
import org .bukkit .util .Vector ;
21
23
22
24
import isaac .bastion .Bastion ;
25
+ import isaac .bastion .BastionBlock ;
23
26
import isaac .bastion .BastionType ;
24
27
import isaac .bastion .storage .BastionBlockStorage ;
25
28
import vg .civcraft .mc .citadel .Utility ;
@@ -32,91 +35,132 @@ public BastionBreakListener(BastionBlockStorage storage) {
32
35
this .storage = storage ;
33
36
}
34
37
35
- private void dropBastionItem (Location loc ) {
36
- BastionType type = storage .getTypeAtLocation (loc );
38
+ private void dropBastionItem (Location loc , BastionType type ) {
37
39
ItemStack item = type .getItemRepresentation ();
38
40
new BukkitRunnable () {
39
41
@ Override
40
42
public void run () {
41
43
loc .getWorld ().dropItem (loc .add (0.5 , 0.5 , 0.5 ), item ).setVelocity (new Vector (0 , 0.05 , 0 ));;
42
44
}
43
45
}.runTaskLater (Bastion .getPlugin (), 1 );
44
- storage .deleteDeadBastion (loc );
46
+ storage .deleteDeadBastion (loc ); // just in case.
45
47
}
46
48
47
- @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
49
+ @ EventHandler (priority = EventPriority .HIGHEST , ignoreCancelled = true )
48
50
public void onBlockBreak (BlockBreakEvent event ) {
51
+ // we will only reach this is allowed by Citadel -- e.g. is not cancelled.
49
52
Block block = Utility .getRealBlock (event .getBlock ());
50
- if (storage .getTypeAtLocation (block .getLocation ()) != null ) {
51
- block .getDrops ().clear ();
52
- dropBastionItem (block .getLocation ());
53
+ BastionType type = storage .getTypeAtLocation (block .getLocation ());
54
+ if (type == null ) {
55
+ type = storage .getAndRemovePendingBastion (block .getLocation ());
56
+ }
57
+ if (type != null ) {
58
+ Bastion .getPlugin ().getLogger ().log (Level .INFO , "BastionType broken {0}" , type .toString ());
59
+ BastionBlock bastion = storage .getBastionBlock (block .getLocation ());
60
+ if (bastion != null ) {
61
+ bastion .destroy ();
62
+ }
63
+ event .setCancelled (true );
64
+ block .setType (Material .AIR );
65
+ dropBastionItem (block .getLocation (), type );
53
66
}
54
67
}
55
68
56
- @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
69
+ @ EventHandler (priority = EventPriority .HIGHEST , ignoreCancelled = true )
57
70
public void onEntityExplode (EntityExplodeEvent event ) {
58
71
Iterator <Block > iterator = event .blockList ().iterator ();
59
- ArrayList <Block > blocks = new ArrayList <Block >();
72
+ HashSet <Block > blocks = new HashSet <Block >();
60
73
while (iterator .hasNext ()) {
61
74
Block block = Utility .getRealBlock (iterator .next ());
62
- if (storage .getTypeAtLocation (block .getLocation ()) != null ) {
75
+ BastionType type = storage .getTypeAtLocation (block .getLocation ());
76
+ if (type == null ) {
77
+ type = storage .getAndRemovePendingBastion (block .getLocation ());
78
+ }
79
+ if ( type != null ) {
63
80
if (blocks .contains (block )) {
64
- block .getDrops ().clear ();
65
81
continue ;
66
82
}
67
83
blocks .add (block );
68
- dropBastionItem (block .getLocation ());
69
- block .getDrops ().clear ();
84
+ block .setType (Material .AIR );
85
+ dropBastionItem (block .getLocation (), type );
86
+ iterator .remove (); // don't explode it, we've got it covered now.
70
87
}
71
88
}
72
89
}
73
90
74
- @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
91
+ @ EventHandler (priority = EventPriority .HIGHEST , ignoreCancelled = true )
75
92
public void onEntityDoorBreak (EntityBreakDoorEvent event ) {
93
+ // we will only reach this if citadel allows
76
94
Block block = Utility .getRealBlock (event .getBlock ());
77
- if (storage .getTypeAtLocation (block .getLocation ()) != null ) {
78
- block .getDrops ().clear ();
79
- dropBastionItem (block .getLocation ());
95
+ BastionType type = storage .getTypeAtLocation (block .getLocation ());
96
+ if (type == null ) {
97
+ type = storage .getAndRemovePendingBastion (block .getLocation ());
98
+ }
99
+ if (type != null ) {
100
+ BastionBlock bastion = storage .getBastionBlock (block .getLocation ());
101
+ if (bastion != null ) {
102
+ bastion .destroy ();
103
+ }
104
+ event .setCancelled (true );
105
+ block .setType (Material .AIR );
106
+ event .getBlock ().setType (Material .AIR );
107
+ dropBastionItem (block .getLocation (), type );
80
108
}
81
109
}
82
-
83
- @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
110
+
111
+ @ EventHandler (priority = EventPriority .HIGHEST , ignoreCancelled = true )
84
112
public void onPistonExtend (BlockPistonExtendEvent event ) {
113
+ // we will only reach this if citadel allows
85
114
for (Block block : event .getBlocks ()) {
86
- if (storage .getTypeAtLocation (block .getLocation ()) != null ) {
115
+ BastionType type = storage .getTypeAtLocation (block .getLocation ());
116
+ if (type == null ) {
117
+ type = storage .getAndRemovePendingBastion (block .getLocation ());
118
+ }
119
+ if (type != null ) {
87
120
if (block .getPistonMoveReaction () == PistonMoveReaction .BREAK ) {
88
- dropBastionItem (block .getLocation ());
89
- block .getDrops (). clear ( );
121
+ dropBastionItem (block .getLocation (), type );
122
+ block .setType ( Material . AIR );
90
123
} else if (block .getPistonMoveReaction () == PistonMoveReaction .MOVE ) {
91
124
Block toBlock = block .getRelative (event .getDirection ());
92
125
storage .moveDeadBastion (block .getLocation (), toBlock .getLocation ());
126
+ // TODO might need special handling if was pending previously
93
127
}
94
128
}
95
129
}
96
130
}
97
131
98
- @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
132
+ @ EventHandler (priority = EventPriority .HIGHEST , ignoreCancelled = true )
99
133
public void onPistonRetract (BlockPistonRetractEvent event ) {
100
134
if (!event .isSticky ()) return ;
101
135
for (Block block : event .getBlocks ()) {
102
- if (storage .getTypeAtLocation (block .getLocation ()) != null ) {
136
+ BastionType type = storage .getTypeAtLocation (block .getLocation ());
137
+ if (type == null ) {
138
+ type = storage .getAndRemovePendingBastion (block .getLocation ());
139
+ }
140
+ if (type != null ) {
103
141
if (block .getPistonMoveReaction () == PistonMoveReaction .BREAK ) {
104
- dropBastionItem (block .getLocation ());
105
- block .getDrops (). clear ( );
142
+ dropBastionItem (block .getLocation (), type );
143
+ block .setType ( Material . AIR );
106
144
} else if (block .getPistonMoveReaction () == PistonMoveReaction .MOVE ) {
107
145
Block toBlock = block .getRelative (event .getDirection ());
108
146
storage .moveDeadBastion (block .getLocation (), toBlock .getLocation ());
147
+ // TODO might need special handling if pending previously
109
148
}
110
149
}
111
150
}
112
151
}
113
152
114
- @ EventHandler (priority = EventPriority .NORMAL , ignoreCancelled = true )
153
+ @ EventHandler (priority = EventPriority .HIGHEST , ignoreCancelled = true )
115
154
public void onBlockBurn (BlockBurnEvent event ) {
116
155
Block block = Utility .getRealBlock (event .getBlock ());
117
- if (storage .getTypeAtLocation (block .getLocation ()) != null ) {
118
- block .getDrops ().clear ();
119
- dropBastionItem (block .getLocation ());
156
+ BastionType type = storage .getTypeAtLocation (block .getLocation ());
157
+ if (type == null ) {
158
+ type = storage .getAndRemovePendingBastion (block .getLocation ());
159
+ }
160
+ if (type != null ) {
161
+ block .setType (Material .AIR );
162
+ dropBastionItem (block .getLocation (), type );
163
+ event .setCancelled (true );
120
164
}
121
165
}
122
166
}
0 commit comments