p0t4t0sandwich / TaterLib

A cross API code library that allows developers to write code that works across multiple modding platforms, and across a wide range of Minecraft versions, all with one JAR file. If TaterLib runs on it, so can your plugin/mod.
GNU General Public License v3.0
7 stars 1 forks source link

Find a way to nicely implement Brigadier on modern Bukkit without compromising the portability of Bukkit code #63

Open p0t4t0sandwich opened 4 months ago

p0t4t0sandwich commented 4 months ago

Description of behavior

Probably need some helper subprojects that get remapped accordingly to whichever obsf revision they're needed in. Easiest would probably be to use reflection to register the helper classes to the TaterLib loader, that way the proper Bukkit code can stay separate

Use case

Brig support isn't that hard to have once it's set up proper

Additional context

No response

p0t4t0sandwich commented 1 month ago
    private static final String CRAFTBUKKIT_PACKAGE =
            Bukkit.getServer().getClass().getPackage().getName();

    /**
     * Returns the CraftBukkit class name for the given class.
     *
     * @param clazz The class name.
     * @return The CraftBukkit class name.
     */
    public static String cbClass(String clazz) {
        return CRAFTBUKKIT_PACKAGE + "." + clazz;
    }

    /**
     * Reflects and casts an object.
     *
     * @param clazz The CraftBukkit class name.
     * @param object The object to cast.
     * @return The cast object.
     */
    public static Object reflectAndCast(String clazz, Object object) {
        try {
            return Class.forName(cbClass(clazz)).cast(object);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns the Minecraft server.
     *
     * @return The Minecraft server.
     */
    public static MinecraftServer server() {
        // ((CraftServer) Bukkit.getServer()).getServer();
        Object craftServer = reflectAndCast("CraftServer", Bukkit.getServer());
        try {
            return (MinecraftServer)
                    craftServer.getClass().getDeclaredMethod("getServer").invoke(craftServer);
        } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Returns a CommandDispatcher.
     *
     * @return The CommandDispatcher.
     */
    public static CommandDispatcher<CommandSourceStack> commandDispatcher() {
        // ((CraftServer)
        // Bukkit.getServer()).getServer().resources.managers().getCommands().getDispatcher();
        return server().resources.managers().getCommands().getDispatcher();
    }

    /**
     * Returns Commands.CommandSelection
     *
     * @return The Commands.CommandSelection
     */
    public static Commands.CommandSelection commandSelection() {
        return Commands.CommandSelection.DEDICATED;
    }