Skip to content

Commit 3745176

Browse files
authored
Merge pull request #4 from BitByLogics/refactor/location
Position Refactor
2 parents 4681bf6 + 5672d51 commit 3745176

File tree

3 files changed

+62
-48
lines changed

3 files changed

+62
-48
lines changed

src/main/java/net/bitbylogic/packetblocks/adapter/ChunkLoadAdapter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void onPacketSend(PacketSendEvent event) {
3232
int chunkX = packet.getColumn().getX();
3333
int chunkZ = packet.getColumn().getZ();
3434

35-
List<PacketBlock> blocks = new ArrayList<>(manager.getBlocks(player.getWorld(), chunkX, chunkZ));
35+
List<PacketBlock> blocks = new ArrayList<>(manager.getBlocks(player.getWorld(), chunkX, chunkZ).values());
3636
if (blocks.isEmpty()) return;
3737

3838
BaseChunk[] sections = packet.getColumn().getChunks();

src/main/java/net/bitbylogic/packetblocks/block/PacketBlock.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import lombok.Setter;
66
import net.bitbylogic.packetblocks.PacketBlocks;
77
import net.bitbylogic.packetblocks.event.PacketBlockBreakEvent;
8-
import net.bitbylogic.utils.Pair;
8+
import net.bitbylogic.packetblocks.util.BoundingBoxes;
9+
import net.bitbylogic.utils.location.ChunkPosition;
10+
import net.bitbylogic.utils.location.WorldPosition;
911
import org.bukkit.Bukkit;
1012
import org.bukkit.Location;
11-
import org.bukkit.World;
1213
import org.bukkit.block.BlockState;
1314
import org.bukkit.block.data.BlockData;
1415
import org.bukkit.entity.Player;
@@ -23,10 +24,12 @@
2324
@Getter
2425
public class PacketBlock {
2526

26-
private final World world;
27+
private final WorldPosition position;
28+
private final ChunkPosition chunk;
29+
2730
private final Location location;
28-
private final Pair<Integer, Integer> chunk;
29-
private final BoundingBox boundingBox;
31+
32+
private final List<BoundingBox> boundingBoxes;
3033

3134
private final Set<Predicate<Player>> viewConditions;
3235
private final HashMap<UUID, PacketBlockPlayerData> viewers;
@@ -52,10 +55,12 @@ public class PacketBlock {
5255
* @param blockData The visual and physical characteristics of the block. Must not be null.
5356
*/
5457
protected PacketBlock(@NonNull Location location, @NonNull BlockData blockData) {
55-
this.world = location.getWorld();
58+
this.position = WorldPosition.ofBlock(location);
59+
this.chunk = position.toChunkPosition();
60+
5661
this.location = location;
57-
this.chunk = new Pair<>(location.getChunk().getX(), location.getChunk().getZ());
58-
this.boundingBox = BoundingBox.of(location.clone(), location.clone().add(1, 1, 1));
62+
63+
this.boundingBoxes = BoundingBoxes.getBoxes(blockData);
5964
this.blockData = blockData;
6065

6166
this.viewConditions = new HashSet<>();

src/main/java/net/bitbylogic/packetblocks/block/PacketBlockManager.java

Lines changed: 48 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import lombok.RequiredArgsConstructor;
66
import net.bitbylogic.packetblocks.PacketBlocks;
77
import net.bitbylogic.utils.location.ChunkPosition;
8-
import net.bitbylogic.utils.location.LocationUtil;
8+
import net.bitbylogic.utils.location.WorldPosition;
99
import org.bukkit.Bukkit;
1010
import org.bukkit.Chunk;
1111
import org.bukkit.Location;
@@ -25,7 +25,7 @@
2525
@Getter
2626
public class PacketBlockManager {
2727

28-
private final ConcurrentHashMap<ChunkPosition, List<PacketBlock>> blockLocations = new ConcurrentHashMap<>();
28+
private final ConcurrentHashMap<ChunkPosition, Map<WorldPosition, PacketBlock>> blockLocations = new ConcurrentHashMap<>();
2929

3030
private final PacketBlocks plugin;
3131

@@ -55,19 +55,19 @@ public PacketBlock createBlock(@NonNull Location location, @NonNull BlockData bl
5555
ChunkPosition identifier = new ChunkPosition(location.getWorld().getName(), chunk.getX(), chunk.getZ());
5656

5757
if (blockLocations.containsKey(identifier)) {
58-
List<PacketBlock> blocks = blockLocations.get(identifier);
58+
Map<WorldPosition, PacketBlock> blocks = blockLocations.get(identifier);
5959

60-
if (blocks.contains(packetBlock)) {
60+
if (blocks.containsValue(packetBlock)) {
6161
return packetBlock;
6262
}
6363

64-
blocks.add(packetBlock);
64+
blocks.put(packetBlock.getPosition(), packetBlock);
6565
blockLocations.put(identifier, blocks);
6666
return packetBlock;
6767
}
6868

69-
List<PacketBlock> newBlocks = new ArrayList<>();
70-
newBlocks.add(packetBlock);
69+
Map<WorldPosition, PacketBlock> newBlocks = new HashMap<>();
70+
newBlocks.put(packetBlock.getPosition(), packetBlock);
7171

7272
blockLocations.put(identifier, newBlocks);
7373
return packetBlock;
@@ -82,29 +82,29 @@ public PacketBlock createBlock(@NonNull Location location, @NonNull BlockData bl
8282
* @param packetBlock the {@link PacketBlock} to be removed; must not be null
8383
*/
8484
public void removeBlock(@NonNull PacketBlock packetBlock) {
85-
for (Map.Entry<ChunkPosition, List<PacketBlock>> entry : blockLocations.entrySet()) {
86-
List<PacketBlock> blocks = entry.getValue();
85+
ChunkPosition chunk = packetBlock.getChunk();
8786

88-
if (!blocks.contains(packetBlock)) {
89-
continue;
90-
}
91-
92-
blocks.remove(packetBlock);
87+
Map<WorldPosition, PacketBlock> blocks = blockLocations.get(chunk);
9388

94-
blockLocations.put(entry.getKey(), blocks);
89+
if (blocks == null) {
90+
return;
9591
}
9692

93+
blocks.remove(packetBlock.getPosition());
94+
9795
packetBlock.getViewers().keySet().forEach(uuid -> {
9896
Player player = Bukkit.getPlayer(uuid);
9997

100-
if (player == null) {
101-
return;
98+
if (player != null) {
99+
player.sendBlockChange(
100+
packetBlock.getLocation(),
101+
packetBlock.getLocation().getBlock().getBlockData()
102+
);
102103
}
103-
104-
player.sendBlockChange(packetBlock.getLocation(), packetBlock.getLocation().getBlock().getBlockData());
105104
});
106105
}
107106

107+
108108
/**
109109
* Removes {@link PacketBlock} instances from the managed collection if they satisfy a specified condition.
110110
* The condition is defined by the provided {@link Predicate}.
@@ -114,10 +114,10 @@ public void removeBlock(@NonNull PacketBlock packetBlock) {
114114
* must not be null.
115115
*/
116116
public void removeIf(Predicate<PacketBlock> removePredicate) {
117-
for (Map.Entry<ChunkPosition, List<PacketBlock>> entry : blockLocations.entrySet()) {
118-
List<PacketBlock> blocks = entry.getValue();
117+
for (Map.Entry<ChunkPosition, Map<WorldPosition, PacketBlock>> entry : blockLocations.entrySet()) {
118+
Map<WorldPosition, PacketBlock> blocks = entry.getValue();
119119

120-
blocks.stream().filter(removePredicate).forEach(packetBlock -> {
120+
blocks.values().stream().filter(removePredicate).forEach(packetBlock -> {
121121
packetBlock.getViewers().keySet().forEach(uuid -> {
122122
Player player = Bukkit.getPlayer(uuid);
123123

@@ -129,7 +129,7 @@ public void removeIf(Predicate<PacketBlock> removePredicate) {
129129
});
130130
});
131131

132-
blocks.removeIf(removePredicate);
132+
blocks.values().removeIf(removePredicate);
133133
blockLocations.put(entry.getKey(), blocks);
134134
}
135135
}
@@ -149,9 +149,15 @@ public Optional<PacketBlock> getBlock(@NonNull Location location) {
149149
}
150150

151151
Chunk chunk = location.getChunk();
152-
return new ArrayList<>(getBlocks(world, chunk.getX(), chunk.getZ())).stream()
153-
.filter(loc -> loc != null && loc.getLocation() != null && LocationUtil.matches(loc.getLocation().toBlockLocation(), location.toBlockLocation()))
154-
.findFirst();
152+
ChunkPosition chunkPosition = ChunkPosition.of(chunk);
153+
154+
Map<WorldPosition, PacketBlock> blocks = blockLocations.get(chunkPosition);
155+
156+
if (blocks == null) {
157+
return Optional.empty();
158+
}
159+
160+
return Optional.ofNullable(blocks.get(WorldPosition.ofBlock(location)));
155161
}
156162

157163
/**
@@ -165,7 +171,7 @@ public List<PacketBlock> getBlocks(@NonNull World world) {
165171
List<PacketBlock> blocks = new ArrayList<>();
166172

167173
blockLocations.values().forEach(packetBlocks -> {
168-
packetBlocks.forEach(block -> {
174+
packetBlocks.values().forEach(block -> {
169175
if(block == null || block.getLocation() == null) {
170176
return;
171177
}
@@ -193,9 +199,9 @@ public List<PacketBlock> getBlocks(@NonNull World world) {
193199
* @return a list of {@link PacketBlock} instances within the specified chunk,
194200
* or an empty list if no blocks are found
195201
*/
196-
public List<PacketBlock> getBlocks(@NonNull World world, int chunkX, int chunkZ) {
202+
public Map<WorldPosition, PacketBlock> getBlocks(@NonNull World world, int chunkX, int chunkZ) {
197203
ChunkPosition chunkIdentifier = new ChunkPosition(world.getName(), chunkX, chunkZ);
198-
return blockLocations.getOrDefault(chunkIdentifier, new ArrayList<>());
204+
return blockLocations.getOrDefault(chunkIdentifier, new HashMap<>());
199205
}
200206

201207
/**
@@ -209,7 +215,7 @@ public List<PacketBlock> getBlocksByViewer(@NonNull Player player) {
209215
List<PacketBlock> blocks = new ArrayList<>();
210216

211217
blockLocations.forEach((identifier, value) -> {
212-
value.forEach(block -> {
218+
value.values().forEach(block -> {
213219
if (!block.getViewers().containsKey(player.getUniqueId())) {
214220
return;
215221
}
@@ -234,7 +240,7 @@ public List<PacketBlock> getBlocksByViewerWithMeta(@NonNull Player player, @NonN
234240
List<PacketBlock> blocks = new ArrayList<>();
235241

236242
blockLocations.forEach((identifier, value) -> {
237-
value.forEach(block -> {
243+
value.values().forEach(block -> {
238244
if (!block.getViewers().containsKey(player.getUniqueId()) || !block.getMetadata().containsKey(metaKey)) {
239245
return;
240246
}
@@ -256,7 +262,7 @@ public List<PacketBlock> getBlocksByMetadata(@NonNull String key) {
256262
List<PacketBlock> blocks = new ArrayList<>();
257263

258264
blockLocations.forEach((identifier, value) -> {
259-
value.forEach(block -> {
265+
value.values().forEach(block -> {
260266
if (!block.getMetadata().containsKey(key)) {
261267
return;
262268
}
@@ -280,12 +286,15 @@ public List<PacketBlock> getHitBlocks(@NonNull World world, @NonNull BoundingBox
280286
List<PacketBlock> blocks = new ArrayList<>();
281287

282288
getChunksInBoundingBox(world, boundingBox).forEach(chunk -> {
283-
getBlocks(world, chunk.getX(), chunk.getZ()).forEach(block -> {
284-
if (!block.getBoundingBox().overlaps(boundingBox)) {
285-
return;
286-
}
289+
getBlocks(world, chunk.getX(), chunk.getZ()).values().forEach(block -> {
290+
for (BoundingBox box : block.getBoundingBoxes()) {
291+
if(!box.overlaps(boundingBox)) {
292+
continue;
293+
}
287294

288-
blocks.add(block);
295+
blocks.add(block);
296+
break;
297+
}
289298
});
290299
});
291300

@@ -333,7 +342,7 @@ public List<PacketBlock> getHitBlocksByViewer(@NonNull Player player, @NonNull B
333342
List<PacketBlock> blocks = new ArrayList<>();
334343

335344
blockLocations.forEach((identifier, value) -> {
336-
value.forEach(block -> {
345+
value.values().forEach(block -> {
337346
if (!block.getViewers().containsKey(player.getUniqueId())) {
338347
return;
339348
}
@@ -365,7 +374,7 @@ public List<PacketBlock> getHitBlocksByViewerWithMeta(@NonNull Player player, @N
365374
List<PacketBlock> blocks = new ArrayList<>();
366375

367376
blockLocations.forEach((identifier, value) -> {
368-
value.forEach(block -> {
377+
value.values().forEach(block -> {
369378
if (!block.getViewers().containsKey(player.getUniqueId())) {
370379
return;
371380
}

0 commit comments

Comments
 (0)