mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-17 08:27:43 +01:00
Give bee counts in beehives to Purpur clients
This commit is contained in:
@@ -43,11 +43,12 @@
|
||||
// CraftBukkit start
|
||||
if (this.server != null) {
|
||||
this.server.spark.disable(); // Paper - spark
|
||||
@@ -1093,6 +_,7 @@
|
||||
@@ -1093,6 +_,8 @@
|
||||
this.safeShutdown(waitForServer, false);
|
||||
}
|
||||
public void safeShutdown(boolean waitForServer, boolean isRestarting) {
|
||||
+ org.purpurmc.purpur.task.BossBarTask.stopAll(); // Purpur - Implement TPSBar
|
||||
+ org.purpurmc.purpur.task.BeehiveTask.instance().unregister(); // Purpur - Give bee counts in beehives to Purpur clients
|
||||
this.isRestarting = isRestarting;
|
||||
this.hasLoggedStop = true; // Paper - Debugging
|
||||
if (isDebugging()) io.papermc.paper.util.TraceUtil.dumpTraceForThread("Server stopped"); // Paper - Debugging
|
||||
|
||||
@@ -47,11 +47,12 @@
|
||||
|
||||
// CraftBukkit start
|
||||
// this.setPlayerList(new DedicatedPlayerList(this, this.registries(), this.playerDataStorage)); // Spigot - moved up
|
||||
@@ -350,6 +_,7 @@
|
||||
@@ -350,6 +_,8 @@
|
||||
LOGGER.info("JMX monitoring enabled");
|
||||
}
|
||||
|
||||
+ org.purpurmc.purpur.task.BossBarTask.startAll(); // Purpur - Implement TPSBar
|
||||
+ if (org.purpurmc.purpur.PurpurConfig.beeCountPayload) org.purpurmc.purpur.task.BeehiveTask.instance().register(); // Purpur - Give bee counts in beehives to Purpur clients
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -428,6 +428,11 @@ public class PurpurConfig {
|
||||
allowWaterPlacementInTheEnd = getBoolean("settings.allow-water-placement-in-the-end", allowWaterPlacementInTheEnd);
|
||||
}
|
||||
|
||||
public static boolean beeCountPayload = false;
|
||||
private static void beeCountPayload() {
|
||||
beeCountPayload = getBoolean("settings.bee-count-payload", beeCountPayload);
|
||||
}
|
||||
|
||||
public static boolean loggerSuppressInitLegacyMaterialError = false;
|
||||
public static boolean loggerSuppressIgnoredAdvancementWarnings = false;
|
||||
public static boolean loggerSuppressUnrecognizedRecipeErrors = false;
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package org.purpurmc.purpur.network;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public record ClientboundBeehivePayload(BlockPos pos, int numOfBees) implements CustomPacketPayload {
|
||||
public static final StreamCodec<FriendlyByteBuf, ClientboundBeehivePayload> STREAM_CODEC = CustomPacketPayload.codec(ClientboundBeehivePayload::write, ClientboundBeehivePayload::new);
|
||||
public static final Type<ClientboundBeehivePayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath("purpur", "beehive_s2c"));
|
||||
|
||||
public ClientboundBeehivePayload(FriendlyByteBuf friendlyByteBuf) {
|
||||
this(friendlyByteBuf.readBlockPos(), friendlyByteBuf.readInt());
|
||||
}
|
||||
|
||||
private void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeBlockPos(this.pos);
|
||||
friendlyByteBuf.writeInt(this.numOfBees);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Type<? extends CustomPacketPayload> type() {
|
||||
return TYPE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package org.purpurmc.purpur.network;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public record ServerboundBeehivePayload(BlockPos pos) implements CustomPacketPayload {
|
||||
public static final StreamCodec<FriendlyByteBuf, ServerboundBeehivePayload> STREAM_CODEC = CustomPacketPayload.codec(ServerboundBeehivePayload::write, ServerboundBeehivePayload::new);
|
||||
public static final Type<ServerboundBeehivePayload> TYPE = new Type<>(ResourceLocation.fromNamespaceAndPath("purpur", "beehive_c2s"));
|
||||
|
||||
public ServerboundBeehivePayload(FriendlyByteBuf friendlyByteBuf) {
|
||||
this(friendlyByteBuf.readBlockPos());
|
||||
}
|
||||
|
||||
private void write(FriendlyByteBuf friendlyByteBuf) {
|
||||
friendlyByteBuf.writeBlockPos(this.pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Type<? extends CustomPacketPayload> type() {
|
||||
return TYPE;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package org.purpurmc.purpur.task;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
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.entity.Player;
|
||||
import org.bukkit.plugin.PluginBase;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.purpurmc.purpur.network.ClientboundBeehivePayload;
|
||||
import org.purpurmc.purpur.network.ServerboundBeehivePayload;
|
||||
import org.purpurmc.purpur.util.MinecraftInternalPlugin;
|
||||
|
||||
public class BeehiveTask implements PluginMessageListener {
|
||||
|
||||
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, ClientboundBeehivePayload.TYPE.id().toString());
|
||||
Bukkit.getMessenger().registerIncomingPluginChannel(this.plugin, ServerboundBeehivePayload.TYPE.id().toString(), this);
|
||||
}
|
||||
|
||||
public void unregister() {
|
||||
Bukkit.getMessenger().unregisterOutgoingPluginChannel(this.plugin, ClientboundBeehivePayload.TYPE.id().toString());
|
||||
Bukkit.getMessenger().unregisterIncomingPluginChannel(this.plugin, ServerboundBeehivePayload.TYPE.id().toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, byte[] bytes) {
|
||||
FriendlyByteBuf byteBuf = new FriendlyByteBuf(Unpooled.copiedBuffer(bytes));
|
||||
ServerboundBeehivePayload payload = ServerboundBeehivePayload.STREAM_CODEC.decode(byteBuf);
|
||||
|
||||
ServerPlayer serverPlayer = ((CraftPlayer) player).getHandle();
|
||||
|
||||
// targeted block info max range specified in client at net.minecraft.client.gui.hud.DebugHud#render
|
||||
if (!payload.pos().getCenter().closerThan(serverPlayer.position(), 20)) return; // Targeted Block info max range is 20
|
||||
if (serverPlayer.level().getChunkIfLoaded(payload.pos()) == null) return;
|
||||
|
||||
BlockEntity blockEntity = serverPlayer.level().getBlockEntity(payload.pos());
|
||||
if (!(blockEntity instanceof BeehiveBlockEntity beehive)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ClientboundBeehivePayload customPacketPayload = new ClientboundBeehivePayload(payload.pos(), beehive.getOccupantCount());
|
||||
FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.buffer());
|
||||
ClientboundBeehivePayload.STREAM_CODEC.encode(friendlyByteBuf, customPacketPayload);
|
||||
byte[] byteArray = new byte[friendlyByteBuf.readableBytes()];
|
||||
friendlyByteBuf.readBytes(byteArray);
|
||||
player.sendPluginMessage(this.plugin, customPacketPayload.type().id().toString(), byteArray);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user