Avoid Bukkit#getWorld, add AxiomUnknownTeleportEvent

Dieser Commit ist enthalten in:
Moulberry 2023-11-16 17:33:24 +08:00
Ursprung 54564c188b
Commit 5b61aebd32
4 geänderte Dateien mit 100 neuen und 7 gelöschten Zeilen

Datei anzeigen

@ -1,7 +1,6 @@
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;

Datei anzeigen

@ -0,0 +1,80 @@
package com.moulberry.axiom.event;
import org.bukkit.NamespacedKey;
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 AxiomUnknownTeleportEvent extends Event implements Cancellable {
private static final HandlerList HANDLERS = new HandlerList();
private final Player player;
private final NamespacedKey world;
private final double x;
private final double y;
private final double z;
private final float yaw;
private final float pitch;
private boolean cancelled = false;
public AxiomUnknownTeleportEvent(Player player, NamespacedKey world, double x, double y, double z, float yaw, float pitch) {
this.player = player;
this.world = world;
this.x = x;
this.y = y;
this.z = z;
this.yaw = yaw;
this.pitch = pitch;
}
public Player getPlayer() {
return this.player;
}
public NamespacedKey getWorld() {
return this.world;
}
public double getX() {
return this.x;
}
public double getY() {
return this.y;
}
public double getZ() {
return this.z;
}
public float getYaw() {
return this.yaw;
}
public float getPitch() {
return this.pitch;
}
@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

@ -92,8 +92,8 @@ public class SetBlockBufferPacketListener {
private void applyBlockBuffer(ServerPlayer player, MinecraftServer server, BlockBuffer buffer, ResourceKey<Level> worldKey) {
server.execute(() -> {
ServerLevel world = server.getLevel(worldKey);
if (world == null) return;
ServerLevel world = player.serverLevel();
if (!world.dimension().equals(worldKey)) return;
if (!this.plugin.canUseAxiom(player.getBukkitEntity())) {
return;
@ -278,8 +278,8 @@ public class SetBlockBufferPacketListener {
private void applyBiomeBuffer(ServerPlayer player, MinecraftServer server, BiomeBuffer biomeBuffer, ResourceKey<Level> worldKey) {
server.execute(() -> {
ServerLevel world = server.getLevel(worldKey);
if (world == null) return;
ServerLevel world = player.serverLevel();
if (!world.dimension().equals(worldKey)) return;
if (!this.plugin.canUseAxiom(player.getBukkitEntity())) {
return;

Datei anzeigen

@ -1,7 +1,7 @@
package com.moulberry.axiom.packet;
import com.moulberry.axiom.AxiomPaper;
import com.moulberry.axiom.event.AxiomGameModeChangeEvent;
import com.moulberry.axiom.event.AxiomUnknownTeleportEvent;
import com.moulberry.axiom.event.AxiomTeleportEvent;
import io.netty.buffer.Unpooled;
import net.minecraft.core.registries.Registries;
@ -9,6 +9,8 @@ import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.Level;
import org.bukkit.*;
import org.bukkit.craftbukkit.v1_20_R2.entity.CraftPlayer;
import org.bukkit.craftbukkit.v1_20_R2.util.CraftNamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.jetbrains.annotations.NotNull;
@ -34,12 +36,24 @@ public class TeleportPacketListener implements PluginMessageListener {
float yRot = friendlyByteBuf.readFloat();
float xRot = friendlyByteBuf.readFloat();
// Prevent teleport based on config value
boolean allowTeleportBetweenWorlds = this.plugin.configuration.getBoolean("allow-teleport-between-worlds");
if (!allowTeleportBetweenWorlds && !((CraftPlayer)player).getHandle().serverLevel().dimension().equals(resourceKey)) {
return;
}
// Call unknown teleport event
AxiomUnknownTeleportEvent preTeleportEvent = new AxiomUnknownTeleportEvent(player,
CraftNamespacedKey.fromMinecraft(resourceKey.location()), x, y, z, yRot, xRot);
Bukkit.getPluginManager().callEvent(preTeleportEvent);
if (preTeleportEvent.isCancelled()) return;
// Get bukkit world
NamespacedKey namespacedKey = new NamespacedKey(resourceKey.location().getNamespace(), resourceKey.location().getPath());
World world = Bukkit.getWorld(namespacedKey);
if (world == null) return;
// Prevent teleport based on config value
boolean allowTeleportBetweenWorlds = this.plugin.configuration.getBoolean("allow-teleport-between-worlds");
if (!allowTeleportBetweenWorlds && world != player.getWorld()) {
return;
}