1010import org .bukkit .World ;
1111import org .bukkit .block .Block ;
1212import org .bukkit .block .BlockFace ;
13+ import org .bukkit .block .data .type .Slab ;
1314import org .bukkit .event .EventHandler ;
1415import org .bukkit .event .EventPriority ;
1516import org .bukkit .event .Listener ;
@@ -85,6 +86,10 @@ public void onRedstoneBlockChange(final BlockRedstoneEvent event) {
8586
8687 }
8788
89+ private boolean materialWithSlabToBlockEquals (final Block blockToCompare , Material material , Optional <Slab .Type > slabType ) {
90+ return blockToCompare .getType ().equals (material ) && Bridge .getSlabType (blockToCompare .getBlockData ()).equals (slabType );
91+ }
92+
8893 /**
8994 * Find a bridge above the adjacent block. A bridge is a rectangular area of
9095 * slabs or double slabs, parallel to the ground. It can have no holes or bits
@@ -110,30 +115,34 @@ private Optional<Bridge> findBridge(final Block block, final BlockFace direction
110115 int y = aboveBlock .getY ();
111116 int z = aboveBlock .getZ ();
112117 int width = 1 , height = 1 ;
118+
113119 final Material material = aboveBlock .getType ();
114120
121+ // Record the slab orientation if the block is a slab.
122+ final Optional <Slab .Type > slabType = Bridge .getSlabType (aboveBlock .getBlockData ());
123+
115124 Block adjacentBlock = aboveBlock .getRelative (BlockFace .WEST );
116- while (adjacentBlock . getType (). equals ( material )) {
125+ while (materialWithSlabToBlockEquals ( adjacentBlock , material , slabType )) {
117126 x --;
118127 width ++;
119128 adjacentBlock = adjacentBlock .getRelative (BlockFace .WEST );
120129 }
121130
122131 adjacentBlock = aboveBlock .getRelative (BlockFace .NORTH );
123- while (adjacentBlock . getType (). equals ( material )) {
132+ while (materialWithSlabToBlockEquals ( adjacentBlock , material , slabType )) {
124133 z --;
125134 height ++;
126135 adjacentBlock = adjacentBlock .getRelative (BlockFace .NORTH );
127136 }
128137
129138 adjacentBlock = aboveBlock .getRelative (BlockFace .EAST );
130- while (adjacentBlock . getType (). equals ( material )) {
139+ while (materialWithSlabToBlockEquals ( adjacentBlock , material , slabType )) {
131140 width ++;
132141 adjacentBlock = adjacentBlock .getRelative (BlockFace .EAST );
133142 }
134143
135144 adjacentBlock = aboveBlock .getRelative (BlockFace .SOUTH );
136- while (adjacentBlock . getType (). equals ( material )) {
145+ while (materialWithSlabToBlockEquals ( adjacentBlock , material , slabType )) {
137146 height ++;
138147 adjacentBlock = adjacentBlock .getRelative (BlockFace .SOUTH );
139148 }
@@ -146,15 +155,15 @@ private Optional<Bridge> findBridge(final Block block, final BlockFace direction
146155
147156 for (int dz = 0 ; dz < height ; dz ++) {
148157 Block edgeBlock = world .getBlockAt (x - 1 , y , z + dz );
149- if (edgeBlock . getType (). equals ( material )) {
158+ if (materialWithSlabToBlockEquals ( edgeBlock , material , slabType )) {
150159 // A block is sticking out.
151160 return Optional .empty ();
152161 } else if (!edgeBlock .getType ().isAir ()) {
153162 blockedDirections .add (BlockFace .WEST );
154163 }
155164
156165 edgeBlock = world .getBlockAt (x + width , y , z + dz );
157- if (edgeBlock . getType (). equals ( material )) {
166+ if (materialWithSlabToBlockEquals ( edgeBlock , material , slabType )) {
158167 // A block is sticking out.
159168 return Optional .empty ();
160169 } else if (!edgeBlock .getType ().isAir ()) {
@@ -164,7 +173,7 @@ private Optional<Bridge> findBridge(final Block block, final BlockFace direction
164173
165174 for (int dx = 0 ; dx < width ; dx ++) {
166175 Block edgeBlock = world .getBlockAt (x + dx , y , z - 1 );
167- if (edgeBlock . getType (). equals ( material )) {
176+ if (materialWithSlabToBlockEquals ( edgeBlock , material , slabType )) {
168177 // A block is sticking out.
169178 return Optional .empty ();
170179 } else if (!edgeBlock .getType ().isAir ()) {
@@ -173,14 +182,14 @@ private Optional<Bridge> findBridge(final Block block, final BlockFace direction
173182
174183 for (int dz = 0 ; dz < height ; dz ++) {
175184 final Block bridgeBlock = world .getBlockAt (x + dx , y , z + dz );
176- if (!bridgeBlock . getType (). equals ( material )) {
185+ if (!materialWithSlabToBlockEquals ( bridgeBlock , material , slabType )) {
177186 // There is a hole in the bridge.
178187 return Optional .empty ();
179188 }
180189 }
181190
182191 edgeBlock = world .getBlockAt (x + dx , y , z + height );
183- if (edgeBlock . getType (). equals ( material )) {
192+ if (materialWithSlabToBlockEquals ( edgeBlock , material , slabType )) {
184193 // A block is sticking out.
185194 return Optional .empty ();
186195 } else if (!edgeBlock .getType ().isAir ()) {
@@ -191,7 +200,7 @@ private Optional<Bridge> findBridge(final Block block, final BlockFace direction
191200 // The bridge is a proper rectangle.
192201 if (this .blockedInThreeDirections (blockedDirections ) ||
193202 this .opposingDirectionsAreBlocked (blockedDirections )) {
194- return Optional .of (new Bridge (world .getName (), x , z , y , width , height , material , blockedDirections ));
203+ return Optional .of (new Bridge (world .getName (), x , z , y , width , height , material , blockedDirections , slabType ));
195204 }
196205 }
197206
0 commit comments