55import lombok .RequiredArgsConstructor ;
66import net .bitbylogic .packetblocks .PacketBlocks ;
77import net .bitbylogic .utils .location .ChunkPosition ;
8- import net .bitbylogic .utils .location .LocationUtil ;
8+ import net .bitbylogic .utils .location .WorldPosition ;
99import org .bukkit .Bukkit ;
1010import org .bukkit .Chunk ;
1111import org .bukkit .Location ;
2525@ Getter
2626public 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