From 02c65bc807e5606264b61602cc3c631c7ead3ff3 Mon Sep 17 00:00:00 2001 From: Moulberry Date: Wed, 6 Sep 2023 12:22:38 +0800 Subject: [PATCH] Add events for fly speed, gamemode and teleport --- .../axiom/event/AxiomFlySpeedChangeEvent.java | 50 ++++++++++++++++++ .../axiom/event/AxiomGameModeChangeEvent.java | 51 +++++++++++++++++++ .../axiom/event/AxiomTeleportEvent.java | 51 +++++++++++++++++++ .../packet/SetFlySpeedPacketListener.java | 12 +++++ .../packet/SetGamemodePacketListener.java | 10 ++++ .../axiom/packet/TeleportPacketListener.java | 19 ++++--- 6 files changed, 186 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/moulberry/axiom/event/AxiomFlySpeedChangeEvent.java create mode 100644 src/main/java/com/moulberry/axiom/event/AxiomGameModeChangeEvent.java create mode 100644 src/main/java/com/moulberry/axiom/event/AxiomTeleportEvent.java diff --git a/src/main/java/com/moulberry/axiom/event/AxiomFlySpeedChangeEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomFlySpeedChangeEvent.java new file mode 100644 index 0000000..a914830 --- /dev/null +++ b/src/main/java/com/moulberry/axiom/event/AxiomFlySpeedChangeEvent.java @@ -0,0 +1,50 @@ +package com.moulberry.axiom.event; + +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class AxiomFlySpeedChangeEvent extends Event implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + + private final Player player; + private final float flySpeed; + private boolean cancelled = false; + + public AxiomFlySpeedChangeEvent(Player player, float flySpeed) { + this.player = player; + this.flySpeed = flySpeed; + } + + public float getFlySpeed() { + return flySpeed; + } + + public Player getPlayer() { + return this.player; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + +} diff --git a/src/main/java/com/moulberry/axiom/event/AxiomGameModeChangeEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomGameModeChangeEvent.java new file mode 100644 index 0000000..86da786 --- /dev/null +++ b/src/main/java/com/moulberry/axiom/event/AxiomGameModeChangeEvent.java @@ -0,0 +1,51 @@ +package com.moulberry.axiom.event; + +import org.bukkit.GameMode; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class AxiomGameModeChangeEvent extends Event implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + + private final Player player; + private final GameMode gameMode; + private boolean cancelled = false; + + public AxiomGameModeChangeEvent(Player player, GameMode gameMode) { + this.player = player; + this.gameMode = gameMode; + } + + public GameMode getGameMode() { + return gameMode; + } + + public Player getPlayer() { + return this.player; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + +} diff --git a/src/main/java/com/moulberry/axiom/event/AxiomTeleportEvent.java b/src/main/java/com/moulberry/axiom/event/AxiomTeleportEvent.java new file mode 100644 index 0000000..2b7f800 --- /dev/null +++ b/src/main/java/com/moulberry/axiom/event/AxiomTeleportEvent.java @@ -0,0 +1,51 @@ +package com.moulberry.axiom.event; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.NotNull; + +public class AxiomTeleportEvent extends Event implements Cancellable { + + private static final HandlerList HANDLERS = new HandlerList(); + + private final Player player; + private final Location location; + private boolean cancelled = false; + + public AxiomTeleportEvent(Player player, Location location) { + this.player = player; + this.location = location; + } + + public Location getLocation() { + return this.location.clone(); + } + + public Player getPlayer() { + return this.player; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + + public static HandlerList getHandlerList() { + return HANDLERS; + } + + @Override + public @NotNull HandlerList getHandlers() { + return HANDLERS; + } + +} diff --git a/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java index e355a61..83b5d44 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetFlySpeedPacketListener.java @@ -1,8 +1,12 @@ package com.moulberry.axiom.packet; +import com.moulberry.axiom.event.AxiomFlySpeedChangeEvent; +import com.moulberry.axiom.event.AxiomGameModeChangeEvent; import io.netty.buffer.Unpooled; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.level.GameType; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; @@ -16,8 +20,16 @@ public class SetFlySpeedPacketListener implements PluginMessageListener { return; } + FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); float flySpeed = friendlyByteBuf.readFloat(); + + // Call event + AxiomFlySpeedChangeEvent flySpeedChangeEvent = new AxiomFlySpeedChangeEvent(player, flySpeed); + Bukkit.getPluginManager().callEvent(flySpeedChangeEvent); + if (flySpeedChangeEvent.isCancelled()) return; + + // Change flying speed ((CraftPlayer)player).getHandle().getAbilities().setFlyingSpeed(flySpeed); } diff --git a/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java b/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java index 638203b..c9c18e3 100644 --- a/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/SetGamemodePacketListener.java @@ -1,8 +1,11 @@ package com.moulberry.axiom.packet; +import com.moulberry.axiom.event.AxiomGameModeChangeEvent; import io.netty.buffer.Unpooled; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.world.level.GameType; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; import org.bukkit.craftbukkit.v1_20_R1.entity.CraftPlayer; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; @@ -18,6 +21,13 @@ public class SetGamemodePacketListener implements PluginMessageListener { FriendlyByteBuf friendlyByteBuf = new FriendlyByteBuf(Unpooled.wrappedBuffer(message)); GameType gameType = GameType.byId(friendlyByteBuf.readByte()); + + // Call event + AxiomGameModeChangeEvent gameModeChangeEvent = new AxiomGameModeChangeEvent(player, GameMode.getByValue(gameType.getId())); + Bukkit.getPluginManager().callEvent(gameModeChangeEvent); + if (gameModeChangeEvent.isCancelled()) return; + + // Change gamemode ((CraftPlayer)player).getHandle().setGameMode(gameType); } diff --git a/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java b/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java index 81b07a9..871cc2a 100644 --- a/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java +++ b/src/main/java/com/moulberry/axiom/packet/TeleportPacketListener.java @@ -1,14 +1,13 @@ package com.moulberry.axiom.packet; +import com.moulberry.axiom.event.AxiomGameModeChangeEvent; +import com.moulberry.axiom.event.AxiomTeleportEvent; import io.netty.buffer.Unpooled; import net.minecraft.core.registries.Registries; import net.minecraft.network.FriendlyByteBuf; import net.minecraft.resources.ResourceKey; import net.minecraft.world.level.Level; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.NamespacedKey; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.entity.Player; import org.bukkit.plugin.messaging.PluginMessageListener; import org.jetbrains.annotations.NotNull; @@ -31,9 +30,15 @@ public class TeleportPacketListener implements PluginMessageListener { NamespacedKey namespacedKey = new NamespacedKey(resourceKey.location().getNamespace(), resourceKey.location().getPath()); World world = Bukkit.getWorld(namespacedKey); - if (world != null) { - player.teleport(new Location(world, x, y, z, yRot, xRot)); - } + if (world == null) return; + + // Call event + AxiomTeleportEvent teleportEvent = new AxiomTeleportEvent(player, new Location(world, x, y, z, yRot, xRot)); + Bukkit.getPluginManager().callEvent(teleportEvent); + if (teleportEvent.isCancelled()) return; + + // Do teleport + player.teleport(new Location(world, x, y, z, yRot, xRot)); } }