Skip to content

Commit

Permalink
Account for spawn method signature change (#2552)
Browse files Browse the repository at this point in the history
  • Loading branch information
tal5 committed Nov 2, 2023
1 parent aabef89 commit 4f91815
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Expand Up @@ -9,6 +9,8 @@
import com.denizenscript.denizen.utilities.PaperAPITools;
import com.denizenscript.denizencore.DenizenCore;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.destroystokyo.paper.profile.PlayerProfile;
import com.destroystokyo.paper.profile.ProfileProperty;
import io.papermc.paper.entity.TeleportFlag;
Expand Down Expand Up @@ -276,12 +278,20 @@ public ProfileProperty getProfileProperty(PlayerProfile profile, String name) {

@Override
public <T extends Entity> T spawnEntity(Location location, Class<T> type, Consumer<T> configure, CreatureSpawnEvent.SpawnReason reason) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_18)) {
return location.getWorld().spawn(location, type, configure, reason);
}
else {
return super.spawnEntity(location, type, configure, reason);
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_19)) {
// Takes the deprecated bukkit consumer on older versions
if (WORLD_SPAWN_BUKKIT_CONSUMER == null) {
WORLD_SPAWN_BUKKIT_CONSUMER = ReflectionHelper.getMethodHandle(RegionAccessor.class, "spawn", Location.class, Class.class, Consumer.class, CreatureSpawnEvent.SpawnReason.class);
}
try {
return (T) WORLD_SPAWN_BUKKIT_CONSUMER.invoke(location.getWorld(), location, type, configure, reason);
}
catch (Throwable e) {
Debug.echoError(e);
return null;
}
}
return location.getWorld().spawn(location, type, configure, reason);
}

@Override
Expand Down
@@ -1,11 +1,15 @@
package com.denizenscript.denizen.utilities;

import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.NMSVersion;
import com.denizenscript.denizen.scripts.commands.entity.TeleportCommand;
import com.denizenscript.denizencore.utilities.ReflectionHelper;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Nameable;
import org.bukkit.RegionAccessor;
import org.bukkit.block.Sign;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
Expand All @@ -19,6 +23,7 @@
import org.bukkit.scoreboard.Team;
import org.bukkit.util.Consumer;

import java.lang.invoke.MethodHandle;
import java.util.List;

public class PaperAPITools {
Expand Down Expand Up @@ -138,7 +143,23 @@ public void setSkinBlob(Player player, String blob) {
NMSHandler.instance.getProfileEditor().setPlayerSkinBlob(player, blob);
}

public static MethodHandle WORLD_SPAWN_BUKKIT_CONSUMER = null;

// TODO once 1.20 is the minimum supported version, use the modern java.util.Consumer
public <T extends Entity> T spawnEntity(Location location, Class<T> type, Consumer<T> configure, CreatureSpawnEvent.SpawnReason reason) {
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_19)) {
// Takes the deprecated bukkit consumer on older versions
if (WORLD_SPAWN_BUKKIT_CONSUMER == null) {
WORLD_SPAWN_BUKKIT_CONSUMER = ReflectionHelper.getMethodHandle(RegionAccessor.class, "spawn", Location.class, Class.class, Consumer.class);
}
try {
return (T) WORLD_SPAWN_BUKKIT_CONSUMER.invoke(location.getWorld(), location, type, configure);
}
catch (Throwable e) {
Debug.echoError(e);
return null;
}
}
return location.getWorld().spawn(location, type, configure);
}

Expand Down

0 comments on commit 4f91815

Please sign in to comment.