3
0
Mirror von https://github.com/Moulberry/AxiomPaperPlugin.git synchronisiert 2024-11-08 17:40:04 +01:00

Add events for fly speed, gamemode and teleport

Dieser Commit ist enthalten in:
Moulberry 2023-09-06 12:22:38 +08:00
Ursprung a720c1da4b
Commit 02c65bc807
6 geänderte Dateien mit 186 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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;
}
}

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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);
}

Datei anzeigen

@ -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));
}
}