Skip to content

Commit 7e4a2a8

Browse files
author
Dalton Caron
committed
Slab data bug fix #2.
1 parent bf1ae24 commit 7e4a2a8

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>xyz.dcaron.bridges</groupId>
88
<artifactId>RetractableBridges</artifactId>
9-
<version>1.0-SNAPSHOT</version>
9+
<version>1.1.1-SNAPSHOT</version>
1010

1111
<name>RetractableBridges</name>
1212
<!-- FIXME change it to the project's website -->

src/main/java/xyz/dcaron/bridges/Bridge.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package xyz.dcaron.bridges;
22

3+
import java.util.Optional;
34
import java.util.Set;
45

56
import org.bukkit.Material;
67
import org.bukkit.block.BlockFace;
8+
import org.bukkit.block.data.BlockData;
9+
import org.bukkit.block.data.type.Slab;
710

11+
import lombok.AllArgsConstructor;
812
import lombok.EqualsAndHashCode;
913
import lombok.Getter;
1014
import lombok.Setter;
1115

1216
@EqualsAndHashCode
17+
@AllArgsConstructor
1318
public class Bridge {
1419

1520
@Getter
@@ -24,18 +29,8 @@ public class Bridge {
2429
@Getter
2530
@Setter
2631
private Set<BlockFace> blockedDirections;
27-
28-
public Bridge(final String worldName, final int x, final int z, final int y, final int width, final int height,
29-
final Material type, final Set<BlockFace> blockedDirections) {
30-
this.worldName = worldName;
31-
this.x = x;
32-
this.z = z;
33-
this.y = y;
34-
this.width = width;
35-
this.height = height;
36-
this.type = type;
37-
this.blockedDirections = blockedDirections;
38-
}
32+
@Getter
33+
private final Optional<Slab.Type> slabType;
3934

4035
@Override
4136
public String toString() {
@@ -50,4 +45,12 @@ public String toString() {
5045
return sb.toString();
5146
}
5247

48+
public static Optional<Slab.Type> getSlabType(final BlockData data) {
49+
50+
if (data instanceof Slab) {
51+
return Optional.of(((Slab) data).getType());
52+
}
53+
return Optional.empty();
54+
}
55+
5356
}

src/main/java/xyz/dcaron/bridges/BridgeBlockListener.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.bukkit.World;
1111
import org.bukkit.block.Block;
1212
import org.bukkit.block.BlockFace;
13+
import org.bukkit.block.data.type.Slab;
1314
import org.bukkit.event.EventHandler;
1415
import org.bukkit.event.EventPriority;
1516
import 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

src/main/java/xyz/dcaron/bridges/BridgeMover.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,20 @@ private boolean tryToMove(final BlockFace direction, final World world, final Se
154154
// Its bridge moving time.
155155
BridgesPlugin.log("Moving bridge " + direction + " one row", Level.FINE);
156156
for (int i = 0; i < length; i++) {
157-
world.getBlockAt(bridge.getX() + dx + i * xMult, bridge.getY(), bridge.getZ() + dz + i * zMult).setType(bridge.getType());
157+
final Block blockAhead = world.getBlockAt(bridge.getX() + dx + i * xMult, bridge.getY(), bridge.getZ() + dz + i * zMult);
158+
159+
// Change the block type.
160+
blockAhead.setType(bridge.getType());
161+
162+
// Loop hoisting will occurr here.
163+
// Modify the block's remembered slab state.
164+
bridge.getSlabType().ifPresent(slabType -> {
165+
final BlockData data = blockAhead.getBlockData();
166+
if (data instanceof Slab) {
167+
((Slab) data).setType(slabType);
168+
blockAhead.setBlockData(data);
169+
}
170+
});
158171
}
159172

160173
// Moves entities standing on the bridge.

0 commit comments

Comments
 (0)