public static List<ItemStack> getItemsFromBlock(
      World world, Block block, int x, int y, int z, int meta, boolean silkTouch, int fortune) {
    boolean canSilk = block.canSilkHarvest(world, null, x, y, z, meta);

    if (canSilk && silkTouch) {
      ArrayList<ItemStack> items = new ArrayList<ItemStack>();
      ItemStack item = new ItemStack(block, 1, meta);

      items.add(item);

      return items;
    } else {
      return block.getDrops(world, x, y, z, meta, fortune);
    }
  }
Esempio n. 2
0
  public static List<ItemStack> breakBlock(
      World worldObj,
      EntityPlayer player,
      int x,
      int y,
      int z,
      Block block,
      int fortune,
      boolean doBreak,
      boolean silkTouch) {

    if (block.getBlockHardness(worldObj, x, y, z) == -1) {
      return new LinkedList<ItemStack>();
    }
    int meta = worldObj.getBlockMetadata(x, y, z);
    List<ItemStack> stacks = null;
    if (silkTouch && block.canSilkHarvest(worldObj, player, x, y, z, meta)) {
      stacks = new LinkedList<ItemStack>();
      stacks.add(createStackedBlock(block, meta));
    } else {
      stacks = block.getDrops(worldObj, x, y, z, meta, fortune);
    }
    if (!doBreak) {
      return stacks;
    }
    worldObj.playAuxSFXAtEntity(player, 2001, x, y, z, Block.getIdFromBlock(block) + (meta << 12));
    worldObj.setBlockToAir(x, y, z);

    List<EntityItem> result =
        worldObj.getEntitiesWithinAABB(
            EntityItem.class,
            AxisAlignedBB.getBoundingBox(x - 2, y - 2, z - 2, x + 3, y + 3, z + 3));
    for (int i = 0; i < result.size(); i++) {
      EntityItem entity = result.get(i);
      if (entity.isDead || entity.getEntityItem().stackSize <= 0) {
        continue;
      }
      stacks.add(entity.getEntityItem());
      entity.worldObj.removeEntity(entity);
    }
    return stacks;
  }
  /**
   * *****************************************************************************************************************
   * ********************************************** Tile Methods
   * ******************************************************
   * *****************************************************************************************************************
   */
  @Override
  @SuppressWarnings("unchecked")
  public void updateEntity() {
    super.updateEntity();
    if (!worldObj.isRemote
        && !isPowered()
        && getCore() != null
        && pickSlot.getStackInSlot(0) != null
        && worldObj.rand.nextInt(20) == 0) {
      SixWayRotation rotation = new SixWayRotation();
      ForgeDirection dir =
          rotation.convertMetaToDirection(worldObj.getBlockMetadata(xCoord, yCoord, zCoord));
      Location blockBreakLocation = new Location(this);
      blockBreakLocation.travel(dir);
      if (!worldObj.isAirBlock(blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z)) {
        Block toBreak =
            worldObj.getBlock(blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z);
        if (toBreak.getMaterial() == Material.water || toBreak.getMaterial() == Material.lava)
          return;
        int harvestLevel =
            pickSlot
                .getStackInSlot(0)
                .getItem()
                .getHarvestLevel(pickSlot.getStackInSlot(0), "pickaxe");
        if (toBreak.getHarvestLevel(
                worldObj.getBlockMetadata(
                    blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z))
            <= harvestLevel) {

          int fortune = 0;
          boolean silkTouch =
              toBreak.canSilkHarvest(
                  worldObj,
                  null,
                  blockBreakLocation.x,
                  blockBreakLocation.y,
                  blockBreakLocation.x,
                  worldObj.getBlockMetadata(
                      blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z));
          boolean hasSilkTouch = false;
          NBTTagList enchantList = pickSlot.getStackInSlot(0).getEnchantmentTagList();
          if (enchantList != null) {
            for (int i = 0; i < enchantList.tagCount(); i++) {
              NBTTagCompound tag = enchantList.getCompoundTagAt(i);
              if (tag.hasKey("id") && tag.getInteger("id") == 35) fortune = tag.getInteger("lvl");
              else if (tag.hasKey("id") && tag.getInteger("id") == 33) hasSilkTouch = true;
            }
          }
          ArrayList<ItemStack> itemStacks = new ArrayList<>();
          if (silkTouch && hasSilkTouch) {
            itemStacks.add(
                new ItemStack(
                    toBreak,
                    1,
                    worldObj.getBlockMetadata(
                        blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z)));
          } else {
            itemStacks =
                toBreak.getDrops(
                    worldObj,
                    blockBreakLocation.x,
                    blockBreakLocation.y,
                    blockBreakLocation.z,
                    worldObj.getBlockMetadata(
                        blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z),
                    fortune);
          }
          for (ItemStack itemStack : itemStacks) {
            if (itemStack != null && itemStack.stackSize > 0) {
              float rx = worldObj.rand.nextFloat() * 0.8F + 0.1F;
              float ry = worldObj.rand.nextFloat() * 0.8F + 0.1F;
              float rz = worldObj.rand.nextFloat() * 0.8F + 0.1F;

              EntityItem entityItem =
                  new EntityItem(
                      worldObj,
                      blockBreakLocation.x + rx,
                      blockBreakLocation.y + ry,
                      blockBreakLocation.z + rz,
                      new ItemStack(
                          itemStack.getItem(), itemStack.stackSize, itemStack.getItemDamage()));

              if (itemStack.hasTagCompound())
                entityItem
                    .getEntityItem()
                    .setTagCompound((NBTTagCompound) itemStack.getTagCompound().copy());

              float factor = 0.05F;
              entityItem.motionX = worldObj.rand.nextGaussian() * factor;
              entityItem.motionY = worldObj.rand.nextGaussian() * factor + 0.2F;
              entityItem.motionZ = worldObj.rand.nextGaussian() * factor;
              worldObj.spawnEntityInWorld(entityItem);

              itemStack.stackSize = 0;
            }
          }
          worldObj.playAuxSFX(
              2001,
              blockBreakLocation.x,
              blockBreakLocation.y,
              blockBreakLocation.z,
              Block.getIdFromBlock(toBreak)
                  + (worldObj.getBlockMetadata(
                          blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z)
                      << 12));
          worldObj.setBlockToAir(blockBreakLocation.x, blockBreakLocation.y, blockBreakLocation.z);
          pickSlot.getStackInSlot(0).setItemDamage(pickSlot.getStackInSlot(0).getItemDamage() + 1);
          if (pickSlot.getStackInSlot(0).getItemDamage()
              > pickSlot.getStackInSlot(0).getMaxDamage()) pickSlot.setStackInSlot(null, 0);
        }
      }
    }
  }