Ejemplo n.º 1
0
  /** Called when a Block is right-clicked with this Item */
  public boolean onItemUse(
      ItemStack stack,
      EntityPlayer playerIn,
      World worldIn,
      BlockPos pos,
      EnumFacing side,
      float hitX,
      float hitY,
      float hitZ) {
    if (side == EnumFacing.DOWN) {
      return false;
    } else {
      IBlockState iblockstate = worldIn.getBlockState(pos);
      Block block = iblockstate.getBlock();
      boolean flag = block.isReplaceable(worldIn, pos);

      if (!flag) {
        if (!worldIn.getBlockState(pos).getBlock().getMaterial().isSolid()) {
          return false;
        }

        pos = pos.offset(side);
      }

      if (!playerIn.canPlayerEdit(pos, side, stack)) {
        return false;
      } else if (!Blocks.skull.canPlaceBlockAt(worldIn, pos)) {
        return false;
      } else {
        if (!worldIn.isRemote) {
          worldIn.setBlockState(
              pos, Blocks.skull.getDefaultState().withProperty(BlockSkull.FACING, side), 3);
          int i = 0;

          if (side == EnumFacing.UP) {
            i =
                MathHelper.floor_double((double) (playerIn.rotationYaw * 16.0F / 360.0F) + 0.5D)
                    & 15;
          }

          TileEntity tileentity = worldIn.getTileEntity(pos);

          if (tileentity instanceof TileEntitySkull) {
            TileEntitySkull tileentityskull = (TileEntitySkull) tileentity;

            if (stack.getMetadata() == 3) {
              GameProfile gameprofile = null;

              if (stack.hasTagCompound()) {
                NBTTagCompound nbttagcompound = stack.getTagCompound();

                if (nbttagcompound.hasKey("SkullOwner", 10)) {
                  gameprofile =
                      NBTUtil.readGameProfileFromNBT(nbttagcompound.getCompoundTag("SkullOwner"));
                } else if (nbttagcompound.hasKey("SkullOwner", 8)
                    && nbttagcompound.getString("SkullOwner").length() > 0) {
                  gameprofile =
                      new GameProfile((UUID) null, nbttagcompound.getString("SkullOwner"));
                }
              }

              tileentityskull.setPlayerProfile(gameprofile);
            } else {
              tileentityskull.setType(stack.getMetadata());
            }

            tileentityskull.setSkullRotation(i);
            Blocks.skull.checkWitherSpawn(worldIn, pos, tileentityskull);
          }

          --stack.stackSize;
        }

        return true;
      }
    }
  }