From cdee54d807348be191281b418d81cabf7fa81953 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Sat, 4 May 2024 22:10:26 +0800 Subject: [PATCH] Add try-catch to all bukkit plugin message handlers to kick player --- .../axiom/packet/BlueprintRequestPacketListener.java | 8 ++++++++ .../axiom/packet/DeleteEntityPacketListener.java | 9 +++++++++ .../moulberry/axiom/packet/HelloPacketListener.java | 8 ++++++++ .../axiom/packet/ManipulateEntityPacketListener.java | 9 +++++++++ .../axiom/packet/MarkerNbtRequestPacketListener.java | 9 +++++++++ .../axiom/packet/RequestChunkDataPacketListener.java | 11 ++++++++++- .../axiom/packet/SetBlockPacketListener.java | 11 ++++++++++- .../axiom/packet/SetEditorViewsPacketListener.java | 9 +++++++++ .../axiom/packet/SetFlySpeedPacketListener.java | 9 +++++++++ .../axiom/packet/SetGamemodePacketListener.java | 9 +++++++++ .../axiom/packet/SetHotbarSlotPacketListener.java | 9 +++++++++ .../moulberry/axiom/packet/SetTimePacketListener.java | 9 +++++++++ .../axiom/packet/SetWorldPropertyListener.java | 9 +++++++++ .../axiom/packet/SpawnEntityPacketListener.java | 9 +++++++++ .../packet/SwitchActiveHotbarPacketListener.java | 9 +++++++++ .../axiom/packet/TeleportPacketListener.java | 9 +++++++++ 16 files changed, 144 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/moulberry/axiom/packet/BlueprintRequestPacketListener.java b/src/main/java/com/moulberry/axiom/packet/BlueprintRequestPacketListener.java index 14f3f93..cd23dd3 100644 --- a/src/main/java/com/moulberry/axiom/packet/BlueprintRequestPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/BlueprintRequestPacketListener.java @@ -28,6 +28,14 @@ public class BlueprintRequestPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java index 2402d71..4e60979 100644 --- a/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/DeleteEntityPacketListener.java @@ -4,6 +4,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.entity.Entity; @@ -26,6 +27,14 @@ public class DeleteEntityPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/HelloPacketListener.java b/src/main/java/com/moulberry/axiom/packet/HelloPacketListener.java index f6b17ac..36ab08b 100644 --- a/src/main/java/com/moulberry/axiom/packet/HelloPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/HelloPacketListener.java @@ -49,6 +49,14 @@ public class HelloPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.hasAxiomPermission(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java index 8db9526..1e84e75 100644 --- a/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/ManipulateEntityPacketListener.java @@ -6,6 +6,7 @@ import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import com.moulberry.axiom.viaversion.ViaVersionHelper; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -80,6 +81,14 @@ public class ManipulateEntityPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/MarkerNbtRequestPacketListener.java b/src/main/java/com/moulberry/axiom/packet/MarkerNbtRequestPacketListener.java index ff640c0..b572bce 100644 --- a/src/main/java/com/moulberry/axiom/packet/MarkerNbtRequestPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/MarkerNbtRequestPacketListener.java @@ -3,6 +3,7 @@ package com.moulberry.axiom.packet; import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.marker.MarkerData; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.server.level.ServerLevel; @@ -24,6 +25,14 @@ public class MarkerNbtRequestPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java b/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java index bf1a862..516ef53 100644 --- a/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/RequestChunkDataPacketListener.java @@ -8,6 +8,7 @@ import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import com.viaversion.viaversion.api.Via; import io.netty.buffer.Unpooled; import it.unimi.dsi.fastutil.longs.*; +import net.kyori.adventure.text.Component; import net.minecraft.core.BlockPos; import net.minecraft.core.registries.Registries; import net.minecraft.nbt.CompoundTag; @@ -42,7 +43,15 @@ public class RequestChunkDataPacketListener implements PluginMessageListener { } @Override - public void onPluginMessageReceived(@NotNull String channel, @NotNull Player bukkitPlayer, @NotNull byte[] message) { + public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player bukkitPlayer, byte[] message) { ServerPlayer player = ((CraftPlayer)bukkitPlayer).getHandle(); FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); long id = friendlyByteBuf.readLong(); diff --git a/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java index 8443817..91c3856 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetBlockPacketListener.java @@ -5,6 +5,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; import net.minecraft.core.IdMapper; @@ -68,7 +69,15 @@ public class SetBlockPacketListener implements PluginMessageListener { } @Override - public void onPluginMessageReceived(@NotNull String channel, @NotNull Player bukkitPlayer, @NotNull byte[] message) { + public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player bukkitPlayer, byte[] message) { if (!this.plugin.canUseAxiom(bukkitPlayer)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SetEditorViewsPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetEditorViewsPacketListener.java index ce1465a..4bfc76f 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetEditorViewsPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetEditorViewsPacketListener.java @@ -6,6 +6,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.View; import com.moulberry.axiom.persistence.UUIDDataType; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.network.FriendlyByteBuf; import org.bukkit.entity.Player; import org.bukkit.persistence.PersistentDataContainer; @@ -26,6 +27,14 @@ public class SetEditorViewsPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java index 8feb59a..ecf9ec4 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java @@ -3,6 +3,7 @@ package com.moulberry.axiom.packet; import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.event.AxiomFlySpeedChangeEvent; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.network.FriendlyByteBuf; import org.bukkit.Bukkit; import org.bukkit.craftbukkit.v1_20_R3.entity.CraftPlayer; @@ -19,6 +20,14 @@ public class SetFlySpeedPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java index 0251410..cdafc54 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java @@ -3,6 +3,7 @@ package com.moulberry.axiom.packet; import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.event.AxiomGameModeChangeEvent; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.level.GameType; import org.bukkit.Bukkit; @@ -21,6 +22,14 @@ public class SetGamemodePacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SetHotbarSlotPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetHotbarSlotPacketListener.java index dc5c1a5..99f9327 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetHotbarSlotPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetHotbarSlotPacketListener.java @@ -5,6 +5,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.persistence.ItemStackDataType; import com.viaversion.viaversion.api.Via; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.network.FriendlyByteBuf; import org.bukkit.NamespacedKey; import org.bukkit.craftbukkit.v1_20_R3.inventory.CraftItemStack; @@ -23,6 +24,14 @@ public class SetHotbarSlotPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player) || this.plugin.isMismatchedDataVersion(player.getUniqueId())) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java index 11ce53d..c6ccb82 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetTimePacketListener.java @@ -4,6 +4,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.event.AxiomTimeChangeEvent; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.core.registries.Registries; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceKey; @@ -25,6 +26,14 @@ public class SetTimePacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java b/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java index a6b4691..5b23f3d 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetWorldPropertyListener.java @@ -5,6 +5,7 @@ import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import com.moulberry.axiom.world_properties.server.ServerWorldPropertiesRegistry; import com.moulberry.axiom.world_properties.server.ServerWorldPropertyHolder; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceLocation; import org.bukkit.entity.Player; @@ -20,6 +21,14 @@ public class SetWorldPropertyListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java index ebfbaf1..7e2d419 100644 --- a/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SpawnEntityPacketListener.java @@ -6,6 +6,7 @@ import com.moulberry.axiom.integration.Integration; import com.moulberry.axiom.integration.plotsquared.PlotSquaredIntegration; import com.moulberry.axiom.viaversion.ViaVersionHelper; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.nbt.CompoundTag; @@ -45,6 +46,14 @@ public class SpawnEntityPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/SwitchActiveHotbarPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SwitchActiveHotbarPacketListener.java index 9700ad3..13c2861 100644 --- a/src/main/java/com/moulberry/axiom/packet/SwitchActiveHotbarPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SwitchActiveHotbarPacketListener.java @@ -4,6 +4,7 @@ import com.moulberry.axiom.AxiomConstants; import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.persistence.ItemStackDataType; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.network.FriendlyByteBuf; import org.bukkit.GameMode; import org.bukkit.Material; @@ -25,6 +26,14 @@ public class SwitchActiveHotbarPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player) || this.plugin.isMismatchedDataVersion(player.getUniqueId())) { return; } diff --git a/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java b/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java index a36a277..54ec61f 100644 --- a/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java @@ -4,6 +4,7 @@ import com.moulberry.axiom.AxiomPaper; import com.moulberry.axiom.event.AxiomUnknownTeleportEvent; import com.moulberry.axiom.event.AxiomTeleportEvent; import io.netty.buffer.Unpooled; +import net.kyori.adventure.text.Component; import net.minecraft.core.registries.Registries; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceKey; @@ -24,6 +25,14 @@ public class TeleportPacketListener implements PluginMessageListener { @Override public void onPluginMessageReceived(@NotNull String channel, @NotNull Player player, @NotNull byte[] message) { + try { + this.process(player, message); + } catch (Throwable t) { + player.kick(Component.text("Error while processing packet " + channel + ": " + t.getMessage())); + } + } + + private void process(Player player, byte[] message) { if (!this.plugin.canUseAxiom(player)) { return; }