Skip to content

Commit 88d6d15

Browse files
committed
made chunks send faster and optimized
1 parent 1e98ead commit 88d6d15

File tree

9 files changed

+49
-40
lines changed

9 files changed

+49
-40
lines changed

src/main/java/net/potato/tuff/TuffX.java

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.bukkit.scheduler.BukkitRunnable;
2323
import org.bukkit.scheduler.BukkitTask;
2424
import org.bukkit.util.Vector;
25+
import org.bukkit.event.block.BlockPhysicsEvent;
2526

2627
import java.io.*;
2728
import java.nio.charset.StandardCharsets;
@@ -34,6 +35,8 @@ public class TuffX extends JavaPlugin implements Listener, PluginMessageListener
3435
public static final String CHANNEL = "eagler:below_y0";
3536
public ViaBlockIds viablockids;
3637

38+
private static final int CHUNKS_PER_TICK = 2;
39+
3740
private final Map<UUID, Queue<Vector>> requestQueue = new ConcurrentHashMap<>();
3841
private BukkitTask processorTask;
3942

@@ -42,29 +45,21 @@ public void onEnable() {
4245
getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL);
4346
getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, this);
4447
getServer().getPluginManager().registerEvents(this, this);
45-
46-
if (this.viablockids == null) {
47-
this.viablockids = new ViaBlockIds(this);
48-
}
49-
48+
if (this.viablockids == null) this.viablockids = new ViaBlockIds(this);
5049
startProcessorTask();
5150
logFancyEnable();
5251
}
5352

5453
@Override
5554
public void onDisable() {
56-
if (processorTask != null) {
57-
processorTask.cancel();
58-
}
55+
if (processorTask != null) processorTask.cancel();
5956
requestQueue.clear();
6057
getLogger().info("TuffX has been disabled.");
6158
}
6259

6360
@Override
6461
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
65-
if (!channel.equals(CHANNEL) || !player.isOnline()) {
66-
return;
67-
}
62+
if (!channel.equals(CHANNEL) || !player.isOnline()) return;
6863
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) {
6964
int x = in.readInt();
7065
int y = in.readInt();
@@ -111,38 +106,48 @@ public void run() {
111106
Player player = getServer().getPlayer(entry.getKey());
112107
Queue<Vector> queue = entry.getValue();
113108

114-
if (player == null || !player.isOnline() || queue.isEmpty()) {
115-
continue;
116-
}
117-
118-
Vector vec = queue.poll();
119-
if (vec != null) {
120-
World world = player.getWorld();
121-
int cx = vec.getBlockX();
122-
int cz = vec.getBlockZ();
123-
if (world.isChunkLoaded(cx, cz)) {
124-
processAndSendChunk(player, world.getChunkAt(cx, cz));
109+
if (player == null || !player.isOnline() || queue.isEmpty()) continue;
110+
111+
for (int i = 0; i < CHUNKS_PER_TICK && !queue.isEmpty(); i++) {
112+
Vector vec = queue.poll();
113+
if (vec != null) {
114+
World world = player.getWorld();
115+
int cx = vec.getBlockX();
116+
int cz = vec.getBlockZ();
117+
if (world.isChunkLoaded(cx, cz)) {
118+
processAndSendChunk(player, world.getChunkAt(cx, cz));
119+
} else {
120+
queue.add(vec);
121+
}
125122
}
126123
}
127124
}
128125
}
129126
}.runTaskTimer(this, 0L, 1L);
130127
}
131128

132-
private void processAndSendChunk(Player player, Chunk chunk) {
129+
private void processAndSendChunk(final Player player, final Chunk chunk) {
133130
if (chunk == null || !player.isOnline()) return;
134131

135-
ChunkSnapshot snapshot = chunk.getChunkSnapshot(false, false, false);
136-
for (int sectionY = -4; sectionY < 0; sectionY++) {
137-
try {
138-
byte[] payload = createSectionPayload(snapshot, chunk.getX(), chunk.getZ(), sectionY);
139-
if (payload != null) {
140-
player.sendPluginMessage(this, CHANNEL, payload);
132+
final ChunkSnapshot snapshot = chunk.getChunkSnapshot(true, false, false);
133+
134+
new BukkitRunnable() {
135+
@Override
136+
public void run() {
137+
for (int sectionY = -4; sectionY < 0; sectionY++) {
138+
if (!player.isOnline()) break;
139+
140+
try {
141+
byte[] payload = createSectionPayload(snapshot, chunk.getX(), chunk.getZ(), sectionY);
142+
if (payload != null) {
143+
player.sendPluginMessage(TuffX.this, CHANNEL, payload);
144+
}
145+
} catch (IOException e) {
146+
getLogger().severe("Payload creation failed for " + chunk.getX() + "," + chunk.getZ() + ": " + e.getMessage());
147+
}
141148
}
142-
} catch (IOException e) {
143-
getLogger().severe("Payload creation failed for " + chunk.getX() + "," + chunk.getZ() + ": " + e.getMessage());
144149
}
145-
}
150+
}.runTaskAsynchronously(this);
146151
}
147152

148153
@EventHandler
@@ -167,7 +172,6 @@ private byte[] createSectionPayload(ChunkSnapshot snapshot, int cx, int cz, int
167172
int baseY = sectionY * 16;
168173
for (int y = 0; y < 16; y++) for (int z = 0; z < 16; z++) for (int x = 0; x < 16; x++) {
169174
int worldY = baseY + y;
170-
if (worldY < -64 || worldY > 319) { out.writeShort(0); continue; }
171175
String blockKey = snapshot.getBlockData(x, worldY, z).getAsString().replace("minecraft:", "");
172176
int[] legacyData = viablockids.toLegacy(blockKey);
173177
if (legacyData[0] != 0) hasNonAirBlock = true;
@@ -177,13 +181,12 @@ private byte[] createSectionPayload(ChunkSnapshot snapshot, int cx, int cz, int
177181
}
178182
}
179183

184+
//@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
185+
//public void onBlockBreak(BlockBreakEvent event) { if (event.getBlock().getY() < 0) sendBlockUpdateToNearby(event.getBlock().getLocation(), Material.AIR.createBlockData()); }
186+
//@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
187+
//public void onBlockPlace(BlockPlaceEvent event) { if (event.getBlock().getY() < 0) sendBlockUpdateToNearby(event.getBlock().getLocation(), event.getBlock().getBlockData()); }
180188
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
181-
public void onBlockBreak(BlockBreakEvent event) { if (event.getBlock().getY() < 0) sendBlockUpdateToNearby(event.getBlock().getLocation(), Material.AIR.createBlockData()); }
182-
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
183-
public void onBlockPlace(BlockPlaceEvent event) { if (event.getBlock().getY() < 0) sendBlockUpdateToNearby(event.getBlock().getLocation(), event.getBlock().getBlockData()); }
184-
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
185-
public void onBlockPhysics(BlockPlaceEvent event) { if (event.getBlock().getY() < 0) sendBlockUpdateToNearby(event.getBlock().getLocation(), event.getBlock().getBlockData()); }
186-
189+
public void onBlockPhysics(BlockPhysicsEvent event) { if (event.getBlock().getY() < 0) sendBlockUpdateToNearby(event.getBlock().getLocation(), event.getBlock().getBlockData()); }
187190

188191
private void sendBlockUpdateToNearby(Location loc, BlockData data) {
189192
try {
@@ -215,6 +218,5 @@ private void logFancyEnable() {
215218
getLogger().info(" ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝");
216219
getLogger().info("");
217220
getLogger().info("Below y0 and TuffX programmed by Potato");
218-
getLogger().info("Edited by coleis1op");
219221
}
220222
}

target/TuffX.jar

29.1 MB
Binary file not shown.
1.93 KB
Binary file not shown.
2.31 KB
Binary file not shown.
2.08 KB
Binary file not shown.
12.3 KB
Binary file not shown.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
artifactId=TuffX
2+
groupId=net.potato.tuff
3+
version=1.0.1-SNAPSHOT

target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
net\potato\tuff\TuffX$2.class
2+
net\potato\tuff\TuffX$1.class
3+
net\potato\tuff\TuffX.class
4+
net\potato\tuff\TuffX$3.class
15
net\potato\tuff\ViaBlockIds$1.class
26
net\potato\tuff\ViaBlockIds.class
37
net\potato\tuff\ChunkSectionKey.class

target/original-TuffX.jar

1.88 MB
Binary file not shown.

0 commit comments

Comments
 (0)