Skip to content

Commit e365b28

Browse files
authored
Update TuffX.java
1 parent 1da4e3b commit e365b28

File tree

1 file changed

+70
-46
lines changed

1 file changed

+70
-46
lines changed

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

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -43,53 +43,75 @@
4343
import org.bukkit.event.block.BlockBreakEvent;
4444
import org.bukkit.event.block.BlockPlaceEvent;
4545
import org.bukkit.event.block.BlockPhysicsEvent;
46-
import org.bukkit.event.EventPriority;
4746

4847
import org.bukkit.ChunkSnapshot;
49-
import org.bukkit.Chunk;
5048

5149
public class TuffX extends JavaPlugin implements Listener, PluginMessageListener, TabCompleter {
5250

5351
public static final String CHANNEL = "eagler:below_y0";
54-
@@ -224,9 +232,14 @@
55-
56-
switch (action.toLowerCase()) {
57-
case "break":
58-
getLogger().info("breaking block at "+loc.getX()+","+loc.getY()+","+loc.getZ());
59-
ItemStack tool = player.getInventory().getItemInMainHand();
60-
if (player.getGameMode() == GameMode.SURVIVAL) {
61-
block.breakNaturally();
62-
BlockBreakEvent breakEvent = new BlockBreakEvent(block, player);
63-
getServer().getPluginManager().callEvent(breakEvent);
64-
65-
if (!breakEvent.isCancelled()) {
66-
block.breakNaturally(player.getInventory().getItemInMainHand());
67-
}
68-
} else {
69-
block.setType(Material.AIR);
70-
}
71-
@@ -290,7 +303,7 @@
72-
}
52+
53+
private final Set<ChunkSectionKey> sentChunks = ConcurrentHashMap.newKeySet();
54+
55+
@Override
56+
public void onEnable() {
57+
getLogger().info("TuffX has been enabled.");
58+
this.getServer().getMessenger().registerOutgoingPluginChannel(this, CHANNEL);
59+
this.getServer().getMessenger().registerIncomingPluginChannel(this, CHANNEL, this);
60+
this.getServer().getPluginManager().registerEvents(this, this);
7361
}
7462

75-
private byte[] createSectionPayload(World world, int cx, int cz, int sectionY) throws IOException {
76-
/* private byte[] createSectionPayload(World world, int cx, int cz, int sectionY) throws IOException {
77-
try (ByteArrayOutputStream bout = new ByteArrayOutputStream(8200); DataOutputStream out = new DataOutputStream(bout)) {
78-
out.writeUTF("chunk_data");
79-
out.writeInt(cx);
80-
@@ -312,6 +325,37 @@
63+
@Override
64+
public void onDisable() {
65+
getLogger().info("TuffX has been disabled.");
66+
}
67+
68+
@Override
69+
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
70+
if (!channel.equals(CHANNEL)) {
71+
return;
72+
}
73+
74+
try (DataInputStream in = new DataInputStream(new ByteArrayInputStream(message))) {
75+
String subchannel = in.readUTF();
76+
if ("block_op".equals(subchannel)) {
77+
String action = in.readUTF();
78+
int x = in.readInt();
79+
int y = in.readInt();
80+
int z = in.readInt();
81+
Location loc = new Location(player.getWorld(), x, y, z);
82+
Block block = loc.getBlock();
83+
84+
switch (action.toLowerCase()) {
85+
case "break":
86+
getLogger().info("breaking block at " + loc.getX() + "," + loc.getY() + "," + loc.getZ());
87+
if (player.getGameMode() == GameMode.SURVIVAL) {
88+
BlockBreakEvent breakEvent = new BlockBreakEvent(block, player);
89+
getServer().getPluginManager().callEvent(breakEvent);
90+
91+
if (!breakEvent.isCancelled()) {
92+
block.breakNaturally(player.getInventory().getItemInMainHand());
93+
}
94+
} else {
95+
block.setType(Material.AIR);
96+
}
97+
break;
98+
default:
99+
getLogger().warning("Received unknown block_op action: " + action);
100+
break;
101+
}
81102
}
82-
return bout.toByteArray();
103+
} catch (IOException e) {
104+
e.printStackTrace();
83105
}
84-
}*/
106+
}
85107

86108
private byte[] createSectionPayload(World world, int cx, int cz, int sectionY) throws IOException {
87109
Chunk chunk = world.getChunkAt(cx, cz);
88-
ChunkSnapshot snapshot = chunk.getChunkSnapshot(true, false, false); // avoid lighting overhead
110+
ChunkSnapshot snapshot = chunk.getChunkSnapshot(true, false, false);
89111

90112
try (ByteArrayOutputStream bout = new ByteArrayOutputStream(8200);
91-
DataOutputStream out = new DataOutputStream(bout)) {
92-
113+
DataOutputStream out = new DataOutputStream(bout)) {
114+
93115
out.writeUTF("chunk_data");
94116
out.writeInt(cx);
95117
out.writeInt(cz);
@@ -100,11 +122,9 @@ private byte[] createSectionPayload(World world, int cx, int cz, int sectionY) t
100122
for (int z = 0; z < 16; z++) {
101123
for (int x = 0; x < 16; x++) {
102124
int worldY = baseY + y;
103-
104-
Material type;
105-
type = snapshot.getBlockType(x, worldY, z);
125+
Material type = snapshot.getBlockType(x, worldY, z);
106126
if (type == null) type = Material.AIR;
107-
127+
108128
short legacyId = LegacyBlockIdManager.getLegacyShort(type);
109129
out.writeShort(legacyId);
110130
}
@@ -114,39 +134,43 @@ private byte[] createSectionPayload(World world, int cx, int cz, int sectionY) t
114134
}
115135
}
116136

117-
@EventHandler(priority = EventPriority.HIGHEST)
118-
@@ -416,4 +460,44 @@
119-
120-
private record ChunkSectionKey(UUID playerId, String worldName, int cx, int cz, int sectionY) {
137+
private record ChunkSectionKey(UUID playerId, String worldName, int cx, int cz, int sectionY) {}
138+
139+
private void sendBlockUpdateToNearby(Location loc, Material material) {
140+
getLogger().info("Sending block update for " + material.name() + " at " + loc.toString());
121141
}
122142

123-
143+
private void sendChunkSectionsAsync(Player player, Chunk chunk) {
144+
getLogger().info("Sending chunk sections to " + player.getName() + " for chunk " + chunk.getX() + "," + chunk.getZ());
145+
}
146+
147+
private boolean isPlayerInChunkRange(Player player, Chunk chunk, int viewDistance) {
148+
int dx = Math.abs(player.getLocation().getChunk().getX() - chunk.getX());
149+
int dz = Math.abs(player.getLocation().getChunk().getZ() - chunk.getZ());
150+
return dx <= viewDistance && dz <= viewDistance;
151+
}
124152

125153
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
126154
public void onBlockBreak(BlockBreakEvent event) {
127155
Block block = event.getBlock();
128156
sendBlockUpdateToNearby(block.getLocation(), Material.AIR);
129-
//sendChunkSectionIfBelowY0(event.getPlayer(), block);
130157
}
131158

132159
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
133160
public void onBlockPlace(BlockPlaceEvent event) {
134161
Block block = event.getBlock();
135162
sendBlockUpdateToNearby(block.getLocation(), block.getType());
136-
//sendChunkSectionIfBelowY0(event.getPlayer(), block);
137163
}
138164

139165
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
140166
public void onBlockPhysics(BlockPhysicsEvent event) {
141167
Block block = event.getBlock();
142168
sendBlockUpdateToNearby(block.getLocation(), block.getType());
143-
//sendChunkSectionIfBelowY0(null, block);
144169
}
145170

146171
private void sendChunkSectionIfBelowY0(Player player, Block block) {
147-
if (block.getY() >= 0) return;
172+
if (block.getY() >= 0) return;
148173

149-
int sectionY = block.getY() >> 4;
150174
Chunk chunk = block.getChunk();
151175

152176
if (player != null) {

0 commit comments

Comments
 (0)