-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
0237-Give-bee-counts-in-beehives-to-Purpur-clients.patch
117 lines (114 loc) · 5.25 KB
/
0237-Give-bee-counts-in-beehives-to-Purpur-clients.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: BillyGalbreath <[email protected]>
Date: Thu, 30 Dec 2021 09:56:43 -0600
Subject: [PATCH] Give bee counts in beehives to Purpur clients
diff --git a/src/main/java/net/minecraft/server/MinecraftServer.java b/src/main/java/net/minecraft/server/MinecraftServer.java
index 3a9fe6d9f2e7406ffc368e2dadaab803b591f5c2..d474e2ce24d822254c1ac0c9d9468ac13f9cca86 100644
--- a/src/main/java/net/minecraft/server/MinecraftServer.java
+++ b/src/main/java/net/minecraft/server/MinecraftServer.java
@@ -1033,6 +1033,7 @@ public abstract class MinecraftServer extends ReentrantBlockableEventLoop<TickTa
}
public void safeShutdown(boolean waitForShutdown, boolean isRestarting) {
org.purpurmc.purpur.task.BossBarTask.stopAll(); // Purpur
+ org.purpurmc.purpur.task.BeehiveTask.instance().unregister(); // Purpur
this.isRestarting = isRestarting;
this.hasLoggedStop = true; // Paper
if (isDebugging()) io.papermc.paper.util.TraceUtil.dumpTraceForThread("Server stopped"); // Paper
diff --git a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
index c8902e2783026c85eaf0c16541adf67173ed4058..38e2c279d6a846a4d76c15e495bdf464bfcd5b96 100644
--- a/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
+++ b/src/main/java/net/minecraft/server/dedicated/DedicatedServer.java
@@ -375,6 +375,7 @@ public class DedicatedServer extends MinecraftServer implements ServerInterface
if (gg.pufferfish.pufferfish.PufferfishConfig.enableAsyncMobSpawning) mobSpawnExecutor.start(); // Pufferfish
org.purpurmc.purpur.task.BossBarTask.startAll(); // Purpur
+ org.purpurmc.purpur.task.BeehiveTask.instance().register(); // Purpur
return true;
}
}
diff --git a/src/main/java/org/purpurmc/purpur/task/BeehiveTask.java b/src/main/java/org/purpurmc/purpur/task/BeehiveTask.java
new file mode 100644
index 0000000000000000000000000000000000000000..2ebbaf5faa92a88bfb4d61298951e5b74157d1e1
--- /dev/null
+++ b/src/main/java/org/purpurmc/purpur/task/BeehiveTask.java
@@ -0,0 +1,81 @@
+package org.purpurmc.purpur.task;
+
+import com.google.common.io.ByteArrayDataInput;
+import com.google.common.io.ByteArrayDataOutput;
+import com.google.common.io.ByteStreams;
+import io.netty.buffer.Unpooled;
+import net.minecraft.core.BlockPos;
+import net.minecraft.network.FriendlyByteBuf;
+import net.minecraft.network.protocol.game.ClientboundCustomPayloadPacket;
+import net.minecraft.resources.ResourceLocation;
+import net.minecraft.server.level.ServerPlayer;
+import net.minecraft.world.level.block.entity.BeehiveBlockEntity;
+import net.minecraft.world.level.block.entity.BlockEntity;
+import org.bukkit.Bukkit;
+import org.bukkit.craftbukkit.entity.CraftPlayer;
+import org.bukkit.craftbukkit.scheduler.MinecraftInternalPlugin;
+import org.bukkit.entity.Player;
+import org.bukkit.plugin.PluginBase;
+import org.bukkit.plugin.messaging.PluginMessageListener;
+import org.jetbrains.annotations.NotNull;
+
+public class BeehiveTask implements PluginMessageListener {
+ public static final ResourceLocation BEEHIVE_C2S = new ResourceLocation("purpur", "beehive_c2s");
+ public static final ResourceLocation BEEHIVE_S2C = new ResourceLocation("purpur", "beehive_s2c");
+
+ private static BeehiveTask instance;
+
+ public static BeehiveTask instance() {
+ if (instance == null) {
+ instance = new BeehiveTask();
+ }
+ return instance;
+ }
+
+ private final PluginBase plugin = new MinecraftInternalPlugin();
+
+ private BeehiveTask() {
+ }
+
+ public void register() {
+ Bukkit.getMessenger().registerOutgoingPluginChannel(this.plugin, BEEHIVE_S2C.toString());
+ Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, BEEHIVE_C2S.toString(), this);
+ }
+
+ public void unregister() {
+ Bukkit.getMessenger().unregisterOutgoingPluginChannel(this.plugin, BEEHIVE_S2C.toString());
+ Bukkit.getMessenger().unregisterIncomingPluginChannel(this.plugin, BEEHIVE_C2S.toString());
+ }
+
+ @Override
+ public void onPluginMessageReceived(@NotNull String channel, Player player, byte[] bytes) {
+ ByteArrayDataInput in = in(bytes);
+ long packedPos = in.readLong();
+ BlockPos pos = BlockPos.of(packedPos);
+
+ ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle();
+
+ BlockEntity blockEntity = serverPlayer.level().getBlockEntity(pos);
+ if (!(blockEntity instanceof BeehiveBlockEntity beehive)) {
+ return;
+ }
+
+ ByteArrayDataOutput out = out();
+
+ out.writeInt(beehive.getOccupantCount());
+ out.writeLong(packedPos);
+
+ FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.wrappedBuffer(out.toByteArray()));
+ serverPlayer.connection.send(new ClientboundCustomPayloadPacket(BEEHIVE_S2C, buf));
+ }
+
+ @SuppressWarnings("UnstableApiUsage")
+ private static ByteArrayDataOutput out() {
+ return ByteStreams.newDataOutput();
+ }
+
+ @SuppressWarnings("UnstableApiUsage")
+ private static ByteArrayDataInput in(byte[] bytes) {
+ return ByteStreams.newDataInput(bytes);
+ }
+}