From 20532998930b7444675f6769b9e440406610f294 Mon Sep 17 00:00:00 2001 From: benwoo1110 <30431861+benwoo1110@users.noreply.github.com> Date: Thu, 18 Feb 2021 14:35:08 +0800 Subject: [PATCH] Add respawn anchor support. --- .../MultiverseCore/MultiverseCore.java | 3 ++ .../MultiverseCore/enums/RespawnType.java | 7 ++++ .../listeners/MVPlayerListener.java | 13 +++++- .../utils/CompatibilityLayer.java | 40 +++++++++++++++++++ 4 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/onarandombox/MultiverseCore/enums/RespawnType.java create mode 100644 src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java diff --git a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java index 4af01ad6..94521888 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java +++ b/src/main/java/com/onarandombox/MultiverseCore/MultiverseCore.java @@ -88,6 +88,7 @@ import com.onarandombox.MultiverseCore.listeners.MVWeatherListener; import com.onarandombox.MultiverseCore.listeners.MVWorldInitListener; import com.onarandombox.MultiverseCore.listeners.MVWorldListener; import com.onarandombox.MultiverseCore.utils.AnchorManager; +import com.onarandombox.MultiverseCore.utils.CompatibilityLayer; import com.onarandombox.MultiverseCore.utils.MVEconomist; import com.onarandombox.MultiverseCore.utils.MVMessaging; import com.onarandombox.MultiverseCore.utils.MVPermissions; @@ -254,6 +255,8 @@ public class MultiverseCore extends JavaPlugin implements MVPlugin, Core { // Setup our SafeTTeleporter this.safeTTeleporter = new SimpleSafeTTeleporter(this); this.unsafeCallWrapper = new UnsafeCallWrapper(this); + // Setup our CompatibilityLayer + CompatibilityLayer.init(); } diff --git a/src/main/java/com/onarandombox/MultiverseCore/enums/RespawnType.java b/src/main/java/com/onarandombox/MultiverseCore/enums/RespawnType.java new file mode 100644 index 00000000..889f0e43 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/enums/RespawnType.java @@ -0,0 +1,7 @@ +package com.onarandombox.MultiverseCore.enums; + +public enum RespawnType { + BED, + ANCHOR, + OTHER +} diff --git a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java index 334d7a78..8e673849 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java +++ b/src/main/java/com/onarandombox/MultiverseCore/listeners/MVPlayerListener.java @@ -15,7 +15,9 @@ import com.dumptruckman.minecraft.util.Logging; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MVWorldManager; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.enums.RespawnType; import com.onarandombox.MultiverseCore.event.MVRespawnEvent; +import com.onarandombox.MultiverseCore.utils.CompatibilityLayer; import com.onarandombox.MultiverseCore.utils.PermissionTools; import org.bukkit.GameMode; import org.bukkit.Location; @@ -69,9 +71,16 @@ public class MVPlayerListener implements Listener { return; } + RespawnType respawnType = RespawnType.OTHER; + if (event.isBedSpawn()) { + respawnType = RespawnType.BED; + } + if (CompatibilityLayer.isAnchorSpawn(event)) { + respawnType = RespawnType.ANCHOR; + } - if (mvWorld.getBedRespawn() && event.isBedSpawn()) { - Logging.fine("Spawning " + event.getPlayer().getName() + " at their bed"); + if (mvWorld.getBedRespawn() && (respawnType == RespawnType.BED || respawnType == RespawnType.ANCHOR)) { + Logging.fine("Spawning %s at their %s", event.getPlayer().getName(), respawnType); return; } diff --git a/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java b/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java new file mode 100644 index 00000000..16249a98 --- /dev/null +++ b/src/main/java/com/onarandombox/MultiverseCore/utils/CompatibilityLayer.java @@ -0,0 +1,40 @@ +package com.onarandombox.MultiverseCore.utils; + +import com.dumptruckman.minecraft.util.Logging; +import org.bukkit.Bukkit; +import org.bukkit.event.player.PlayerRespawnEvent; + +import java.lang.reflect.Method; + +public class CompatibilityLayer { + + private static Method checkAnchorSpawn; + + public static void init() { + try { + checkAnchorSpawn = PlayerRespawnEvent.class.getDeclaredMethod("isAnchorSpawn"); + } catch (NoSuchMethodException e) { + Logging.fine("%s does not support respawn anchors.", Bukkit.getVersion()); + } + } + + /** + * Check if the respawn point is of respawn anchor type. + * Introduced in minecraft 1.16 + * + * @param event A player respawn event. + * @return If the respawn location is an anchor point. + */ + public static boolean isAnchorSpawn(PlayerRespawnEvent event) { + if (checkAnchorSpawn == null) { + return false; + } + try { + return (boolean) checkAnchorSpawn.invoke(event); + } catch (Exception e) { + Logging.warning("Error checking for: %s", checkAnchorSpawn); + e.printStackTrace(); + } + return false; + } +}