Skip to content

Commit b79d9b2

Browse files
authored
Merge pull request #2 from Kooperlol/1.0.6
1.0.6
2 parents 57f20ff + b63f7f8 commit b79d9b2

8 files changed

Lines changed: 75 additions & 332 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = 'codes.kooper'
7-
version = '1.0.5-beta'
7+
version = '1.0.6-beta'
88

99
dependencies {
1010
implementation 'org.projectlombok:lombok:1.18.28'

src/main/java/codes/kooper/blockify/Blockify.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import codes.kooper.blockify.protocol.BlockPlaceAdapter;
99
import codes.kooper.blockify.protocol.ChunkLoadAdapter;
1010
import codes.kooper.blockify.protocol.PlayerInfoAdapter;
11-
import codes.kooper.blockify.utils.MiningUtils;
1211
import com.github.retrooper.packetevents.PacketEvents;
1312
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
1413
import lombok.Getter;
@@ -19,7 +18,6 @@
1918
public final class Blockify extends JavaPlugin {
2019
private StageManager stageManager;
2120
private BlockChangeManager blockChangeManager;
22-
private MiningUtils miningUtils;
2321

2422
@Override
2523
public void onLoad() {
@@ -37,7 +35,6 @@ public void onEnable() {
3735

3836
stageManager = new StageManager();
3937
blockChangeManager = new BlockChangeManager();
40-
miningUtils = new MiningUtils();
4138

4239
getServer().getPluginManager().registerEvents(new StageBoundListener(), this);
4340

src/main/java/codes/kooper/blockify/managers/BlockChangeManager.java

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerMultiBlockChange;
1414
import lombok.Getter;
1515
import org.bukkit.Bukkit;
16+
import org.bukkit.Location;
1617
import org.bukkit.block.data.BlockData;
1718
import org.bukkit.entity.Player;
1819
import org.bukkit.scheduler.BukkitTask;
@@ -81,28 +82,49 @@ public void sendBlockChanges(Stage stage, Audience audience, ConcurrentHashMap<B
8182
// Send multiple block changes to the players
8283
for (UUID uuid : audience.getPlayers()) {
8384
Player onlinePlayer = Bukkit.getPlayer(uuid);
85+
if (onlinePlayer == null) continue;
86+
Location playerLocation = onlinePlayer.getLocation();
87+
8488
// The chunk index is used to keep track of the current chunk being sent
8589
AtomicInteger chunkIndex = new AtomicInteger(0);
86-
if (onlinePlayer != null) {
87-
// Create an array of chunks to send from the block changes map
88-
BlockifyChunk[] chunksToSend = blockChanges.keySet().toArray(new BlockifyChunk[0]);
89-
// Create a task to send a chunk to the player every tick
90-
blockChangeTasks.put(uuid, Bukkit.getScheduler().runTaskTimer(Blockify.getInstance(), () -> {
91-
// If the chunk index is greater than the length of the chunks to send array, cancel the task
92-
if (chunkIndex.get() >= chunksToSend.length) {
90+
// Create an array of chunks to send from the block changes map
91+
List<BlockifyChunk> chunksToSend = new ArrayList<>(List.of(blockChanges.keySet().toArray(new BlockifyChunk[0])));
92+
chunksToSend.sort((chunk1, chunk2) -> {
93+
// Get distance from chunks to player
94+
int x = playerLocation.getBlockX() / 16;
95+
int z = playerLocation.getBlockZ() / 16;
96+
int chunkX1 = chunk1.x();
97+
int chunkZ1 = chunk1.z();
98+
int chunkX2 = chunk2.x();
99+
int chunkZ2 = chunk2.z();
100+
101+
// Calculate squared distances (more efficient than using square root)
102+
int distanceSquared1 = (chunkX1 - x) * (chunkX1 - x) + (chunkZ1 - z) * (chunkZ1 - z);
103+
int distanceSquared2 = (chunkX2 - x) * (chunkX2 - x) + (chunkZ2 - z) * (chunkZ2 - z);
104+
105+
// Compare distances and return accordingly
106+
return Integer.compare(distanceSquared1, distanceSquared2);
107+
});
108+
109+
// Create a task to send a chunk to the player every tick
110+
blockChangeTasks.put(uuid, Bukkit.getScheduler().runTaskTimer(Blockify.getInstance(), () -> {
111+
// Loop through the chunks per tick
112+
for (int i = 0; i < stage.getChunksPerTick(); i++) {
113+
// Check if the chunk index is greater than the chunks to send length
114+
if (chunkIndex.get() >= chunksToSend.size()) {
93115
blockChangeTasks.get(uuid).cancel();
94116
blockChangeTasks.remove(uuid);
95117
return;
96118
}
97119
// Get the chunk from the chunks to send array
98-
BlockifyChunk chunk = chunksToSend[chunkIndex.get()];
120+
BlockifyChunk chunk = chunksToSend.get(chunkIndex.get());
99121
chunkIndex.getAndIncrement();
100122
// Check if the chunk is loaded, if not, return
101123
if (!stage.getWorld().isChunkLoaded(chunk.x(), chunk.z())) return;
102124
// Send the chunk packet to the player
103125
Bukkit.getScheduler().runTaskAsynchronously(Blockify.getInstance(), () -> sendChunkPacket(stage, onlinePlayer, chunk, blockChanges));
104-
}, 0L, 1L));
105-
}
126+
}
127+
}, 0L, 1L));
106128
}
107129
}
108130

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package codes.kooper.blockify.models;
22

3-
import codes.kooper.blockify.Blockify;
43
import lombok.Getter;
54
import lombok.Setter;
65

7-
import java.util.HashMap;
8-
import java.util.Map;
96
import java.util.Set;
107
import java.util.UUID;
118

@@ -14,14 +11,12 @@
1411
public class Audience {
1512
private boolean arePlayersHidden = false;
1613
private final Set<UUID> players;
17-
private final Map<UUID, Float> miningSpeeds;
1814

1915
/**
2016
* @param players The players to show the block to
2117
*/
2218
public Audience(Set<UUID> players) {
2319
this.players = players;
24-
this.miningSpeeds = new HashMap<>();
2520
}
2621

2722
/**
@@ -31,7 +26,6 @@ public Audience(Set<UUID> players) {
3126
public Audience(Set<UUID> players, boolean arePlayersHidden) {
3227
this.players = players;
3328
this.arePlayersHidden = arePlayersHidden;
34-
this.miningSpeeds = new HashMap<>();
3529
}
3630

3731
/**
@@ -52,34 +46,4 @@ public Set<UUID> removePlayer(UUID player) {
5246
return players;
5347
}
5448

55-
/**
56-
* Sets the mining speed for a player
57-
* @param player The player
58-
* @param speed The speed
59-
*/
60-
public void setMiningSpeed(UUID player, float speed) {
61-
if (speed <= 0 || speed == 1) {
62-
Blockify.getInstance().getLogger().warning("Invalid mining speed for player " + player + ": " + speed);
63-
return;
64-
}
65-
miningSpeeds.put(player, speed);
66-
}
67-
68-
/**
69-
* Resets the mining speed for a player
70-
* @param player The player
71-
*/
72-
public void resetMiningSpeed(UUID player) {
73-
miningSpeeds.remove(player);
74-
}
75-
76-
/**
77-
* Gets the mining speed of a player
78-
* @param player The player
79-
* @return The mining speed
80-
*/
81-
public float getMiningSpeed(UUID player) {
82-
return miningSpeeds.getOrDefault(player, 1f);
83-
}
84-
8549
}

src/main/java/codes/kooper/blockify/models/Stage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class Stage {
2020
private final World world;
2121
private BlockifyPosition maxPosition, minPosition;
2222
private final Set<View> views;
23-
private boolean chunkSorting = false;
23+
private int chunksPerTick;
2424
private final Audience audience;
2525

2626
/**
@@ -39,6 +39,7 @@ public Stage(String name, World world, BlockifyPosition pos1, BlockifyPosition p
3939
this.minPosition = new BlockifyPosition(Math.min(pos1.getX(), pos2.getX()), Math.min(pos1.getY(), pos2.getY()), Math.min(pos1.getZ(), pos2.getZ()));
4040
this.views = new HashSet<>();
4141
this.audience = audience;
42+
this.chunksPerTick = 1;
4243
}
4344

4445

src/main/java/codes/kooper/blockify/protocol/BlockDigAdapter.java

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package codes.kooper.blockify.protocol;
22

33
import codes.kooper.blockify.Blockify;
4+
import codes.kooper.blockify.events.BlockifyBreakEvent;
45
import codes.kooper.blockify.events.BlockifyInteractEvent;
56
import codes.kooper.blockify.models.Stage;
67
import codes.kooper.blockify.models.View;
@@ -15,6 +16,8 @@
1516
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerBlockChange;
1617
import io.github.retrooper.packetevents.util.SpigotConversionUtil;
1718
import org.bukkit.Bukkit;
19+
import org.bukkit.GameMode;
20+
import org.bukkit.Material;
1821
import org.bukkit.block.data.BlockData;
1922
import org.bukkit.entity.Player;
2023

@@ -56,11 +59,26 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
5659
return;
5760
}
5861

59-
// Check if player has custom mining speed, if so, handle custom digging, else handle normal digging
60-
if (view.getStage().getAudience().getMiningSpeed(player.getUniqueId()) != 1) {
61-
Blockify.getInstance().getMiningUtils().handleCustomDigging(player, view, actionType, blockData, position);
62-
} else {
63-
Blockify.getInstance().getMiningUtils().handleNormalDigging(player, view, actionType, blockData, position);
62+
if (actionType == DiggingAction.START_DIGGING) {
63+
if (canInstantBreak(player, blockData)) {
64+
actionType = DiggingAction.FINISHED_DIGGING;
65+
}
66+
}
67+
68+
// Block break functionality
69+
if (actionType == DiggingAction.FINISHED_DIGGING) {
70+
Bukkit.getScheduler().runTask(Blockify.getInstance(), () -> {
71+
// Call BlockifyBreakEvent
72+
BlockifyBreakEvent ghostBreakEvent = new BlockifyBreakEvent(player, position.toPosition(), blockData, view, view.getStage());
73+
ghostBreakEvent.callEvent();
74+
// If block is not cancelled, break the block, otherwise, revert the block
75+
if (!ghostBreakEvent.isCancelled()) {
76+
Blockify.getInstance().getBlockChangeManager().sendBlockChange(view.getStage(), view.getStage().getAudience(), position, Material.AIR.createBlockData());
77+
view.setBlock(position, Material.AIR.createBlockData());
78+
} else {
79+
player.sendBlockChange(position.toLocation(player.getWorld()), blockData);
80+
}
81+
});
6482
}
6583

6684
return;
@@ -70,4 +88,15 @@ public void onPacketPlayReceive(PacketPlayReceiveEvent event) {
7088
}
7189
}
7290

91+
/**
92+
* Check if player can instantly break block
93+
*
94+
* @param player Player who is digging
95+
* @param blockData BlockData of the block
96+
* @return boolean
97+
*/
98+
public boolean canInstantBreak(Player player, BlockData blockData) {
99+
return blockData.getDestroySpeed(player.getInventory().getItemInMainHand(), true) >= blockData.getMaterial().getHardness() * 30 || player.getGameMode() == GameMode.CREATIVE;
100+
}
101+
73102
}

src/main/java/codes/kooper/blockify/protocol/ChunkLoadAdapter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ public void onPacketPlaySend(PacketPlaySendEvent event) {
3333
// Loop through the stages and views to check if the chunk is in the view.
3434
for (Stage stage : stages) {
3535
for (View view : stage.getViews()) {
36+
37+
// Check if the view has any blocks in the bound in the first place
38+
if (!view.hasChunk(chunkX, chunkZ)) return;
3639
BlockifyChunk blockifyChunk = new BlockifyChunk(chunkX, chunkZ);
40+
3741
// If the chunk is being sent to the player, return.
3842
if (Blockify.getInstance().getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()) != null && Blockify.getInstance().getBlockChangeManager().getChunksBeingSent().get(player.getUniqueId()).contains(blockifyChunk.getChunkKey())) {
3943
return;
4044
}
41-
// If the view contains the chunk, send the chunk's blocks to the player.
42-
if (view.getBlocks().containsKey(blockifyChunk)) {
43-
Blockify.getInstance().getBlockChangeManager().sendChunkPacket(stage, player, blockifyChunk, view.getBlocks());
44-
}
45+
46+
Blockify.getInstance().getBlockChangeManager().sendChunkPacket(stage, player, blockifyChunk, view.getBlocks());
4547
}
4648
}
4749
}

0 commit comments

Comments
 (0)