Home›Java›Mods›NMSLib
NMSLib
ModsJava

NMSLib

Java mod listed for Minecraft 1.7–1.10. Downloads from the MC Java Mods app on Android.

⬇ Download on Spigot
header.png
Introduction

NMS-Lib provides all tools and functions to develop easily, multi version compatible nms code.
It's made for developers, who don't want to argue with all the unnecessary stuff, nms developing brings with it like incomprehensible exceptions, multiple catch clauses and so one.

Features
​

Code (Text):
npc.autoInvokePrint("setLocation", loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());


The autoInvokePrint method is seeking automatically for a valid method without the need of adding class parameters.
It also outputs a console line with a line of code for the normal invoking method.
Output: Output-exp.png

Example ​

That's how a normal NPC spawn method looks like:

Code (Text):
public void spawn(){
    MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
    WorldServer nmsWorld = ((CraftWorld) loc.getWorld()).getHandle();
    EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld,
                    new GameProfile(UUID.randomUUID(), "NPC"),
                    new PlayerInteractManager(nmsWorld));
    npc.setLocation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
   
    PlayerConnection connection = ((CraftPlayer) player).getHandle().playerConnection;
    connection.sendPacket(new PacketPlayOutPlayerInfo(EnumPlayerInfoAction.ADD_PLAYER, npc));
    connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
}
 
Advantage:
-easy to read

Disadvantages:
-version depend(just one version support)

That would be a version independent NMS solution:

Code (Text):
public void spawn(){[/COLOR]
try {
    Object nmsServer = Bukkit.getServer().getClass().getMethod("getServer").invoke(Bukkit.getServer());
    Object nmsWorld = loc.getWorld().getClass().getMethod("getHandle").invoke(loc.getWorld());
    Object playerIM = Class.forName("net.minecraft.server.v1_10_R1.PlayerInteractManager").getConstructor(nmsWorld.getClass()).newInstance(nmsWorld);
    Object npc = Class.forName("net.minecraft.server.v1_10_R1.EntityPlayer").getConstructor(new Class<?>[]{
    nmsServer.getClass(), nmsWorld.getClass(), GameProfile.class,}).newInstance(nmsServer, nmsWorld, new GameProfile(UUID.randomUUID(), "NPC"), playerIM);

    npc.getClass().getMethod("setLocation", Double.TYPE, Double.TYPE, Double.TYPE, Float.TYPE, Float.TYPE).invoke(npc,
    loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());

     Object handle = player.getClass().getMethod("getHandle").invoke(player);
    Field conField = handle.getClass().getDeclaredField("playerConnection");
    conField.setAccessible(true);
    Object con = conField.get(player);
   
    Class<?> epia = Class.forName("net.minecraft.server.v1_10_R1.PacketPlayOutPlayerInfo$EnumPlayerInfoAction");
    Object ppopi = Class.forName("net.minecraft.server.v1_10_R1.PacketPlayOutPlayerInfo").getConstructor(epia, npc.getClass()).newInstance(epia.getEnumConstants()[0], npc);
    Object ppones = Class.forName("net.minecraft.server.v1_10_R1.PacketPlayOutNamedEntitySpawn").getConstructor(npc.getClass()).newInstance(npc);
    Method send = con.getClass().getMethod("sendPacket",    Class.forName("net.minecraft.server.v1_10_R1.Packet"));
    send.invoke(con, ppopi);
    send.invoke(con, ppones);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | InstantiationException | ClassNotFoundException | NoSuchFieldException e) {
    e.printStackTrace();
    }
[COLOR=rgb(0, 0, 0)]}
 
Advantage:
-version independent

Disadvantage:
-difficult to find errors
-confusing

And that's how it looks with NMS-Lib :

Code (Text):
public void spawn(){
    Object nmsWorld = NMSBuilder.getWorldHandle(loc.getWorld());
    NMSObject npc = new NMSObject("EntityPlayer", NMSBuilder.getMinecraftServer(), nmsWorld,
                    new GameProfile(UUID.randomUUID(), "NMS-NPC"),
                    new NMSObject("PlayerInteractManager", nmsWorld).toObject());
    npc.invoke("setLocation", new Class<?>[]{Double.TYPE, Double.TYPE, Double.TYPE, Float.TYPE, Float.TYPE},
                    loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
           
    Object addPlayerEnum = new NMSEnum(ClassGetter.getNMSSubClass
                    ("PacketPlayOutPlayerInfo", "EnumPlayerInfoAction")).getConstant("ADD_PLAYER");
            new NMSPacket("PacketPlayOutPlayerInfo", addPlayerEnum, npc.asArray()).send(player);
            new NMSPacket("PacketPlayOutNamedEntitySpawn", npc.toObject()).send(player);
}
Advantages:
-version independent
-easy to find errors (custom exception print stacks)
-clean overview

I think the difference is clear.

Just try it out.
Quick facts

Install steps are the general flow for this file type — How to install Minecraft Java mods & modpacks walks through it step by step.

NMSLib is a free Minecraft Java mod. Compatible with Minecraft 1.7, 1.8, 1.9, 1.10. Downloaded 245 times (via Spigot). Download it and open it directly in the game.

Explore more