Solved Modify physics bounding box of a Block

Discussion in 'Spigot Plugin Development' started by FlyingLlama, Aug 24, 2016.

  1. I am attempting to change the collision box for a block, unsuccessfully
    So far i have made a class that extends block and returns a custom AxisAlignedBB instead of the default cube (Block.k)
    The custom Block class has been put into the block registry and into the Blocks enum, both tested successfully.

    AI sees the changed boundbox and attempts to walk through or jump over the box, but the physics shape is unchanged. I have tested with stone, carpet and half slabs.
     
  2. Choco

    Moderator

    If I'm not mistaken, you have to keep in mind that the collision detection for a block is run on the client-side, not only the server-side. Entity AI on the other hand is run on the server side. iirc
     
  3. well, you can do that but with client side modifications, lets say you want to change collision box of cobblestone, it will be 0.9 insted of 1.0 tall, the client will be 0.1 pixel above the cobblestone then the server will think, so the player will be flying basicly for the server but not for the client, and also it will be incompatibilte with all anticheats, i wold not recommend to do that on Spigot use Forge instead.
     
    • Agree Agree x 1
  4. No, it is not. The fact that mobs bounced off the block proves it is server-side, and arrows will not penetrate it, ect. The client has nothing to do with server physics.

    I know exactly what it will cause and how it will work, but it is too much to expect any answers to advanced questions on these forums..
     
  5. @FlyingLlama Just to confirm? Are you using machine learning and neural nets?
     
  6. Are you trying to modify server-side collision or client-side collision?
     
  7. I think he meant AI as in AI of the Minecraft Mobs..
     
  8. Ok, it works as intended now. The old BlockData still lurks in various references, so i used reflection to inject the new Block class into the old BlockData.

    An arrow stuck in a 1x1x1 wool carpet.

    [​IMG]

    Code (Java):

    /**
    This is really messy still
    I need to clean it up and add proper code so i can apply it to any block
     without messing up the block functionality, but it is a proof of concept.
    */

    import java.lang.reflect.Field;
    import java.lang.reflect.Modifier;

    import net.minecraft.server.v1_10_R1.AxisAlignedBB;
    import net.minecraft.server.v1_10_R1.Block;
    import net.minecraft.server.v1_10_R1.BlockCarpet;
    import net.minecraft.server.v1_10_R1.BlockPosition;
    import net.minecraft.server.v1_10_R1.Blocks;
    import net.minecraft.server.v1_10_R1.IBlockAccess;
    import net.minecraft.server.v1_10_R1.IBlockData;
    import net.minecraft.server.v1_10_R1.MinecraftKey;

    public class BlockCustom extends BlockCarpet
    {
       protected static final AxisAlignedBB b = new AxisAlignedBB(0.0D, 0.0D, 0.0D, 1.0D, 1.0D, 1.0D);
     
       public BlockCustom(Block b, IBlockData oldData)
       {
         super();
     
         try
         {
           // get blockStateList.IBlockData, set the Block to this class
           Field bd_a = this.blockStateList.getBlockData().getClass().getDeclaredField("a");
           bd_a.setAccessible(true);
           Field modif = Field.class.getDeclaredField("modifiers");
          modif.setAccessible(true);
          modif.setInt(bd_a, bd_a.getModifiers() & ~Modifier.FINAL);
          // inject this class into the old BlockData lurking in memory, because finding and replacing every reference would be a nightmare
          bd_a.set(oldData, this);
         
          // Set the IBlockData in this class to the modified one
          Field blockData = Block.class.getDeclaredField("blockData");
          blockData.setAccessible(true);
          blockData.set(this, this.blockStateList.getBlockData());

           Block.REGISTRY_ID.a(this.getBlockData(), 171);
           Block.REGISTRY.a(171, new MinecraftKey("carpet"), this.c(0.1F).c("woolCarpet"));
         
         }
         catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e1)
         {
           e1.printStackTrace();
         }
       
         Field f;
         try
         {
           f = Blocks.class.getDeclaredField("CARPET");
           f.setAccessible(true);
           Field modif = Field.class.getDeclaredField("modifiers");
          modif.setAccessible(true);
          modif.setInt(f, f.getModifiers() & ~Modifier.FINAL);
           f.set(null, this);
         }
         catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e)
         {
           e.printStackTrace();
         }
       }
     
       @Override
      public AxisAlignedBB a(IBlockData iblockdata, IBlockAccess iblockaccess, BlockPosition blockposition)
       {
      return b;
      }

    }