Solved Block Dragon Eggs from falling

Discussion in 'Plugin Development' started by YoFuzzy3, Nov 8, 2012.

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

    YoFuzzy3

    I've looked through the api docs and have tried many different things, but I can't seem to disable dragon eggs from falling. Is there a way to do it, and how so?
     
  2. Offline

    netherfoam

    It's not BlockPhysicsEvent is it?
     
  3. Offline

    YoFuzzy3

    Yeah it is that but I'm not sure how to check if it's a Dragon Egg that's falling.

    This doesn't work:
    Code:
        @EventHandler
        public void onDragonEggFall(BlockPhysicsEvent event){
            if(getConfig().getBoolean("DragonEgg.BlockFall") == true){
                if(event.getChangedType() == Material.DRAGON_EGG){
                    event.setCancelled(true);
                }
            }
        }
    
    This also doesn't work:
    Code:
        @EventHandler
        public void onDragonEggFall(BlockPhysicsEvent event){
            if(getConfig().getBoolean("DragonEgg.BlockFall") == true){
                if(event.getChangedTypeId() == 122){
                    event.setCancelled(true);
                }
            }
        }
    
    Any suggestions?
     
  4. Offline

    ZeusAllMighty11

    what I would do:


    Code:
    @EventHandler
    public void onRightClick(PlayerInteractEvent e){
        Action action = e.getAction();
        Block clicked = e.getClickedBlock();
        Material type = clicked.getType();
        
            if(action == Action.RIGHT_CLICK_BLOCK){
                if(type == Material.DRAGON_EGG){
                
                    if(getConfig().getBoolean("DragonEgg.BlockFall") == true){
                        e.setCancelled(false);
                    } else{
                        e.setCancelled(true);
                    }
                }
            }
        }
    
    Prevents even right clickig the egg to stop it from moving.
     
  5. Offline

    one4me

    Try this, make sure you check the block, not the changedtype.
    Code:
    @EventHandler
    public void onBlockPhysicsEvent(BlockPhysicsEvent event) {
      if(event.getBlock().getTypeId() == 122) {
        if(getConfig().getBoolean("DragonEgg.BlockFall")){
          event.setCancelled(true);
        }
      }
    }
    
     
  6. Offline

    YoFuzzy3

    I already knew that and it's not what I was asking help for in this thread.

    Now if you place the dragon egg on a block, break the block underneath, it won't fall. But if you place the dragon egg on the side of a block it will still fall.
     
  7. Offline

    one4me

    Interesting. My guess would be that when you place a block that is effected by gravity you're actually spawning a falling entity, which doesn't fire a BlockPhysicsEvent.
     
  8. Offline

    Deleted user

    Here's what I'd do.. prevent the damn thing from dropping haha. :D

    Code:
    @EventHandler
    public void onEnderDragonDeath(EntityDeathEvent event) {
    if (!(event.getEntity() instanceof EnderDragon)) {
    return;
    }
    if (getConfig().getBoolean("DragonEgg.BlockFall")) {
    event.getDrops().clear();
    }
    }
    

    Edit:

    I don't actually know if this will work haha.
     
  9. Offline

    YoFuzzy3

    I don't want to clear the drops of the ender dragon, I just want to stop dragon eggs from being affected by gravity.

    So do you have any idea how I would stop dragon egg specific entities from falling? Dragon Egg doesn't seem to be an entity, so I'm clueless.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
  10. Offline

    one4me

    It would be a FallingBlockEntity, this issue has been around for over 5 months and its priority is marked as minor.

    There was a work around posted in this thread. It involved placing a block under it (only if the egg was placed on top of fire, water, or air) whenever BlockPlaceEvent was fired and then 2 ticks later changing the block back to normal.
     
  11. Offline

    YoFuzzy3

    How would I get the block underneath the placed block?
    EDIT: Never mind :p
     
  12. Offline

    one4me

    YoFuzzy3
    Hey here's a class that would stop DragonEggs from falling.
    Code:
    import java.lang.reflect.Method;
    import java.util.Random;
    
    import net.minecraft.server.Block;
    import net.minecraft.server.BlockDragonEgg;
    import net.minecraft.server.World;
    
    public class ModifiedBlockDragonEgg extends BlockDragonEgg{
      public ModifiedBlockDragonEgg(int i, int j) {
        super(i, j);
      }
      @Override
      public void b(World world, int i, int j, int k, Random random) {
        world.setTypeId(i, j, k, this.id);
      }
      public static void enable() {
        try {
          Method c = Block.class.getDeclaredMethod("c", float.class);
          Method b = Block.class.getDeclaredMethod("b", float.class);
          Method a = Block.class.getDeclaredMethod("a", net.minecraft.server.StepSound.class);
          Method af = Block.class.getDeclaredMethod("a", float.class);
          c.setAccessible(true);
          b.setAccessible(true);
          a.setAccessible(true);
          af.setAccessible(true);
          Block.byId[Block.DRAGON_EGG.id] = null;
          Block mde = (ModifiedBlockDragonEgg)(new ModifiedBlockDragonEgg(122, 167)).b("dragonEgg");
          mde = (Block)c.invoke(mde, 3.0F);
          mde = (Block)b.invoke(mde, 15.0F);
          mde = (Block)a.invoke(mde, Block.h);
          mde = (Block)af.invoke(mde, 0.125F);
          Block.byId[Block.DRAGON_EGG.id] = mde;
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void disable() {
        Block.byId[Block.DRAGON_EGG.id] = null;
        Block.byId[Block.DRAGON_EGG.id] = Block.DRAGON_EGG;
      }
    }
    
    You would add this to onEnable()
    Code:
    ModifiedBlockDragonEgg.enable();
    
    and you would add this in onDisable()
    Code:
    ModifiedBlockDragonEgg.disable();
    
     
  13. Offline

    YoFuzzy3

    It works perfectly, thanks so much! ..But can you explain how it works? :p
     
  14. Offline

    one4me

    The class ModifiedBlockDragonEgg extends BlockDragonEgg and overrides method b(World world, int i, int j, int k, Random random).

    Originally something along the lines of this happens when a DragonEgg spawns
    Code:
    public void b(World world, int i, int j, int k, Random random) {
      l(world, i, j, k);
    }
    private void l(World world, int i, int j, int k) {
      if ((BlockSand.canFall(world, i, j - 1, k)) && (j >= 0)) {
        byte b0 = 32;
        if ((!BlockSand.instaFall) && (world.d(i - b0, j - b0, k - b0, i + b0, j + b0, k + b0))) {
          EntityFallingBlock entityfallingblock = new EntityFallingBlock(world, i + 0.5F, j + 0.5F, k + 0.5F, this.id, world.getData(i, j, k));
          world.addEntity(entityfallingblock);
        }
        else {
          world.setTypeId(i, j, k, 0);
          while ((BlockSand.canFall(world, i, j - 1, k)) && (j > 0)) {
            j--;
          }
          if (j > 0) {
            world.setTypeId(i, j, k, this.id);
          }
        }
      }
    }
    
    By overriding b(World world, int i, int j, int k, Random random) the method which spawns the EntityFallingBlock is never called.

    The enable method uses some reflection to setup the ModifiedBlockDragonEgg instance and replaces the original BlockDragonEgg with the modified one. The disable method sets the modified BlockDragonEgg back to the original.
     
  15. Offline

    YoFuzzy3

Thread Status:
Not open for further replies.

Share This Page