Changing Block Drops

Discussion in 'Spigot Plugin Development' started by _Zaboomafoo_, Jun 4, 2015.

  1. Hey all, I am wondering how I would be able to change the drop of a block using the Enchantment API found here:
    http://dev.bukkit.org/bukkit-plugins/enchantmentapi/

    This is what I have so far but I am not able to use a BlockBreakEvent just a BlockEvent. This code just makes the player break the block instantly and then drops the item. I want it to where it will take the normal amount of time to break the block then drop the item.

    Code: http://hastebin.com/kikaxuqeba.avrasm

    Code (Text):
    package me.zaboomafoo;

    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.BlockEvent;
    import org.bukkit.inventory.ItemStack;

    import com.rit.sucy.CustomEnchantment;

    public class OresEnchantment extends CustomEnchantment {
     
        static final Material[] ORES_ITEMS = new Material[] {
            Material.DIAMOND_PICKAXE, Material.GOLD_PICKAXE, Material.IRON_PICKAXE, Material.STONE_PICKAXE, Material.WOOD_PICKAXE
        };
     
        public OresEnchantment() {
            super("Ores", ORES_ITEMS, 10);
            this.max = 1;
            this.base = 1;
            this.interval = 1;
        }

        @Override
        public void applyToolEffect(Player player, Block block, int enchantlevel, BlockEvent event) {
         
            if (event.getBlock().getType().equals(new ItemStack(Material.IRON_BLOCK))) {
                event.getBlock().setType(Material.AIR);
                event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.DIAMOND_BLOCK));
            }
         
        }
     
    }
     
     
  2. Code (Text):

        @Override
        public void applyToolEffect(Player player, Block block, int enchantlevel, BlockEvent event) {
            if (!(event instanceof BlockBreakEvent)) {
                return; // block not broken yet. do nothing.
            }
            if (event.getBlock().getType().equals(new ItemStack(Material.IRON_BLOCK))) {
                event.getBlock().setType(Material.AIR);
                event.getBlock().getWorld().dropItemNaturally(event.getBlock().getLocation(), new ItemStack(Material.DIAMOND_BLOCK));
            }
        }
     
     
    • Winner Winner x 1
  3. ok that works thanks.
     

  4. Sorry for gravedigging, but this is for people coming here in the future from the quick google search I did.

    Instead of dropItemNaturally use dropItem as it looks a lot more natural, Weirdly when using Naturally it flys out at an incredible speed.
    So the code will just look like this. Similar but will look cleaner. This might just be for 1.9 so if you try this and it doesn't work try the other one. :)

    Code (Text):
    event.getBlock().getWorld().dropItem(event.getBlock().getLocation(), new ItemStack(Material.DIAMOND, 1));
    And the "1" at the end is the amount to drop.
     
    • Friendly Friendly x 2
    • Winner Winner x 1
  5. Not my thread :eek:
     
  6. This is a temporary bug and is already fixed in the most recent vanilla snapshots. The problem is where you spawn your items at. if they are touching a block in any way they will gain too much momentum.

    https://bugs.mojang.com/browse/MC-97916

    dropItemNaturually() is what makes an item appear in the world with a slight velocity (so it appears to "pop" into world like when breaking a block). The other method, dropItem() does not apply this velocity. Both of these are effected by the bug mentioned, so both of their velocities are exponentially larger than should be, which makes dropItem() appear to do what dropItemNaturally() should do.

    ;)
     
  7. Yea I just changed the line of code in my own plugin and it looked a lot better so I figured this will explain it to people.
     
  8. Adding (0.5, 0.5, 0.5) to the location fixes that problem already for both spawn methods.
     
    • Agree Agree x 1