New 1.5 feature: Custom minecart blocks

Discussion in 'Resources' started by bergerkiller, Mar 21, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    bergerkiller

    Some of you may remember, but 1.5 adds the functionality of setting the blocks of minecarts. Bukkit has not yet added support, but they probably will. In the meantime, you can use the following (NMS) code to do it:

    PHP:
        /**
        * Gets the block type for this Minecart
        *
        * @return block type
        */
        
    public Material getBlockType() {
            return 
    Material.getMaterial(getBlockId());
        }
     
        
    /**
        * Gets the block type id for this Minecart
        *
        * @return block type id
        */
        
    public int getBlockId() {
            return 
    getHandle(EntityMinecartAbstract.class).getDataWatcher().getInt(20) & '\uffff';
        }
     
        
    /**
        * Gets the block data for this Minecart
        *
        * @return block data
        */
        
    public int getBlockData() {
            return 
    getHandle(EntityMinecartAbstract.class).o();
        }
     
        
    /**
        * Sets the Block displayed in this Minecart
        *
        * @param blockType of the Block
        */
        
    public void setBlock(Material blockType) {
            
    setBlock(blockType0);
        }
     
        
    /**
        * Sets the Block displayed in this Minecart
        *
        * @param blockId of the Block
        */
        
    public void setBlock(int blockId) {
            
    setBlock(blockId0);
        }
     
        
    /**
        * Sets the Block displayed in this Minecart
        *
        * @param blockType of the Block
        * @param blockData of the Block
        */
        
    public void setBlock(Material blockTypeint blockData) {
            
    setBlock(blockType == null blockType.getId(), blockData);
        }
     
        
    /**
        * Sets the Block displayed in this Minecart
        *
        * @param blockId of the Block
        * @param blockData of the Block
        */
        
    public void setBlock(int blockIdint blockData) {
            
    EntityMinecartAbstract handle getHandle(EntityMinecartAbstract.class);
            
    handle.k(blockId);
            
    handle.l(blockData);
        }
    This is the same code I use in BKCommonLib, where a lot of other functionality is as well. I just recently found out that the '20' datawatcher value was added, so why not add support?
     
  2. Offline

    MCForger

    bergerkiller
    Very cool I will be sure to test this out later!
     
  3. Offline

    devilquak

    MCForger

    This looks awesome, I didn't even know they added this. The time for mobile pig spawners is near...
     
  4. Offline

    chasechocolate

    bergerkiller What's the "getHandle()" method from? Does that class extend any other, or is it a method that you created, but is not in your code?
     
  5. Offline

    bergerkiller

    chasechocolate
    Well it being BKCommonLib, getHandle refers to the 'CraftEntity.getHandle()' method, cast to a type. Equivalent call:

    Code:
    Minecart entity = ???;
    EntityMinecartAbstract handle = (EntityMinecartAbstract) ((CraftEntity) entity).getHandle();
    handle.etc
     
  6. Offline

    CreeperShift

    Wait, are you saying this allows any block to be inside a minecart? :O
     
  7. Offline

    bergerkiller

  8. Offline

    CreeperShift

    Pure awesomeness! 1.5 added so many small things that aren't public knowledge :3
     
  9. Offline

    WarmakerT

    Watch Sethbling, he normally reveals these kind of features.
     
  10. Offline

    CreeperShift

    I do occasionally :D But hes too busy stacking entities to make rocketships ;DD
     
    chasechocolate likes this.
  11. Offline

    bergerkiller

    I fully implemented this feature in TrainCarts now, liking it thus far. For those that don't know what this is about:

    [​IMG]
     
    zack6849 and CreeperShift like this.
  12. Offline

    turt2live

    When you break said minecart, would the block in it drop as well?
     
  13. Offline

    bergerkiller

    turt2live
    Not entirely sure, I do suspect that it will drop both a regular Minecart and the Block type. There is no combined item for all blocks though, so it is not possible to place these minecarts with special blocks on-the-fly.
     
  14. Offline

    KollegahDerBoss

    --- solved ---
     
Thread Status:
Not open for further replies.

Share This Page