Last active
December 15, 2021 09:26
-
-
Save AngryCarrot789/ca9e5ec498b39c923753c342a744406a to your computer and use it in GitHub Desktop.
NMSAPI - conversion between minecraft source and bukkit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package reghzy.api.utils; | |
import net.minecraft.entity.Entity; | |
import net.minecraft.entity.item.EntityItem; | |
import net.minecraft.entity.player.EntityPlayer; | |
import net.minecraft.item.ItemStack; | |
import net.minecraft.server.v1_6_R3.MinecraftServer; | |
import net.minecraft.tileentity.TileEntity; | |
import net.minecraft.tileentity.TileEntityChest; | |
import net.minecraft.world.ChunkPosition; | |
import net.minecraft.world.World; | |
import net.minecraft.world.WorldServer; | |
import net.minecraft.world.chunk.Chunk; | |
import org.bukkit.Bukkit; | |
import org.bukkit.Location; | |
import org.bukkit.Material; | |
import org.bukkit.block.Chest; | |
import org.bukkit.craftbukkit.v1_6_R3.CraftChunk; | |
import org.bukkit.craftbukkit.v1_6_R3.CraftServer; | |
import org.bukkit.craftbukkit.v1_6_R3.CraftWorld; | |
import org.bukkit.craftbukkit.v1_6_R3.entity.CraftEntity; | |
import org.bukkit.craftbukkit.v1_6_R3.entity.CraftItem; | |
import org.bukkit.craftbukkit.v1_6_R3.inventory.CraftItemStack; | |
import org.bukkit.entity.Item; | |
import reghzy.api.utils.types.Vec3i; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
/** | |
* A helper class for converting between minecraft and bukkit class instances, without having to go through craftbukkit. | |
* This allows debug hot-swapping without errors being thrown due to craftbukkit :-) | |
*/ | |
public class NMSAPI { | |
private static final KVObjectCache<String, WorldServer> lastAccessedWorld = new KVObjectCache<String, WorldServer>() { | |
@Override | |
public WorldServer getValue(String key) { | |
org.bukkit.World bukkitWorld = Bukkit.getWorld(key); | |
if (bukkitWorld == null) { | |
return null; | |
} | |
return Remap.cast((((CraftWorld) bukkitWorld).getHandle())); | |
} | |
}; | |
public static WorldServer getWorld(String name) { | |
return lastAccessedWorld.get(name); | |
} | |
public static WorldServer getWorld(org.bukkit.World bukkitWorld) { | |
return Remap.cast(((CraftWorld) bukkitWorld).getHandle()); | |
} | |
public static org.bukkit.World getBukkitWorld(World world) { | |
return Remap.<net.minecraft.server.v1_6_R3.World>cast(world).getWorld(); | |
} | |
public static ItemStack getItemStack(org.bukkit.inventory.ItemStack bukkitStack) { | |
return Remap.cast((CraftItemStack.asNMSCopy(bukkitStack))); | |
} | |
public static ItemStack getItemStack(Item item) { | |
if (item instanceof CraftItem) { | |
Entity entity = (Entity) (Object) ((CraftItem) item).getHandle(); | |
if (entity instanceof EntityItem) { | |
return ((EntityItem) entity).func_92059_d(); | |
} | |
else { | |
return (ItemStack) ((Object) CraftItemStack.asNMSCopy(item.getItemStack())); | |
} | |
} | |
else { | |
return (ItemStack) ((Object) CraftItemStack.asNMSCopy(item.getItemStack())); | |
} | |
} | |
public static org.bukkit.inventory.ItemStack getBukkitStack(ItemStack itemStack) { | |
return CraftItemStack.asBukkitCopy(Remap.<net.minecraft.server.v1_6_R3.ItemStack>cast(itemStack)); | |
} | |
public static Chunk getChunk(org.bukkit.Chunk chunk) { | |
return Remap.cast(((CraftChunk) chunk).getHandle()); | |
} | |
public static Chunk getChunk(org.bukkit.World world, int x, int z) { | |
return Remap.cast(((CraftChunk) world.getChunkAt(x, z)).getHandle()); | |
} | |
public static Chunk getChunk(Location location) { | |
return Remap.cast(((CraftChunk) location.getChunk()).getHandle()); | |
} | |
public static Collection<WorldServer> getLoadedWorlds() { | |
return Remap.cast(MinecraftServer.getServer().worlds); | |
} | |
public static Collection<Chunk> getLoadedChunks(WorldServer world) { | |
return Remap.cast(Remap.<net.minecraft.server.v1_6_R3.WorldServer>cast(world).chunkProviderServer.chunks.values()); | |
} | |
public static Collection<Chunk> getLoadedChunks(World world) { | |
return Remap.cast(Remap.<net.minecraft.server.v1_6_R3.WorldServer>cast(world).chunkProviderServer.chunks.values()); | |
} | |
public static Collection<Chunk> getLoadedChunks(org.bukkit.World world) { | |
return Remap.cast(((CraftWorld) world).getHandle().chunkProviderServer.chunks.values()); | |
} | |
public static Collection<TileEntity> getTileEntities(Chunk chunk) { | |
return chunk.field_76648_i.values(); | |
} | |
public static Collection<TileEntity> getTileEntities(org.bukkit.Chunk chunk) { | |
return (((CraftChunk) chunk).getHandle()).tileEntities.values(); | |
} | |
// ---------------------- TILE ENTITIES ---------------------- | |
public static TileEntity getTileEntity(Chunk chunk, int chunkX, int chunkY, int chunkZ) { | |
return chunk.func_76597_e(chunkX, chunkY, chunkZ); | |
} | |
public static TileEntity getTileEntity(Chunk chunk, ChunkPosition chunkPos) { | |
return chunk.func_76597_e(chunkPos.field_76930_a, chunkPos.field_76928_b, chunkPos.field_76929_c); | |
} | |
public static TileEntity getTileEntity(Chunk chunk, Vec3i chunkPos) { | |
return chunk.func_76597_e(chunkPos.x, chunkPos.y, chunkPos.z); | |
} | |
public static TileEntity getTileEntity(org.bukkit.Chunk chunk, int chunkX, int chunkY, int chunkZ) { | |
return Remap.cast(((CraftChunk) chunk).getHandle().e(chunkX, chunkY, chunkZ)); | |
} | |
public static TileEntity getTileEntity(org.bukkit.Chunk chunk, ChunkPosition chunkPos) { | |
return Remap.cast(((CraftChunk) chunk).getHandle().e(chunkPos.field_76930_a, chunkPos.field_76928_b, chunkPos.field_76929_c)); | |
} | |
public static TileEntity getTileEntity(org.bukkit.Chunk chunk, Vec3i chunkPos) { | |
return Remap.cast(((CraftChunk) chunk).getHandle().e(chunkPos.x, chunkPos.y, chunkPos.z)); | |
} | |
public static TileEntity getTileEntity(World world, int worldX, int worldY, int worldZ) { | |
return world.func_72964_e(worldX >> 4, worldZ >> 4).func_76597_e(worldX & 15, worldY & 255, worldZ & 15); | |
} | |
public static TileEntity getTileEntity(World world, ChunkPosition worldPos) { | |
return world.func_72964_e(worldPos.field_76930_a >> 4, worldPos.field_76929_c >> 4).func_76597_e(worldPos.field_76930_a & 15, worldPos.field_76928_b & 255, worldPos.field_76929_c & 15); | |
} | |
public static TileEntity getTileEntity(World world, Vec3i worldPos) { | |
return world.func_72964_e(worldPos.x >> 4, worldPos.z >> 4).func_76597_e(worldPos.x & 15, worldPos.y & 255, worldPos.z & 15); | |
} | |
public static TileEntity getTileEntity(org.bukkit.World world, int worldX, int worldY, int worldZ) { | |
return Remap.cast(((CraftWorld) world).getHandle().getChunkAt(worldX >> 4, worldZ >> 4).e(worldX & 15, worldY & 255, worldZ & 15)); | |
} | |
public static TileEntity getTileEntity(org.bukkit.World world, ChunkPosition worldPos) { | |
return Remap.cast(((CraftWorld) world).getHandle().getChunkAt(worldPos.field_76930_a >> 4, worldPos.field_76929_c >> 4).e(worldPos.field_76930_a & 15, worldPos.field_76928_b & 255, worldPos.field_76929_c & 15)); | |
} | |
public static TileEntity getTileEntity(org.bukkit.World world, Vec3i worldPos) { | |
return Remap.cast(((CraftWorld) world).getHandle().getChunkAt(worldPos.x >> 4, worldPos.z >> 4).e(worldPos.x & 15, worldPos.y & 255, worldPos.z & 15)); | |
} | |
// ---------------------- ENTITIES ---------------------- | |
public static Collection<Entity> getEntities(Chunk chunk) { | |
if (chunk.field_76644_m) { | |
List[] slices = chunk.field_76645_j; | |
ArrayList<Entity> entities = new ArrayList<Entity>(32); | |
for(int i = 0; i < 16; i++) { | |
entities.addAll(slices[i]); | |
} | |
return entities; | |
} | |
return Collections.emptyList(); | |
} | |
public static Collection<Entity> getEntities(org.bukkit.Chunk chunk) { | |
net.minecraft.server.v1_6_R3.Chunk nmsChunk = ((CraftChunk) chunk).getHandle(); | |
if (nmsChunk.m) { | |
List[] slices = nmsChunk.entitySlices; | |
ArrayList<Entity> entities = new ArrayList<Entity>(32); | |
for (int i = 0; i < 16; i++) { | |
entities.addAll(slices[i]); | |
} | |
return entities; | |
} | |
return Collections.emptyList(); | |
} | |
public static Collection<EntityPlayer> getOnlinePlayers() { | |
return (((CraftServer) Bukkit.getServer())).getHandle().players; | |
} | |
/** | |
* Chunks are split up into 16x16x16 slices. slice 0 would contain any entities whose Y value is less than 16. | |
* Slice 2 would contain any entities whose Y was above or equal to 48 and below 64 | |
*/ | |
public static Collection<Entity> getEntitiesInSlice(Chunk chunk, int sliceY) { | |
return chunk.field_76645_j[sliceY & 15]; | |
} | |
/** | |
* Chunks are split up into 16x16x16 slices. slice 0 would contain any entities whose Y value is less than 16. | |
* Slice 2 would contain any entities whose Y was above or equal to 48 and below 64 | |
*/ | |
public static Collection<Entity> getEntitiesInSlice(org.bukkit.Chunk chunk, int sliceY) { | |
return ((CraftChunk) chunk).getHandle().entitySlices[sliceY & 15]; | |
} | |
public static Collection<Entity> getEntitiesInWorld(World world) { | |
return world.field_72996_f; | |
} | |
public static Collection<Entity> getEntitiesInWorld(org.bukkit.World world) { | |
return ((CraftWorld) world).getHandle().entityList; | |
} | |
public static org.bukkit.Chunk getBukkitChunk(Chunk chunk) { | |
return Remap.<net.minecraft.server.v1_6_R3.Chunk>cast(chunk).bukkitChunk; | |
} | |
public static org.bukkit.Chunk getBukkitChunk(World world, int x, int z) { | |
return Remap.<net.minecraft.server.v1_6_R3.Chunk>cast(world.func_72964_e(x, z)).bukkitChunk; | |
} | |
public static Entity getEntity(org.bukkit.entity.Entity entity) { | |
return Remap.cast(((CraftEntity) entity).getHandle()); | |
} | |
public static org.bukkit.entity.Entity getBukkitEntity(Entity entity) { | |
return Remap.<net.minecraft.server.v1_6_R3.Entity>cast(entity).getBukkitEntity(); | |
} | |
public static TileEntityChest getOrCreateChest(Location loc) { | |
TileEntity tileEntity = getTileEntity(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); | |
if (tileEntity instanceof TileEntityChest) { | |
return (TileEntityChest) tileEntity; | |
} | |
else { | |
loc.getBlock().setType(Material.CHEST); | |
return (TileEntityChest) getTileEntity(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); | |
} | |
} | |
public static TileEntityChest getChest(Location loc) { | |
TileEntity tileEntity = getTileEntity(loc.getWorld(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); | |
if (tileEntity instanceof TileEntityChest) { | |
return (TileEntityChest) tileEntity; | |
} | |
return null; | |
} | |
public static TileEntityChest getChest(Chest chest) { | |
TileEntity tileEntity = getTileEntity(chest.getWorld(), chest.getX(), chest.getY(), chest.getZ()); | |
if (tileEntity instanceof TileEntityChest) { | |
return (TileEntityChest) tileEntity; | |
} | |
return null; | |
} | |
public static Location getTileLocation(TileEntity tileEntity) { | |
return new Location(Remap.<net.minecraft.server.v1_6_R3.World>cast(tileEntity.func_70314_l()).getWorld(), | |
tileEntity.field_70329_l, tileEntity.field_70330_m, tileEntity.field_70327_n); | |
} | |
public static Location getEntityLocation(Entity entity) { | |
return new Location(Remap.<net.minecraft.server.v1_6_R3.World>cast(entity.field_70170_p).getWorld(), | |
entity.field_70165_t, entity.field_70163_u, entity.field_70161_v); | |
} | |
public static Location newLocation(World world, double x, double y, double z) { | |
return new Location(Remap.<net.minecraft.server.v1_6_R3.World>cast(world).getWorld(), x, y, z); | |
} | |
public static Location newLocation(World world, int x, int y, int z) { | |
return new Location(Remap.<net.minecraft.server.v1_6_R3.World>cast(world).getWorld(), x, y, z); | |
} | |
public static int getBlockId(World world, int x, int y, int z) { | |
return world.func_72964_e(x >> 4, z >> 4).func_76610_a(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockId(org.bukkit.World world, int x, int y, int z) { | |
return (((CraftWorld) world).getHandle()).getChunkAt(x >> 4, z >> 4).getTypeId(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockId(Chunk chunk, int x, int y, int z) { | |
return chunk.func_76610_a(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockId(org.bukkit.Chunk chunk, int x, int y, int z) { | |
return ((CraftChunk) chunk).getHandle().getTypeId(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockId(Location location) { | |
return (((CraftWorld) (location.getWorld())).getHandle()).getChunkAt(location.getBlockX() >> 4, location.getBlockZ() >> 4).getTypeId(location.getBlockX() & 15, location.getBlockY() & 255, location.getBlockZ() & 15); | |
} | |
public static int getBlockData(World world, int x, int y, int z) { | |
return world.func_72964_e(x >> 4, z >> 4).func_76628_c(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockData(org.bukkit.World world, int x, int y, int z) { | |
return (((CraftWorld) world).getHandle()).getChunkAt(x >> 4, z >> 4).getData(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockData(Chunk chunk, int x, int y, int z) { | |
return chunk.func_76628_c(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockData(org.bukkit.Chunk chunk, int x, int y, int z) { | |
return ((CraftChunk) chunk).getHandle().getData(x & 15, y & 255, z & 15); | |
} | |
public static int getBlockData(Location location) { | |
return (((CraftWorld) (location.getWorld())).getHandle()).getChunkAt(location.getBlockX() >> 4, location.getBlockZ() >> 4).getData(location.getBlockX() & 15, location.getBlockY() & 255, location.getBlockZ() & 15); | |
} | |
public static boolean setBlockIdData(World world, int x, int y, int z, int id, int data, int destroyKeepReplace123) { | |
return world.func_72832_d(x, y, z, id, data, destroyKeepReplace123); | |
} | |
public static boolean setBlockIdData(World world, int x, int y, int z, int id, int data) { | |
return world.func_72832_d(x, y, z, id, data, 3); | |
} | |
public static boolean setBlockIdData(org.bukkit.World world, int x, int y, int z, int id, int data, int destroyKeepReplace123) { | |
return (((CraftWorld) world).getHandle()).setTypeIdAndData(x, y, z, id, data, destroyKeepReplace123); | |
} | |
public static boolean setBlockIdData(org.bukkit.World world, int x, int y, int z, int id, int data) { | |
return (((CraftWorld) world).getHandle()).setTypeIdAndData(x, y, z, id, data, 3); | |
} | |
public static boolean setBlockIdData(Chunk chunk, int chunkX, int y, int chunkZ, int id, int data, int destroyKeepReplace123) { | |
return chunk.field_76637_e.func_72832_d(chunk.field_76635_g << 4 | (chunkX & 15), y, chunk.field_76647_h << 4 | (chunkZ & 15), id, data, destroyKeepReplace123); | |
} | |
public static boolean setBlockIdData(Chunk chunk, int chunkX, int y, int chunkZ, int id, int data) { | |
return chunk.field_76637_e.func_72832_d(chunk.field_76635_g << 4 | (chunkX & 15), y, chunk.field_76647_h << 4 | (chunkZ & 15), id, data, 3); | |
} | |
public static boolean setBlockIdData(org.bukkit.Chunk chunk, int chunkX, int y, int chunkZ, int id, int data, int destroyKeepReplace123) { | |
return (((CraftWorld) chunk.getWorld()).getHandle()).setTypeIdAndData(chunk.getX() << 4 | (chunkX & 15), y, chunk.getZ() << 4 | (chunkZ & 15), id, data, destroyKeepReplace123); | |
} | |
public static boolean setBlockIdData(org.bukkit.Chunk chunk, int chunkX, int y, int chunkZ, int id, int data) { | |
return (((CraftWorld) chunk.getWorld()).getHandle()).setTypeIdAndData(chunk.getX() << 4 | (chunkX & 15), y, chunk.getZ() << 4 | (chunkZ & 15), id, data, 3); | |
} | |
public static boolean setBlockIdData(Location location, int id, int data, int destroyKeepReplace123) { | |
return (((CraftWorld) location.getWorld()).getHandle()).setTypeIdAndData(location.getBlockX(), location.getBlockY(), location.getBlockZ(), id, data, destroyKeepReplace123); | |
} | |
public static boolean setBlockIdData(Location location, int id, int data) { | |
return (((CraftWorld) location.getWorld()).getHandle()).setTypeIdAndData(location.getBlockX(), location.getBlockY(), location.getBlockZ(), id, data, 3); | |
} | |
/** | |
* Gets the block coordinate from a chunk coordinate and the coordinate within that chunk. | |
* This only works with the X and Z coordinates, obviously | |
* @param chunk The chunk coordinate | |
* @param coordinate The coordinate within the chunk (0-15) | |
* @return The world/block coordinate | |
*/ | |
public static int chunkToBlock(int chunk, int coordinate) { | |
return chunk << 4 | (coordinate & 15); | |
} | |
/** | |
* Gets the block X coordinate from the given chunk and the coordinate within that chunk. | |
* @param chunk The chunk coordinate | |
* @param x The x coordinate within the chunk (0-15) | |
* @return The world/block coordinate | |
*/ | |
public static int chunkToBlockX(Chunk chunk, int x) { | |
return chunk.field_76635_g << 4 | (x & 15); | |
} | |
/** | |
* Gets the block Z coordinate from the given chunk and the coordinate within that chunk. | |
* @param chunk The chunk coordinate | |
* @param z The z coordinate within the chunk (0-15) | |
* @return The world/block coordinate | |
*/ | |
public static int chunkToBlockZ(Chunk chunk, int z) { | |
return chunk.field_76647_h << 4 | (z & 15); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment