Skip to content

Commit

Permalink
Fixed block changes not sending because it was not getting the amount…
Browse files Browse the repository at this point in the history
… of blocks correctly.
  • Loading branch information
Kooperlol committed May 16, 2024
1 parent 81329d4 commit 0fe81e3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@
public class BlockChangeManager {

private final ConcurrentHashMap<Player, BukkitTask> blockChangeTasks;
private final ConcurrentHashMap<Player, Vector<Long>> chunksBeingSent;
private final ConcurrentHashMap<BlockData, Integer> blockDataToId;

public BlockChangeManager() {
this.blockChangeTasks = new ConcurrentHashMap<>();
this.chunksBeingSent = new ConcurrentHashMap<>();
//this.chunksBeingSent = new ConcurrentHashMap<>();
this.blockDataToId = new ConcurrentHashMap<>();
}

Expand Down Expand Up @@ -133,7 +132,11 @@ public void sendBlockChanges(Stage stage, Audience audience, ConcurrentHashMap<B
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> new OnBlockChangeSendEvent(stage, blockChanges).callEvent());

// If there is only one block change, send it to the player directly
if (blockChanges.size() == 1) {
int blockCount = 0;
for (Map.Entry<BlockifyChunk, ConcurrentHashMap<BlockifyPosition, BlockData>> entry : blockChanges.entrySet()) {
blockCount += entry.getValue().size();
}
if (blockCount == 1) {
for (Player onlinePlayer : audience.getPlayers()) {
if (onlinePlayer != null) {
for (Map.Entry<BlockifyChunk, ConcurrentHashMap<BlockifyPosition, BlockData>> entry : blockChanges.entrySet()) {
Expand Down Expand Up @@ -222,18 +225,6 @@ public void sendChunkPacket(Stage stage, Player player, BlockifyChunk chunk, Con
// Get the user from PacketEvents API
User user = PacketEvents.getAPI().getPlayerManager().getUser(player);

// Initialize the chunksBeingSent map for this player if not present
chunksBeingSent.computeIfAbsent(player, k -> new Vector<>());

Vector<Long> playerChunksBeingSent = chunksBeingSent.get(player);

// Ensure the chunk isn't already being sent
if (playerChunksBeingSent.contains(chunk.getChunkKey())) {
return;
}
// Add this chunk to the chunks being sent list
playerChunksBeingSent.add(chunk.getChunkKey());

// Loop through the chunks y positions
for (int chunkY = stage.getMinPosition().getY() >> 4; chunkY <= stage.getMaxPosition().getY() >> 4; chunkY++) {
// Create a list of encoded blocks for PacketEvents wrapper
Expand Down Expand Up @@ -264,12 +255,6 @@ public void sendChunkPacket(Stage stage, Player player, BlockifyChunk chunk, Con
WrapperPlayServerMultiBlockChange wrapper = new WrapperPlayServerMultiBlockChange(new Vector3i(chunk.x(), chunkY, chunk.z()), true, encodedBlocksArray);
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> user.sendPacket(wrapper));
}

// Remove the chunk from the chunks being sent list
playerChunksBeingSent.remove(chunk.getChunkKey());
if (playerChunksBeingSent.isEmpty()) {
chunksBeingSent.remove(player);
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

@Getter
public class StageManager {
private final Map<String, Stage> stages = new HashMap<>();
private final Map<String, Stage> stages;

public StageManager() {
this.stages = new HashMap<>();
}

/**
* Create a new stage
Expand Down
1 change: 1 addition & 0 deletions src/main/java/codes/kooper/blockify/models/Stage.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public void sendBlocksToAudience() {
* @param blocks Blocks to refresh to the audience.
*/
public void refreshBlocksToAudience(Set<BlockifyPosition> blocks) {
Blockify.getInstance().getLogger().info("Refresh with size " + blocks.size());
ConcurrentHashMap<BlockifyChunk, ConcurrentHashMap<BlockifyPosition, BlockData>> blockChanges = new ConcurrentHashMap<>();
for (View view : views) {
for (BlockifyPosition position : blocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,6 @@ public void onPacketPlaySend(PacketPlaySendEvent event) {
// If the chunk is not in the world, return.
if (!stage.getWorld().equals(player.getWorld())) return;

// If the chunk is being sent to the player, return.
if (Blockify.getInstance().getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()) != null && Blockify.getInstance().getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()).contains(blockifyChunk.getChunkKey())) {
return;
}

Blockify.getInstance().getBlockChangeManager().sendChunkPacket(stage, player, blockifyChunk, view.getBlocks());
}
}
Expand Down

0 comments on commit 0fe81e3

Please sign in to comment.