Skip to content
This repository was archived by the owner on Apr 13, 2025. It is now read-only.

Commit 3507102

Browse files
BillyGalbreathgranny
authored andcommitted
Give bee counts in beehives to Purpur clients
1 parent 64c235a commit 3507102

7 files changed

Lines changed: 129 additions & 186 deletions

File tree

patches/server/0223-Give-bee-counts-in-beehives-to-Purpur-clients.patch

Lines changed: 0 additions & 184 deletions
This file was deleted.

purpur-server/minecraft-patches/sources/net/minecraft/server/MinecraftServer.java.patch

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@
4343
// CraftBukkit start
4444
if (this.server != null) {
4545
this.server.spark.disable(); // Paper - spark
46-
@@ -1093,6 +_,7 @@
46+
@@ -1093,6 +_,8 @@
4747
this.safeShutdown(waitForServer, false);
4848
}
4949
public void safeShutdown(boolean waitForServer, boolean isRestarting) {
5050
+ org.purpurmc.purpur.task.BossBarTask.stopAll(); // Purpur - Implement TPSBar
51+
+ org.purpurmc.purpur.task.BeehiveTask.instance().unregister(); // Purpur - Give bee counts in beehives to Purpur clients
5152
this.isRestarting = isRestarting;
5253
this.hasLoggedStop = true; // Paper - Debugging
5354
if (isDebugging()) io.papermc.paper.util.TraceUtil.dumpTraceForThread("Server stopped"); // Paper - Debugging

purpur-server/minecraft-patches/sources/net/minecraft/server/dedicated/DedicatedServer.java.patch

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,12 @@
4747

4848
// CraftBukkit start
4949
// this.setPlayerList(new DedicatedPlayerList(this, this.registries(), this.playerDataStorage)); // Spigot - moved up
50-
@@ -350,6 +_,7 @@
50+
@@ -350,6 +_,8 @@
5151
LOGGER.info("JMX monitoring enabled");
5252
}
5353

5454
+ org.purpurmc.purpur.task.BossBarTask.startAll(); // Purpur - Implement TPSBar
55+
+ if (org.purpurmc.purpur.PurpurConfig.beeCountPayload) org.purpurmc.purpur.task.BeehiveTask.instance().register(); // Purpur - Give bee counts in beehives to Purpur clients
5556
return true;
5657
}
5758
}

purpur-server/src/main/java/org/purpurmc/purpur/PurpurConfig.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,11 @@ private static void allowWaterPlacementInEnd() {
428428
allowWaterPlacementInTheEnd = getBoolean("settings.allow-water-placement-in-the-end", allowWaterPlacementInTheEnd);
429429
}
430430

431+
public static boolean beeCountPayload = false;
432+
private static void beeCountPayload() {
433+
beeCountPayload = getBoolean("settings.bee-count-payload", beeCountPayload);
434+
}
435+
431436
public static boolean loggerSuppressInitLegacyMaterialError = false;
432437
public static boolean loggerSuppressIgnoredAdvancementWarnings = false;
433438
public static boolean loggerSuppressUnrecognizedRecipeErrors = false;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.purpurmc.purpur.network;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.network.FriendlyByteBuf;
5+
import net.minecraft.network.codec.StreamCodec;
6+
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
7+
import net.minecraft.resources.ResourceLocation;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public record ClientboundBeehivePayload(BlockPos pos, int numOfBees) implements CustomPacketPayload {
11+
public static final StreamCodec<FriendlyByteBuf, ClientboundBeehivePayload> STREAM_CODEC = CustomPacketPayload.codec(ClientboundBeehivePayload::write, ClientboundBeehivePayload::new);
12+
public static final Type<ClientboundBeehivePayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath("purpur", "beehive_s2c"));
13+
14+
public ClientboundBeehivePayload(FriendlyByteBuf friendlyByteBuf) {
15+
this(friendlyByteBuf.readBlockPos(), friendlyByteBuf.readInt());
16+
}
17+
18+
private void write(FriendlyByteBuf friendlyByteBuf) {
19+
friendlyByteBuf.writeBlockPos(this.pos);
20+
friendlyByteBuf.writeInt(this.numOfBees);
21+
}
22+
23+
@Override
24+
public @NotNull Type<? extends CustomPacketPayload> type() {
25+
return TYPE;
26+
}
27+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.purpurmc.purpur.network;
2+
3+
import net.minecraft.core.BlockPos;
4+
import net.minecraft.network.FriendlyByteBuf;
5+
import net.minecraft.network.codec.StreamCodec;
6+
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
7+
import net.minecraft.resources.ResourceLocation;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public record ServerboundBeehivePayload(BlockPos pos) implements CustomPacketPayload {
11+
public static final StreamCodec<FriendlyByteBuf, ServerboundBeehivePayload> STREAM_CODEC = CustomPacketPayload.codec(ServerboundBeehivePayload::write, ServerboundBeehivePayload::new);
12+
public static final Type<ServerboundBeehivePayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath("purpur", "beehive_c2s"));
13+
14+
public ServerboundBeehivePayload(FriendlyByteBuf friendlyByteBuf) {
15+
this(friendlyByteBuf.readBlockPos());
16+
}
17+
18+
private void write(FriendlyByteBuf friendlyByteBuf) {
19+
friendlyByteBuf.writeBlockPos(this.pos);
20+
}
21+
22+
@Override
23+
public @NotNull Type<? extends CustomPacketPayload> type() {
24+
return TYPE;
25+
}
26+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.purpurmc.purpur.task;
2+
3+
import io.netty.buffer.Unpooled;
4+
import net.minecraft.network.FriendlyByteBuf;
5+
import net.minecraft.server.level.ServerPlayer;
6+
import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
7+
import net.minecraft.world.level.block.entity.BlockEntity;
8+
import org.bukkit.Bukkit;
9+
import org.bukkit.craftbukkit.entity.CraftPlayer;
10+
import org.bukkit.entity.Player;
11+
import org.bukkit.plugin.PluginBase;
12+
import org.bukkit.plugin.messaging.PluginMessageListener;
13+
import org.jetbrains.annotations.NotNull;
14+
import org.purpurmc.purpur.network.ClientboundBeehivePayload;
15+
import org.purpurmc.purpur.network.ServerboundBeehivePayload;
16+
import org.purpurmc.purpur.util.MinecraftInternalPlugin;
17+
18+
public class BeehiveTask implements PluginMessageListener {
19+
20+
private static BeehiveTask instance;
21+
22+
public static BeehiveTask instance() {
23+
if (instance == null) {
24+
instance = new BeehiveTask();
25+
}
26+
return instance;
27+
}
28+
29+
private final PluginBase plugin = new MinecraftInternalPlugin();
30+
31+
private BeehiveTask() {
32+
}
33+
34+
public void register() {
35+
Bukkit.getMessenger().registerOutgoingPluginChannel(this.plugin, ClientboundBeehivePayload.TYPE.id().toString());
36+
Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, ServerboundBeehivePayload.TYPE.id().toString(), this);
37+
}
38+
39+
public void unregister() {
40+
Bukkit.getMessenger().unregisterOutgoingPluginChannel(this.plugin, ClientboundBeehivePayload.TYPE.id().toString());
41+
Bukkit.getMessenger().unregisterIncomingPluginChannel(this.plugin, ServerboundBeehivePayload.TYPE.id().toString());
42+
}
43+
44+
@Override
45+
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] bytes) {
46+
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.copiedBuffer(bytes));
47+
ServerboundBeehivePayload payload = ServerboundBeehivePayload.STREAM_CODEC.decode(byteBuf);
48+
49+
ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle();
50+
51+
// targeted block info max range specified in client at net.minecraft.client.gui.hud.DebugHud#render
52+
if (!payload.pos().getCenter().closerThan(serverPlayer.position(), 20)) return; // Targeted Block info max range is 20
53+
if (serverPlayer.level().getChunkIfLoaded(payload.pos()) == null) return;
54+
55+
BlockEntity blockEntity = serverPlayer.level().getBlockEntity(payload.pos());
56+
if (!(blockEntity instanceof BeehiveBlockEntity beehive)) {
57+
return;
58+
}
59+
60+
ClientboundBeehivePayload customPacketPayload = new ClientboundBeehivePayload(payload.pos(), beehive.getOccupantCount());
61+
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer());
62+
ClientboundBeehivePayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload);
63+
byte[] byteArray = new byte[friendlyByteBuf.readableBytes()];
64+
friendlyByteBuf.readBytes(byteArray);
65+
player.sendPluginMessage(this.plugin, customPacketPayload.type().id().toString(), byteArray);
66+
}
67+
}

0 commit comments

Comments
 (0)