Resolve the native Trails API without optional PlaceholderAPI

This commit is contained in:
Dev 2026-11-23 22:42:47 +01:00
commit 93f98b55d8
2 changed files with 9 additions and 2 deletions

View file

@ -1,6 +1,8 @@
package com.example.duneterrain;
import java.lang.reflect.*;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
@ -22,7 +24,12 @@ public final class TrailsBridge implements Listener {
private PersistentDataContainer data(Plugin plugin,Block block)throws ReflectiveOperationException{
return (PersistentDataContainer)plugin.getClass().getClassLoader().loadClass("me.ccrama.Trails.customblockdata.CustomBlockData").getConstructor(Block.class,Plugin.class).newInstance(block,plugin);
}
private Object listener(Plugin plugin)throws ReflectiveOperationException{return plugin.getClass().getMethod("getMoveEventListener").invoke(plugin);}
static Object nativeListener(Plugin plugin)throws ReflectiveOperationException{
Class<?> result=plugin.getClass().getClassLoader().loadClass("me.ccrama.Trails.listeners.MoveEventListener");
// Resolve this one API method without resolving optional PAPI hook return types.
try{return MethodHandles.publicLookup().findVirtual(plugin.getClass(),"getMoveEventListener",MethodType.methodType(result)).invoke(plugin);}catch(Throwable failure){throw new ReflectiveOperationException("Native Trails movement API unavailable",failure);}
}
private Object listener(Plugin plugin)throws ReflectiveOperationException{return nativeListener(plugin);}
private Object link(Plugin plugin,Block block)throws ReflectiveOperationException{Object listener=listener(plugin);return listener.getClass().getMethod("getLink",Block.class).invoke(listener,block);}
private static boolean player(Player p){return p.getGameMode()==GameMode.SURVIVAL||p.getGameMode()==GameMode.ADVENTURE;}
private void failure(Exception failure){owner.getLogger().warning("Native trail brush unavailable: "+failure.getClass().getSimpleName());}

View file

@ -31,7 +31,7 @@ public final class ArrakisFaunaSmoke extends JavaPlugin {
var block=world.getBlockAt(8,65,8);block.setType(Material.SAND,false);
var constructor=TrailsBridge.class.getDeclaredConstructor(Plugin.class);constructor.setAccessible(true);TrailsBridge bridge=constructor.newInstance(this);
bridge.step(new PlayerMoveEvent(player,new Location(world,8.5,66,8.5),new Location(world,9.5,66,8.5)));
Object listener=trails.getClass().getMethod("getMoveEventListener").invoke(trails);
Object listener=TrailsBridge.nativeListener(trails);
Object link=listener.getClass().getMethod("getLink",org.bukkit.block.Block.class).invoke(listener,block);check(link!=null,"Sand trail absent");
for(int i=0;i<40&&block.getType()==Material.SAND;i++)listener.getClass().getMethod("makePath",Player.class,org.bukkit.block.Block.class,link.getClass(),boolean.class).invoke(listener,player,block,link,false);
check(block.getType()==Material.SANDSTONE,"Native wear did not create sandstone");