How to use (General) - acrylic-style/NMSAPI GitHub Wiki

This page will introduce how to use NMSAPI.

Calling Mojang APIs

import xyz.acrylicstyle.shared.BaseMojangAPI;
import java.util.UUID;

...

UUID uuid = BaseMojangAPI.getUniqueId("i want to hang myself");
if (uuid != null) {
    System.out.println("Matches!");
} else {
    System.out.println("No match");
}
// => Prints "No match!"

// There are more Mojang APIs in the xyz.acrylicstyle.api.MojangAPI

Invoking #getHandle

import xyz.acrylicstyle.craftbukkit.<version>.util.CraftUtils;
import org.bukkit.entity.Player;
import org.bukkit.Bukkit;

...

Player player = Bukkit.getPlayer("i want to die");
if (player == null) return null;
CraftUtils.getHandle(player); // => Returns instance of CraftPlayer

Creating your NMS(net.minecraft.server) class

import xyz.acrylicstyle.shared.NMSAPI;

public class Chunk extends NMSAPI {
    public Chunk(Object o) {
        super(o, "Chunk");
        // this.sections = ICollectionList
        //         .asList((Object[]) getField("sections"))
        //         .map((s, i) -> s == null ? new ChunkSection(this, i << 4) : new ChunkSection(this, s))
        //         .toArray(new ChunkSection[0]);
    }

    // public final ChunkSection[] sections;

    public void initLighting() {
        invoke("initLighting");
    }

    public void test(String paramString1, String paramString2) {
        invoke("test", paramString1, paramString2); // this method will determine classes automatically to call #getMethod.
        // --- or ---
        invoke1("Test", String.class, String.class, paramString1, paramString2); // this method can specify class
    }
}

Creating your OBC(org.bukkit.craftbukkit) class

import xyz.acrylicstyle.minecraft.Entity;
import xyz.acrylicstyle.shared.OBCAPI;

public class CraftEntity extends OBCAPI implements org.bukkit.entity.Entity {
    public static final Class<?> CLASS = getClassWithoutException("entity.CraftEntity");

    @Override // Overrides method on OBCAPI
    public Entity getHandle() {
        return new Entity(super.getOBCClass());
    }

    private org.bukkit.entity.Entity entity;

    /**
     * Constructs CraftEntity.
     * @param o Object. Compatible types:
     *          <ul>
     *          <li>obc.CraftEntity</li>
     *          <li>org.bukkit.entity.Entity</li>
     *          </ul>
     */
    public CraftEntity(@NotNull Object o) {
        super(o, "entity.CraftEntity");
        this.entity = (org.bukkit.entity.Entity) o;
    }

    // ... (you must implement all methods in the org.bukkit.entity.Entity, just forward their calls to entity field.)
}

Converting bukkit classes into NMSAPI classes and using it

// for NMSAPI-1.8.8:
import xyz.acrylicstyle.craftbukkit.v1_8_R3.CraftPlayer;
import xyz.acrylicstyle.minecraft.EntityPlayer;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

...

Player player = Bukkit.getPlayer("notch");
if (player == null) return;
CraftPlayer craftPlayer = new CraftPlayer(player);
EntityPlayer entityPlayer = craftPlayer.getHandle();
entityPlayer.playerConnection.sendPacket(...);

// tl;dr: You need to use the constructor instead of just casting because it IS an instance of OBC class, not NMSAPI classes.
⚠️ **GitHub.com Fallback** ⚠️