Resource Getting a block's collision box(es)

Discussion in 'Spigot Plugin Development' started by Islandscout, Aug 12, 2018 at 10:00 PM.

  1. Notice:
    Before I start, I want to clarify that hit boxes are not necessarily the same as collision boxes. Hit boxes play a role in hit detection by defining the bounds for which a player can attack an object/entity (in the case of Minecraft, hit detection is determined by hit box & cursor ray intersection). Collision boxes play a role in physics by defining the bounds that solid objects/entities cannot pass through. https://en.wikipedia.org/wiki/Hitbox

    If you happened to stumble across this thread and wanted to get a block's hit box instead, please visit this thread: https://www.spigotmc.org/threads/hitboxes-and-ray-tracing.174358/

    Introduction:
    NMS has a vast number of powerful tools and utilities to do incredible things in the Minecraft server. For some plugins, such as anti-cheating systems, getting the collision boxes of blocks is essential for advanced collision detection. Although most blocks usually have one collision box, some blocks, such as brewing stands, have more than one collision box. Every block defined in NMS has its own collision boxes, and they are used by the server to perform basic, but accurate collision detection.

    In 1.7 and 1.8, the NMS Block class has a void method that adds all its collision boxes that collide with a specified AABB (Axis-Aligned Bounding Box) to a given List. You'll need to pass in an empty List, and after calling the method, you may have AABB references in your List. I've given two examples below:

    Examples:
    1.7 R4:
    7.jpg
    Code (Text):
    Location blockLoc = block.getLocation(); //location of your Bukkit block.
    net.minecraft.server.v1_7_R4.Block b = ((CraftWorld)blockLoc.getWorld()).getHandle().getType(blockLoc.getBlockX(), blockLoc.getBlockY(), blockLoc.getBlockZ());

    List<AxisAlignedBB> bbs = new ArrayList<>();
    //This AxisAlignedBB is the shape of a 1x1x1 cuboid. This will define the bounds of the block.
    AxisAlignedBB cube = AxisAlignedBB.a(blockLoc.getBlockX(), blockLoc.getBlockY(), blockLoc.getBlockZ(), blockLoc.getBlockX() + 1, blockLoc.getBlockY() + 1, blockLoc.getBlockZ() + 1);
    b.a(((CraftWorld) blockLoc.getWorld()).getHandle(), blockLoc.getBlockX(), blockLoc.getBlockY(), blockLoc.getBlockZ(), cube, bbs, null);
    //Check your bbs List. It should contain something.
    1.8 R3:
    8.jpg
    Code (Text):
    Location blockLoc = block.getLocation(); //location of your Bukkit block.
    BlockPosition.MutableBlockPosition bPos = new BlockPosition.MutableBlockPosition();
    bPos.c(blockLoc.getBlockX(), blockLoc.getBlockY(), blockLoc.getBlockZ());
    IBlockData data = ((CraftWorld) block.getWorld()).getHandle().getType(bPos);
    net.minecraft.server.v1_8_R3.Block b = data.getBlock();
    b.updateShape(((CraftWorld) block.getWorld()).getHandle(), bPos);

    List<AxisAlignedBB> bbs = new ArrayList<>();
    //This AxisAlignedBB is the shape of a 1x1x1 cuboid. This will define the bounds of the block.
    AxisAlignedBB cube = AxisAlignedBB.a(blockLoc.getBlockX(), blockLoc.getBlockY(), blockLoc.getBlockZ(), blockLoc.getBlockX() + 1, blockLoc.getBlockY() + 1, blockLoc.getBlockZ() + 1);
    b.a(((CraftWorld) blockLoc.getWorld()).getHandle(), bPos, data, cube, bbs, null);
    //Check your bbs List. It should contain something.
    I currently don't have any examples for 1.9+. I'm sure things have changed. If anyone wants to chip in, please, feel free to do so. This post will be updated over time.
     
    • Like Like x 1
    • Informative Informative x 1
  2. This is interesting, and something I didn't entirely know about, though I knew getting it was somehow possible. In AntiAura I have always written manual lists, however if I could get this working with multi-version compatibility, that would be cool.

    Though I'm working on my 1.13 update for AntiAura at the moment, if I eventually manage to make some sort of multi-version block bounding box checker, I'll be sure to post the code here.
     
  3. Ouch, defining collision boxes for every block in the game must be a pain. :LOL: But, yeah, posting a multi-version solution would be appreciated a lot. GL
     
  4. Well to be honest there aren't *that* many blocks with a collision box that isn't just the block, so it's not actually too too hard to do manually, but I inevitably miss a few sometimes.....
     

Share This Page